I'm creating a shell in C and I'm getting an error. I don't know what to do, can any of you guys help? Sorry, it's probably really easy for you..
http://i176.photobucket.com/albums/w...ontworktim.png
full code:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
main()
{
char buf[1024];
char *args[64];
for(;;)
{
printf("Command:");
if(gets(buf) == NULL)
{
printf("\n");
exit(0);
}
if(strcmp(buf, "exit")==0)
{
exit(0);
}
parse(buf, args);
execute(args);
}
}
parse(buf, args)
char *buf;
char **args;
{
while(*buf != NULL)
{
while((*buf == ' ') || (*buf == '\t'))
*buf++ = NULL;
*args++ = buf;
while((*buf != NULL) && (*buf != ' ' ) && (*buf != '\t'))
buf++;
}
*args= NULL;
}
execute(args)
char **args;
{
int pid,status;
if ((pid= fork()) <0)
{
perror("fork");
exit(1);
}
if (pid==0)
{
if(strcmp(*args, "help") ==0)
{
help();
}
if(strcmp(*args, "clr") ==0)
{
clr();
}
if(strcmp(*args, "host") ==0)
{
host();
}
if(strcmp(*args, "dir") ==0)
{
dir();
}
if(strcmp(*args, "environ") ==0)
{
environment();
}
if(strcmp(*args, "echo") ==0)
{
echo();
}
if(strcmp(*args, "cp") ==0)
{
cp();
}
if(strcmp(*args, "cd") ==0)
{
cd();
}
else
{
execvp(*args, args);
}
perror(*args);
exit(1);
}
while(wait(&status) !=pid)
;
}
help()
{
printf("help - prints the list of commands supported\n");
printf("exit - exits the shell\n");
printf("clr - clears the screen\n");
printf("host - prints the hostname of the machine\n");
printf("dir - lists the contents within a directory ***\n");
printf("environ - lists the environment settings\n");
printf("echo - lists the environment settings ***\n");
exit(0);
}
clr()
{
system("clear");
exit(0);
}
host()
{
system("hostname");
exit(0);
}
dir()
{
system("ls");
exit(0);
}
environment()
{
extern char **environ;
int i =0;
for(i=0;environ[i] !=NULL;i++)
{
printf(environ[i]);
printf("\n");
}
}
echo()
{
puts("*args");
exit(0);
}
cp()
{
system("cp -piv");
exit(0);
}
cd()
{
int chdir(const char **args[2]);
int fchdir(int fd);
}