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

  1. /* Open a pipe to read from a program without intermediary sh.
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie.  */
  19.  
  20. #if HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23.  
  24. #include <stdio.h>
  25.  
  26. #if __STDC__
  27. #include <stdarg.h>
  28. #else
  29. #include <varargs.h>
  30. #endif
  31.  
  32. #if HAVE_UNISTD_H
  33. # include <unistd.h>
  34. #endif
  35.  
  36. #ifdef __amigaos__
  37. #define fork vfork
  38. #endif
  39.  
  40. /* Open a pipe to read from a program without intermediary sh.  Checks
  41.    PATH.  Sample use:
  42.    
  43.    stream = readpipe ("progname", "arg1", "arg2", (char *) 0);
  44.    
  45.    Return 0 on error.  */
  46.  
  47. #if __STDC__
  48. FILE *
  49. readpipe (char *progname, ...)
  50. #else
  51. FILE *
  52. readpipe (...)
  53. #endif
  54. {
  55. #if ! __STDC__
  56.   char *progname;
  57. #endif
  58.   int fds[2];
  59.   va_list ap;
  60.   char *args[100];
  61.   int argno = 0;
  62.  
  63.   /* Copy arguments into `args'. */
  64. #if __STDC__
  65.   va_start (ap, progname);
  66. #else
  67.   va_start (ap);
  68.   progname = va_arg (ap, char *);
  69. #endif
  70.   args[argno++] = progname;
  71.   while ((args[argno++] = va_arg (ap, char *)) != NULL)
  72.     ;
  73.   va_end (ap);
  74.  
  75.   if (pipe (fds) == -1)
  76.     return 0;
  77.  
  78.   switch (fork ())
  79.     {
  80.     case 0:            /* Child.  Write to pipe. */
  81.       close (fds[0]);        /* Not needed. */
  82.       if (fds[1] != 1)        /* Redirect 1 (stdout) only if needed.  */
  83.     {
  84.       close (1);        /* We don't want the old stdout. */
  85.       if (dup (fds[1]) == 0)/* Maybe stdin was closed. */
  86.         {
  87.           dup (fds[1]);    /* Guaranteed to dup to 1 (stdout). */
  88.           close (0);
  89.         }
  90.       close (fds[1]);    /* No longer needed. */
  91.     }
  92.       execvp (args[0], args);
  93.       _exit (2);        /* 2 for `cmp'. */
  94.     case -1:            /* Error. */
  95.       return 0;
  96.     default:            /* Parent.  Read from pipe. */
  97.       close (fds[1]);        /* Not needed. */
  98.       return fdopen (fds[0], "r");
  99.     }
  100. }
  101.