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!

32 or 64 bit? The magic of pointers.

Thinking and talking about the issue of whether my OS is 32-bit or a 64-bit one, I found out there were many ways to know that. But we discuss none of them over here. Here we discuss a cool way in order to find it out.

We all know about pointers in C right? It points to a memory location. That is it holds an address whose contents maybe dereferenced using the ‘*’ operator. There I already said it! The pointer stores an address.

But what is the size of an address? And who determines it?

That is where the matter of 32 and 64 bit comes in. A 32-bit OS means that its addresses are of size 32-bits and it can address upto 2^32 memory locations. As such, a maximum of 4GB. In a previous post of mine, I have mentioned the fact of the file system FAT32 not supporting files larger than 4GB. It is because that file system uses 32-bit addressing.

But our concern is with the OS. The OS determines the addressing and as such, a 32-bit OS allocates 32 bits to a pointer whereas a 64-bit OS allocates 64 bits to a pointer.

We can exploit this allocation to find out if the OS running on our system is 32 or 64 bit. For that, we write a small C code as follows:

#include<stdio.h>

int main(){

     int *p;

     printf(“Size is : %d\n”, sizeof(p));

     return 0;

}

Compile the code using gcc as:

gcc filename.c

And run it as:

./a.out

See what number you get as your output. If it is 4, your OS is 32-bit and if it 8, your OS is 64-bit. This is because the 4 and 8 are shown in bytes.

I was hoping to get a way of knowing what my system’s architecture is. But this method can only recognize the OS and not the system.

Happy hacking!