execvp system call??

This is a discussion on execvp system call?? within the Linux Programming forums, part of the Platform Specific Boards category; in below program why was new_args[0], rather than the reference the_cmd, passed to the execvp system call? Code: /* A ...

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

    execvp system call??

    in below program why was new_args[0], rather than the reference the_cmd, passed to the execvp system call?

    Code:
    /*
        A _very_ limited shell program
    */
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <unistd.h>
    using namespace std;
    
    const int MAX    =256;
    const int CMD_MAX=10;
    char *valid_cmds = " ls  ps  df ";
    int
    main( ){
      char  line_input[MAX], the_cmd[CMD_MAX];
      char  *new_args[CMD_MAX], *cp;
      int   i;
      while (1) {
        cout << "cmd> ";
        if (cin.getline(line_input, MAX, '\n') != NULL) {
          cp = line_input;
          i  = 0;
          if ((new_args[i] = strtok(cp, " ")) != NULL) {
            strcpy(the_cmd, new_args[i]);
            strcat(the_cmd, " ");
            if ((strstr(valid_cmds, the_cmd) - valid_cmds) % 4 == 1) {
              do {
                cp = NULL;
                new_args[++i] = strtok(cp, " ");
              } while (i < CMD_MAX && new_args[i] != NULL);
              new_args[i] = NULL;
              switch (fork( )) {
              case 0:
                execvp(new_args[0], new_args);
                perror("exec failure");
                exit(1);
              case -1:
                perror("fork failure");
                exit(2);
              default:
                // In the parent we should be waiting for
                // the child to finish
                ;
              }
            } else
              cout << "huh?" << endl;
          }
        }
      }
    }

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,627
    Because the author wanted too. You should ask him.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a system call
    By estebangh in forum Linux Programming
    Replies: 1
    Last Post: 08-19-2010, 04:34 AM
  2. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  3. nanosleep() -system call does some confusing things
    By jtk in forum Linux Programming
    Replies: 5
    Last Post: 08-30-2007, 04:15 AM
  4. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  5. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 07:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21