Submitted By: Vishaldeep Singh Section: A1805 Roll No.: B27 Reg. No.: 10802706
Submitted By: Vishaldeep Singh Section: A1805 Roll No.: B27 Reg. No.: 10802706
Vishaldeep Singh
Section: A1805
#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 */
/* 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()
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 */
void func_grep()
char fline[1024];
char *newline;
int count=0;
int o=0;
else
{
/* 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';
printf("%s: %d %s\n",parse[1],count,fline);
o++;
if(o == 0)
}
/* 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 */
else /* The error message is displayed if in any case fopen cannot open or
create the file */
/* 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;
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("\tSyntax: help");
else
FILE *from;
char ch;
while(!feof(from))
{
ch = fgetc(from);
if(!feof(from))
printf("%c",ch);
else
printf("\n\n");
break;
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 */
char ch;
/* This checks whether or not the arguments given to the command cp are correct in
number or not */
else
a=0;
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 */
a=0;
break;
fputc(ch, to);
if(ferror(to)) /* This checks for any eeror while writing to the
destination file */
a=0;
break;
if(fclose(from)==EOF) /* This checks for any error while closing the source
file i.e. saving to the file */
a=0;
if(fclose(to)==EOF) /* This checks for any error while closing the destination file
i.e. saving to the file */
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 */
/* 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 */
else
/* These are the two pointers which store the value of souurce file's file pointer
*/
FILE *from;
char ch;
if(ferror(from)) /* This checks for any error while reading from the
file */
break;
}
//fputc(ch, to);
printf("%c",ch);
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 */
else
{
/* This re-confirms that whether or not the user wants to delete the file
*/
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]))
else
void func_rename()
/* This checks whether or not the number of arguments given to the command are
correct in number or not */
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)
else
/* If any error occurs it notifies the error else it dosplays that file is
successfully copied */
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 */
DIR *dir;
chdir(path);
dir=opendir(".");
while (1)
dirslot=readdir(dir);
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)
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;
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()
else
star();
if(strcmp(parse[1],"ls") == 0)
else if(strcmp(parse[1],"grep") == 0)
else if(strcmp(parse[1],"cp") == 0)
else if(strcmp(parse[1],"del") == 0)
else if(strcmp(parse[1],"rename") == 0)
else if(strcmp(parse[1],"man") == 0)
else if(strcmp(parse[1],"touch") == 0)
else if(strcmp(parse[1],"clear") == 0)
else if(strcmp(parse[1],"cat") == 0)
else
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];
char xstr[100];
char ystr[100];
essq[0] = '\0';
strcat(essq, "\033[");
strcat(essq, ystr);
strcat(essq, "d");
strcat(essq, "\033[");
strcat(essq, xstr);
strcat(essq, "G");
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)
else
while(i != 100)
i++;
printf("\n");
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
int main()
int i;
/* Firstly when shell will run the screen would get cleared */
func_clear();
start();
while(1)
parse[i] = NULL;
break;
else
return 0;
}
***********************************************************************
***********************************************************************
***********************
6) LS SYNTAX: ls
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..............
***********************************************************************
***********************************************************************
***********************