LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to import arguments to system() function (https://www.linuxquestions.org/questions/programming-9/how-to-import-arguments-to-system-function-305774/)

skie_knite007 03-24-2005 11:47 PM

How to import arguments to system() function
 
I need to create a iso fs using a C pgm. I can do it juz by calling system("mkisofs ---"); but how I can make it user interactive by providing arguments(variables) to this function

nixcraft 03-25-2005 12:33 AM

Try something as follows:
Code:

char s[100];
char cmd[200];

...
gets(s);
....
fprintf(cmd,"mkisofs %s",s);
...
system(cmd);


skie_knite007 03-25-2005 06:35 AM

Thanx nixcraft,

But I 've got a problem.
A warning message like
" passing arg 1 of fprintf from incompatible pointer type " is displayed and the pgm problably is not working.
How to fix it out.

TheLinuxDuck 03-25-2005 08:05 AM

Actually, you'd want to use sprintf, not fprintf. fprintf is for printing to file handles/streams. Secondly, when getting user input, one should never use an input routine that does not restrict the # of chars the user is allowed to enter.
Code:

  char s[100];
 
  gets(s);  // WRONG
  fgets(s, 99, stdin); // RIGHT

And finally, don't use system. Use popen.

One reason for this is in the system man page:
Quote:

During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

nixcraft 03-25-2005 08:12 AM

Opps :o it should be sprintf and not fprintf sorry...
here is the right one

Code:

#include<stdio.h>
main()
{
  char s[100];
  char cmd[200];
  printf("Enter arg : ");
  fgets(s,100,stdin);
  sprintf(cmd,"ls %s",s);
  system(cmd);
}


skie_knite007 03-25-2005 08:49 PM

Thanx for u all,

It did really work


All times are GMT -5. The time now is 12:30 AM.