home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / stdio / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  6.1 KB  |  220 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. /*
  4.    This is popen() and pclose() for MSDOS.  They were developed using
  5.    Microsoft C, but should port easily to DOS C any compiler.
  6.    
  7.    Original author: pacetti@fl-ngnet.army.mil
  8.  
  9.    These routines are hacks, that is they SIMULATE thier UNIX
  10.    counterparts.  Since MSDOS and won't allow concurrent process spawning,
  11.    you can't really pipe.  I came up with this nearly stupid way around
  12.    piping because I wanted to have some portability between UNIX and MSDOS.
  13.    I'm probably not the first person to have this idea or implement it, but
  14.    I think I'm the first to give it away for free (no points?!).
  15.  
  16.    The functions simulate popen() and pclose() by redirecting stdin or
  17.    stdout, then spawning a child processes via system().
  18.  
  19.    If you popen() for read, the stdout is redirected to a temporary
  20.    file, and the child is spawned.  The stdout is reopened via dup2(), the
  21.    temporary file is opened for read and a file pointer to it is returned.
  22.  
  23.    If you popen() for write, a temporary file is opened for write, and
  24.    a file pointer to it is returned.  When you pclose(), the stdin is
  25.    redirected to the temporary file and the child is spawned.
  26.  
  27.    In both cases, pclose() closes and unlinks the temporary file.
  28.  
  29.    A static linked list of C structures is built to store necessary
  30.    info for all popen()ed files so you can popen() more than one file at
  31.    a time.
  32.  
  33.    The popen() function will return NULL on an error, or a valid FILE
  34.    *pointer on a successful open.  The pclose() function will return
  35.    negative one (-1) on an error or zero (0) on a successful close.
  36.  
  37.    The function prototypes are:
  38.  
  39.    FILE *popen(command, mode)
  40.         char *command, char *mode;
  41.  
  42.    int pclose(pp)
  43.        FILE *pp;
  44.  
  45.    Where command is a character string equivilant to a MSDOS command
  46.    line, mode is "r" for read or "w" for write, and pp is a pointer to a
  47.    file opened through popen().
  48.  
  49.    A main() function has been included for testing purposes, to compile
  50.    it define the preprocessor token TEST at compile time.
  51.  */
  52.  
  53. #include <libc/stubs.h>
  54. #include <io.h>
  55. #include <errno.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <unistd.h>
  60. #include <libc/file.h>
  61.  
  62. /* hold file pointer, descriptor, command, mode, temporary file name */
  63. struct pipe_list {
  64.   FILE *fp;
  65.   int fd;
  66.   char *command, mode[10], temp_name[FILENAME_MAX];
  67.   struct pipe_list *next;
  68. };
  69.  
  70. /* static, global list pointer */
  71. static struct pipe_list *pl = NULL;
  72.  
  73. FILE *
  74. popen (const char *cm, const char *md) /* program name, pipe mode */
  75. {
  76.   struct pipe_list *l1, *l2;
  77.   static char *tn = NULL;       /* temporary file basename */
  78.  
  79.   if (!tn)
  80.     if ((tn = tmpnam(0)) == NULL)
  81.       return NULL;
  82.  
  83.   /* make new node */
  84.   if ((l1 = (struct pipe_list *) malloc (sizeof (struct pipe_list))) == NULL)
  85.     return NULL;
  86.  
  87.   /* zero out elements to we'll get here */
  88.   l1->fd = 0;
  89.   l1->fp = NULL;
  90.   l1->next = NULL;
  91.  
  92.   /* if empty list - just grab new node */
  93.   if (!pl)
  94.     pl = l1;
  95.   else
  96.   {
  97.     /* otherwise, find last node in list */
  98.     ++(l1->fd);
  99.     l2 = pl;
  100.     while (l2->next)
  101.     {
  102.       ++(l1->fd);
  103.       l2 = l2->next;
  104.     };
  105.     /* add new node to list */
  106.     l2->next = l1;
  107.   }
  108.  
  109.   /* stick in elements we know already */
  110.   strcpy (l1->mode, md);
  111.   sprintf (l1->temp_name, "%s.%d", tn, l1->fd);
  112.  
  113.   /* if can save the program name, build temp file */
  114.   if ((l1->command = malloc(strlen(cm)+1)))
  115.   {
  116.     strcpy(l1->command, cm);
  117.     /* if caller wants to read */
  118.     if (l1->mode[0] == 'r')
  119.     {
  120.       /* dup stdout */
  121.       if ((l1->fd = dup (fileno (stdout))) == EOF)
  122.     l1->fp = NULL;
  123.       else if (!(l1->fp = freopen (l1->temp_name, "wb", stdout)))
  124.     l1->fp = NULL;
  125.       else
  126.     /* exec cmd */
  127.     if (system (cm) == EOF)
  128.       l1->fp = NULL;
  129.       /* reopen real stdout */
  130.       if (dup2 (l1->fd, fileno (stdout)) == EOF)
  131.     l1->fp = NULL;
  132.       else
  133.     /* open file for reader */
  134.     l1->fp = fopen (l1->temp_name, l1->mode);
  135.       close(l1->fd);
  136.     }
  137.     else
  138.       /* if caller wants to write */
  139.       if (l1->mode[0] == 'w')
  140.         /* open temp file */
  141.         l1->fp = fopen (l1->temp_name, l1->mode);
  142.       else
  143.         /* unknown mode */
  144.         l1->fp = NULL;
  145.   }
  146.   return l1->fp;              /* return == NULL ? ERROR : OK */
  147. }
  148.  
  149. int
  150. pclose (FILE *pp)
  151. {
  152.   struct pipe_list *l1, *l2;    /* list pointers */
  153.   int retval=0;            /* function return value */
  154.  
  155.   /* if pointer is first node */
  156.   if (pl->fp == pp)
  157.   {
  158.     /* save node and take it out the list */
  159.     l1 = pl;
  160.     pl = l1->next;
  161.   }
  162.   else
  163.     /* if more than one node in list */
  164.     if (pl->next)
  165.     {
  166.       /* find right node */
  167.       for (l2 = pl, l1 = pl->next; l1; l2 = l1, l1 = l2->next)
  168.         if (l1->fp == pp)
  169.           break;
  170.  
  171.       /* take node out of list */
  172.       l2->next = l1->next;
  173.     }
  174.     else
  175.       return -1;
  176.  
  177.   /* if FILE not in list - return error */
  178.   if (l1->fp == pp)
  179.   {
  180.     /* close the (hopefully) popen()ed file */
  181.     fclose (l1->fp);
  182.  
  183.     /* if pipe was opened to write */
  184.     if (l1->mode[0] == 'w')
  185.     {
  186.       /* dup stdin */
  187.       if ((l1->fd = dup (fileno (stdin))) == EOF)
  188.     retval = -1;
  189.       else
  190.     /* open temp stdin */
  191.     if (!(l1->fp = freopen (l1->temp_name, "rb", stdin)))
  192.       retval = -1;
  193.     else
  194.       /* exec cmd */
  195.           if (system (l1->command) == EOF)
  196.             retval = -1;
  197.           else
  198.       {
  199.             /* reopen stdin */
  200.         if (dup2 (l1->fd, fileno (stdin)) == EOF)
  201.           retval = -1;
  202.       }
  203.       close(l1->fd);
  204.     }
  205.     else
  206.       /* if pipe was opened to read */
  207.       if (l1->mode[0] == 'r')
  208.         retval = 0;
  209.       else
  210.         /* invalid mode */
  211.         retval = -1;
  212.   }
  213.   remove (l1->temp_name);       /* remove temporary file */
  214.   free (l1->command);           /* dealloc memory */
  215.   free (l1);                    /* dealloc memory */
  216.   l1 = NULL;                    /* make pointer bogus */
  217.  
  218.   return retval;              /* retval==0 ? OK : ERROR */
  219. }
  220.