Archive for the 'Code Poetry' Category

Expanding a leading tilde in C/C++

If you’re writing an app that accepts a path to a filename as user-input or in config-files, you’ll have to be able to parse the famous leading tilde and expand it to the correct home directory of the correct user. For example, if I enter “~/.vimrc” it needs to be expanded to the file in my userdir “/home/david/.vimrc” before you can do anything with it. You can use “word expand” or wordexp to accomplish this.

Here’s a sample application showing how:

#include <stdio.h>
#include <wordexp.h>
 
int main(int argc, char* argv[]) {
	wordexp_t exp_result;
	wordexp(arv[1], &exp_result, 0);
	printf(exp_result.we_wordv[0]);
}

That should pretty much tell you everything you need to know. Here are some of the results output by this app:

~/.vimrc becomes /home/david/.vimrc
.vimrc becomes .vimrc
~.vimrc becomes ~.vimrc
~blacky/.vimrc becomes /home/admin/blacky/.vimrc (blacky’s homedir is /home/admin/blacky)

As you can see, it handles pretty much every situation correctly.

Gmail Carbon Copy

Today I’d like to introduce Gmail Carbon Copy, an application I’ve coded during the last couple of months. The latest version is stable and works, so I’m deeming it fit for public consumption.

Gmail Carbon Copy, or Gmailcc simply creates a back-up of your Gmail. It differs from existing alternatives because of two clever tricks: each mail is downloaded only once instead of once for every label while still saving the labels, and they’re stored in an actually usable, sparse Maildir format.

Gmail’s IMAP implementation is unique in that it maps labels to folders. The same mail will appear in different folders for every label attached to it. Regular IMAP clients like Thunderbird or getmail think each copy of the mail in a different folder is a new mail, and will download it again, even though it might just be a copy of a mail it downloaded earlier. Gmailcc detects “doubles” and will download each mail just once. Backups, especially the initial one, will finish much faster because of this and will take far less traffic.

Saving it in a usable Maildir format has the advantage that any regular mailserver like Courier can access your backup. It’s very practical: I’m using Gmailcc and Roundcube to access my mails on a webinterface if Gmail is down. It’s sparse because every mail is saved only once, while for every label a sizeless link is created instead of a true copy. This minimizes the space used to store the backup.

There are still some issues but it shouldn’t make your PC explode or kill your Gmail account. If you encounter bugs or would like to have features added, I encourage you to sign up and add a ticket.

Gmail Carbon Copy is open source (C++), licensed under the MIT license and works only on Linux at this time.

Fatal error: Allowed memory size exhausted

Fatal error: Allowed memory size of 8388608 bytes exhausted

Ever got that error? It’s PHP telling you you’ve gone overboard on memory usage, and then dying (…pussy). When you’re running Wordpress, you might have encountered this error when trying to activate some plug-in. Wordpress isn’t exactly the thinnest script on the block. I got this error when trying out the stats plug-in Wassup.

To solve it, we need to tell PHP that scripts can use more than the default amount of memory of 8MB. This is usually set in the php.ini file, but on shared hosts you won’t be able to access it. So we have to resort to .htaccess files, which are equally great, but your host might have disabled changing this setting, in which case this fix won’t work.

Wordpress Fix

Here’s to solve it if you’re using Wordpress. For everything else, scroll down!

Go to Site Admin, Select Manage and then Files. Select .htaccess (for rewrite rules), it should be in the list to the right under Common. If not, scroll down and enter .htaccess in Other Files and press Edit file.

Now you should see something like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Just add the following line to it…

php_value memory_limit 16M

…and press Update File. There, now it should work!

General Solution

Browse to a parent directory of your script (any parent, even root, will do). Create a file named “.htaccess“. That’s including the first dot. In that file, enter the following line. If the file already exists, just edit it, and append it.

php_value memory_limit 16M

Now try again.

Still not working?

If it still gives you the error, but now with a different amount of bytes exhausted, the fix is working but you need to set the memory limit even higher. Try increasing 16M to 32M, and so on. Don’t go too high, though! It might just be that your script has a bug that eats up all available memory, in which case you need to check out the code instead of increase the memory limit.
If the error stays the same, your host will probably have disabled the ability to change this setting, and you’re out of luck :( .

IRC Quote (1)

Here’s a small puzzle:

There’s a file with an unknown number of lines. From that file, I want to read and display 1 line. The line needs to be picked completely at random, so that every line has an equal chance to be selected.

What’s the fastest algorithm you can come up with?

Religion and Programming Languages

One Michael Kimsal put up a survey to determine the correlation between what religion you believe in and what programming language is your favorite. Although of little real use, and probably not to be taken seriously, it’s going to be interesting to see the results.

At the time of writing, there are 1003 entries. Why not put in yours? It’ll make the world a better place… (not really ;) )

I bet Scientologists will love LOGO. Har Har.

Scratching an itch: Cocaine

Cocaine Context Menu

Ever have a file you need to quickly upload to a remote location so that you can share it with others? I have. For example, some photo I want to show someone, real quick.

So I wrote a script to do just that. It uploads a file to a previously specified location, and then puts the URL to that file in the clipboard, for easy pasting in whatever esoteric communication device you’re using, like IRC, IM, or E-mail. In fact, it copies the URL first, and then uploads the file, so you can paste the URL even before the file has uploaded.

Get it here: Cocaine v1.0

It’s a python script with about 30 functional lines. It requires pyGTK to interact with the clipboard. Edit the file and insert your ftp-server, username, password, and HTTP-subpath. I recommend using Nautilus-actions for Gnome users to add Cocaine to your context-menu, so you can easily right-click->upload any file straight out of nautilus.

(Both pygtk and nautilus-actions are in Ubuntu repositories)