Thread: Passing arguments to the system

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Passing arguments to the system

    I'm trying to write a shell program in c. In this example I'm attempting to change to the directory that is specified in args[1].

    Essentially I'm trying to pass one argument to the system and when I compile it tells me that I'm trying to pass to many arguments to the system.

    Is there a way to pass arguments to the system in c?

    Code:
    if(!strcmp(args[0]), "cd"))
    {
    system("cd %s", args[1])
    continue;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That code is extremely incomplete, what with extra parentheses and missing semicolons etc. It really helps other people help you if you post real code.

    Fortunately, I think I see what your problem is: using format specifiers like %s only works for printf(), not for other string-manipulating functions like system(). If you want to, you could do something like this.
    Code:
    char command[100];  /* buffer to hold the command to execute */
    sprintf(command, "cd %s", args[1]);
    system(command);
    I should warn you, however, of a caveat: system() spawns a new shell to run the command you pass it. In other words, if you ask system to run a "cd" command, it will spawn a new shell, and that shell's current working directory will be changed to the location you specify; but then that shell will exit.

    There is, unfortunately, no platform-independent way to change the working directory of your program. You can use chdir from unistd.h (try the command "man chdir") if you're on UNIX/Linux, or check MSDN for whatever the equivalent Windows function is if you're using Windows.

    P.S. It is very common practise for the arguments to main() to be named "argc" and "argv", respectively (short for "argument count" and "argument vector").
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Not directly. You can use snprintf() to build up the string, though:
    Code:
    char cmd[1024];
    snprintf(cmd, sizeof cmd, "find / -name %s", args[1]);
    system(cmd);
    If you don't have snprintf() (it's a C99 thing, though some systems have had it for a good long while) you can use sprintf(), but be careful: it is dangerous, in that you can't tell it how large the buffer is.

    In your particular case, doing system("cd /somewhere"); is likely not going to do anything. While the way system() works is implementation-defined, it'll likely fire up a shell (or equivalent), change the directory in the shell, then immediately exit, leaving your program in the same directory it was before. In this particular case you'd want chdir() on a POSIX/unix-like system, or the equivalent for whatever platform you may be on.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    actually this is more of a question than a suggestion for a solution but is it possible to use the get env and put env for the changing of the directory. from what i seen of two examples on getenv and putenv it suggest to me that it could be one use for the function even that i never used it and only tried the one sample i found once with changing the enviromental value of PATH. if it can change PATH can it not change the current directory a program is working under ? or is the function used in other context .

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by cmay View Post
    actually this is more of a question than a suggestion for a solution but is it possible to use the get env and put env for the changing of the directory. from what i seen of two examples on getenv and putenv it suggest to me that it could be one use for the function even that i never used it and only tried the one sample i found once with changing the enviromental value of PATH. if it can change PATH can it not change the current directory a program is working under ? or is the function used in other context .
    $PATH is not the current directory; it's the search path for executables. At any rate, simply changing an environment variable won't change your directory, at least not on any platform I'm aware of. As I noted, use chdir() on POSIX systems.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by cas
    If you don't have snprintf() (it's a C99 thing, though some systems have had it for a good long while) you can use sprintf(), but be careful: it is dangerous, in that you can't tell it how large the buffer is.
    Code:
        char line[100];
    
        sprintf(line, "%.*s", sizeof line - 1, argument);

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The trouble comes when you have a command like "cd %s ; ./make.sh ; cd -". Then you have to count the characters that aren't part of format specifiers and subtract that too. And it's difficult if you have two %s's, for example.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  2. passing unspecialised template classes as function arguments
    By reanimated in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2006, 11:50 AM
  3. passing arguments
    By kas2002 in forum C++ Programming
    Replies: 12
    Last Post: 07-16-2002, 10:38 AM
  4. Passing system time to a function
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 01:56 PM
  5. passing a parameter to a system invoke
    By iain in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2001, 09:21 AM

Tags for this Thread