c - Passing an array to execvp() from the user's input -


I am trying to pass the arguments entered by the user in execvp () .

So far, I have divided the string if the user type saved as ls -a , temporary as "ls" and "-a" After that, there is a tap character, I am not sure how to properly point it out in execvp . In the examples I have seen it using the execvp (temp [position], temp) . I know that the time I am trying to do it is wrong, but I am not sure how to do it properly! At the moment I am getting a division error.

  int main (int argc, char * argv []) {char line [124]; Int size = 124; Char * temp = NULL; While (fgets (line, size, stdin)! = Null) {if (strcmp (line, "exit \ n") == 0) {exit (EXIT_SUCCESS); } Temp = strtok (line, ""); While (temporarily! = Null) {printf ("% s \ n", floating); Temp = strtok (NULL, ""); } Execvp (temp, & temp); } Return EXIT_SUCCESS; }    

Your problem is that temp is a pointer , And give you an array of the indicator execvp () .

Requires to pass:

  enum {MAX_ARGS = 64}; Four * args [MAX_ARGS]; Char ** next = args; Temp = strtok (line, ""); While (temp! = NULL) {* Next ++ = temp; Printf ("% s \ n", floating); Temp = strtok (NULL, ""); } * Next = Faucet; Execvp (args [0], args);   

Note that the argument list is given as an empty indicator in Terminator, such as argv [argc] == null in main . Clearly, I have skimmed on error checking (if you pass over 63 arguments, then you are going to the overflow args array). But this is the main idea.


With this example, I can not get the simple command of ls to work, I have tried mkdir And echo and they seem to work fine. Passing LS -1 to execvp () .

I'm not sure what the problem might be ???? All these things for me:

  • LS
  • LS-L
  • Ls -l madump.c (where madump.c I am testing the directory is going to be a file)

    I used The code was:

      #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; # Include & lt; Unistd.h & gt; Int main (zero) {four lines [1024]; (Fgets (line, size, line, stdin) = = (if (strcmp (line, "exit \ n") == 0) exit (EXIT_SUCCESS); four * args [64]; char ** Next = args; char * temp = strtok (line, "\ n"); while (temp! = NULL) {* next ++ = temp; printf ("% s \ n", temporary); temp = strtok (NULL , "\ N");} * Next = Faucet; ("Checking:"); for (next = Args; Next! = 0; Next ++) puts (* Next); Execvp (Args [0], args);} return EXIT_SUCCESS;}   

    Note that I have the \ n to strtok () token Added to the list, finally with a new line After creating the directory, its name is good for annoying friends and it is trickier of semi-educated enemies, but a nuisance from most other points of view. Note how I am actually printing the data before doing this, Passing code> execvp () is often used, I use printf ("> ; \ n", * next); Instead of a puts () instead of a clear where the argument starts and ends Is.

    The command was running from the command ( doit ):

      $ ./doit ls - L madump.c ls -l madump.c check : Ls -l madump.c-rw-r - r-- 1 jleffler employee 2352 July 28 2011 madump.c $   

    What do you get from your version?

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -