#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <link.h>
#include <sys/wait.h>
#include <errno.h>
#include <myPipe.h>
#include <tokenize.h>
#include <myCD.h>
int main(){
char input_line[256]; //input line
char *command[100];
while (1) { //print consegatively the shell until the user press "exit"
//char *path[100];
//getcwd(path, 100);
//printf("\n[MY_SHELL ] %s ", path);
printf("\n[MY_SHELL ]");
fgets(input_line,sizeof(input_line),stdin);
input_line[strlen(input_line)-1] = '\0';
//if user wants to exit from the current shell
if(strstr(input_line,"exit")!=NULL) {
exit(0); //exit from the shell
}
//if user wants to direct to another directory
else if(strstr( input_line,"cd")!=NULL) //if user uses "cd" command to change the directory
{
myCD(input_line);//run myCD method
}
else if(strstr( input_line,"$PATH")!=NULL){
printf("path: %s \n",getenv("PATH"));
}
//if user asks for pipe function
else if(strstr(input_line,"|")!=NULL){
myPipe(input_line);//run pipe method
}
else{
tokenize(input_line, command);
//use the execvp function to run the process i.e the user's command
if(!fork()){
if (execvp(command[0],command) == -1)
{
printf("Command not found\n");
}
}else{
wait(NULL);
}
}
}
return 0;
}
myPipe
void myPipe(char *input_line){
int temp;
int index=0;
for(temp = 0; temp < strlen(input_line); temp++){
if(input_line[temp] == '|'){
index=temp;
break;
}
}
char one[128];
char two[128];
int len=strlen(input_line);
strncpy(one, input_line, index);
one[index] = '\0';
//Copying two characters from the middle of string S to D:
//strncpy(two, &string[5], 2);
strncpy(two, &input_line[index+1], (len-index+1));
two[len-index-1] = '\0';
int pfds[2];
pipe(pfds);
if (!fork()) {
close(1); /* close normal stdout */
dup(pfds[1]); /* make stdout same as pfds[1] */
close(pfds[0]); /* we don't need this */
execlp(one,one , NULL);
}
else {
close(0); /* close normal stdin */
dup(pfds[0]); /* make stdin same as pfds[0] */
close(pfds[1]); /* we don't need this */
execlp(two,two , NULL);
}
}
tokenize
void tokenize(char *input_line, char **z)
{
int x = 0; //used to continue through the array
int a = 0;
char *tray[100]; //to keep elements that are tokanized
tray[a] = strtok(input_line, " "); //get pointer to first token and store in 0 place in array
while(tray[a]!= NULL){ //make sure a pointer is found
a++;
tray[a] = strtok(NULL, " "); //continue to tokenize the string
}
for(x = 0; x< a; x++){ //for loop to assign tokenz in to an array
z[x] = tray[x];
}
z[a] = NULL;
}
myCD
char *cdir[100];
int dec;
void myCD(char *input_line){
//create a char pointer array to be passed to the chdir function
//tokenize the command enter by user
tokenize(input_line, cdir);
//pass the first element of the pointer arrat to the chdir function
dec=chdir(cdir[1]);
if(dec==-1){
printf("No such file or directory\n");//continue;
}
}
PS
myPipe, tokenize and myCD included as header files. If someone wants to make use of this code, save them in separate 3 files and run the main code
No comments:
Post a Comment