home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / lang / popensrc.lzh / POPEN2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-05  |  6.5 KB  |  275 lines

  1. /*
  2. ** popen.c
  3. ** Written by Rick Schaeffer (ricks@isc-br.isc-br.com)
  4. NAME
  5.      popen, pclose - initiate I/O to/from a process
  6.  
  7. SYNOPSIS
  8.      #include <stdio.h>
  9.  
  10.      FILE *popen(command, type)
  11.      char *command, *type;
  12.  
  13.      pclose(stream)
  14.      FILE *stream;
  15.  
  16. DESCRIPTION
  17.      The arguments to popen are pointers to null-terminated
  18.      strings containing respectively a command line and an
  19.      I/O mode, either "r" for reading or "w" for writing.  It
  20.      creates a pipe between the calling process and the command
  21.      to be executed.  The value returned is a stream pointer that
  22.      can be used (as appropriate) to write to the standard input
  23.      of the command or read from its standard output.
  24.  
  25.      A stream opened by popen **MUST** be closed by pclose, which
  26.      waits for the associated process to terminate and returns
  27.      the exit status of the command.
  28.  
  29.      Because stdio files are shared, a type "r" command may be
  30.      used as an input filter, and a type "w" as an output filter.
  31.  
  32. DIAGNOSTICS
  33.      Popen returns a null pointer if files or processes cannot be
  34.      created.
  35.  
  36.      Pclose returns -1 if stream is not associated with a
  37.      `popened' command.
  38.  
  39. */
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44.  
  45. #include <exec/types.h>
  46. #include <exec/memory.h>
  47. #include <dos/dos.h>
  48. #include <dos/dosextens.h>
  49. #include <dos/record.h>
  50. #include <dos/dostags.h>
  51.  
  52. #include <proto/exec.h>
  53. #include <proto/dos.h>
  54.  
  55. struct POmsg {
  56.     struct Message POm;
  57.     int        rc;
  58.     char    *cmd;
  59.     };
  60.  
  61.  
  62. struct pstruct {
  63.     FILE    *fptr;
  64.     struct POmsg    childmsg;
  65.     };
  66.  
  67. struct pstruct poarray[6];
  68.  
  69. static struct Process *thistask;
  70.  
  71. FILE *popen(cmd,mode)
  72. char    *cmd;
  73. char    *mode;
  74. {
  75.     static char tempname[] = "pipe:pXXX.XXX";
  76.     char         *pname,redir[20],*mktemp();
  77.     short        i;
  78.     int            pmode;
  79.     int            childprocess();
  80.     struct TagItem nptags[] = {
  81.         {NP_Entry,(Tag) childprocess},
  82.         {NP_Input,0},
  83.         {NP_Output,0},
  84.         {NP_CloseInput,0},
  85.         {NP_CloseOutput,0},
  86.         {NP_StackSize,40000},
  87.         {NP_Cli,1},
  88.         {TAG_DONE,0}
  89.         };
  90.     struct pstruct    *poptr;
  91.     BPTR            pfd;
  92.     struct Process    *child;
  93.     struct CommandLineInterface *cli;
  94.  
  95.     /* First, get pointers to our process and cli structs */
  96.     if (thistask == NULL)
  97.         thistask = (struct Process *) FindTask(NULL);
  98.     cli = Cli();
  99.     poptr = NULL;
  100.  
  101.     /* now find an open pipe (we currently only allow 6 simultaneously
  102.        open pipes) */
  103.     for (i=0; i<6; i++) {
  104.         if (poarray[i].fptr == NULL) {
  105.             poptr = &poarray[i];
  106.             break;
  107.             }
  108.         }
  109.     if (poptr == NULL) {
  110.         fprintf(stderr,"popen: Unable to find an open pipe\n");
  111.         return(NULL);
  112.         }
  113.     if (strcmp(mode,"r") == 0)
  114.         pmode = MODE_NEWFILE;
  115.     else if (strcmp(mode,"w") == 0)
  116.         pmode = MODE_OLDFILE;
  117.     else {
  118.         fprintf(stderr,"popen: Mode must be 'r' or 'w'\n");
  119.         return(NULL);
  120.         }
  121.  
  122.     /* Try to make a guaranteed unique file name for the pipe */
  123.     tempname[5] = 'a' + i;
  124.     strcpy(redir,tempname);
  125.     pname = mktemp(redir);                /* set up a pipe: file name */
  126.  
  127.     /* Now get the child's stack and priority set up */
  128.     if (thistask->pr_CLI)
  129.         nptags[5].ti_Data = cli->cli_DefaultStack << 2;
  130.     else
  131.         nptags[5].ti_Data = thistask->pr_StackSize;
  132.  
  133.     /* Open the side of the pipe for the child */
  134.     pfd = Open(pname,pmode);
  135.     if (pfd == 0) {
  136.         fprintf(stderr,"popen: Unable to open pipe file\n");
  137.         return(NULL);
  138.         }
  139.  
  140.     /* set up the tags for the new process */
  141.     if (pmode == MODE_NEWFILE) {
  142.         nptags[1].ti_Data = (Tag) Input();
  143.         nptags[2].ti_Data = (Tag) pfd;
  144.         nptags[3].ti_Data = FALSE;
  145.         nptags[4].ti_Data = TRUE;
  146.         }
  147.     else {
  148.         nptags[1].ti_Data = (Tag) pfd;
  149.         nptags[2].ti_Data = (Tag) Output();
  150.         nptags[3].ti_Data = TRUE;
  151.         nptags[4].ti_Data = FALSE;
  152.         }
  153.  
  154.     /* create the command.  since the "System" function runs through
  155.        the default shell, we need to tell it not to fail so that we
  156.        ALWAYS get back the exit status.  This wouldn't be necessary
  157.        if the CLI created by the System function inherited the parent's
  158.        FAILAT level
  159.     */
  160.     poptr->childmsg.cmd = malloc(strlen(cmd) + 15);
  161.     strcpy(poptr->childmsg.cmd,"failat 9999\n");
  162.     strcat(poptr->childmsg.cmd,cmd);
  163.  
  164.     /* Create a port that we can get the child's exit status through */
  165.     poptr->childmsg.POm.mn_ReplyPort = CreatePort(NULL,0);
  166.     poptr->childmsg.POm.mn_Node.ln_Type = NT_MESSAGE;
  167.     poptr->childmsg.POm.mn_Node.ln_Pri = 0;
  168.     if (poptr->childmsg.POm.mn_ReplyPort == 0) {
  169.         fprintf(stderr,"popen: Couldn't create message port\n");
  170.         return(NULL);
  171.         }
  172.  
  173.     /* Now we can start the new process.  NOTE: this is actually going
  174.        to create a process consisting ONLY of the function "childprocess"
  175.        which can be seen below.  childprocess() then runs the command
  176.        passed in the startup message.
  177.     */
  178.     child = CreateNewProc(nptags);
  179.     /* now pass the child the startup message */
  180.     PutMsg(&child->pr_MsgPort,(struct Message *) &poptr->childmsg);
  181.  
  182.     /* Now open our side of the pipe */
  183.     poptr->fptr = fopen(pname,mode);
  184.     if (poptr->fptr == NULL) {
  185.         fprintf(stderr,"popen: Unable to open pipe file %s\n",pname);
  186.         DeletePort(poptr->childmsg.POm.mn_ReplyPort);
  187.         return(NULL);
  188.         }
  189.     return(poptr->fptr);
  190. }
  191.  
  192. pclose(fptr)
  193. FILE    *fptr;
  194. {
  195.     short        i;
  196.  
  197.     /* Figure out which pipe we used for this file */
  198.     for (i=0; i<6; i++)
  199.         if (poarray[i].fptr == fptr)
  200.             break;
  201.     if (i > 5) {
  202.         fprintf(stderr,"popen: DISASTER...couldn't find file pointer in pclose\n");
  203.         exit(1);
  204.         }
  205.  
  206.     /* close the file */
  207.     fclose(fptr);
  208.  
  209.     /* now wait for the exit status */
  210.     WaitPort(poarray[i].childmsg.POm.mn_ReplyPort);
  211.     poarray[i].fptr = NULL;
  212.  
  213.     /* clean things up */
  214.     DeletePort(poarray[i].childmsg.POm.mn_ReplyPort);
  215.     free(poarray[i].childmsg.cmd);
  216.     return(poarray[i].childmsg.rc);
  217. }
  218.  
  219. char *mktemp(template)
  220. char *template;
  221. {
  222.     register char *cp;
  223.     register unsigned long val;
  224.  
  225.     cp = template;
  226.     cp += strlen(cp);
  227.     for (val = (unsigned long) FindTask(0L) ; ; )
  228.         if (*--cp == 'X') {
  229.             *cp = val%10 + '0';
  230.             val /= 10;
  231.         } else if (*cp != '.')
  232.             break;
  233.  
  234.     if (*++cp != 0) {
  235.         *cp = 'A';
  236.         while (access(template, 0) == 0) {
  237.             if (*cp == 'Z') {
  238.                 *template = 0;
  239.                 break;
  240.             }
  241.             ++*cp;
  242.         }
  243.     } else {
  244.         if (access(template, 0) == 0)
  245.             *template = 0;
  246.     }
  247.     return template;
  248. }
  249.  
  250. childprocess()
  251. {
  252.     struct TagItem systags[] = {
  253.         {TAG_DONE,0}
  254.         };
  255.     struct Process    *me;
  256.     struct POmsg    *startupmsg;
  257.     int                i;
  258.  
  259.     /* find our process structure */
  260.     me = (struct Process *) FindTask(NULL);
  261.  
  262.     /* Wait for the parent to kick us off */
  263.     WaitPort(&me->pr_MsgPort);
  264.  
  265.     /* Get the command to execute */
  266.     startupmsg = (struct POmsg *) GetMsg(&me->pr_MsgPort);
  267.  
  268.     /* Now run the command.  stdin and stdout are already set up */
  269.     i = System(startupmsg->cmd,systags);
  270.     startupmsg->rc = i;
  271.     /* pass the exit code back to the parent */
  272.     ReplyMsg((struct Message *) startupmsg);
  273.     return(0);
  274. }
  275.