i was able to create this shell below. how can i make this shell support commands like cp, del, cd, host, quit etc. i have tried the help calling the function of help (lines 38 - 43). please would be highly greatful if any assistance or suggestions can be shown. thanks
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
main ()
{
char buf [1024];
char *args [64];
for (;;)
{
/*
*prompt for and read a command.
*/
printf ("trial_shell:");
if (gets(buf) == NULL)
{
printf ("\n");
exit (0);
}
/*
* Split the string into Arguments.
*/
parse (buf, args);
/*
* Execute the command.
*/
execute (args);
} // endof for loop
}
/*Simple help function that dumps help text to the screen*/
void help()
{
printf ("*******trial_shell usage/help options********/n");
printf ("help - prints the list of commands supported by trial_shell/n");
}
/* parse -- Splits the command in buf into individual arguments */
parse (buf, args)
char *buf;
char **args;
{
while ( *buf != NULL)
{
/*
*Strip White Spaces. Use Nulls, so
*that the previous argument is terminated automatically
*/
while ((*buf == ' ') || (*buf == '\t'))
*buf++ = NULL;
/* Save the Argument. */
*args++ = buf;
/* Skip over the argument. */
while ((* buf != NULL) && (*buf != ' ' ) && (*buf != '\t'))
buf++;
}
*args= NULL;
}
/* execute -- spawns a child process and execute the program */
execute (args)
char **args;
{
int pid,status;
/* Get a child process */
if ((pid= fork()) <0)
{
perror("fork");
exit (1);
/* NOTE. perror() produces a short error message on the standard
error describing the last error encountered during a call to a
system or library function */
}
/*
* The Child executes the code inside the if
*/
if (pid == 0)
{
execvp (*args, args);
perror (*args);
exit (1);
if (strcmp(*args, "help")== 0)
{
help(); /*call the help function*/
}
else
{
execvp (*args, args);
}
perror (*args);
exit (1);
}
while (wait (&status) !=pid)
/*empty */;
}
Anyway, I don't get what you're trying to do. You just want to print that help message when someone types "help" or you want to get it as a command line argument? If it's the former, use strcmp. If it's the latter, use getopt_long.