home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-19.28-src.tgz / tar.out / fsf / emacs / unixlib / src / popen.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  2KB  |  94 lines

  1. #include "amiga.h"
  2. #include "processes.h"
  3. #include "fifofd.h"
  4. #include <amiga/ioctl.h>
  5. #include <exec/memory.h>
  6. #include <dos/dosextens.h>
  7. #include <dos/dostags.h>
  8. #include <string.h>
  9.  
  10. struct pprocess
  11. {
  12.   struct pprocess *next;
  13.   FILE *f;
  14.   int pid;
  15. };
  16.  
  17. static struct pprocess *_pplist;
  18.  
  19. FILE *popen(char *command, char *type)
  20. {
  21.   FILE *pipe;
  22.   BPTR in, out;
  23.   int close_in, close_out;
  24.   char pname[24];
  25.   struct pprocess *pp = (struct pprocess *)malloc(sizeof(struct pprocess));
  26.  
  27.   _sprintf(pname, "pipe:uxopen.%lx", _fifo_base + _fifo_offset++);
  28.  
  29.   if (type[0] == 'w' && type[1] == '\0')
  30.     {
  31.       pipe = fopen(pname, "w");
  32.       out = Output(); close_out = FALSE;
  33.       in = Open(pname, MODE_OLDFILE); close_in = TRUE;
  34.     }
  35.   else if (type[0] == 'r' && type[1] == '\0')
  36.     {
  37.       pipe = fopen(pname, "r");
  38.       in = Input(); close_in = FALSE;
  39.       out = Open(pname, MODE_NEWFILE); close_out = TRUE;
  40.     }
  41.   else
  42.     {
  43.       errno = EINVAL;
  44.       return NULL;
  45.     }
  46.  
  47.   if (!in || !out) _seterr();
  48.   if (pipe && in && out && pp)
  49.     {
  50.       pp->pid = _start_process(command, in, close_in, out, close_out,
  51.                    -1, FALSE, 0, 0);
  52.       
  53.       if (pp->pid)
  54.     {
  55.       pp->next = _pplist;
  56.       _pplist = pp;
  57.       pp->f = pipe;
  58.  
  59.       return pipe;
  60.     }
  61.     }
  62.   if (pp) free(pp);
  63.   if (in && close_in) Close(in);
  64.   if (out && close_out) Close(out);
  65.   if (pipe) fclose(pipe);
  66.  
  67.   return NULL;
  68. }
  69.  
  70. int pclose(FILE *f)
  71. {
  72.   struct pprocess **scan = &_pplist;
  73.  
  74.   while (*scan)
  75.     {
  76.       if ((*scan)->f == f) /* found */
  77.     {
  78.       struct pprocess *son = *scan;
  79.       int status;
  80.       int pid = son->pid;
  81.  
  82.       *scan = son->next;    /* Remove process from list */
  83.  
  84.       fclose(son->f);
  85.       free(son);
  86.       /* Wait for process to terminate */
  87.       if (waitpid(pid, &status, NULL) >= 0) return status;
  88.       return -1;
  89.     }
  90.       scan = &(*scan)->next;
  91.     }
  92.   return -1;
  93. }
  94.