home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / c2man-2.0pl33.lha / c2man-2.0 / amiga / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-30  |  9.3 KB  |  369 lines

  1. RCS_ID_C="$Id: popen.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      popen.c - Unix comaptible popen() and pclose()
  4.  *
  5.  *      Author: Rick Schaeffer <ricks@isc-br.isc-br.com>
  6.  */
  7.  
  8. /****** net.lib/popen *******************************************************
  9.     NAME
  10.         popen, pclose - initiate I/O to/from a process
  11.  
  12.     SYNOPSIS
  13.         #include <stdio.h>
  14.  
  15.         FILE *popen(command, type)
  16.         char *command, *type;
  17.  
  18.         pclose(stream)
  19.         FILE *stream;
  20.  
  21.     DESCRIPTION
  22.         The arguments to popen are pointers to null-terminated
  23.         strings containing respectively a command line and an
  24.         I/O mode, either "r" for reading or "w" for writing.  It
  25.         creates a pipe between the calling process and the command
  26.         to be executed.  The value returned is a stream pointer that
  27.         can be used (as appropriate) to write to the standard input
  28.         of the command or read from its standard output.
  29.  
  30.         A stream opened by popen **MUST** be closed by pclose, which
  31.         waits for the associated process to terminate and returns
  32.         the exit status of the command.
  33.  
  34.         Because stdio files are shared, a type "r" command may be
  35.         used as an input filter, and a type "w" as an output filter.
  36.  
  37.     DIAGNOSTICS
  38.         Popen returns a null pointer if files or processes cannot be
  39.         created.
  40.  
  41.         Pclose returns -1 if stream is not associated with a
  42.         `popened' command.
  43.  
  44.     AUTHOR
  45.         Original version by Rick Schaeffer <ricks@isc-br.isc-br.com>
  46. */
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <stdarg.h>
  52.  
  53. #include <exec/types.h>
  54. #include <exec/memory.h>
  55. #include <dos/dos.h>
  56. #include <dos/dosextens.h>
  57. #include <dos/record.h>
  58. #include <dos/dostags.h>
  59.  
  60. #include <proto/exec.h>
  61. #include <proto/dos.h>
  62. #include <clib/alib_protos.h>
  63.  
  64. #include <errno.h>
  65.  
  66. #define NOTDEF
  67.  
  68. extern char *mktemp(char *);
  69.  
  70. struct POmsg {
  71.     struct Message  POm;
  72.     int     rc;
  73.     char        *cmd;
  74.     struct Library  *DOSBase;
  75.     };
  76.  
  77.  
  78. struct pstruct {
  79.     FILE    *fptr;
  80.     struct POmsg    childmsg;
  81.     };
  82.  
  83. #define MAXPIPES    6
  84. static struct pstruct poarray[MAXPIPES];
  85.  
  86. static int childprocess(void);
  87.  
  88. FILE *popen(const char *cmd, const char *mode)
  89. {
  90.     static char tempname[] = "pipe:pXXX.XXX";
  91.     char        *pname,redir[20];
  92.     short       i;
  93.     int         pmode;
  94.     struct pstruct  *poptr;
  95.     BPTR            pfd;
  96.     struct Process  *child;
  97.     struct CommandLineInterface *cli;
  98.     BPTR Binfh, Boutfh;
  99.     ULONG closeinp, closeoutp;
  100.     ULONG stacksize;
  101.     struct Process *thistask;
  102.  
  103.     /* First, get pointers to our process and cli structs */
  104.     thistask = (struct Process *) FindTask(NULL);
  105.     cli = Cli();
  106.     poptr = NULL;
  107.  
  108.     /* now find an open pipe (we currently only allow 6 simultaneously
  109.        open pipes) */
  110.     for (i=0; i<MAXPIPES; i++) {
  111.         if (poarray[i].fptr == NULL) {
  112.             poptr = &poarray[i];
  113.             break;
  114.             }
  115.         }
  116.     if (poptr == NULL) {
  117.         fprintf(stderr,"popen: Unable to find an open pipe\n");
  118.         errno = EMFILE;
  119.         return(NULL);
  120.         }
  121.     if (strcmp(mode,"r") == 0)
  122.         pmode = MODE_NEWFILE;
  123.     else if (strcmp(mode,"w") == 0)
  124.         pmode = MODE_OLDFILE;
  125.     else {
  126.         fprintf(stderr,"popen: Mode must be 'r' or 'w'\n");
  127.         errno = EINVAL;
  128.         return(NULL);
  129.         }
  130.  
  131.     /* Try to make a guaranteed unique file name for the pipe */
  132.     strcpy(redir,tempname);
  133.     redir[5] = 'a' + i;
  134.  
  135.     pname = mktemp(redir);            /* set up a pipe: file name */
  136.  
  137.     /* Now get the child's stack and priority set up */
  138.     if (cli)
  139.         stacksize = cli->cli_DefaultStack << 2;
  140.     else
  141.         stacksize = thistask->pr_StackSize;
  142.  
  143.     /* Open the side of the pipe for the child */
  144.     pfd = Open(pname,pmode);
  145.     if (pfd == 0) {
  146.         errno = __io2errno(_OSERR = IoErr());
  147.         fprintf(stderr,"popen: Unable to open pipe file\n");
  148.         return(NULL);
  149.         }
  150.  
  151.     /* set up the tags for the new process */
  152.     if (pmode == MODE_NEWFILE) {
  153.         Binfh     = (Tag) Input();
  154.         Boutfh    = (Tag) pfd;
  155.         closeinp  = FALSE;
  156.         closeoutp = TRUE;
  157.         }
  158.     else {
  159.         Binfh     = (Tag) pfd;
  160.         Boutfh    = (Tag) Output();
  161.         closeinp  = TRUE;
  162.         closeoutp = FALSE;
  163.         }
  164.  
  165.     /* create the command.  since the "System" function runs through
  166.        the default shell, we need to tell it not to fail so that we
  167.        ALWAYS get back the exit status.  This wouldn't be necessary
  168.        if the CLI created by the System function inherited the parent's
  169.        FAILAT level
  170.     */
  171.     poptr->childmsg.cmd = malloc(strlen(cmd) + 15);
  172.     strcpy(poptr->childmsg.cmd,"failat 9999\n");
  173.     strcat(poptr->childmsg.cmd,cmd);
  174.  
  175.     /* Create a port that we can get the child's exit status through */
  176.     poptr->childmsg.POm.mn_ReplyPort = CreateMsgPort();
  177.     poptr->childmsg.POm.mn_Node.ln_Type = NT_MESSAGE;
  178.     poptr->childmsg.POm.mn_Node.ln_Pri = 0;
  179.     if (poptr->childmsg.POm.mn_ReplyPort == 0) {
  180.         fprintf(stderr,"popen: Couldn't create message port\n");
  181.         errno = ENOMEM;
  182.         return(NULL);
  183.         }
  184.  
  185.     /* Now we can start the new process.  NOTE: this is actually going
  186.        to create a process consisting ONLY of the function "childprocess"
  187.        which can be seen below.  childprocess() then runs the command
  188.        passed in the startup message.
  189.     */
  190.     child = CreateNewProcTags(
  191.         NP_Entry,   (Tag) childprocess,
  192.         NP_Input,   Binfh,
  193.         NP_Output,  Boutfh,
  194.         NP_CloseInput,  closeinp,
  195.         NP_CloseOutput, closeoutp,
  196.         NP_StackSize,   stacksize,
  197.         NP_Cli,     TRUE,
  198.         TAG_DONE
  199.         );
  200.  
  201.     poptr->childmsg.DOSBase = (struct Library *)DOSBase;
  202.  
  203.     /* now pass the child the startup message */
  204.     PutMsg(&child->pr_MsgPort,(struct Message *) &poptr->childmsg);
  205.  
  206.     /* Now open our side of the pipe */
  207.     poptr->fptr = fopen(pname,mode);
  208.     if (poptr->fptr == NULL) {
  209.         fprintf(stderr,"popen: Unable to open pipe file %s\n",pname);
  210.         DeleteMsgPort(poptr->childmsg.POm.mn_ReplyPort);
  211.         return(NULL);
  212.         }
  213.     return(poptr->fptr);
  214. }
  215.  
  216. FILE *popenl(const char *arg0, ...)
  217. {
  218.     va_list ap;
  219.     char argbuf[512], *mode;
  220.  
  221.     strcpy(argbuf, arg0);
  222.     va_start(ap, arg0);
  223.     while(1)
  224.     {
  225.         char *s = va_arg(ap, char *);
  226.  
  227.         if(s == NULL)
  228.         {
  229.         strcat(argbuf, "\n");
  230.         break;
  231.         } /* if */
  232.  
  233.         strcat(argbuf, " ");
  234.  
  235.         if(strchr(s, ' '))
  236.         {
  237.         strcat(argbuf, "\"");
  238.         strcat(argbuf, s);
  239.         strcat(argbuf, "\"");
  240.         }
  241.         else
  242.         {
  243.         strcat(argbuf, s);
  244.         } /* if */
  245.     }
  246.     mode = va_arg(ap, char *);
  247.     va_end(ap);
  248.  
  249.     return(popen(argbuf, mode));
  250.  
  251. } /* popenl */
  252.  
  253. int pclose(FILE *fptr)
  254. {
  255.     short       i;
  256.  
  257.     /* Figure out which pipe we used for this file */
  258.     for (i=0; i<MAXPIPES; i++)
  259.         if (poarray[i].fptr == fptr)
  260.             break;
  261.     if (i >= MAXPIPES) {
  262.         fprintf(stderr,"popen: DISASTER...couldn't find file pointer in pclose\n");
  263.         exit(1);
  264.         }
  265.  
  266.     /* close the file */
  267.     fclose(fptr);
  268.  
  269.     /* now wait for the exit status */
  270.     WaitPort(poarray[i].childmsg.POm.mn_ReplyPort);
  271.     poarray[i].fptr = NULL;
  272.  
  273.     /* clean things up */
  274.     DeletePort(poarray[i].childmsg.POm.mn_ReplyPort);
  275.     free(poarray[i].childmsg.cmd);
  276.     return(poarray[i].childmsg.rc);
  277. }
  278.  
  279. /* SAS/C autoinitialization for cleanup! */
  280. void __stdargs _STD_4000_popen(void)
  281. {
  282.     short i;
  283.  
  284.     /* Close all the open pipes! */
  285.     for(i=0; i<MAXPIPES; i++)
  286.     {
  287.         if(poarray[i].fptr)
  288.         {
  289.             pclose(poarray[i].fptr);
  290.         } /* if */
  291.     } /* for */
  292.  
  293. } /* _STD_4000_popen */
  294.  
  295. #ifdef NOTDEF
  296.  
  297. char *mktemp(char * template)
  298. {
  299.     register char *cp;
  300.     register unsigned long val;
  301.  
  302.     cp = template;
  303.     cp += strlen(cp);
  304.     for (val = (unsigned long) FindTask(0L) ; ; )
  305.         if (*--cp == 'X') {
  306.             *cp = val%10 + '0';
  307.             val /= 10;
  308.         } else if (*cp != '.')
  309.             break;
  310.  
  311.     if (*++cp != 0) {
  312.         *cp = 'A';
  313.         while (access(template, 0) == 0) {
  314.             if (*cp == 'Z') {
  315.                 *template = 0;
  316.                 break;
  317.             }
  318.             ++*cp;
  319.         }
  320.     } else {
  321.         if (access(template, 0) == 0)
  322.             *template = 0;
  323.     }
  324.     return template;
  325. }
  326.  
  327. #endif
  328.  
  329. /* WATCH OUT! This only works without __saveds because of the special
  330.    SAS/C 6.1 tricks I use! Check the output with omd! */
  331. static int __interrupt childprocess(void)
  332. {
  333.     struct ExecBase *SysBase = *((struct ExecBase **)4);
  334.     struct Library *DOSBase;
  335.     struct Process  *me;
  336.     struct POmsg    *startupmsg;
  337.     int             i = RETURN_FAIL;
  338.  
  339.     /* find our process structure */
  340.     me = (struct Process *) FindTask(NULL);
  341.  
  342.     /* Wait for the parent to kick us off */
  343.     WaitPort(&me->pr_MsgPort);
  344.  
  345.     /* Get the command to execute */
  346.     startupmsg = (struct POmsg *) GetMsg(&me->pr_MsgPort);
  347.  
  348.     DOSBase = startupmsg->DOSBase;
  349.  
  350.     if(DOSBase)
  351.     {
  352.         /* Now run the command.  stdin and stdout are already set up */
  353.         i = SystemTags(startupmsg->cmd,
  354.                SYS_UserShell, 1,
  355.                TAG_DONE);
  356.     } /* if */
  357.  
  358.     if(i > 0)
  359.     {
  360.         /* UNIX compatibility ... */
  361.         i <<= 8;
  362.     } /* if */
  363.  
  364.     startupmsg->rc = i;
  365.     /* pass the exit code back to the parent */
  366.     ReplyMsg((struct Message *) startupmsg);
  367.     return(0);
  368. }
  369.