Hello there,
I need help with this program
its a c program called my shell and it allows custom calls like del instead of rm.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
main()
{
char buffer[1024];
char *args[256];
for(;
{
printf("Command:");
if(gets(buffer) == NULL)
{
printf("\n");
exit (0);
}
parse(buffer,args);
printf("%s\n", args[0]);
/*printf("%s\n", args[1]);
printf("%s\n", args[2]);*/
cases(args);
execute(args);
}
}
parse(buffer,args)
char *buffer;
char **args;
{
while(*buffer !=NULL)
{
while((*buffer == ' ') || (*buffer == '\t'))
*buffer++ = NULL;
*args++ = buffer;
while((*buffer !=NULL) && (*buffer != ' ') && (*buffer != '\t'))
buffer++;
}
*args=NULL;
}
execute(args)
char **args;
{
int pid,status;
if ((pid= fork())<0)
{
perror("fork");
exit(1);
}
if (pid==0)
{
execvp(*args,args);
perror(*args);
exit(1);
}
while(wait(&status) !=pid)
;
}
cases(args)
char **args;
{
char *argu;
*argu=args[0];
if (*argu == "help")
{
help();
}
else if (*argu == "del")
{
args[0]="rm";
execute(args);
}
}
help()
{
printf("help")
}
i want to replace the get with a scanf but when i do i only seem to beable to get one argument passed. also the whole cases part does not work. What i want to do there is change part of the array before it is passed up so that i could change del to rm ect.
Any help would be great. Thanks
I need help with this program
its a c program called my shell and it allows custom calls like del instead of rm.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
main()
{
char buffer[1024];
char *args[256];
for(;
{
printf("Command:");
if(gets(buffer) == NULL)
{
printf("\n");
exit (0);
}
parse(buffer,args);
printf("%s\n", args[0]);
/*printf("%s\n", args[1]);
printf("%s\n", args[2]);*/
cases(args);
execute(args);
}
}
parse(buffer,args)
char *buffer;
char **args;
{
while(*buffer !=NULL)
{
while((*buffer == ' ') || (*buffer == '\t'))
*buffer++ = NULL;
*args++ = buffer;
while((*buffer !=NULL) && (*buffer != ' ') && (*buffer != '\t'))
buffer++;
}
*args=NULL;
}
execute(args)
char **args;
{
int pid,status;
if ((pid= fork())<0)
{
perror("fork");
exit(1);
}
if (pid==0)
{
execvp(*args,args);
perror(*args);
exit(1);
}
while(wait(&status) !=pid)
;
}
cases(args)
char **args;
{
char *argu;
*argu=args[0];
if (*argu == "help")
{
help();
}
else if (*argu == "del")
{
args[0]="rm";
execute(args);
}
}
help()
{
printf("help")
}
i want to replace the get with a scanf but when i do i only seem to beable to get one argument passed. also the whole cases part does not work. What i want to do there is change part of the array before it is passed up so that i could change del to rm ect.
Any help would be great. Thanks