Submitted By: Vishaldeep Singh Section: A1805 Roll No. : B27 Reg. No.: 10802706 Sub.

Code: CSE 251

#include<stdio.h> #include<string.h> #include<stdlib.h> #define INP_SIZE 100 the user can enter */ /* This MACROS defines the max size of the commandline which

#define PARSE_SIZE 90 /* This MACROS defines the max size of the parsed commandline identities which the user can enter */ #include <sys/types.h> #include <unistd.h> #include <dirent.h> #include <sys/stat.h> #include <pwd.h>

char inp[INP_SIZE]; the user */

/*

This array is to store the whole command line entered by /* This array is to store the parsed command line

char* parse[PARSE_SIZE]; */ int recls(char *);

int lsdetails(struct stat*); //note alternative prototype

/* This function is used to take the command line from the user and parse the input and store it in the 2D array namely parse */ void input_command() { int i = 0; /* This is just a looping variable */

char* temp; /* This is temporary variable used to stored the parsed input for a short period of time till it is not stored in the array */

printf("[vishal$]: "); striked */

/*

This is print the prompt each time after enter is

int count=0. } } /* This function illustrates the grep functionality of bash shell */ void func_grep() { FILE *fp. /*This function is a inbuilt library function used to parse the input seperated by a delimiter and in this case we take the delimiter to be space */ while(temp != NULL) array */ { parse[i] = temp. " "). int o=0. /* /* This loop will help the parsed input to store in the Loop will end when the strtok function encounters a NULL temp = strtok(NULL. value */ i++. if(parse[1] == NULL || parse[2] == NULL || parse[3] != NULL) /* This checks whether or not the number of arguments of the command are correct or not */ { printf("\tPlease take care of the syntax\n"). } else This will be printed when . /* there is some eeror in the syntax */ printf("\tRefer to \"man grep\"\n"). variable inp */ /* This will get the input commandline and will store it in the temp = strtok(inp.gets(inp). /* This is a file pointer to the file to be opened */ char fline[1024]." "). char *newline.

if(newline=strchr(fline. } } } . Note theat this command is case sensitive */ { printf("%s: %d %s\n".'\n')) *newline='\0'.fline). } } if(o == 0) { printf("\tSorry no match found\n"). will be printed when file could not be opened */ } /* This /* This loop will go on taking the input from the specified file fp and will store it in the fline till it encounters a newline character */ while(fgets(fline. Here parse[1] denotes the name of the file since the name to be entered is to ne taken as the second string of the command line.parse[1]).parse[1]. This also stores the file pointer of the file in the variable named fp */ { printf("grep : Couldnot open file : %s\n".parse[2])!=NULL) /* This will search for the specified pattern and will find it in the each line with the help of strstr function and if found it will print the whole line if the pattern exactly matches.1000. if(strstr(fline. o++."r"))) /* This will open the file.{ if(!(fp=fopen(parse[1].fp)!=NULL) { count++.count.

if((from =fopen("title".\n"). "wb"))==NULL) process the file is created */ { printf("\tCannot make source file."rb")) != NULL) { while(!feof(from)) { ch = fgetc(from). } else /* create the file { printf("\tFile successfully created\n"). } } /* Opens the file and hence in the The error message is displayed if in any case fopen cannot open or */ /* This function just serves the purpose of displaying the basic help file at the start of the shell so that the user is aware hoe to use the shell */ void start() { FILE *from. . to be opened /* */ This is a file pointer which stores the pointer to the file which is if((p = fopen(parse[1]./* This function is used to create a new file */ void func_touch() { FILE *p. char ch.

char ch. } } } } /* This function provides the basic functionality of all commands.if(!feof(from)) printf("%c". break.ch)."rb")) != NULL) { while(!feof(from)) { ch = fgetc(from). . printf("\tSyntax: } else { FILE *from. help"). else { printf("\n\n"). If user forgets anything he can call help command which indeed works on this function */ void func_help() { if(parse[1] != NULL) { printf("\tPlease take care of the suntax\n"). if((from =fopen("title".

if(!feof(from)) printf("%c". printf("\tRefer to \"man cp\" \n"). break. . char ch. } } } } } /* This function is used to copy a file to another file in the pwd */ void func_cp() { int a =1. /* This variable is to check that if there is any error its value is put to zero and hence it is checked in the end */ /* These are the two pointers which store the value of souurce file's file pointer and destination file's file pointer */ FILE *from.ch). *to. else { printf("\n\n"). a=0. /* This checks whether or not the arguments given to the command cp are correct in number or not */ if(parse[1] == NULL || parse[2] == NULL || parse[3] != NULL) { printf("\tPlease take care of the syntax\n").

a=0. a=0. } if(!feof(from)) /* This will take one character at a time form the source file to the other destination file */ fputc(ch.\n"). /* This checks for any error while reading from the file . "rb"))==NULL) /* open the source file or not */ { printf("\tCannot open source file. a=0. if(ferror(from)) */ { printf("\tError reading source file.\n"). to). } This checks whether we can if((to = fopen(parse[2]. "wb"))==NULL) open the destination file or not */ { /* This checks whether we can printf("\tCannot open destination file. } while(!feof(from)) /* This takes the variable one by one from the source file and stores it in the character variable ch till it meets the end of the file */ { ch = fgetc(from).\n"). break.} else { if((from = fopen(parse[1].

} } /* This function will be used in illustrating the use of cat command in the bash shell */ void func_cat() . a=0.\n"). saving to the file */ { This checks for any error while closing the source printf("\tError closing source file.e.e. } if(a==1) /* This will get printed if any error had occured in the above coding since the value of a would be 0 in that case */ printf("\tFile successfully copied\n").\n"). } if(fclose(to)==EOF) /* This checks for any error while closing the destination file i. } } if(fclose(from)==EOF) /* file i.if(ferror(to)) destination file */ { /* This checks for any eeror while writing to the printf("\tError writing destination file. a=0.\n"). break. a=0. saving to the file */ { printf("\tError closing destination file.

break. char ch. "rb"))==NULL) while opening the file */ { printf("\tCannot open source file. if((from = fopen(parse[1].\n"). */ if(ferror(from)) { /* /* This tahes up the one character form the file at a This checks for any error while reading from the file printf("\tError reading source file. } /* This checks for any error reached while(!feof(from)) */ { /* This loop executes till the end of source file is time */ ch = fgetc(from). } .{ /* This checks whether or not the arguments given to the command cp are correct in number or not */ if(parse[1] == NULL || parse[2] != NULL) { printf("\tPlease take care of the syntax\n").\n"). printf("\tRefer to \"man cat\""). } else { /* */ These are the two pointers which store the value of souurce file's file pointer FILE *from.

} /* This function is to delete a file. } } printf("\n"). to). /* This is to check that the number of arguments are the same or not if(parse[1] == NULL || parse[2] != NULL) { printf("\tPlease take care of the syntax\n"). } if(fclose(from)==EOF) /* closing the file */ { This is to check any error which has occured while printf("\tError closing source file. printf("\tRefer to \"man del \" \n").ch). Its a given the name of the file as the argument to the del command */ void func_del() { char p. */ } else { /* */ This re-confirms that whether or not the user wants to delete the file .\n").if(!feof(from)) path to the screen */ /* This prints one character at a time form the source //fputc(ch. printf("%c".

else /* This will be printed if the file is successfully deleter else an error message will be displayed*/ printf("\tFile successfully deleted\n"). It takes the old filename as first argument and the new filename as the second argument */ . scanf("%c". } /* This is to rename the file */ else { /* The rename function is used to rename the file.&p).printf("\tAre you sure to delete file ? (Y/N) "). printf("\tRefer to \"man rename\" \n"). if(toupper(p) == 'Y') { argument /* */ This remove function is used to delete the file which is given to it as its if(remove(parse[1])) printf("\tCannot remove file\n"). } } } /* This function is to rename a file */ void func_rename() { /* This checks whether or not the number of arguments given to the command are correct in number or not */ if(parse[1] == NULL || parse[2] == NULL || parse[3] != NULL) { printf("\tPlease take care of the syntax\n").

if(rename(parse[1]. printf(" %s\n". while (1) { .parse[2]) != 0) printf("\tCannot rename file\n"). struct stat statbuff. printf(" %d ". astatbuff->st_atime). dir=opendir("."). else /* If any error occurs it notifies the error else it dosplays that file is successfully copied */ printf("\tFile successfully renamed\n"). chdir(path). } } /* This function is used to list the details of the ls command */ int lsdetails(struct stat *astatbuff) { printf(" %d ". astatbuff->st_size). return 0. struct dirent *dirslot. getpwuid(astatbuff->st_uid)->pw_name). } /* This function does go to the directries to knoe the INODE number of the file and then collect the file statistics of the file using stat and the it finally displays all the information on the terminal */ int recls(char *path) { DIR *dir.

//note if (strcmp(dirslot->d_name... dirslot->d_name). if (dirslot==NULL) break. lsdetails(&statbuff). if (S_ISDIR(statbuff. if (dirslot==NULL) return. &statbuff). } rewinddir(dir). ".")==0) continue. stat(dirslot->d_name.//note change printf("\n%s ". if (strcmp(dirslot->d_name. recls(". while (1) { dirslot=readdir(dir).dirslot=readdir(dir). chdir(". stat(dirslot->d_name. &statbuff).st_mode)) { printf("\n%s". ".").")==0) continue. chdir(dirslot->d_name)."). } } } /* This is the main ls illustration command it does so with the help of two functions which it calls recls and ls details */ void func_ls() { . dirslot->d_name).

if(parse[1] != NULL) { printf("\nPlease take care of the syntax\n"). } printf("\n"). } /* This function just prints a series of stars and it is just to improve the rediability of some commands and some other text also */ void star() { int i. } .i<=300. /* It prints 300 stars */ for(i=1. printf("Refer \"man ls\"\n"). printf("\tRefer to \"man man\" \n").i++) { printf("*"). } /* This functions contains some small details of all the commands hence being used in this shell and this function gets called whwnever man command is executed */ void func_man() { if(parse[1] == NULL || parse[2] != NULL) { printf("\tPlease take care of the syntax\n"). } else recls(parse[1]).

"del") == 0) { printf("The del command is used to delete a given file\nThe syntax: del <filename> \n"). } else if(strcmp(parse[1]. It takes no argument."man") == 0) { . } else if(strcmp(parse[1]."ls") == 0) { printf("The ls command is used to list all the directries in the present working directry. } else if(strcmp(parse[1]. } else if(strcmp(parse[1]. if(strcmp(parse[1]."rename") == 0) { printf("The rename command is used to rename given file\nThe syntax: rename <existing_filename> <new_filename> \n")."cp") == 0) { syntax: printf("The cp command is used to copy a file to another file\nThe cp <source_filename> <destination_filename> \n").\nThe syntax: ls\n"). } else if(strcmp(parse[1].else { star()."grep") == 0) { printf("The grep command is used to match a given pattern from a given file\nThe syntax: grep <filename> <pattern> \n").

} } /* This function is just ti find an alternative to the gotoxy function to place the cursor at a particular point on the screen.printf("The man command is used to display some information of any command\nThe syntax: man <command_name> \n")."cat") == 0) { printf("The cat command is used to display the contents of a file on the terminal.\nThe syntax: clear\n"). } star(). } else if(strcmp(parse[1].\nThe syntax: cat <filename>\n"). } else { printf("No match found\n"). Since gotoxy cannot be used in linux */ int gotoxy_func1(int x. It takes no argument. } else if(strcmp(parse[1]. printf("Refer to \"man man\"\n"). ."touch") == 0) { printf("The touch command is used to create a new file\nThe syntax: touch <commandname> \n"). } else if(strcmp(parse[1]. int y) { char essq[100]."clear") == 0) { printf("The clear command is used to clear the screen.

// Strings to hold the x and y coordinates char ystr[100]. strcat(essq. xstr). strcat(essq. ystr). // Described in man terminfo as hpa=\E[%p1%dG strcat(essq. // Execute the escape sequence ** This will move the cursor to x. /* Checks whether the number of arguments given to the clear command are correct in number */ . "d"). "%d".// String variable to hold the escape sequence char xstr[100]. "G"). //Horizontal move ** Horizontal position absolute strcat(essq. x). "%d". // Described in man terminfo as vpa=\E[%p1%dd ** Vertical position absolute strcat(essq. sprintf(ystr. return 0. // Build the escape sequence (vertical move) essq[0] = '\0'. } /* This function helps clear the screen by putting 100 newlines and the placing the cursor at (0. y printf("%s". y). essq). "\033["). strcat(essq. // Escape sequences must be built with characters // Convert the screen coordinates to strings sprintf(xstr.0) co-ordinate */ void func_clear() { int i=0. "\033[").

if(parse[1] != NULL) { printf("\tPlease take care of the syntax\n").0).0) */ gotoxy_func1(0. else if(strcmp(check. } else { /* Prints 100 new lines */ while(i != 100) { i++. printf("\tRefer to man clear\n")."cp") == 0) . } /* This function is used to identify which command user has entered and hence it calls the corresponding function */ void find_command() { char* check. } } /* It places the cursor at (0."grep") == 0) func_grep(). if(strcmp(check. check = parse[0]."clear") == 0) func_clear(). else if(strcmp(check. printf("\n").

"help") == 0) func_help(). else if(strcmp(check. else if(strcmp(check."del") == 0) func_del()."touch") == 0) func_touch().func_cp(). else if(strcmp(check. else if(strcmp(check. /* Firstly when shell will run the screen would get cleared */ func_clear(). else if(strcmp(check."man") == 0) func_man(). else if(strcmp(check."ls") == 0) func_ls()."rename") == 0) func_rename(). else if(strcmp(check."cat") == 0) func_cat(). else printf("\tBad command or file name\n"). /* If none of the above command sare matched then this line would be printed */ } /* This is the main function */ int main() { int i. . /* The starting instructions would get printed */ start().

} ************************************************************************ ************************************************************************ ********************* . This is to intialize the array parse[] */ input_command(). /* the whole command line */ This function is to take a input from the user parse be printed if(parse[0] == NULL) */ /* If nothing is entered then the message would printf("\tNo command entered\n").i++) /* which indeed will store the parsed commandline parse[i] = NULL. /* This is find and execute the action corresponding to the any command entered by the user */ } if at any time user would enter return 0. else find_command(). else if(strcmp(parse[0].i<=PARSE_SIZE."exit") == 0) /* exit command the shell would end */ break.while(1) { for(i=0.

and 'help'.......Welcome to this small shell illustration. ************************************************************************ ************************************************************************ ********************* ....... Thank you.............. clear cp <source_file> <destination_file> del <file_name> rename <existing_filename> grep <filename> <pattern> ls exit touch <filename> man <command_name> cat <filename> help 4) RENAME <new_filename> 5) 6) 7) 8) 9) 10) 11) GREP LS EXIT TOUCH MAN CAT HELP This is just a small illustration and it entertains only the above commands If you require any help please use the "man' command........ By: VISHALDEEP SINGH In this shell you can use the following commands: 1) 2) 3) CLEAR CP DEL SYNTAX: SYNTAX: SYNTAX: SYNTAX: SYNTAX: SYNTAX: SYNTAX: SYNTAX: SYNTAX: SYNTAX: SYNTAX....

Sign up to vote on this title
UsefulNot useful