/******************************
* Header Files *
*****************************/
#include <iostream>
#include <string.h>
#include <ctime>
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sstream>
using namespace std;
/****************************
* Gloal Variables *
**************************/
int argc = 0; //this will count the number of parameters in the command entered
string words[4]; //array of words
/****************************
* Function Prototypes *
***************************/
int date();
int ls();
int pwd();
int cal();
int mv(const char*, const char*);
int cp(const char*, const char*);
void rm(const char*);
//Each command will passed to these functions as a string parameter for implementation
//this function will return the number of arguments
int get_argument_count(string);
//this function will execute the command
void execute(string);
// this function will split the command line to separate words
// and will save them to an array of strings
void split_to_words(string);
/*this function will pick up a word
from the array of strings we created
and will convert them to const char* */
const char *convert_to_cstring(string);
//We are doing this because exec() system call accepts const char* as argument
/***************************************************
* Main Function *
***************************************************/
int main()
{
string command; // user will enter command using this variable
while(true)
{
cout<<"#shell~ ";
getline(cin, command);
execute(command);
}
return 0;
}
/*********************************
* Functions Definition *
********************************/
int date()
{
time_t tim;
time(&tim);
cout << ctime(&tim);
}
int ls()
{
pid_t pid;
pid = fork();
if(pid<0)
{
fprintf(stderr, "Fork Failed !\n"); //we will have to execute each exec in a new process because it kills the current process.
return 1;
}
else if(pid == 0)
{
execl("/bin/ls", "ls", NULL);
}
else
{
sleep(1);
}
}
int pwd()
{
pid_t pid;
pid = fork();
if(pid<0)
{
fprintf(stderr, "Fork Failed !\n");
return 1;
}
else if(pid == 0)
{
execl("/bin/pwd", "pwd", NULL);
}
else
{
sleep(1);
}
}
int cal()
{
pid_t pid;
pid = fork();
if(pid<0)
{
fprintf(stderr, "Fork Failed !\n");
return 1;
}
else if(pid == 0)
{
execl("/usr/bin/cal", "cal", "8", "2013", (char *)0);
}
else
{
sleep(1);
}
}
int get_argument_count(string command)
{
int i ;
for(i = 0; i<command.length(); i++)
{
if(command[i] == ' '|| command[i] == '\t')
{
argc++;
}
}
return argc+1; //because there will be a word after last space entered
}
void execute(string command)
{
int arguments;
arguments = get_argument_count(command);
split_to_words(command);
string temp; //temporaaray variable to copy value from words array into it
const char *source; //for cp and mv commands
const char *destination; //for cp and mv commands
const char *filename; //for rm command
const char *numeric_value;
const char *numeric_value2; //for issue command
if(words[0] == "date")
{
date();
}
else if(words[0] == "quit")
{
exit(0);
}
else if(words[0] == "ls")
{
ls();
}
else if(words[0] == "pwd")
{
pwd();
}
else if(words[0] == "cal")
{
cal();
}
else if(words[0] == "mv")
{
if(arguments != 3)
{
cout<<"Error ! usage: mv <source> <destination> oe the file does not exist !\n";
}
else
{
temp = words[1];
source = convert_to_cstring(temp);
temp = words[2];
destination = convert_to_cstring(temp);
mv(source, destination);
}
}
else if(words[0] == "cp")
{
if(arguments != 3)
{
cout<<"Error ! usage: cp <source> <destination> or the file does not exist !\n";
}
else
{
temp = words[1];
source = convert_to_cstring(temp);
temp = words[2];
destination = convert_to_cstring(temp);
cp(source, destination);
}
}
else if(words[0] == "rm")
{
if(arguments != 2)
{
cout<<"Error ! usage: rm <filename>\n";
}
else
{
temp = words[1];
filename = convert_to_cstring(temp);
rm(filename);
}
}
else
{
cout<<"No such command exist in this shell !\n";
}
}
void split_to_words(string command)
{
istringstream iss(command);
int i = 0;
do
{
string sub;
iss>>sub; //this will separate the command line to words
words[i] = sub; //saving words to the array
i++;
}while(iss);
}
const char *convert_to_cstring(string word)
{
const char *c = word.c_str();
return c;
}
int mv(const char *source, const char *destination)
{
pid_t pid;
pid = fork();
if(pid<0)
{
fprintf(stderr, "Fork Failed !\n");
return 1;
}
else if(pid == 0)
{
execlp("mv", "mv",source, destination, (char *)0);
}
else
{
cout<<"Moving file...\n";
sleep(2);
cout<<"Done.\n";
}
}
int cp(const char *source, const char *destination)
{
pid_t pid;
pid = fork();
if(pid<0)
{
fprintf(stderr, "Fork Failed !\n");
return 1;
}
else if(pid == 0)
{
execlp("cp", "cp",source, destination, (char *)0);
}
else
{
cout<<"Copying file...\n";
sleep(2); //waits 2 seconds so that file is completely copied
cout<<"Done.\n";
}
}
void rm(const char *filename)
{
pid_t pid;
pid = fork();
if(pid < 0)
{
cout<<"Fork Failed !\n";
}
else if(pid == 0)
{
execlp("rm", "rm", filename, (char *)0);
}
else
{
cout<<"Removing...";
sleep(2); //waits 2 seconds so that the file is completely removed
cout<<"Done.\n";
}
}