Find the answer to your Linux question:
Results 1 to 3 of 3
Hi everybody. I am trying to simulate a shell. So what I do is checking of having the parameters from standard input, suc as "/bin/ls -l /home/france/Documents", and then passing ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    11

    Trouble in passing char * vector argument

    Hi everybody. I am trying to simulate a shell. So what I do is checking of having the parameters from standard input, suc as "/bin/ls -l /home/france/Documents", and then passing them to function execute, which at some point calls execvp(argv[0],argv)
    The problem is that I don't succeed in using these arguments, while if I call execvp(paramList[0],paramList) it works!!!! Where paramList is exactly what I would put on standard input, but defined statically.

    The code::


    Code:
    #include<unistd.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define MAXARG 20
    
    int getargs (int * pargc,char * argv[]);
    static void execute (int argc, char *argv[]);
    void copia(char * argv[],int argc,char * temp);
    
    int main (void) {
      int i=0;
      char * argv [MAXARG]; int argc;
      if (1) {          
        printf("ready: ");         
        if (getargs(&argc,argv)!= -1) {
          printf("Andato tutto bene\n");
    
          while(strcmp(argv[i],"NULL")!=0)
    	{
    	  printf("argv[%d] è: %s\n",i,argv[i]);
    	  i++;
    	}
    
          execute(argc, argv);
          
        }
        else
          printf("Oops!\n");
      }
      return(0);
    }
    
    
    
    
    int getargs (int * argc,char * argv[])
     {
       int i=0;
       char * str=malloc(MAXARG*sizeof(char));
       scanf("%s",str);
       do{
         argv[i]=malloc(MAXARG*sizeof(char *));
         strcpy(argv[i],str);
         i++;
         scanf("%s",str);
       } while (strcmp(str,"NULL")!=0);
       argv[i]=malloc(MAXARG*sizeof(char *));
       strcpy(argv[i],str);
       *argc=i;
       return(0);
     }
    
    
    static void execute (int argc, char *argv[])
    {   
      int i=0;
      char *const paramList[] = 
       {"/bin/ls", "-l", "/home/france/Documents", "NULL"};
      pid_t pid;
    
          while(strcmp(paramList[i],"NULL")!=0)
    	{
    	  printf("paramList[%d] è: %s\n",i,paramList[i]);
    	  i++;
    	}
          i=0;
          while(strcmp(argv[i],"NULL")!=0)
    	{
    	  printf("argv[%d] è: %s\n",i,argv[i]);
    	  i++;
    	}
      switch(pid=fork())
        {
        case -1: 
          {
          perror ("Cannot fork");
          break;
          }
        case 0:
          {
    	execvp(argv[0],argv);
    	perror("Cannot exec");
    	break;
          }
        default: exit(0);
        }
    }
    Last edited by Cabhan; 06-20-2010 at 06:38 PM. Reason: Added [code] tags

  2. #2
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    Without examining the logic of your program, I see that you are using the literal string "NULL", which I suppose you think is a NULL pointer. It isn't. The symbol NULL is probably what you want.
    --- rod.
    Stuff happens. Then stays happened.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    School work problems are not permitted by terms of use of these forums.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...