Monday, 20 July 2015

Command-line arguments in the C language!

 Many non-graphical programs receive input in the form of command-line arguments.The C language provides a method to pass parameters or arguments to the main() function. For hackers, you must know that command console is stronger than GUI and what a command console can do sometimes GUI can’t do it, most of remote attacks are possible using command console. GUI needs more memory and CPU resources than command console. Therefore, console is also faster than GUI. But remember, hacking has nothing to do with the user interfaces, it is meant for the algorithms used irrespective of the user interface, and therefore, we should focus on algorithms; instead of user interfaces. A hacker should be capable of handling any kind of user interface, may it be the interface of missile systems or the satellite control system or the interface of nuclear reactor, which may be the fusion of GUI & Command-line Interface .

In C, command-line arguments can be accessed in the main() function by including two additional arguments to the function:
int main(int argc, char *argv[])
where,
argc - Number of arguments in the command line including program name
argv[] – This is carrying all the arguments. This is a pointer to an array of character string .

Suppose you run the program  named "test" and pass arguments " this is a program "
root@r00t:~/Desktop# ./test this is a program
Where,
argc            =       5
argv[0]         =       “test”
argv[1]         =       “this”
argv[2]         =       “is”
argv[3]         =       “a”
argv[4]         =       “program”
argv[5]         =       NULL

The zeroth argument is always the name of the executing binary. So argv[0] is the name by which the program was invoked, so argc is least 1. if agrc is 1, there are no command-line arguments after the program. In the example above, argc is 5 and  argv[0], argv[1], argv[2], argv[3], argv [4] , argv[5] are "test", "this" , "is","a", "program", NULL respectively.

Now let's write a simple C code :
#include <stdio.h>

int main( int argc, char *argv[] ) 
{
   if( argc == 2 )
   {
      printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
      printf("Too many arguments supplied.\n");
   }
   else
   {
      printf("One argument expected.\n");
   }
}

This program simply check if argc value is 2 i.e. argc==2(Remeber, if agrc will 1, there are no command-line arguments after the program) then print the supplied argument. Otherwise print a message "One argument expected".
Now compile and execute with a single argument:

root@r00t:~/Desktop# ./a.out hello
The argument supplied is hello
root@r00t:~/Desktop#

               
 
                 


You may run this program with no argument, more than one arguments and see the output.

A program will want to use a command-line argument as an integer not a string.All you get from the command-line is character arrays; if you want to treat an argument as some other type that are integers or floating-point numbers, we need to converting it(character arrays) inside our program. The simplest ones to use are atoi( ), atol( ), and atof( ) to convert an ASCII character array to an int, long, and double, respectively.

standard conversion functions :
base = atof(argv[1]); // use atof with double
exponent = atoi(argv[2]); // use atoi with integer 

If you like this post or have any question, please feel free to comment!
Facebook Twitter Google+

 
Back To Top