execv problem

This is a discussion on execv problem within the C Programming forums, part of the General Programming Boards category; Why isn't execv working? Output: ./cli /bash/ls -l . & counter = 0 str_token = /bash/ls counter = 1 str_token ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    execv problem

    Why isn't execv working?
    Output:

    ./cli
    /bash/ls -l . &
    counter = 0 str_token = /bash/ls
    counter = 1 str_token = -l
    counter = 2 str_token = .
    counter = 3 str_token = &
    array arg[last] = (null)
    bg flag
    cmdPath = /bash/ls
    array arg[4] = (null)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main(void){
    
      char buffer[80];
      int bg_flag;
      int rv;
      char *baseNameStr;
      char *cmdArray[10];
      char *cmdPath;
    
      void chomp (char *line) {
        int len=strlen(line);
        if ((len>0) && (line[len-1]=='\n'))
          line[len-1]='\0';
      }
    
      int command_type(char *line){
        int len=strlen(line);
        if ((len>0) && (line[len-2]=='&'))
          return 1;
        else
          return 0;
      }
    
      void my_system (char *cmdPath, char *cmdArray[]) {
        pid_t pid;
        pid = fork();
        switch(pid){
        case -1:
          perror("fork failed");
          exit(1);
        case 0:
          printf("array arg[4] = %s\n", cmdArray[4]);
          execv(cmdPath, cmdArray);
          break;
        default:
          break;
          execv(cmdPath, cmdArray);
        }
        if(pid != 0) {
          int stat_val;
          pid_t child_pid;
          child_pid = wait(&stat_val);
        }
     }
    
      while(fgets(buffer, 80, stdin)!=NULL) {
    
        bg_flag = command_type(buffer);
        chomp(buffer);
        char *str_token;
        int token_counter = 0;
        str_token = strtok(buffer, " ");
    
        if(strcmp(str_token, "cd") == 0){
          printf("In chdir condition\n");
          while(str_token != NULL){
            if(token_counter == 1){
              chdir(str_token);
            }
            str_token = strtok(NULL, " ");
            token_counter++;
          }
        }else{
          while(str_token != NULL){
            if(token_counter == 0){
              cmdPath = str_token;
              cmdArray[0] = basename(str_token);
            }else{
               cmdArray[token_counter] = str_token;
            }
            printf("counter = %d str_token = %s\n", token_counter, str_token);
            token_counter++;
            str_token = strtok(NULL, " ");
          }
          cmdArray[token_counter + 1] = (char *)0;
          printf("array arg[last] = %s\n", cmdArray[token_counter + 1]);
        }
    
        if(bg_flag) {
          printf("bg flag\n");
          chomp(buffer);
          printf("cmdPath = %s\n", cmdPath);
          my_system(cmdPath, cmdArray);
        }else{
          //rv = system(buffer);
        }
        if(strcmp(buffer, "exit") == 0) {
          exit(3);
        }
      }
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Aaaaaaaaaaaaaaaaaaaaaaaaargh silly annoying mistrake warble gaaaaaaaarrrrbeleble

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    You mean aside from the fact that you've got functions declared inside functions.
    This is C, not Pascal.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. No call to execv
    By protocol78 in forum C Programming
    Replies: 4
    Last Post: 05-08-2007, 11:43 AM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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