C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-25-2002, 11:25 PM   #1
Registered User
 
Join Date: Apr 2002
Posts: 17
simple shell (c), fgets problem

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

Last edited by razza; 05-25-2002 at 11:28 PM.
razza is offline   Reply With Quote
Old 05-26-2002, 12:13 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,693
> 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
Salem is offline   Reply With Quote
Old 05-26-2002, 12:25 AM   #3
Registered User
 
Join Date: Apr 2002
Posts: 17
well for ls for example:

ls /

(lists directory contents properly)

ls / &

ls: : No such file or directory
/:
razza is offline   Reply With Quote
Old 05-26-2002, 12:32 AM   #4
Registered User
 
Join Date: Apr 2002
Posts: 17
Code:
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?
razza is offline   Reply With Quote
Old 05-26-2002, 01:36 AM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,693
> 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
Code:
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
Salem is offline   Reply With Quote
Old 05-26-2002, 05:51 AM   #6
Registered User
 
Join Date: Apr 2002
Posts: 17
Thanks I didn't think of putting the value in ' ', which did help me spot something

Managed to fix it, so it works now.
razza is offline   Reply With Quote
Old 05-26-2002, 10:44 PM   #7
Comment your source code!
 
Lynux-Penguin's Avatar
 
Join Date: Apr 2002
Posts: 533
try calling the other functions from main and adding return 0; to main

ERR nm
__________________
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
Lynux-Penguin is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using sscanf fgets and overflow checking jou00jou C Programming 5 02-18-2008 06:42 AM
Help: A Simple Shell roaming_builder C Programming 6 02-10-2008 10:12 PM
problem with A simple modeless messagebox hanhao C++ Programming 8 07-05-2005 11:18 PM
problem with fgets learninC C Programming 3 05-19-2005 08:10 AM
C / OpenGL help REALLY needed for simple but annoying problem! Oz_joker C Programming 5 12-03-2003 05:47 PM


All times are GMT -6. The time now is 05:32 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22