I'm trying to get this to emulate as a shell (this program) sitting on a shell (the actual one which came with the OS).
If I type just ls, all that returns is the environmental variables (BLOCKSIZE=, PWD=, TERM, USER, VENDOR, etc.. ) returned as : No such file or directory.
If I type ls -la : the fork'd process returns back to the main program and I get no such file (coded by myself (the error message))
If anyone has any ideas, thanks in advance.
the code in its entirety is below...
#include "prompt.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
prompt:
{
compName = "Andrew$";
dir = "";
}
prompt::~prompt(void)
{
}
void prompt:
{
int i = 0;
pid_t pidValue;
char *command;
char *partmp[20];
char *tok;
tok = " ";
command = strtok(args, " "
tok = strtok(NULL," "
if (command == '\0')
{
return;
}
// Breaks the string up into each individual parameters
while (tok != NULL)
{
partmp = tok;
tok = strtok(NULL," "
i++;
}
int j =0;
char *params;
// Copies the number of parameters into a dynamically allocated array
while ( j <= i )
{
params[j] = partmp[j];
j++;
}
// looking for the cd command to change the directory name
if ( strcmp(command, "cd" ) == 0)
{
dir = params[0];
}
// a way to exit the shell since it is set into an infinite loop
else if ( strcmp(command, "exit" ) == 0)
{
exit(0);
}
// When all else fails its time to test the unix environment to see if they own the command
else
{
pidValue = fork();
// pidValue is Zero for the child process
if (pidValue == 0)
{
printf("Child program entering Execution...\n"
if ( i == 0)
{
// If no parameters are typed into the shell, we don't want to send garbage data to be misinterpretted
printf("My command is: %s\n", command);
execvp(command, NULL);
}
else
{
// if i is something other than Zero
// chances are that there are parameters to be passed and we do just that
printf("My command is: %s\n", command);
printf("My parameters are: %s\n", params[0]);
execlp(command, params);
}
printf("Invalid File or Command\n"
}
else
{
// the wait command holds the parent process until the before called child process terminates
wait(0);
}
}
}
// This Method's Purpose was to update and redraw
// the directory and machine name everytime.
void prompt::draw_path()
{
printf("%s %s>", compName, dir);
}
void prompt::get_command()
{
cin.getline(cmd, 200, '\n');
parse_args(cmd);
}
// Alot of this program was broken up into alot of little pieces
// in an attempt to make it more readable and more modular
void main()
{
// Wouldn't be an Object Oriented Program without an Object. Allows for more diverse use in the future.
prompt p;
while (1)
{
p.draw_path();
p.get_command();
}
}