Help required with using system() call


 
Thread Tools Search this Thread
Top Forums Programming Help required with using system() call
# 8  
Old 03-20-2008
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.

Code:
#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);
   }
}

# 9  
Old 03-24-2008
Hammer & Screwdriver Its working fine now.

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
# 10  
Old 03-24-2008
MySQL

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>='a'&& i<='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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

system call

Trying to figure out a load issue with a webserver. I have traced a php script and noticed the following connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("XX.XX.XX.XX")}, 16) = -1 EINPROGRESS (Operation now in progress) <0.000035> poll(, 1, 2000) = 1 () <0.000120>... (5 Replies)
Discussion started by: rajan007
5 Replies

2. Programming

need help with system call

hi everyone i wrote a system call and compiled the kernel succesfully... my system call is in a file in the kernel folder named my_syscall1.c (kernel/my_syscall1.c) the header file for this system call i added it in the folder include like this include/my_syscall1/my_syscall1.h my problem is... (2 Replies)
Discussion started by: demis87
2 Replies

3. Programming

system call

I have a cgi script which is called after certain time interval, which has this: system ("ls -l /tmp/cgic* | grep -v \"cgicsave.env\" | awk '{print $5}'"); During the execution of this script,the output is 0 sometimes. But due to this the system call is not working at all and doesnt o/p... (2 Replies)
Discussion started by: xs2punit
2 Replies

4. UNIX for Advanced & Expert Users

ftok system call

In the system call msgget(key,mode) for the argument key i want to use ftok call . I am really confused with the argument pathname in ftok call. I want to know the pathname of which file I want to specify over there. I am just a beginer to advanced unix plz help me out. (1 Reply)
Discussion started by: jimmyuk
1 Replies

5. Programming

C:system call

Hi I'm studing the system call. I've written a small program that return the time spent in doing some operations. Now I'd like to write one that return the time spent in user mode of a process. I'm reading that i should use the tms struct: clock_t times(struct tms *buf); struct tms {... (2 Replies)
Discussion started by: Dedalus
2 Replies

6. Shell Programming and Scripting

system call

Hi, How to write a system calls in a script ? > cd $HOME > ls -ltr thanks in advance.. (10 Replies)
Discussion started by: hegdeshashi
10 Replies

7. Programming

c system call

How the c compiler differentiates the system calls and function calls? (1 Reply)
Discussion started by: rangaswamy
1 Replies

8. UNIX for Advanced & Expert Users

System hanged. help required.

HI, I have a Fedora running box. I am not able to ping/login. I guess it got hanged. Can any one help me how to login to that box with out restart? (8 Replies)
Discussion started by: praveen_b744
8 Replies

9. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies

10. UNIX for Advanced & Expert Users

URGENT Help required regarding the use of FORK system call

I desperately wanted one of the UNIX Gurus to help me resolve my problem asap(I have to deliver the code to the client by Monday 08-oct). I have a file with around 5 million records (50 lakhs). Now my original process was taking around 30 hours to read the complete file, process each and every... (4 Replies)
Discussion started by: kkumar1975
4 Replies
Login or Register to Ask a Question