UNIX command prompt

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!
 
W

Walter Roberson

I'm writing a command prompt for unix and I've run into some problems:

Try comp.unix.programming . You used several calls which are
not part of standard C (such as fork()), and it is best to take
the questions to people who are familiar with the meaning of those
calls.

comp.lang.c prefers to restrict itself to standard C.
 
R

repairman2003

Ok thanks, I'll post it up there. But I know the unix calls all work,
just more or less a matter of the reading in from user and handling the
strings, I'm not used to doing that in C.
 
M

Mark McIntyre

On 28 Jan 2006 21:23:42 -0800, in comp.lang.c ,
Ok thanks, I'll post it up there. But I know the unix calls all work,
just more or less a matter of the reading in from user and handling the
strings, I'm not used to doing that in C.

I suggest you try a simpler example without all the unix-ey bits, just
reading user input and handling some strings. If you get stuck with
that, then post here.
Mark McIntyre
 
S

serrand

I'm writing a command prompt for unix and I've run into some problems:

You're doing as if array of char * was char** (pointer on char *)
see faq 6.19 (thanks Flash Gordon)
//Initilzation of varibles /* instead of */
char **arguments = (char **) malloc (8);
/*use this */
char * arguments[8] = {NULL};
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);
while (i) arguments[i--] = 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!

Fine... but we can't see the command's result...
But... it's not the place here to tell you more on this topic ;-)


Xavier
 
A

Arndt Jonasson

I'm writing a command prompt for unix and I've run into some problems:

Three suggestions:
1) Use a debugger;
2) Add debug print statements at appropriate places;
3) Write a small C (or shell or Perl or whatever) program which prints
out its arguments in a way that you know exactly what it received,
then call it in various ways from your shell ("command prompt" to me
sounds like just, well, the prompt).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,417
Latest member
BonitaNile
Top