10-02-2001
In general, you want to write a program that take "line" input
(i.e. gets() ) and parses it looking for "special" characters
(don't forget to process escape "\" characters as well). From
that point, you can use fork() exec() to actually run the commands
and use popen() to facilitate redirection to/from parent and
children processes.
I hope this gives you an idea of where to start. Note that in
at least one C programming book (I don't remember which),
they actually give you a rudimentary shell as a programming
example.
10 More Discussions You Might Find Interesting
1. Programming
//save in/out
int tmpin = dup(0);
int tmpout = dup(1);
//set initial input
int fdin;
if(_inputFile)
{
fdin = open(_inputFile, O_RDONLY | O_CREAT, S_IREAD | S_IWRITE);
}
else
{
//use default input
fdin = dup(tmpin);
}
int ret;
int fdout;
for(int i = 0; i... (14 Replies)
Discussion started by: AirBronto
14 Replies
2. UNIX for Advanced & Expert Users
It is possible for me to obtain the Inode of the path name using
ls -i <pathname> command
Can anyone tell me how its implemented... (3 Replies)
Discussion started by: ganapathy.psgit
3 Replies
3. Shell Programming and Scripting
Hi,
I am accepting a string from user. compare this output with the awk output as below...
echo "\n\n\tDay : \c"
read day
awk '{ if($day == $2) { if ($mon == $1) { print "Yes" }}}' syslog.txt
I am getting the follwoing error
awk: Field $() is not correct.
The input line... (5 Replies)
Discussion started by: EmbedUX
5 Replies
4. Programming
Hey Guys
I am trying to implement the malloc function for my OS class and I am having a little trouble with it. I would be really grateful if I could get some hints on this problem.
So I am using a doubly-linked list as my data structure and I have to allocate memory for it (duh...). The... (1 Reply)
Discussion started by: Gambit_b
1 Replies
5. UNIX for Advanced & Expert Users
Hey Guys
Some of my friends have got together and we are trying to write a basic kernel similar to Linux. I am trying to implement the malloc function in C and I am using a doubly linked list as the primary data structure. I need to allocate memory for this link list (duh...) and I don't feel... (2 Replies)
Discussion started by: rbansal2
2 Replies
6. Linux
Hi
I'm trying to implement CAPWAP protocol for my application.i'm able to configure my server side but i'm getting error at client(WTP) side as IOCTL error.while running the command
#./WTP /mnt/cf/capwap/
: wlan2
Starting WTP...
# WTP Loads... (0 Replies)
Discussion started by: ran789
0 Replies
7. UNIX for Dummies Questions & Answers
Hi everybody,
i've been googling for ages now and gotten kinda desperate... The question, however, might be rather trivial for the experts: What is it exactly, i.e. physically, the POSIX function (for a file) "lseek" does? Does it trigger some kind of synchronization on disk? Is it just for the... (4 Replies)
Discussion started by: Humudituu
4 Replies
8. Linux
Hi,
I am trying to execute a program with pipes to run a few basic commands by forking children. When I try to run commands in the child process without pipe, I am unable to run the command as execv fails. However for commands that are given with pipes execute successfully.
for example:... (1 Reply)
Discussion started by: mmurali2
1 Replies
9. UNIX for Advanced & Expert Users
How can i implement Ipsec between two machines in linux_ ubuntu?
any link?? suggestion?? (0 Replies)
Discussion started by: elinaz
0 Replies
10. Programming
I have this code from a programming book:
#include <stdio.h>
#include <string.h>
char buf; /* input line buffer */
char* field; /* fields */
char* unquote( char* );
/* csvgetline: read and parse line, return field count */
/* sample input:... (3 Replies)
Discussion started by: totoro125
3 Replies
LEARN ABOUT REDHAT
pclose
POPEN(3) Linux Programmer's Manual POPEN(3)
NAME
popen, pclose - process I/O
SYNOPSIS
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
DESCRIPTION
The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional,
the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only.
The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the
-c flag; interpretation, if any, is performed by the shell. The mode argument is a pointer to a null-terminated string which must be
either `r' for reading or `w' for writing.
The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than
fclose(). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the
process that called popen(), unless this is altered by the command itself. Conversely, reading from a ``popened'' stream reads the com-
mand's standard output, and the command's standard input is the same as that of the process that called popen.
Note that output popen streams are fully buffered by default.
The pclose function waits for the associated process to terminate and returns the exit status of the command as returned by wait4.
RETURN VALUE
The popen function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory.
The pclose function returns -1 if wait4 returns an error, or some other error is detected.
ERRORS
The popen function does not set errno if memory allocation fails. If the underlying fork() or pipe() fails, errno is set appropriately.
If the mode argument is invalid, and this condition is detected, errno is set to EINVAL.
If pclose() cannot obtain the child status, errno is set to ECHILD.
CONFORMING TO
POSIX.2
BUGS
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original
process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for
writing may become intermingled with that of the original process. The latter can be avoided by calling fflush(3) before popen.
Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The
only hint is an exit status of 127.
HISTORY
A popen() and a pclose() function appeared in Version 7 AT&T UNIX.
SEE ALSO
fork(2), sh(1), pipe(2), wait4(2), fflush(3), fclose(3), fopen(3), stdio(3), system(3)
BSD MANPAGE
1998-05-07 POPEN(3)