Tuesday, December 25, 2012

Linux: The power of Open Source

Everyone that had a go with Linux, has an idea of what Open Source Software means. For those who don't, open source means that the code of the programming language that the software is written and in general the pre-compiled units of it are available to the public.
This gives a lot of advantages. Many programmers can contribute to the code or users that know a couple of things in programming can modify a piece of code and re-compile it to cover their needs and then maybe share it with others that have the same needs. That's how every open source community has been working so far.
We've all heard about it, but few of us have seen it. How do we modify the code? How do we find it? There are a lot of possibilities and options here. For example, most recent programs for Ubuntu are written in Python (e.g. Gwibber). In those programs it won't be difficult to find the code, since Python is not compiled but it's an interpreter based environment. In other words, you can see the code if you open the program with a text editor.

Let's see the real stuff now with an example. We came along the program "fortune" recently and tried to run fortune -f in Fedora, which displays the files that fortune looks at to generate a fortune cookie. We wanted to pass the output data through pipe but it didn't work:
fortune -f | grep humour
After a couple of failures, we assumed that the output was redirected to STDERR instead of STDOUT. But, this is unacceptable code practice. Why send data that are normal output to the error stream? So we downloaded the fortune source rpm package. Here is the process that followed:

  • Find the repository of the fedora-mod.rpm, google is your friend.
  • Find the source code which is in form fortune-mod.src.rpm
    Create a folder in your home directory (/home/username/) named rpmbuild and inside it a directory named SOURCES 
  • Install developer tools and recode-devel: sudo yum install @development-tools recode-devel
  • Extract the rpm in the /home/username/SOURCE/ directory.
  • Modify the code, in that case we found fortune.c and changed in the function print_list() every fprintf(stderr... to fprintf(stdout... (yes it was that simple)
  • To compile it and pack it run rpmbuild -ba fortune-mod.spec.
You can find the rpm now in the rpmbuild/RPMS directory from where you can install it but first run yum remove fortune-mod if you already have it install in your machine.

We've found the source from here