In: Computer Science
Create a shell in C language
1) Create an argument tokenizer then write a main() function that prints a prompt inputed by user, accepts input, and tokenizes the input.
2) Use the argument vector to an executeCmd() function with int executeCmd(char **args);
3) The function should return a -1 if an error, or a zero otherwise. Inputing an “x” in the prompter will exit the program.
4) Write an executeCmd() function that can execute any program in the background and redirect the output of any program to a file. Any number of functions can be executed in executeCmd() such as listing the files in the current directory, listing files in long format or displaying current working directory (ls, ls -a, pwd).
5) The program should run in the background working on the function, but the prompt should be available to take new requests ($ cat prog.c &).
6) Function execBackground() should be used to determine when a job should run in the background. The function will strip the ‘&’ character from the argument vector. This function will also let the output of a running program to be redirected to a file ($ grep if prog.c > foo).
7) The maker should include two source files and one header file as well as a makefile. The main() function, the executeCmd() function and all supporting functions will be in a file program_exec.c and the function prototype for executeCmd() program_exec.h.
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/utsname.h>
#include<pwd.h>
#define _GNU_SOURCE
int executeCmd(char *args){
int rec;
rec =
system(args);
if(rec < 0)
return -1;
else
return 0;
}
main()
{
int i=0, j, k=0,
err;char a[50],*b;
struct stat s;
stat("prompt.c",&s);
struct passwd
*pwd;
pwd=getpwuid(s.st_uid);
struct utsname
u;
if(uname(&u)==-1);
b=u.nodename;
for(j=0;b[j]!='.';j++);
b[j]='\0';
while(1)
{
if(fork() == 0)
{
printf("<%d.%s@%s>",++i,pwd->pw_name,u.nodename);
gets(a);
err = executeCmd(a);
if(err == -1){
perror("executeCmd:");
break;
}
}
else
wait(0);
}
}