Professional Documents
Culture Documents
Mini Project 1
Mini Project 1
#include<stdio.h> // printf
#include<string.h> // to compare strings
#include<stdlib.h> // exit()
#include<unistd.h> //
#include<sys/types.h>
#include<sys/wait.h> // sleep
#include<fcntl.h> // open , write
#include<errno.h> // for showing error
#include<readline/readline.h> // prompt
#include<readline/history.h> // save the command history
// Welcomig
void init_shell()
{
clear();
char* username = getenv("USER");
printf("\n\n\n\n******************************************");
printf("\n\n\n\t\t**** Welcome %s ****",username);
printf("\n\n\t\t-BY VARSHA SINGH-");
sleep(2);
clear();
}
void mycp(char *from,char *to)
{
int fd1=open(from,O_RDONLY);
if(fd1==-1)
{
perror("Error: ");
return;
}
int fd2=open(to,O_CREAT|O_WRONLY,0777);
static char buf[1];
while(read(fd1,buf,1))
{
write(fd2,buf,1);
}
}
void myrm(char *file) // deletes only single file at a time
{
if (remove(file) == 0) {
printf("The file is deleted successfully.");
}
else
perror("Error: ");
}
void mymv(char *file,char *newName)
{
int value;
// File name is changed here
value = rename(file, newName);
// Print the result
if(!value)
printf("%s", "File name changed successfully");
else
perror("Error");
}
// Function to take input
int takeInput(char* str)
{
char* buf;
if (pid == -1) {
printf("\nFailed forking child..");
return;
}
else if (pid == 0)// child process
{
if (execvp(parsed[0], parsed) < 0) {
printf("\nCould not execute command..");
}
exit(0);
}
else {
wait(NULL); // waiting for child to terminate
return;
}
}
ListOfOwnCmds[0] = "exit";
ListOfOwnCmds[1] = "mycd";
ListOfOwnCmds[2] = "help";
ListOfOwnCmds[3] = "hello";
ListOfOwnCmds[4] = "myhead";
ListOfOwnCmds[5] = "mytail";
ListOfOwnCmds[6] = "mycat";
ListOfOwnCmds[7] = "mymv";
ListOfOwnCmds[8] = "myrm";
ListOfOwnCmds[9] = "mycp";
for (i = 0; i < NoOfOwnCmds; i++) {
if (strcmp(parsed[0], ListOfOwnCmds[i]) == 0) {
switchOwnArg = i + 1;
break;
}
}
switch (switchOwnArg) {
case 1:
printf("\nNamaste\n");
exit(0);
case 2:
chdir(parsed[1]);
return 1;
case 3:
openHelp();
return 1;
case 4:
username = getenv("USER");
printf("\nHello %s.\nMind that this is "
"not a place to play around."
"\nUse help to know more..\n",
username);
return 1;
case 5: //head -n 5 file
if(strcmp(parsed[1],"-n")==0)
myhead(atoi(parsed[2]),parsed[3]);
else
myhead(10, parsed[1]);
return 1;
case 6:
if(strcmp(parsed[1],"-n")==0)
mytail(atoi(parsed[2]),parsed[3]);
else
mytail(10, parsed[1]);
return 1;
case 7:
mycat(parsed[1]);
return 1;
case 8:
mymv(parsed[1],parsed[2]);
return 1;
case 9:
myrm(parsed[1]);
return 1;
case 10:
mycp(parsed[1],parsed[2]);
return 1;
default:
break;
}
return 0;
}
if (parsed[i] == NULL)
break;
if (strlen(parsed[i]) == 0)
i--;
}
}
int main()
{
char inputString[1000], *parsedArgs[100];
int execFlag = 0;
init_shell();
while (1) {
// print shell line
printDir();
// take input
if (takeInput(inputString))
continue;
// process
execFlag = processString(inputString,
parsedArgs);
// execflag returns zero if there is no command
// or it is a builtin command,
// 1 if it is a simple command
// execute
if (execFlag == 1)
execArgs(parsedArgs);
}
return 0;
}