Help required with using system() call

Hi,

I try to write a C program which lists the output of a paticular command with all the available options (a to z) for the command in the directory of execution.

This program will generate the output if the option exists for the particular command else it will display some message saying "Option not available for the command".

ex: ls command has option "l" but not a option "y".
It will display the output on ls -l execution and error message on ls -y execution.

I am passing the "command to be executed(ex:ls,more)" in the command line argument. With in the program, I am using a while loop which initiated with 'a' and runs up to 'z'. I am using the "system()" unix call for the execution of command. please find my program below:

# include <stdio.h>
# include <stdlib.h>
# include <strings.h>

main(argc,argv)
int argc;
char *argv[];
{
    int i;
    printf("%d is arg\nc",argc);
    printf("arg0 is %s\n",argv[0]);
    printf("arg1 is %s\n",argv[1]);

    if(argc!=2)
    {
        printf("usage Wrong");
        exit(0);
    }

    printf("The command you ahve entered is %s\n",argv[1]);
    printf("Let us display the various outputs for different options for the command");

    i='a';
    while(i>='a'&& i<='z')
    {
        printf("\n option is %d ASCII is %c",i,i);
        system("cat > /tmp/cmd_opt.sh << EOF
                                         echo cmd is $1
                                         echo opt is $2
                                         $1 -$2
                                         EOF ");
        system("sh /tmp/cmd_opt.sh argv[1] i");
        i++;
    }

}

on compiling the program with cc, its giving a warning message "multi-line string literals are deprecated"

But however on execution of the program, the output is as follows:

[ramki@lindesk3 ramki]$ cc options_wth_exe_cmds.c -o cmd_opt
options_wth_exe_cmds.c:28:16: warning: multi-line string literals are deprecated
[ramki@lindesk3 ramki]$ ./cmd_opt ls
2 is arg
carg0 is ./cmd_opt
arg1 is ls
The command you ahve entered is ls
Let us display the various outputs for different options for the command
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 97 ASCII is a
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 98 ASCII is b
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 99 ASCII is c
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 100 ASCII is d
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 101 ASCII is e
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 102 ASCII is f
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 103 ASCII is g
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 104 ASCII is h
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 105 ASCII is i
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 106 ASCII is j
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 107 ASCII is k
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 108 ASCII is l
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 109 ASCII is m
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 110 ASCII is n
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 111 ASCII is o
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 112 ASCII is p
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 113 ASCII is q
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 114 ASCII is r
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 115 ASCII is s
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 116 ASCII is t
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 117 ASCII is u
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 118 ASCII is v
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 119 ASCII is w
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 120 ASCII is x
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 121 ASCII is y
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
 option is 122 ASCII is z[ramki@lindesk3 ramki]$

Please help me in this..

Thanks,
Ramkrix.

You are going to have to write your multi-line command into a file, chmod the file to set the execute bit, then call system("./filename.sh");

Why are you using C for this in the first place? Sounds like a shell script would be better.

Most UNIX commands take alphanumeric characters as command line options. ls takes both uppercase/lowercase letters of the alphabet as well as numbers as command line options. So testing for i between 'a' and 'z' is too exclusive. It should take into account uppercase alphabets and numbers ranging from 0 - 9 as well.

Ya I knew I cud do it simply by using a shell script. But the requirement is to be done with an C program and hence I did with C

Hi Now, I have changed the source code as follows by putting that multiple literals in the system() earlier to an shellscript ./filename.sh now.

But I have confusion how to pass my argv[1] and the value of i as argument to the shell script. To ask simple: How system() call recognize the command line args passed to an C program.

SOURCE CODE:

# include <stdio.h>
# include <stdlib.h>
# include <strings.h>

main(argc,argv)
int argc;
char *argv;
{
int i;
/char *cmd,*opt;/
printf("%d is arg\nc",argc);
printf("arg0 is %s\n",argv[0]);
printf("arg1 is %s\n",argv[1]);

if\(argc!=2\)
\{
    printf\("usage Wrong"\);
    exit\(0\);
\}

printf\("The command you ahve entered is %s\\n",argv[1]\);
printf\("Let us display the various outputs for different options for the command"\);

i='a';
while\(i&gt;='a'&& i&lt;='z'\)
\{
    printf\("\\n option is %d ASCII is %c",i,i\);
    system\("./filename.sh argv[1] i"\);
    i\+\+;
\}

}

OUTPUT:

[ramki@lindesk3 ramki]$ cc options_wth_exe_cmds.c -o cmd_opt
[ramki@lindesk3 ramki]$ ./cmd_opt ls
2 is arg
carg0 is ./cmd_opt
arg1 is ls
The command you ahve entered is ls
Let us display the various outputs for different options for the command
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 97 ASCII is a
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 98 ASCII is b
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 99 ASCII is c
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 100 ASCII is d
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 101 ASCII is e
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 102 ASCII is f
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 103 ASCII is g
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 104 ASCII is h
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 105 ASCII is i
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 106 ASCII is j
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 107 ASCII is k
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 108 ASCII is l
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 109 ASCII is m
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 110 ASCII is n
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 111 ASCII is o
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 112 ASCII is p
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 113 ASCII is q
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 114 ASCII is r
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 115 ASCII is s
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 116 ASCII is t
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 117 ASCII is u
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 118 ASCII is v
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 119 ASCII is w
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 120 ASCII is x
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found
option is 121 ASCII is y
cmd is argv[1]
opt is i
./filename.sh: line 3: argv[1]: command not found

Please help me in get rid of these..

You are always passing the string "./filename.sh argv[1] i". You need to modify your code to pass the actual values of argv[1] and i.

Putting Code tags and indenting the source listings makes it so much easier to follow. The statements for initializing and incrementing i and the while are unneccessary as a single for loop can replace 'em.

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
main(argc,argv)
int argc;
char *argv[];
{
   int i;
   char cmd[1024];
   printf("%d is arg\n",argc);
   printf("arg0 is %s\n",argv[0]);
   printf("arg1 is %s\n",argv[1]);
   if(argc!=2)
   {
      printf("usage Wrong\n");
      exit(0);
   }
   printf("The command you ahve entered is %s\n",argv[1]);
   printf("Let us display the various outputs for different options for the command\n");
   for (i= 'a'; i <= 'z'; ++i)
   {
      printf("\n option is %d ASCII is %c\n",i,i);
      sprintf(cmd, "%s -%c", argv[1], i);
      system(cmd);
   }
}

Hi Shamrock,

Thankyou very much for the solution rendered. Its working very fine now. Sorry for the delayed reply. I wasn't logged for the past 3 days.

Thanks,
Ramkrix

Hi Shamrock/fpmurphy,

I have changed the code using sprintf and is working fine and I have one more doubt with the output of the program now.

Code:

# include <stdio.h>
# include <stdlib.h>
# include <strings.h>

main(argc,argv)
int argc;
char *argv[];
{
int i;
/char *cmd,*opt;/
char cmd[1024];
printf("%d is arg\nc",argc);
printf("arg0 is %s\n",argv[0]);
printf("arg1 is %s\n",argv[1]);

if\(argc!=2\)
\{
    printf\("usage Wrong"\);
    exit\(0\);
\}

printf\("The command you ahve entered is %s\\n",argv[1]\);
printf\("Let us display the various outputs for different options for the command"\);

i='a';
while\(i&gt;='a'&& i&lt;='z'\)
\{
    printf\("\\n option is %d ASCII is %c",i,i\);
    sprintf\(cmd,"%s -%c",argv[1],i\);
    system\(cmd\);
    i\+\+;
\}

}

Output:

[ramki@lindesk3 ramki]$ ./cmd_opt ls
2 is arg
carg0 is ./cmd_opt
arg1 is ls
The command you ahve entered is ls
Let us display the various outputs for different options for the command
. .bash_history .bashrc db_cnnct_stps .gtkrc myshell sys_info xplrng_c
.. .bash_logout CCS .emacs hi.txt .mysql_history sysint_ex
ascii .bash_profile cmd_opt filename.sh .kde options_wth_exe_cmds.c .viminfo option is 97 ASCII is a
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 98 ASCII is b
options_wth_exe_cmds.c hi.txt cmd_opt sysint_ex filename.sh db_cnnct_stps CCS xplrng_c myshell ascii sys_info
option is 99 ASCII is c
.
option is 100 ASCII is d
ls: invalid option -- e
Try `ls --help' for more information.
option is 101 ASCII is e
. .emacs .bashrc .viminfo .mysql_history cmd_opt xplrng_c CCS
.. .bash_logout .gtkrc myshell db_cnnct_stps filename.sh sys_info
.kde .bash_profile ascii .bash_history sysint_ex hi.txt options_wth_exe_cmds.c
option is 102 ASCII is f
total 52
-rw-r--r-- 1 users 1426 Mar 3 16:29 ascii
drwxr-xr-x 2 users 4096 Mar 13 15:55 CCS
-rwxr-xr-x 1 users 5434 Mar 24 10:09 cmd_opt
-rw-r--r-- 1 users 122 Mar 14 14:04 db_cnnct_stps
-rwxr--r-- 1 users 37 Mar 20 17:28 filename.sh
-rw-r--r-- 1 users 5698 Mar 24 10:20 hi.txt
drwxr-xr-x 2 users 4096 Mar 11 11:43 myshell
-rw-r--r-- 1 users 690 Mar 24 10:24 options_wth_exe_cmds.c
drwxr-xr-x 2 users 4096 Mar 3 12:23 sys_info
drwxr-xr-x 2 users 4096 Mar 20 17:47 sysint_ex
drwxr-xr-x 7 users 4096 Mar 12 15:35 xplrng_c
option is 103 ASCII is g
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 104 ASCII is h
147745 ascii 148103 db_cnnct_stps 908434 myshell 908425 sysint_ex
908426 CCS 148232 filename.sh 148221 options_wth_exe_cmds.c 907491 xplrng_c
147812 cmd_opt 148142 hi.txt 908388 sys_info
option is 105 ASCII is i
ls: invalid option -- j
Try `ls --help' for more information.
option is 106 ASCII is j
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 107 ASCII is k
total 52
-rw-r--r-- 1 ramki users 1426 Mar 3 16:29 ascii
drwxr-xr-x 2 ramki users 4096 Mar 13 15:55 CCS
-rwxr-xr-x 1 ramki users 5434 Mar 24 10:09 cmd_opt
-rw-r--r-- 1 ramki users 122 Mar 14 14:04 db_cnnct_stps
-rwxr--r-- 1 ramki users 37 Mar 20 17:28 filename.sh
-rw-r--r-- 1 ramki users 5698 Mar 24 10:20 hi.txt
drwxr-xr-x 2 ramki users 4096 Mar 11 11:43 myshell
-rw-r--r-- 1 ramki users 690 Mar 24 10:24 options_wth_exe_cmds.c
drwxr-xr-x 2 ramki users 4096 Mar 3 12:23 sys_info
drwxr-xr-x 2 ramki users 4096 Mar 20 17:47 sysint_ex
drwxr-xr-x 7 ramki users 4096 Mar 12 15:35 xplrng_c
option is 108 ASCII is l
ascii, CCS, cmd_opt, db_cnnct_stps, filename.sh, hi.txt, myshell, options_wth_exe_cmds.c, sys_info, sysint_ex, xplrng_c
option is 109 ASCII is m
total 52
-rw-r--r-- 1 578 100 1426 Mar 3 16:29 ascii
drwxr-xr-x 2 578 100 4096 Mar 13 15:55 CCS
-rwxr-xr-x 1 578 100 5434 Mar 24 10:09 cmd_opt
-rw-r--r-- 1 578 100 122 Mar 14 14:04 db_cnnct_stps
-rwxr--r-- 1 578 100 37 Mar 20 17:28 filename.sh
-rw-r--r-- 1 578 100 5698 Mar 24 10:20 hi.txt
drwxr-xr-x 2 578 100 4096 Mar 11 11:43 myshell
-rw-r--r-- 1 578 100 690 Mar 24 10:24 options_wth_exe_cmds.c
drwxr-xr-x 2 578 100 4096 Mar 3 12:23 sys_info
drwxr-xr-x 2 578 100 4096 Mar 20 17:47 sysint_ex
drwxr-xr-x 7 578 100 4096 Mar 12 15:35 xplrng_c
option is 110 ASCII is n
total 52
-rw-r--r-- 1 ramki 1426 Mar 3 16:29 ascii
drwxr-xr-x 2 ramki 4096 Mar 13 15:55 CCS
-rwxr-xr-x 1 ramki 5434 Mar 24 10:09 cmd_opt
-rw-r--r-- 1 ramki 122 Mar 14 14:04 db_cnnct_stps
-rwxr--r-- 1 ramki 37 Mar 20 17:28 filename.sh
-rw-r--r-- 1 ramki 5698 Mar 24 10:20 hi.txt
drwxr-xr-x 2 ramki 4096 Mar 11 11:43 myshell
-rw-r--r-- 1 ramki 690 Mar 24 10:24 options_wth_exe_cmds.c
drwxr-xr-x 2 ramki 4096 Mar 3 12:23 sys_info
drwxr-xr-x 2 ramki 4096 Mar 20 17:47 sysint_ex
drwxr-xr-x 7 ramki 4096 Mar 12 15:35 xplrng_c
option is 111 ASCII is o
ascii CCS/ cmd_opt db_cnnct_stps filename.sh hi.txt myshell/ options_wth_exe_cmds.c sys_info/ sysint_ex/ xplrng_c/
option is 112 ASCII is p
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 113 ASCII is q
xplrng_c sysint_ex sys_info options_wth_exe_cmds.c myshell hi.txt filename.sh db_cnnct_stps cmd_opt CCS ascii
option is 114 ASCII is r
total 52
4 ascii 8 cmd_opt 4 filename.sh 4 myshell 4 sys_info 4 xplrng_c
4 CCS 4 db_cnnct_stps 8 hi.txt 4 options_wth_exe_cmds.c 4 sysint_ex
option is 115 ASCII is s
options_wth_exe_cmds.c hi.txt cmd_opt sysint_ex filename.sh db_cnnct_stps CCS xplrng_c myshell ascii sys_info
option is 116 ASCII is t
cmd_opt options_wth_exe_cmds.c hi.txt CCS myshell sys_info sysint_ex xplrng_c ascii filename.sh db_cnnct_stps
option is 117 ASCII is u
CCS ascii cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 118 ASCII is v
ls: option requires an argument -- w
Try `ls --help' for more information.
option is 119 ASCII is w
ascii CCS cmd_opt db_cnnct_stps filename.sh hi.txt myshell options_wth_exe_cmds.c sys_info sysint_ex xplrng_c
option is 120 ASCII is x
ls: invalid option -- y
Try `ls --help' for more information.
option is 121 ASCII is y
ls: invalid option -- z
Try `ls --help' for more information.
option is 122 ASCII is z

ACtually the "option is 97 ASCII is a" has to be printed first and then its output ". .bash_history .bashrc db_cnnct_stps .gtkrc myshell sys_info xplrng_c
.. .bash_logout CCS .emacs hi.txt .mysql_history sysint_ex
ascii .bash_profile cmd_opt filename.sh .kde options_wth_exe_cmds.c .viminfo"

I experienced the same thing with another C program.. Where the unix system call [which has been called 5 times in a while loop] output is printed first and then the output of printf 5 times gets printed [printf is used preceding to the unix system vall in that particular program] . Are the unix system calls functions that much faster so that the sequence of program written is not followed?

Please explain.

Thanks,
Ramkrix