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

  1. /* Open a pipe to write to 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 write to a program without intermediary sh.  Checks
  41.    PATH.  Sample use:
  42.    
  43.    stream = writepipe ("progname", "arg1", "arg2", (char *) 0);
  44.    
  45.    Return 0 on error.  */
  46.  
  47. #if __STDC__
  48. FILE *
  49. writepipe (char *progname, ...)
  50. #else
  51. FILE *
  52. writepipe (...)
  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.  Read from pipe. */
  81.       close (fds[1]);        /* Not needed. */
  82.       if (fds[0] != 0)        /* Redirect 0 (stdin) only if needed.  */
  83.     {
  84.       close (0);        /* We don't want the old stdin. */
  85.       dup (fds[0]);        /* Guaranteed to dup to 0 (stdin). */
  86.       close (fds[0]);    /* No longer needed. */
  87.     }
  88.       execvp (args[0], args);
  89.       _exit (2);        /* 2 for `cmp'. */
  90.     case -1:            /* Error. */
  91.       return 0;
  92.     default:            /* Parent.  Write to pipe. */
  93.       close (fds[0]);        /* Not needed. */
  94.       return fdopen (fds[1], "w");
  95.     }
  96. }
  97.