RSS

Create your own UNIX Shell

08 Mar

C program for creating your own UNIX Shell:- 🙂

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<wait.h>
#define BUF 200
#define ARGV 40
#define DELIM “\n\t\r “
int main(int argc, char **argv){
int i,n;
char buf[BUF+1];
char *clargs[ARGV];
int returnval;
for(;;)
{
n=1;
for(i=0;i<ARGV;i++)
clargs[i]=””;
for(i=0;i<=BUF;i++)
buf[i]=”;
write(STDOUT_FILENO, “Shell> “, 7);
/*2nd ARGUMENT IS THE NAME OF YOUR SHELL AND THE 3rd ARGUMENT IS THE LENGTH OF THE NAME*/
read(STDIN_FILENO, buf, BUF);
printf(“BUF = %s”,buf);
if(!strcmp(buf, “exit\n”))
exit(0);
clargs[0]=strtok(buf, DELIM);
while(clargs[n]=strtok(NULL,DELIM))
n++;
switch(fork())
{
case 0:if((execvp(clargs[0], &clargs[0]))<0)
exit(200);
default:wait(&returnval);
printf(“Exit status of command: %d\n”, WEXITSTATUS(returnval));
}
}
}
Advertisements
 
Leave a comment

Posted by on March 8, 2011 in Unix/Linux

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: