So I have to make a UNIX shell that can basically take in any UNIX command and then keep a record of the time each process took, by forking off children processes, recording their time, and recording the parent's time, and then return that time into a linked list or vector so that i can print out all processes and their times when someone wants to see them.
Now the problem I am having is that I have no idea how a regular shell parses out bad input. Say for instance you type "pig" into the command line of your standar tcsh. It will say "pig": Bad command name or something to that effect. I want mine to do that but right now it is just accepting "pig" and calling it another running instance of my current shell. I know i have to do some sort of search for $PATH but no one has ever told me what functions do that kind of search, or even how to search that kind of thing so that I can error check. I pasted my code in here in hopes that perhaps someone can show me where and what the hell to do with making sure bad input stays bad input and doesn't get logged as a process.
/*
* Program: This is a shell that reports stats of running times of processes initiated in
* the shell as well as the shell itself's process time
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/times.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/socket.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
/**
* method displayID
* returns: nothing
* parameters: none
* Description: This method displays the author information
*/
void displayID()
{
}
/**
* method displayInfo
* returns: nothing
* parameters: none
* Description: This method displays the Game info
*/
void displayInfo()
{
cout << "The function of the shell is to operate child processes" << endl;
cout << "And return statistics of process time of each process" << endl;
cout << "That has been running in the shell as well as the shell's" << endl;
cout << "process time" << endl;
}
/**
* method takeInpt
* returns: nothing
* parameters: none
* Description: This method takes the input for the shell
*/
void takeInpt(char* cmnd, char* cmd[], char inpt[])
{
//ask for and get the input
cout << "statsh$: ";
cin.getline(inpt,81);
cmnd = strtok(inpt, " ");
int c = 0;
while(cmnd != NULL)
{
cmd[c] = cmnd;
c++;
cmnd = strtok(NULL, " ");
}
}
/**
* method runProc
* returns: nothing
* parameters: none
* Description: This method runs exec and forking commands
*/
void runProc(char* cmd[])
{
pid_t pid;
// fork
/ /do i check for bad input here??!!
pid = fork();
if(pid < 0)
{
cout << "Fork Failed" << endl;
exit(-1);
}
else if( pid == 0)
{
execvp(cmd[0], cmd);
}
else
{
wait(NULL);
cout << "Job's Done" << endl;
}
}
int main()
{
char* cmnd;
char* cmd[40];
char inpt[81];
displayID();
displayInfo();
while(1)
{
//clean out the array each time
for(int m=0; m < 40; m++)
{
cmd[m] = NULL;
}
takeInpt(cmnd, cmd, inpt);
if(cmd[0] == NULL)
{
cout << "Enter a command please" << endl;
}
else if(strcmp(cmd[0], "exit") == 0 )
{
break;
}
else
{
runProc(cmd);
}
/*
* testing print statement to see user input is taken and tokenized
for(int i=0; i < 40; i++)
{
if(cmd[i])
{
cout<< cmd[i] << endl;
}
}
*/
}
return 1;
}
If anyone could help me I would greatly appreciate it. In the meantime I'll keep hittin away at it and try to get it right.