Unix commands through C

Unix commands through C

Post by Jagdeep Shiru » Mon, 25 Apr 1994 06:44:20



Hello,
      I am having a problem in executing Unix commands from C. For example,

      If I want to execute a 'ls -l' command from C, I type
        system("ls -l");
      But, for a 'mv ' command, I cannot put the filenames in the
      'system' command, as I will be giving them during run time.

      That is 'system("mv filename1 filename2") should execute with
      filename1 and filename2 as variable strings, the values of which
      will be given at runtime.
      Also, after 'moving' how do I delete filename1 ?

      Could someone help me on this problem ?

      Please mail your replies to


      Thanks for your time

 
 
 

Unix commands through C

Post by Jae W. Chan » Mon, 25 Apr 1994 10:13:14



>       I am having a problem in executing Unix commands from C. For example,

>       If I want to execute a 'ls -l' command from C, I type
>         system("ls -l");
>       But, for a 'mv ' command, I cannot put the filenames in the
>       'system' command, as I will be giving them during run time.

>       That is 'system("mv filename1 filename2") should execute with
>       filename1 and filename2 as variable strings, the values of which
>       will be given at runtime.
>       Also, after 'moving' how do I delete filename1 ?

filename1 will be renamed to filename2, so if you did try:

mv filename1 filename2
rm filename1

you will get an error saying no such file.

You can pass system any string at all. It can be a statically defined
string you define during compile time, or it can be a string which is
created during run-time - same diff!

#include <stdio.h>
main( int argc, char * argv[] )
{
    int i;
    char buf[ 128 ] = "";
    for ( i=1; i < argc; i++ )
        sprintf( buf, "%s %s", buf, argv[ i ] );
    system( buf );

Quote:}

As an example, here's a program that executes its arguments.

Jae

--------------------------


 
 
 

Unix commands through C

Post by Bill 'BONES' Goodri » Wed, 27 Apr 1994 00:16:44


|>
|>       If I want to execute a 'ls -l' command from C, I type
|>         system("ls -l");
|>       But, for a 'mv ' command, I cannot put the filenames in the
|>       'system' command, as I will be giving them during run time.
|>       That is 'system("mv filename1 filename2") should execute with
|>       filename1 and filename2 as variable strings, the values of which
|>       will be given at runtime.

You can use the sprintf command to build the system command. Once you
get the filename and the new destination try something like

sprintf(cmd,"mv %s %s",filename,newfilename);
system(cmd); /*no quotes*/

|>       Also, after 'moving' how do I delete filename1 ?
|>
use the unlink(filename) , but since you did a unix system mv, the file
will be removed (unlinked).

--
==========================================================
Bill 'Bones' Goodrich BNR     || Phone (919) 991-7720
35 Davis Drive                || Fax   (919) 991-8369

==========================================================