Running commands from C
Sometimes it is very helpful to run a program or command that is already installed on the system, rather than developing your own solution to do the same job. You can run Linux commands (and programs) by using the ‘system()’ function. It is defined as ‘int system(char *string)’, where ‘string’ can be the name of a UNIX utility, an executable shell script or a user program.
[sourcecode language=”cpp”]#include <stdio.h>
printf (“Files in the Directory are :n”);
Files in the Directory are :
drwxr-xr-x 2 kunal kunal 4096 2010-09-20 10:39 Desktop
drwxr-xr-x 2 kunal kunal 4096 2010-09-20 15:31 Documents
drwxr-xr-x 2 kunal kunal 4096 2010-09-20 08:09 Downloads
-rw-r–r– 1 kunal kunal 179 2010-08-26 16:13 examples.desktop
-rw-r–r– 1 kunal kunal 105 2010-09-20 17:37 main.c
drwxr-xr-x 2 kunal kunal 4096 2010-09-20 13:01 Music
drwxr-xr-x 2 kunal kunal 4096 2010-09-20 13:01 Pictures
drwxr-xr-x 2 kunal kunal 4096 2010-09-20 13:01 Public
drwxr-xr-x 2 kunal kunal 4096 2010-09-20 13:01 Templates
drwxr-xr-x 2 kunal kunal 4096 2010-09-21 10:12 tips
drwxr-xr-x 2 kunal kunal 4096 2010-09-20 13:01 Videos
-rw-r–r– 1 kunal kunal 105445445 2010-08-01 22:11 VMwareTools-8.4.3-282344.tar.gz
drwxr-xr-x 7 kunal kunal 4096 2010-08-01 22:11 vmware-tools-distrib
Creating and applying patches
A Patch is a piece of code which is used to fix problems in a program. Patches are very popular in the open source software world. Most open source projects accept patches from community members, and as they mature they become core members of the project. So in a way patches are tickets to becoming an open source developer. Patch files contain the line-by-line differences between the original and the modified document. These patch files can further be used by the patch utility to apply the changes to the original file.
To create a patch file:
[sourcecode language=”cpp”]$ diff -uE qt_hello_v1.cpp qt_hello_v2.cpp > patch.diff
Sample patch file contents:
[sourcecode language=”cpp”]— qt_hello_v1.cpp 2010-09-21 14:11:39.145133443 +0530
+++ qt_hello_v2.cpp 2010-09-22 07:18:17.064900182 +0530
@@ -6,6 +6,8 @@
QApplication app(argc, argv);
QPushButton hello(“Hello world!”);
+ hello.setFont(QFont(“Times”, 18, QFont::Bold));
+ QObject::connect(&hello, SIGNAL(clicked()), &app, SLOT(quit()));
To apply a patch:
[sourcecode language=”cpp”]$ patch -b < patch.diff
This command will also create a backup of the original file with the .orig extension.
One of the primary strengths of open source software is its cross-platform compatibility. By making your software cross-compatible, you are not only playing nice with the open source community but also expanding the market for your software. There are multiple ways by which you can achieve cross-platform compatibility, out of which one of the methods is called conditional compilation.
You can use conditional compilation to select particular sections of code to compile, while excluding other sections. The #if, #ifdef, #ifndef, #else, #elif and #endif directives can be used for conditional compilation.
In the following example we are placing platform-specific code which will be compiled differently on each platform.
printf (“Running on an Apple Platformn”);
printf (“Using the GNU compilern”);
printf (“Running on a Microsoft Platformn”);
Using the GNU compiler
Running on an Apple Platform
Using the GNU compiler
On Windows (With Visual C++):
Running on a Microsoft Platform