home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / libgplus.5 / libio / iopopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-27  |  5.1 KB  |  213 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /*  written by Per Bothner (bothner@cygnus.com) */
  26.  
  27. #include "libioP.h"
  28.  
  29. #if _IO_HAVE_SYS_WAIT
  30. #include <signal.h>
  31. #include <unistd.h>
  32. #include <sys/wait.h>
  33.  
  34. #ifndef _IO_fork
  35. #define _IO_fork vfork /* defined in libiberty, if needed */
  36. _IO_pid_t _IO_fork(void);
  37. #endif
  38.  
  39. #endif /* _IO_HAVE_SYS_WAIT */
  40.  
  41. #ifndef _IO_pipe
  42. #define _IO_pipe pipe
  43. extern int _IO_pipe();
  44. #endif
  45.  
  46. #ifndef _IO_dup2
  47. #define _IO_dup2 dup2
  48. extern int _IO_dup2();
  49. #endif
  50.  
  51. #ifndef _IO_execl
  52. #define _IO_execl execl
  53. #endif
  54. #ifndef _IO__exit
  55. #define _IO__exit _exit
  56. #endif
  57.  
  58. #ifdef TODO
  59. /* Ditto for signal, sigxxxmask, wait etc */
  60. #endif
  61.  
  62. struct _IO_proc_file
  63. {
  64.   struct _IO_FILE_plus _file;
  65.   /* Following fields must match those in class procbuf (procbuf.h) */
  66.   _IO_pid_t _pid;
  67. };
  68. typedef struct _IO_proc_file _IO_proc_file;
  69.  
  70. _IO_FILE *
  71. _IO_proc_open(fp, command, mode)
  72.      _IO_FILE* fp;
  73.      const char *command;
  74.      const char *mode;
  75. {
  76. #if _IO_HAVE_SYS_WAIT
  77.   int read_or_write;
  78.   int pipe_fds[2];
  79.   int parent_end, child_end;
  80.   _IO_pid_t child_pid;
  81.   if (_IO_file_is_open(fp))
  82.     return NULL;
  83.   if (_IO_pipe(pipe_fds) < 0)
  84.     return NULL;
  85.   if (mode[0] == 'r')
  86.     {
  87.       parent_end = pipe_fds[0];
  88.       child_end = pipe_fds[1];
  89.       read_or_write = _IO_NO_WRITES;
  90.     }
  91.   else
  92.     {
  93.       parent_end = pipe_fds[1];
  94.       child_end = pipe_fds[0];
  95.       read_or_write = _IO_NO_READS;
  96.     }
  97.   ((_IO_proc_file*)fp)->_pid = child_pid = _IO_fork();
  98.   if (child_pid == 0)
  99.     {
  100.       int child_std_end = mode[0] == 'r' ? 1 : 0;
  101.       _IO_close(parent_end);
  102.       if (child_end != child_std_end)
  103.     {
  104.       _IO_dup2(child_end, child_std_end);
  105.       _IO_close(child_end);
  106.     }
  107.       _IO_execl("/bin/sh", "sh", "-c", command, NULL);
  108.       _IO__exit(127);
  109.     }
  110.   _IO_close(child_end);
  111.   if (child_pid < 0)
  112.     {
  113.       _IO_close(parent_end);
  114.       return NULL;
  115.     }
  116.   _IO_fileno(fp) = parent_end;
  117.   fp->_IO_file_flags
  118.     = read_or_write | (fp->_IO_file_flags & ~(_IO_NO_READS|_IO_NO_WRITES));
  119.   return fp;
  120. #else /* !_IO_HAVE_SYS_WAIT */
  121.   return NULL;
  122. #endif
  123. }
  124.  
  125. _IO_FILE *
  126. _IO_popen(command, mode)
  127.      const char *command;
  128.      const char *mode;
  129. {
  130.   _IO_proc_file *fpx = (_IO_proc_file*)malloc(sizeof(_IO_proc_file));
  131.   _IO_FILE *fp = (_IO_FILE*)fpx;
  132.   _IO_init(fp, 0);
  133.   fp->_jumps = &_IO_proc_jumps;
  134.   _IO_file_init(fp);
  135.   if (fp == NULL)
  136.     return NULL;
  137.   ((struct _IO_FILE_plus*)fp)->_vtable = NULL;
  138.   if (_IO_proc_open (fp, command, mode) != NULL)
  139.     return fp;
  140.   free (fpx);
  141.   return NULL;
  142. }
  143.  
  144. int
  145. _IO_proc_close(fp)
  146.      _IO_FILE *fp;
  147. {
  148.   /* This is not name-space clean. FIXME! */
  149. #if _IO_HAVE_SYS_WAIT
  150.   int wstatus;
  151.   _IO_pid_t wait_pid;
  152.   int status = _IO_close(_IO_fileno(fp));
  153. #if defined(SIG_BLOCK) && defined(SIG_SETMASK)
  154.   sigset_t set, oset;
  155. #endif
  156.   if (status < 0)
  157.     return status;
  158. #if defined(SIG_BLOCK) && defined(SIG_SETMASK)
  159.   sigemptyset (&set);
  160.   sigaddset (&set, SIGINT);
  161.   sigaddset (&set, SIGQUIT);
  162.   sigaddset (&set, SIGHUP);
  163.   sigprocmask (SIG_BLOCK, &set, &oset);
  164. #else
  165. #ifdef USE_SIGMASK
  166.   int mask = sigblock(sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGHUP));
  167. #else
  168.   typedef void (*void_func)(int);
  169.   void_func intsave = (void_func)signal(SIGINT, SIG_IGN);
  170.   void_func quitsave = (void_func)signal(SIGQUIT, SIG_IGN);
  171.   void_func hupsave = (void_func)signal(SIGHUP, SIG_IGN);
  172. #endif
  173. #endif
  174.   while ((wait_pid = wait(&wstatus)) != ((_IO_proc_file*)fp)->_pid
  175.      && wait_pid != -1) { }
  176. #if defined(SIG_BLOCK) && defined(SIG_SETMASK)
  177.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  178. #else
  179. #ifdef USE_SIGMASK
  180.   (void) sigsetmask(mask);
  181. #else
  182.   signal(SIGINT, intsave);
  183.   signal(SIGQUIT, quitsave);
  184.   signal(SIGHUP, hupsave);
  185. #endif
  186. #endif
  187.   if (wait_pid == -1)
  188.     return -1;
  189.   return 0;
  190. #else /* !_IO_HAVE_SYS_WAIT */
  191.   return -1;
  192. #endif
  193. }
  194.  
  195. struct _IO_jump_t _IO_proc_jumps = {
  196.   _IO_file_overflow,
  197.   _IO_file_underflow,
  198.   _IO_file_xsputn,
  199.   _IO_default_xsgetn,
  200.   _IO_file_read,
  201.   _IO_file_write,
  202.   _IO_file_doallocate,
  203.   _IO_default_pbackfail,
  204.   _IO_file_setbuf,
  205.   _IO_file_sync,
  206.   _IO_file_finish,
  207.   _IO_proc_close,
  208.   _IO_file_stat,
  209.   _IO_file_seek,
  210.   _IO_file_seekoff,
  211.   _IO_default_seekpos,
  212. };
  213.