Code:
#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
void parse(char List[][100], int &iLength);
void CMDS(char List[][100], int &iLength);
void run(char List[][100],int &iLength);
void COPY(string oldFname[][100], string newFname;)
//Pre: User enters a valid command for the input, the number of strings should
// up to 30, the number of characters in each string should be no more than 100
//Post: Parse and CMDS function returns and the valid command is executed.
// main funtion
int main()
{
// creates a 2 dimentional array of 30 columns and each column is 100 chars
// creates a Int that will be used to count the number of arrays
char List[30][100];
int iLength;
// do while loop to run program until user enters quit
do
{
// this is the funtion call to do the work, it passes the List and Length of arrays
parse(List, iLength);
CMDS(List, iLength);
if((strcmp(List[0], "quit") == 0) || (strcmp(List[0],"QUIT") == 0))
{
break;
}
}while (true);
return 0;
}
// starts the function
void parse(char List[][100],int &iLength)
{
// initializes variables that are going to be used in function
iLength = 0;
int i = 0;
char ch2= ' ';
char TempArray[100];
cout<<endl;
// give instructions to user, asks them to enter input
cout << "CMD:";
// do this while the input is not a new line
while(ch2 != '\n')
{
// if the character is whitespace then it gets next character and doesnt count it as part of array
if(isspace(ch2))
{
cin.get(ch2);
}
// iff the character is NOT whitespace then it will take character and add it to the TempArray, and get next character
while(!isspace(ch2))
{
TempArray[i] = ch2;
i++;
cin.get(ch2);
// if the next character is whitespace then it must be the end of a string, so we add the null \0 to it,
// and strcpy it into the List of arrays, reset the TempArray[i] to [0] and increments the number for arrays
if (isspace(ch2))
{
TempArray[i] = '\0';
strcpy(List[iLength],TempArray);
i = 0;
iLength++;
}
}
}
}
void CMDS(char List[][100],int &iLength)
{
//if((strcmp(List[0], "RUN") == 0) && iLength < 2 )
if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength < 2)
{
cout << "too few parameters for RUN cmd" << endl;
return;
}
//if((strcmp(List[0], "RUN") == 0) && iLength > 2 )
if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength > 2)
{
cout << "too many parameters for RUN cmd" << endl;
return;
}
//if((strcmp(List[0], "RUN") == 0) && iLength == 2 )
if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength == 2)
{
cout <<List[0]<<"ning "<< List[1] << endl;
run(List, iLength);
return;
}
//if((strcmp(List[0], "LIST") == 0) && iLength < 2)
if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength < 2)
{
cout <<List[0]<<"ing the files in this directory." << endl;
return;
}
//if((strcmp(List[0], "LIST") == 0) && iLength > 2)
if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength > 2)
{
cout << "too many parameters for LIST cmd" << endl;
return;
}
//if((strcmp(List[0], "LIST") == 0) && iLength == 2)
if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength == 2)
{
cout <<List[0]<<"ing the files in "<< List[1] << endl;
return;
}
//if((strcmp(List[0], "COPY") == 0) && iLength < 3)
if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength < 3)
{
cout << "too few parameters for COPY cmd" << endl;
return;
}
//if((strcmp(List[0], "COPY") == 0) && iLength > 3)
if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength > 3)
{
cout << "too many parameters for COPY cmd" << endl;
return;
}
//if((strcmp(List[0], "COPY") == 0) && iLength == 3)
if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength == 3)
{
cout <<List[0]<<"ing "<< List[1] << " and creating " << List[2] << endl;
copy(List, iLength);
return;
}
if((strcmp(List[0], "HELP") == 0) || (strcmp(List[0],"help") == 0))
{
cout << "Valid Commands are :" << endl;
cout << " RUN filename" << endl;
cout << " LIST" << endl;
cout << " LIST directory" << endl;
cout << " COPY old-filename new-filename" << endl;
cout << " HELP" << endl;
cout << " QUIT" << endl;
return;
}
// this performs the string compare to quit or QUIT and exits if needed
if((strcmp(List[0], "quit") == 0) || (strcmp(List[0],"QUIT") == 0))
{
return;
}
// otherwise the command entered is not recognized.
else
{
cout << "Invalid command - "<< List[0] << " Command not recognized" << endl;
}
}
//initiate the run command
void run(char List[][100],int &iLength)
{
pid_t pid;
//Initiate a fork
pid = fork();
//Check if the pid is less than zero or not
if(pid < 0)
{
fprintf(stderr, "Fork Failed");
return;
}
//If pid is zero, then execute run
else if (pid==0)
{
execl(List[1], List[1], NULL);
}
else
{
wait(NULL);
}
//Return to the start
return;
}
//Initiate the copy command
void COPY(string oldFname, string newFname)
{ cout << "Copying " << oldFname << " to " << newFname <<endl;
//Converting a string to cstring
char *copyFile =(char*)oldFname.c_str();
char *pasteFile=(char*)newFname.c_str();
//Declaring the readers and writers
ifstream in;
ofstream out;
string input;
char c;
//Opening the file to read
in.open(copyFile);
//If condition if the open fails
if(in.fail())
{
cout<< "The system couldn't find the file"<<endl;
}
//If condition if the open is successful
else
{
ifstream outTest;
//Checking if the file already exist
outTest.open(pasteFile);
if(outTest.fail())
{
cout<< "Creating a new file"<<endl;
out.open(pasteFile);
//Copying from a oldfile to a newfile
while (!in.eof())
{
c=(char)in.get();
out.put(c);
}
out.close();
outTest.close();
}
else
// If the file being copied already exists, system asks user to overwrite
{
cout<<"The file is already there!\n Are you sure you want to overwrite it.\n Enter y to overwrite it."<<endl;
cin>>input;
cin.ignore(200,'\n');
if(input.compare("y")==0)
{
out.open(pasteFile);
while (!in.eof())
{
c=(char)in.get();
out.put(c);
}
out.close();
outTest.close();
}
else
{
cout<<"The copy function is cancelled"<<endl;
}
}
//File close
in.close();
}