simple shell (c), fgets problem [Archive] - C Board

PDA

View Full Version : simple shell (c), fgets problem


razza
05-25-2002, 11:25 PM
Hi all,

Could you please look at my source at http://razza.org/source.html (its formatted, and color coded)

I have two problems, they seem to be related
1:)

when I use fgets to grab from the command line, rather than gets, I seem unable to run programs like ping ipaddress (ping doesnt receive the ip) If I use gets ping will work.

2:)
However my shell should also run programs in the background (that is fork the process off, and return to the command prompt before the process has finished). To do this you would type say echo hello & (the '&' should always trail), this by the way actually works for echo but if you try ls / & ( then it complains)

I presumed that their is something wrong with the parse function, so I substituted another users function in, the shell then worked correctly with fgets.

Since I check for the & symbol in my parse function I was unable to check wether the background process bug is also fixed.

Hopefully one of you guys can tell me whats up with my parse function

Thanks

razza

Salem
05-26-2002, 12:13 AM
> when I use fgets to grab from the command line, rather than gets
You've got to strip off that newline before passing the parameters to your programs

> if you try ls / & ( then it complains)
Can you be more specific?

> its formatted, and color coded
Well it's colour coded, but I wouldn't say it was formatted
Formatted is where all the statements at the same scope level actually line up

razza
05-26-2002, 12:25 AM
well for ls for example:

ls /

(lists directory contents properly)

ls / &

ls: : No such file or directory
/:

razza
05-26-2002, 12:32 AM
int parse(char *buffer, char **v) {
int temp = 0;
while (*buffer != '\0') // if not the end of buffer yet.
{
while (*buffer == ' ' || *buffer == '\t' || *buffer == '\n')
*buffer++ = '\0'; // replace white spaces (includes tabs etc..)
*v++ = buffer; // save buffer place to v.


Once out of the while loop v is null terminated with *v = '\0'

wouldn't the above code take care of the final new line character?

Salem
05-26-2002, 01:36 AM
> wouldn't the above code take care of the final new line character?
On the face of it, yes
But that doens't mean it actually works

Try printing each v[i] using this loop

for ( i = 0 ; v[i] != NULL ; i++ ) {
printf( "param %d is '%s'\n", i, v[i] );
}

printing the string between two '' will allow you to see if there are any extra space/newlines which you didn't expect.


> Once out of the while loop v is null terminated with *v = '\0'
*v = NULL;
would be better

razza
05-26-2002, 05:51 AM
Thanks I didn't think of putting the value in ' ', which did help me spot something :)

Managed to fix it, so it works now.

Lynux-Penguin
05-26-2002, 10:44 PM
try calling the other functions from main and adding return 0; to main

ERR nm