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);
}