LINUX TIPS AND TRICKS --- October 20, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Launching A Shell
by Danny Kalev

This week I will discuss two functions that launch a command shell. The 
first function is system(), which is declared in "stdlib.h" as follows:

    int system(const char * cmd);
 
system() launches a shell as a child process, passes a command for the 
shell to execute and returns the child's status. Note that the status 
returned is the exit code of the command shell, not the status of a 
process that was subsequently launched from the command shell. The 
following call returns 0 if the command shell was successfully 
launched; it doesn't return the exit status of the program "calculator":

    int stat=system("calculator");

The child process writes to stdout and receives input from stdin. The 
parent process waits until the shell exits. Thus, system() is mostly 
useful for implementing menu-based applications. Often, a parent 
process needs to read from the child process or send data to it. For 
this purpose, you should use the popen() function.

popen() is similar to system() except that it returns FILE * and takes 
an additional parameter, the mode type:

    FILE * popen(const char *cmd, const char *mode); 

The two mode types are "r" if the parent wishes to read from the 
child's standard output, and "w" if the parent wishes to write to the 
child's standard input (you can't do both read and write with popen()). 
A successful popen() call returns a non-null FILE pointer. When you 
write to that file, you're actually writing to the child's stdin, and 
when you read from that file you're reading the child's stdout. You 
must call pclose() before exiting from the parent process to close the 
stream and terminate the child process, if it's still running. It's 
also recommended that you flush the stream after every write operation 
to avoid buffering delays. The following program creates an expression 
string that is passed to another program, calc, which in turn performs 
all the actual calculation. Notice how the string is passed to the 
child's stdin using fprintf():

    #include stdio.h
    int main()
    {
     char buff[1024];
     int stat;
     FILE* pipe;

     pipe=popen("calc","w")
     if(!pipe)
     {
      perror("calc failed"); 
      exit -1
     }
     printf("enter a math expression: ");
     fgets(buff, sizeof(buff), stdin); /*read expression*/
     /*send user's expression to calc program using pipe*/
     fprintf(pipe,"%s", buff);
     fflush(pipe);/*avoid stdio buffering delay*/
     stat=pclose(pipe); /*close child's pipe*/
     return stat;
    }

Next Week:  Security issues pertaining to system() and popen().


RESOURCES

Understanding Unix shells and environment variables 
How much do you know? 
http://www.itworld.com/jsw/lintps_nl/swol-05-2000/swol-05-unix101.html

The language of shells 
Making sense of shell commands 
http://www.itworld.com/jsw/lintps_nl/swol-07-2000/swol-0728-unix101.html

Quick lessons on shell programming 
How to start writing your own shell scripts 
http://www.itworld.com/jsw/lintps_nl/swol-09-1997/swol-09-unix101.html


COMMUNITY DISCUSSION

Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels. Moderated by Danny Kalev
http://www.itworld.com/jump/lindsk_nl/forums.itworld.com/webx?14@@.ee6b652/175!skip=121

Ask questions, offer solutions, and tell your tales in this lively 
discussion of the good, bad, and ugly sides of managing Unix systems. 
http://www.itworld.com/jump/unxtps_nl/forums.itworld.com/webx?14@@.ee6b677/257!skip=204 

*********************************************************************

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.
 
*********************************************************************

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
dan_chupka@itworld.com
* For recruitment advertising information, write Jamie Swartz, Eastern
Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
Western Regional Sales Manager at: paul_duthie@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager,
Newsletters at: jodie_naze@itworld.com

*********************************************************************

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com
