You are on page 1of 23

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 /* This MACROS defines the max size of the commandline which
the user can enter */

#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]; /* This array is to store the whole command line entered by
the user */

char* parse[PARSE_SIZE]; /* This array is to store the parsed command line


*/

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$]: "); /* This is print the prompt each time after enter is
striked */
gets(inp); /* This will get the input commandline and will store it in the
variable inp */

temp = strtok(inp," "); /* 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) /* This loop will help the parsed input to store in the
array */

parse[i] = temp;

i++; /* Loop will end when the strtok function encounters a NULL
value */

temp = strtok(NULL, " ");

/* This function illustrates the grep functionality of bash shell */

void func_grep()

FILE *fp; /* This is a file pointer to the file to be opened */

char fline[1024];

char *newline;

int count=0;

int o=0;

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"); /* This will be printed when


there is some eeror in the syntax */

printf("\tRefer to \"man grep\"\n");

else
{

if(!(fp=fopen(parse[1],"r"))) /* This will open the file. 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. This also stores the file pointer of the file in the variable
named fp */

printf("grep : Couldnot open file : %s\n",parse[1]); /* This


will be printed when file could not be opened */

/* 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,1000,fp)!=NULL)

count++;

if(newline=strchr(fline,'\n'))

*newline='\0';

if(strstr(fline,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. Note theat this command
is case sensitive */

printf("%s: %d %s\n",parse[1],count,fline);

o++;

if(o == 0)

printf("\tSorry no match found\n");

}
/* This function is used to create a new file */

void func_touch()

FILE *p; /* This is a file pointer which stores the pointer to the file which is
to be opened */

if((p = fopen(parse[1], "wb"))==NULL) /* Opens the file and hence in the


process the file is created */

printf("\tCannot make source file.\n");

else /* The error message is displayed if in any case fopen cannot open or
create the file */

printf("\tFile successfully created\n");

/* 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;

char ch;

if((from =fopen("title","rb")) != NULL)

while(!feof(from))

ch = fgetc(from);
if(!feof(from))

printf("%c",ch);

else

printf("\n\n");

break;

/* This function provides the basic functionality of all commands. 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");

printf("\tSyntax: help");

else

FILE *from;

char ch;

if((from =fopen("title","rb")) != NULL)

while(!feof(from))

{
ch = fgetc(from);

if(!feof(from))

printf("%c",ch);

else

printf("\n\n");

break;

/* 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, *to;

char ch;

/* 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");

printf("\tRefer to \"man cp\" \n");


a=0;

else

if((from = fopen(parse[1], "rb"))==NULL) /* This checks whether we can


open the source file or not */

printf("\tCannot open source file.\n");

a=0;

if((to = fopen(parse[2], "wb"))==NULL) /* This checks whether we can


open the destination file or not */

printf("\tCannot open destination file.\n");

a=0;

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);

if(ferror(from)) /* This checks for any error while reading from the
file */

printf("\tError reading source file.\n");

a=0;

break;

if(!feof(from)) /* This will take one character at a time form the


source file to the other destination file */

fputc(ch, to);
if(ferror(to)) /* This checks for any eeror while writing to the
destination file */

printf("\tError writing destination file.\n");

a=0;

break;

if(fclose(from)==EOF) /* This checks for any error while closing the source
file i.e. saving to the file */

printf("\tError closing source file.\n");

a=0;

if(fclose(to)==EOF) /* This checks for any error while closing the destination file
i.e. saving to the file */

printf("\tError closing destination file.\n");

a=0;

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");

/* This function will be used in illustrating the use of cat command in the bash shell
*/
void func_cat()

/* 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");

printf("\tRefer to \"man cat\"");

else

/* These are the two pointers which store the value of souurce file's file pointer
*/

FILE *from;

char ch;

if((from = fopen(parse[1], "rb"))==NULL) /* This checks for any error


while opening the file */

printf("\tCannot open source file.\n");

while(!feof(from)) /* This loop executes till the end of source file is


reached */

ch = fgetc(from); /* This tahes up the one character form the file at a


time */

if(ferror(from)) /* This checks for any error while reading from the
file */

printf("\tError reading source file.\n");

break;
}

if(!feof(from)) /* This prints one character at a time form the source


path to the screen */

//fputc(ch, to);

printf("%c",ch);

if(fclose(from)==EOF) /* This is to check any error which has occured while


closing the file */

printf("\tError closing source file.\n");

printf("\n");

/* This function is to delete a file. Its a given the name of the file as the argument to
the del command */

void func_del()

char p;

/* 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");

printf("\tRefer to \"man del \" \n");

else

{
/* This re-confirms that whether or not the user wants to delete the file
*/

printf("\tAre you sure to delete file ? (Y/N) ");

scanf("%c",&p);

if(toupper(p) == 'Y')

/* This remove function is used to delete the file which is given to it as its
argument */

if(remove(parse[1]))

printf("\tCannot remove file\n");

else

/* This will be printed if the file is successfully deleter else an error


message will be displayed*/

printf("\tFile successfully deleted\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");

printf("\tRefer to \"man rename\" \n");

/* This is to rename the file */

else
{

/* The rename function is used to rename the file, It takes the old filename
as first argument and the new filename as the second argument */

if(rename(parse[1],parse[2]) != 0)

printf("\tCannot rename file\n");

else

/* If any error occurs it notifies the error else it dosplays that file is
successfully copied */

printf("\tFile successfully renamed\n");

/* This function is used to list the details of the ls command */

int lsdetails(struct stat *astatbuff)

printf(" %d ", astatbuff->st_size);

printf(" %d ", astatbuff->st_atime);

printf(" %s\n", getpwuid(astatbuff->st_uid)->pw_name);

return 0;

/* 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;

struct dirent *dirslot;

struct stat statbuff;

chdir(path);
dir=opendir(".");

while (1)

dirslot=readdir(dir);

if (dirslot==NULL) break;//note change

printf("\n%s ", dirslot->d_name);

stat(dirslot->d_name, &statbuff);

lsdetails(&statbuff);

rewinddir(dir);

while (1)

dirslot=readdir(dir);

if (dirslot==NULL) return;//note

if (strcmp(dirslot->d_name, ".")==0)

continue;

if (strcmp(dirslot->d_name, "..")==0)

continue;

stat(dirslot->d_name, &statbuff);

if (S_ISDIR(statbuff.st_mode))

printf("\n%s", dirslot->d_name);

chdir(dirslot->d_name);

recls(".");

chdir("..");

}
/* 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()

if(parse[1] != NULL)

printf("\nPlease take care of the syntax\n");

printf("Refer \"man ls\"\n");

else

recls(parse[1]);

/* 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;

/* It prints 300 stars */

for(i=1;i<=300;i++)

printf("*");

printf("\n");

/* 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");

printf("\tRefer to \"man man\" \n");

else

star();

if(strcmp(parse[1],"ls") == 0)

printf("The ls command is used to list all the directries in the present


working directry. It takes no argument.\nThe syntax: ls\n");

else if(strcmp(parse[1],"grep") == 0)

printf("The grep command is used to match a given pattern from a


given file\nThe syntax: grep <filename> <pattern> \n");

else if(strcmp(parse[1],"cp") == 0)

printf("The cp command is used to copy a file to another file\nThe


syntax: cp <source_filename> <destination_filename> \n");

else if(strcmp(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],"rename") == 0)

printf("The rename command is used to rename given file\nThe


syntax: rename <existing_filename> <new_filename> \n");
}

else if(strcmp(parse[1],"man") == 0)

printf("The man command is used to display some information of any


command\nThe syntax: man <command_name> \n");

else if(strcmp(parse[1],"touch") == 0)

printf("The touch command is used to create a new file\nThe syntax:


touch <commandname> \n");

else if(strcmp(parse[1],"clear") == 0)

printf("The clear command is used to clear the screen. It takes no


argument.\nThe syntax: clear\n");

else if(strcmp(parse[1],"cat") == 0)

printf("The cat command is used to display the contents of a file on


the terminal.\nThe syntax: cat <filename>\n");

else

printf("No match found\n");

printf("Refer to \"man man\"\n");

star();

/* This function is just ti find an alternative to the gotoxy function to place the cursor
at a particular point on the screen. Since gotoxy cannot be used in linux */
int gotoxy_func1(int x, int y)

char essq[100];

// String variable to hold the escape sequence

char xstr[100];

// Strings to hold the x and y coordinates

char ystr[100];

// Escape sequences must be built with characters

// Convert the screen coordinates to strings

sprintf(xstr, "%d", x); sprintf(ystr, "%d", y);

// Build the escape sequence (vertical move)

essq[0] = '\0';

strcat(essq, "\033[");

strcat(essq, ystr);

// Described in man terminfo as vpa=\E[%p1%dd ** Vertical position absolute

strcat(essq, "d");

//Horizontal move ** Horizontal position absolute

strcat(essq, "\033[");

strcat(essq, xstr);

// Described in man terminfo as hpa=\E[%p1%dG

strcat(essq, "G");

// Execute the escape sequence ** This will move the cursor to x, y

printf("%s", essq);

return 0;

/* This function helps clear the screen by putting 100 newlines and the placing the
cursor at (0,0) co-ordinate */

void func_clear()

{
int i=0;

/* Checks whether the number of arguments given to the clear command are correct
in number */

if(parse[1] != NULL)

printf("\tPlease take care of the syntax\n");

printf("\tRefer to man clear\n");

else

/* Prints 100 new lines */

while(i != 100)

i++;

printf("\n");

/* It places the cursor at (0,0) */

gotoxy_func1(0,0);

/* This function is used to identify which command user has entered and hence it calls
the corresponding function */

void find_command()

char* check;

check = parse[0];

if(strcmp(check,"clear") == 0)
func_clear();

else if(strcmp(check,"grep") == 0)

func_grep();

else if(strcmp(check,"cp") == 0)

func_cp();

else if(strcmp(check,"del") == 0)

func_del();

else if(strcmp(check,"rename") == 0)

func_rename();

else if(strcmp(check,"ls") == 0)

func_ls();

else if(strcmp(check,"man") == 0)

func_man();

else if(strcmp(check,"touch") == 0)

func_touch();

else if(strcmp(check,"help") == 0)

func_help();

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;

/* Firstly when shell will run the screen would get cleared */
func_clear();

/* The starting instructions would get printed */

start();

while(1)

for(i=0;i<=PARSE_SIZE;i++) /* This is to intialize the array parse[]


which indeed will store the parsed commandline */

parse[i] = NULL;

input_command(); /* This function is to take a input from the user parse


the whole command line */

if(parse[0] == NULL) /* If nothing is entered then the message


would be printed */

printf("\tNo command entered\n");

else if(strcmp(parse[0],"exit") == 0) /* if at any time user would enter


exit command the shell would end */

break;

else

find_command(); /* This is find and execute the action


corresponding to the any command entered by the user */

return 0;

}
***********************************************************************
***********************************************************************
***********************

Welcome to this small shell illustration.

By: VISHALDEEP SINGH

In this shell you can use the following commands:

1) CLEAR SYNTAX: clear

2) CP SYNTAX: cp <source_file> <destination_file>

3) DEL SYNTAX: del <file_name>

4) RENAME SYNTAX: rename <existing_filename>


<new_filename>

5) GREP SYNTAX: grep <filename> <pattern>

6) LS SYNTAX: ls

7) EXIT SYNTAX: exit

8) TOUCH SYNTAX: touch <filename>

9) MAN SYNTAX: man <command_name>

10) CAT SYNTAX: cat <filename>

11) HELP SYNTAX; help

This is just a small illustration and it entertains only the above commands

If you require any help please use the "man' command.....and 'help'...................

Thank you..............

***********************************************************************
***********************************************************************
***********************

You might also like