** SEE COMMENTS ** Add the functionality for command recognition and implementat
ID: 3772908 • Letter: #
Question
** SEE COMMENTS **
Add the functionality for command recognition and implementation for the HELP, QUIT, and COPY, LIST, and RUN functions, written in C++
After a line of user input has been read, you program should determine if the first element of your array of cstrings is one of the valid commands. If it is a valid command, your program will check that the number of parameters is correct. Your program should give detailed error messages if the command is not valid or the number of parameters is incorrect.
Recall that the valid commands are
RUN executable-file
LIST
LIST directory
COPY old-filename new-filename
CD new-directory
SHOW filename
HELP
QUIT
If the entered command is invalid or if the number of parameters to that command is incorrect your program will give an appropriate and specific error message.
If the command is valid, your program will carry out the command.
HELP
List the valid commands and their proper syntax. E.g.
You rang? > HELP
The valid commands are:
RUN executable-file
LIST
LIST directory
COPY old-filename new-filename
HELP
QUIT
SHOW
Simply prints the given file to standard output.
QUIT
Terminates the session. E.g.
Shell > QUIT
Goodbye.
COPY
Creates a copy of the source file.
When you implement the COPY command, you should verify that the source file exists and is readable. If the destination file exists you should ask the user if they wish to overwrite it before opening it to write. Once both files have been successfully opened simple read the characters on at a time from the source file and write them to the destination file.
CD
Changes the working directory to the argument if it is a valid directory name.
Your shell should display a message indicating the new directory upon successful execution o the command. You will need to use the chdir and getcwd commands.
LIST
Displays the files in a directory
Your LIST command takes either no arguments or one argument. The contents of the directory specified by the argument should be listed or in the case of no argument, the contents of the current directory should be listed. The syntax is:
LIST
or
LIST directory
The algorithm for LIST is reasonable simple.
Open the directory
Read the first entry in the directory
While not all of the directory has been displayed
Display the entry just read
Read the next entry
Close the directory
Working with directories is a relatively low level operation. To access the contents of a directory we need to use a collection of system calls declared in the header file.
opendir( dname ) is passed a c-string dname and returns a pointer to the directory dname (type DIR*) if it is found and NULL otherwise.
readdir( dPointer ) is passed a DIR* and returns a pointer to the next entry in the directory linked to dPointer or NULL if there are no more entries.
closedir( dPointer ) closes the link to the directory
The directory entries are of type dirent, a structure that contains the member d_name, which contains the name of the current entry.
RUN
Executes the given program.
Your RUN command takes one argument. The executable file specified by the argument should be run and when finished, the prompt displayed. The syntax is:
RUN executable-file
The algorithm for RUN would be as follows.
Fork a child process.
Execute the program in the child process.
Have the parent wait until the child is finished before continuing.
This will require the following library functions
fork( ) creates a child process and returns the PID of the child process to the parent. The child gets 0.
execl( list of cstrings ) executes a program in the current process — the first argument is the path to the executable file, the remaining arguments are the strings of the command line
wait( ) causes the parent to block until the child finishes
Explanation / Answer
Below is the code for COPY Function:
#include<iostream.h>
#include<conio.h>
class copy
{
int var,fact;
public:
copy(int temp)
{
var = temp;
}
double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
clrscr();
int n;
cout<<" Enter the Number : ";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<" "<<n<<" Factorial is:"<<obj.calculate();
cout<<" "<<n<<" Factorial is:"<<cpy.calculate();
getch();
}
Sample Output:
Enter the Number: 5
Factorial is: 120
Factorial is: 120
Below is the code for Exit function:
This code can be used for the exit funcion.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.