Having fun with gcc.

I actually stumbled upon this idea quite a awhile ago along with my friend Sunil. I don’t exactly remember what we were trying to accomplish back then but here is what I got from the little hacking time we had.

This is about how you can mimic commands. You use the commands cd, ls, cp, etc, right? What if you want something of your own like that? For example your name as a command? Ha! Let’s do that itself.

I did this on my Ubuntu 10.04 distro and I have the gstreamer plugins for mp3  files downloaded and installed.

Let’s assume that you have a song called rockthee.mp3 in your /home/<yourname>/music/ directory.

Now what we are going to do is this. We are going to manipulate a few things such that when you type in your name and hit enter, totem movie player will open up and the song rockthee.mp3 will start playing. And we are going to use the system() command to accomplish this.

For this, go to any directory you wish and open up a text editor. Type in the following code there:

#include<stdio.h>
int main()
{
system("totem ~/music/rockthee.mp3 &");
return 1;
}

Now save it as filename.c and quit the editor.

Going back to the terminal, compile the code as follows:

gcc filename.c -o <your name>

(Replace “<your name>” with your name)

In my case, it would be

gcc filename.c -o haris

Now if you do an ls, you will see that your name is there. Typing ./haris will open up totem and play rockthee.mp3. But that’s not how we want it to be, is it? I mean this is just like executing a C program from the folder it is written in! We shouldn’t use that ‘./’. So what do we do?

Ah ha! We have two options. Either (1) add the path of the current folder (got by executing the command ‘pwd’) to the PATH variable or (2) simply copy the file having your name into any one of the locations given by $PATH.

We will explain both here. But first, try executing ‘$PATH’ in your shell (terminal) and see what comes. Those are the directories which will be searched for when you type in a command at the prompt and hit enter. If you try

ls /bin

you can see many familiar commands including ‘ls’ itself. So when we type ‘ls’ and hit enter, our system searches all the directories specified in the PATH variable and only if it finds ‘ls’ in any one of those locations will ‘ls’ be executed.

So let’s try method (2) first. It is simpler. Run the following:

sudo cp <your name> /bin/

You’re done. Now close the terminal, open up a new one and try simply typing in your name and hitting enter. Viola! There comes Totem with your song! Now to remove what you copied, do

sudo rm /bin/<your name>

Now let’s go to method (1). In order to permanently include the path of your current folder in the PATH variable, you have to edit a file called “.bashrc” in your home folder. But first execute ‘pwd’ and remember the output. Now open up .bashrc using any text editor.

gedit ~/.bashrc

And add the following two lines to the end of that file.

PATH=$PATH:<output of executing ‘pwd’ earlier>
export PATH

In my case it would be

PATH=$PATH:/home/haris/music/ (assuming I wrote and compiled filename.c in the ‘music’ folder itself)
export PATH

Now save the file and close it. Close your current shell (terminal) and open a new one. Type in your name and hit enter and see the wonder works! Also now try executing ‘$PATH’ and you will see that a new path has been added to it.

You can make it a bit more interesting if you add a few printf statements in that C program before the system command saying “This software is created by haris and its version is 0.xx” or something. You can show your non-linux user friends and mess with them.

You can try system command for many things. Here are a few examples:

system(“firefox gmail.com &”);

system(“evince <path>/filename.pdf &”);

system(“gedit <path>/filename.c &”);

Try playing with it and I’m sure you will get more and more ideas.

All the very best!

Firefox download getting stuck. ‘wget’ to the rescue!

The firefox download manager has been giving me quite a headache whenever I try to do big downloads. Even if it is a 70MB download, after 25 or so MB, it gets stuck. Then I have to pause and hit continue for the download to proceed again. It so happens that many times an error occurs and I can’t continue the download from where it got stuck and I have to download the whole thing again… and again… and again…

Ha! But I’m not to be trifled with. Not as long as ‘wget’ is there! ‘wget’ is a GNU free software package that is used for retrieving files over the web. If you have got it installed, then all you have to do is to get the download link and use wget to download it.

In order to get the download link, right click on whichever download you want in the firefox download manager. There will be an option ‘copy download link’. Click on it.

Then open a terminal and go to whichever folder you want the file downloaded to. Assuming it is in the Desktop, type the following command at the prompt:

haris@asylum:~/Desktop$ wget <paste the download link>

Now even if it gets stuck in the middle, you can resume from wherever it was interrupted by giving the option ‘-c’ to ‘wget’. That is, the command will be like

haris@asylum:~/Desktop$ wget -c <paste the download link>

And there it continues beautifully.