Passing argument to command in C

Hello all,

New to C and I'm trying to write a program which can run a unix command. Would like to have the option of giving the user the ability to enter arguments e.g for "ls" be able to run "ls -l".

I would appreciate any help.

Thanks

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

main()
{
   int menuchoice;
int execv(const char* path, char* const argv[]);

   do
 {
   
      printf("  Select an action to perform:\n");
      printf("  1. This will execute the sleep command\n");
      printf("  2. This will execute the ps command\n");
      printf("  0. This will exit the script\n");
    
                
     scanf("%d", &menuchoice);
      switch (menuchoice)
        { 
         case 1:
               if (fork())
                  wait(0);
                else
        char* args[] = {"sleep","30","60","90", (char* ) NULL};
          printf("Please enter any of the following sleep time\n");
            printf("30,60,90\n");
        execv("sleep", args);
                       break;
          case 2:
               if (fork())
                  wait(0);
               else
                  system("ps -ef", "ps -ef", (char *)NULL);
                  break;
          case 0:
        printf("Goodbye\n");
return 0;
               break;
          default:
               printf("Please enter only 0-6\n");

   }
  }

   while (menuchoice != 0);
}

You need to take advantage of the complete definition of the main function declarion:

main() // this wont work for your application

int main(int argc, char *argv[]); // this is what you need

the argc is the argument count, this always has at least one argument passed to it "The program name", so you will need to check if the user entered an argument with something like

if (argc > 1)
// handle this

I will leave it up to you to research argv

---------- Post updated at 11:50 AM ---------- Previous update was at 11:43 AM ----------

ah misunderstood your question

you need something like this:

put you need to parse your array of strings

pid_t  pid;
     int    status;

     if ((pid = fork()) < 0) {     /* fork a child process           */
          printf("*** ERROR: Child failedd\n");
          exit(1);
     }
     else if (pid == 0) {          /* for the child process:         */
          if (execvp(*argv, argv) < 0) {     /* execute the command  */
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
     }
     else {                                  /* for the parent:      */
          while (wait(&status) != pid)       /* wait for completion  */
               ;
     }

Many Thanks @ Zacharoni.

Not sure i follow you though, perhaps i wasnt clear with my problem statement.

If you look at case 2 of my code, i have the "ps -ef" command hardcoded into my program, i do not want to do that - i want the user to have the ability to use the "ps" command with any argument [a, e, f , l, x] if selected from the menu.

Assemble the command into a character array. Put in different strings depending on what the user wants.

char str[64];
strcpy(str, "ps ");
strcat(str, " -ef");
system(str);