df inside a C program AKA any unix command within a C prog

Don Ingli AGRISCS at umcvmb.missouri.edu
Wed Dec 27 02:11:07 AEST 1989


Standard unix utilities(programs) probably work a whole lot better then
some of the stuff that I can write to do the same thing.  I developed the
following program so that you can 'link' c with the unix shell...

#include <stdio.h>
#include <signal.h>



main(argc,argv)
int argc; char *argv[];
{
/* this program shows how to link unix commands with C */
FILE *fp, *popoen();
char buf[BUFSIZ];

/* place any unix command(s) where ls -l  is like you would have
   typed them in in shell...     */

/* try replacing ls -l with    du -ra | awk '{print $2}' */


if((fp=popen("ls -l","r"))==NULL) {
/* REMEMBER the string ls -l could have been replaced with a char varible
   as well.... */
  fprintf(stderr,"%s: bad popen\n",argv[0]);
  exit(1);
                                  }
while(fgets(buf,sizeof buf,fp)!=NULL) {
/* the char varible buf now contains the first line of output from
   your unix command...  */

     printf("%s",buf);
     /* no \n used because fgets keeps the \n in the string buf */

                                      }
}

Hope this helps you all...


 DON INGLI------------------------------------------------------------+
 UNITED STATES DEPARTMENT OF AGRICULTURE - SOIL CONSERVATION SERVICE  |
 bitnet: agriscs at umcvmb.bitnet  internet: agriscs at umcvmb.missouri.edu |
 attmail: attmail!attbl!arpa!umcvmb.missouri.edu!agriscs              |
 ALL OPINIONS IN THIS NOTE ARE OF MY OWN AND DO NOT REPRESENT THE     |
 FEDERAL GOVERNMENT OR THE UNIVERSITY OF MISSOURI-COLUMBIA -----------+



More information about the Comp.unix.questions mailing list