unix shell command line and interpreter
Author |
Message |
Shaw #1 / 27
|
 unix shell command line and interpreter
Hi I would like some help on writing a replacement for the shell command line and interpreter under Unix. The shell should read an input line at a time from a standard input until end of file, where it exits. I need some guidance especially on arranging the piping between proocesses for each command. some sample code would be greatly appreciated. Thanks Smith
|
Mon, 04 Apr 2005 14:20:27 GMT |
|
 |
Simon Bibe #2 / 27
|
 unix shell command line and interpreter
Quote:
> Hi > I would like some help on writing a replacement for the > shell command line and interpreter under Unix. The shell > should read an input line at a time from a standard input > until end of file, where it exits. I need some guidance > especially on arranging the piping between proocesses for > each command. > some sample code would be greatly appreciated. Thanks
Here's some sample code: #include <stdio.h> #include <stdlib.h> int main(void) { char buf[100]; while(fgets(buf, sizeof buf, stdin)) system(buf); return 0; Quote: }
On Unix, piping does work with this command interpreter, as the system function takes care of it. If you want any code that's not standard C, head over to comp.unix.programmer. -- Simon.
|
Mon, 04 Apr 2005 19:18:15 GMT |
|
 |
Dan P #3 / 27
|
 unix shell command line and interpreter
Quote: > I would like some help on writing a replacement for the shell >command line and interpreter under Unix. The shell should read an >input line at a time from a standard input until end of file, where it >exits. I need some guidance especially on arranging the piping between >proocesses for each command. > some sample code would be greatly appreciated. Thanks
Many books about Unix system programming show how to implement a simple shell, as an excellent example of the usage of the fork, exec and pipe system calls. Dan -- Dan Pop DESY Zeuthen, RZ group
|
Mon, 04 Apr 2005 20:07:38 GMT |
|
 |
Kenny McCorma #4 / 27
|
 unix shell command line and interpreter
Quote:
>> Hi >> I would like some help on writing a replacement for the >> shell command line and interpreter under Unix. The shell >> should read an input line at a time from a standard input >> until end of file, where it exits. I need some guidance >> especially on arranging the piping between proocesses for >> each command. >> some sample code would be greatly appreciated. Thanks >Here's some sample code: >#include <stdio.h> >#include <stdlib.h> >int main(void) >{ > char buf[100]; > while(fgets(buf, sizeof buf, stdin)) system(buf); > return 0; >}
This is, of course, very clever (appluase, appluase!), but, since the point of clc is to go apey-nuclear-nuts over trivia, let me add a couple of nit-picks: 1) fgets() isn't really all that much safer than gets(), because if the input line is longer than your specificed length (100 in your case, which is, BTW, much too small. Surely, you should at least be using ARG_MAX or something like that), fgets() returns the extra characters in subsequent calls, which will obviously cause problems here. In some sense, the failure isn't quite as mysterious/gruesome as it might be with gets(), but from a go-nogo point-of-view, it's the same. Interested readers should use google to lookup the last time we had a gets() brouhaha. 2) You should use "exit(0)", not "return(0)". I will not go into details, but again, google is your friend. The argument that you will find there is pretty convincing to me. I think, BTW, that "return(0)" looks more "clc-ish", but "exit(0)" is every bit as conforming. HTH...
|
Mon, 04 Apr 2005 22:22:48 GMT |
|
 |
Jeremy Yallo #5 / 27
|
 unix shell command line and interpreter
Quote:
> >#include <stdio.h> > >#include <stdlib.h> > >int main(void) > >{ > > char buf[100]; > > while(fgets(buf, sizeof buf, stdin)) system(buf); > > return 0; > >} > 2) You should use "exit(0)", not "return(0)". I will not go into > details, but again, google is your friend. The argument that you will find > there is pretty convincing to me. I think, BTW, that "return(0)" looks more > "clc-ish", but "exit(0)" is every bit as conforming.
I don't know what argument you mean, but the C standard says: 5.1.2.2.3 Program termination If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; so the argument is incorrect (unless it's purely on style grounds, of course). The parentheses are superfluous with return, BTW. Jeremy.
|
Mon, 04 Apr 2005 22:37:19 GMT |
|
 |
Mark A. Odel #6 / 27
|
 unix shell command line and interpreter
Quote: > 1) fgets() isn't really all that much safer than gets(), because if > the > input line is longer than your specificed length (100 in your case, > which is, BTW, much too small.
It's safer in that you can't overwrite the end of buf and destroy other "things". I'll take that bit of free safety any day.
|
Mon, 04 Apr 2005 22:44:09 GMT |
|
 |
#7 / 27
|
 unix shell command line and interpreter
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Malte Starosti #8 / 27
|
 unix shell command line and interpreter
Quote:
>> 2) You should use "exit(0)", not "return(0)". I will not go into >> details, but again, google is your friend. The argument that you will >> find >> there is pretty convincing to me. I think, BTW, that "return(0)" looks >> more "clc-ish", but "exit(0)" is every bit as conforming. > I don't know what argument you mean, but the C standard says:
I assume the argument is that return might break if the stack has been tampered with. OTOH I'd say in that case all bets are off anyway, IMBW. Quote: > 5.1.2.2.3 Program termination > If the return type of the main function is a type compatible with > int, a return from the initial call to the main function is > equivalent to calling the exit function with the value returned by > the main function as its argument;
Reading this (mostly the "initial call" part) and some parts of the exit vs. return thread on google one question arises for me: is it legal in C to recursively call main(), or call main() at all for that matter? Please excuse my ignorance, but all I know is that in C++ it is not. Thanks, -Malte
|
Mon, 04 Apr 2005 22:54:34 GMT |
|
 |
Jeremy Yallo #9 / 27
|
 unix shell command line and interpreter
* Malte Starostik | Reading this (mostly the "initial call" part) and some parts of the exit vs. | return thread on google one question arises for me: is it legal in C to | recursively call main(), or call main() at all for that matter? Yes, in both cases. | Please excuse my ignorance, but all I know is that in C++ it is not. Right. Jeremy.
|
Mon, 04 Apr 2005 22:57:37 GMT |
|
 |
#10 / 27
|
 unix shell command line and interpreter
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Dan P #11 / 27
|
 unix shell command line and interpreter
Quote:
>> >#include <stdio.h> >> >#include <stdlib.h> >> >int main(void) >> >{ >> > char buf[100]; >> > while(fgets(buf, sizeof buf, stdin)) system(buf); >> > return 0; >> >} >> 2) You should use "exit(0)", not "return(0)". I will not go into >> details, but again, google is your friend. The argument that you will find >> there is pretty convincing to me. I think, BTW, that "return(0)" looks more >> "clc-ish", but "exit(0)" is every bit as conforming. >I don't know what argument you mean, but the C standard says: > 5.1.2.2.3 Program termination > If the return type of the main function is a type compatible with > int, a return from the initial call to the main function is > equivalent to calling the exit function with the value returned by > the main function as its argument; >so the argument is incorrect (unless it's purely on style grounds, of >course).
There is exactly one case when exit() should be used instead of returning from main() and it doesn't apply to any sane program: when one (or more) of the atexit registered functions operate(s) on objects that are automatically allocated in main(). If you call exit(), the objects are still in scope, if you return from main()... Dan -- Dan Pop DESY Zeuthen, RZ group
|
Mon, 04 Apr 2005 22:53:26 GMT |
|
 |
Dan P #12 / 27
|
 unix shell command line and interpreter
Quote:
>> 1) fgets() isn't really all that much safer than gets(), because if >> the >> input line is longer than your specificed length (100 in your case, >> which is, BTW, much too small. >It's safer in that you can't overwrite the end of buf and destroy other >"things". I'll take that bit of free safety any day.
But this bit of safety if NOT enough to make fgets safe. If you blindly assume that each fgets call reads a whole line of text, it is trivial for a malicious user to provide input that is incorrectly handled by your program. Dan -- Dan Pop DESY Zeuthen, RZ group
|
Mon, 04 Apr 2005 23:39:28 GMT |
|
 |
Dan P #13 / 27
|
 unix shell command line and interpreter
Quote:
>> Hi >> I would like some help on writing a replacement for the >> shell command line and interpreter under Unix. The shell >> should read an input line at a time from a standard input >> until end of file, where it exits. I need some guidance >> especially on arranging the piping between proocesses for >> each command. >> some sample code would be greatly appreciated. Thanks >Here's some sample code: >#include <stdio.h> >#include <stdlib.h> >int main(void) >{ > char buf[100]; > while(fgets(buf, sizeof buf, stdin)) system(buf); > return 0; >} >On Unix, piping does work with this command interpreter, as the system >function takes care of it.
Wrong, the system function doesn't take care of it. The system function passes the string pointed to by string to the host environment to be executed by a command processor in an implementation-defined manner. In other words, your program is NOT a shell replacement (which is what the OP was asking for), it is merely a front end to one of the Unix shells. Quote: >If you want any code that's not standard C, head over to >comp.unix.programmer.
This thread is crossposted to comp.unix.programmer, so I fail to see your point. Of course, the question shouldn't have been crossposted to comp.lang.c in the first place. Dan -- Dan Pop DESY Zeuthen, RZ group
|
Mon, 04 Apr 2005 23:50:47 GMT |
|
 |
Simon Bibe #14 / 27
|
 unix shell command line and interpreter
Quote:
> >On Unix, piping does work with this command interpreter, > >as the system function takes care of it. > Wrong, the system function doesn't take care of it.
If I said that "strcpy copies strings", it may in fact be that the strcpy function itself merely contains a call to another internal function which actually does the copying. So, when I say "the system function takes care of it", I'm referring to not just the system function itself, but also any other routines that it might make use of. Quote: > The system function passes the string pointed to by string > to the host environment to be executed by a command processor > in an implementation-defined manner. > In other words, your program is NOT a shell replacement (which > is what the OP was asking for), it is merely a front end to one > of the Unix shells.
Did I ever claim it was a "shell replacement"? Need I know what a "shell" is, when reading on comp.lang.c? My sample code demonstrates in standard C how I would take commands, and pass them to the system to be executed by the system's command processor. This was the only interpretation I could see that would make the question applicable to the C language. If the OP didn't want such an interpretation, he should not have posted to comp.lang.c! Obviously if an operating system provides some way to break into the inner workings of a command processor, or implement your own one, then that is not specified by the C standard. Quote: > >If you want any code that's not standard C, head over to > >comp.unix.programmer. > This thread is crossposted to comp.unix.programmer, so I fail > to see your point. > Of course, the question shouldn't have been crossposted to > comp.lang.c in the first place.
Yes, I failed to see the crosspost, so my comment was misphrased. I should have said "If you want help with any code that's not standard C, remove comp.lang.c from the cross-post." -- Simon.
|
Tue, 05 Apr 2005 01:04:02 GMT |
|
 |
Joshua Jone #15 / 27
|
 unix shell command line and interpreter
Quote: > Did I ever claim it was a "shell replacement"? Need I know what a "shell" > is, when reading on comp.lang.c?
No, but keep it on clc... no need for that mess in cup. -- josh(at)intmain.net | http://intmain.net
1788 local keystrokes since last reboot (today)
|
Tue, 05 Apr 2005 01:15:32 GMT |
|
|
Page 1 of 2
|
[ 27 post ] |
|
Go to page:
[1]
[2] |
|