R
repairman2003
I'm writing a command prompt for unix and I've run into some problems:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#define EXIT "exit"
#define DELIMITER " "
we are in
int main(argc, argv)
{
//Initilzation of varibles
char **arguments = (char **) malloc (8);
char *command_buffer = (char *) malloc (256);
char *arg_ptr;
int i = 0;
int pid;
int nbytes = 256;
int not_exiting = 1;
do{
//prints prompt and gets line of text
printf("sim> ");
getline(&command_buffer, &nbytes, stdin);
//Special workaround
//replaced the '\n' at the end of the input, which was giving errors
//with '\0'
command_buffer[strlen(command_buffer) -1] = '\0';
//picks line apart and
//replaces spaces with '\0' and adds pointers to argument[]
do{
if(i == 0)
{
arguments = strtok(command_buffer, DELIMITER);
i++;
}
else
{
arg_ptr = strtok(NULL, DELIMITER);
if(arg_ptr != NULL)
{
arguments = arg_ptr;
i++;
}
else
{
i++;
break;
}
}
}while(1);
//are we exiting?
not_exiting = strcmp(command_buffer, EXIT);
if(not_exiting)
{
//create child process
pid = fork();
if(pid == 0) //fork() == 0 means child process
{
execvp(*arguments, arguments);
exit(0);
}
else if(pid > 0) //fork() > 0 means we are in parent process
{
wait(NULL);
}
else //otherwise we have error
{
fprintf(stderr, "fork() returned error");
exit(1);
}
}
else
{
printf("Exiting\n");
}
}while(not_exiting);
return 0;
}
The problems
1. The workaround listed above.
2. One argument commands work fine but multiple don't. Eg. ls -a
works fine, ls -al works fine but ls -a -l doesn't work. Any
subsequent commands that don't have arguments don't work either, eg.
ls works, then ls -a works, but ls again doesn't work.
Any help is greatly appriciated!
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#define EXIT "exit"
#define DELIMITER " "
we are in
int main(argc, argv)
{
//Initilzation of varibles
char **arguments = (char **) malloc (8);
char *command_buffer = (char *) malloc (256);
char *arg_ptr;
int i = 0;
int pid;
int nbytes = 256;
int not_exiting = 1;
do{
//prints prompt and gets line of text
printf("sim> ");
getline(&command_buffer, &nbytes, stdin);
//Special workaround
//replaced the '\n' at the end of the input, which was giving errors
//with '\0'
command_buffer[strlen(command_buffer) -1] = '\0';
//picks line apart and
//replaces spaces with '\0' and adds pointers to argument[]
do{
if(i == 0)
{
arguments = strtok(command_buffer, DELIMITER);
i++;
}
else
{
arg_ptr = strtok(NULL, DELIMITER);
if(arg_ptr != NULL)
{
arguments = arg_ptr;
i++;
}
else
{
i++;
break;
}
}
}while(1);
//are we exiting?
not_exiting = strcmp(command_buffer, EXIT);
if(not_exiting)
{
//create child process
pid = fork();
if(pid == 0) //fork() == 0 means child process
{
execvp(*arguments, arguments);
exit(0);
}
else if(pid > 0) //fork() > 0 means we are in parent process
{
wait(NULL);
}
else //otherwise we have error
{
fprintf(stderr, "fork() returned error");
exit(1);
}
}
else
{
printf("Exiting\n");
}
}while(not_exiting);
return 0;
}
The problems
1. The workaround listed above.
2. One argument commands work fine but multiple don't. Eg. ls -a
works fine, ls -al works fine but ls -a -l doesn't work. Any
subsequent commands that don't have arguments don't work either, eg.
ls works, then ls -a works, but ls again doesn't work.
Any help is greatly appriciated!