home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / ftpd / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  4.7 KB  |  165 lines

  1. /*
  2.  * Copyright (c) 1988 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software written by Ken Arnold and
  6.  * published in UNIX Review, Vol. 6, No. 8.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  *
  36.  */
  37.  
  38. #ifndef lint
  39. /*static char sccsid[] = "from: @(#)popen.c    5.9 (Berkeley) 2/25/91";*/
  40. static char rcsid[] = "$Id: popen.c,v 1.1 1994/05/23 09:03:47 rzsfl Exp rzsfl $";
  41. #endif /* not lint */
  42.  
  43. #include <sys/types.h>
  44. #include <sys/wait.h>
  45. #include <signal.h>
  46. #include <unistd.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50.  
  51. /*
  52.  * Special version of popen which avoids call to shell.  This insures noone
  53.  * may create a pipe to a hidden program as a side effect of a list or dir
  54.  * command.
  55.  */
  56. static int *pids;
  57. static int fds;
  58.  
  59. FILE *
  60. ftpd_popen(program, type)
  61.     char *program, *type;
  62. {
  63.     register char *cp;
  64.     FILE *iop;
  65.     int argc, gargc, pdes[2], pid;
  66.     char **pop, *argv[100], *gargv[1000], *vv[2];
  67.     extern char **ftpglob(), **copyblk();
  68.  
  69.     if (*type != 'r' && *type != 'w' || type[1])
  70.         return(NULL);
  71.  
  72.     if (!pids) {
  73.         if ((fds = getdtablesize()) <= 0)
  74.             return(NULL);
  75.         if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
  76.             return(NULL);
  77.         bzero((char *)pids, fds * sizeof(int));
  78.     }
  79.     if (pipe(pdes) < 0)
  80.         return(NULL);
  81.  
  82.     /* break up string into pieces */
  83.     for (argc = 0, cp = program;; cp = NULL)
  84.         if (!(argv[argc++] = strtok(cp, " \t\n")))
  85.             break;
  86.  
  87.     /* glob each piece */
  88.     gargv[0] = argv[0];
  89.     for (gargc = argc = 1; argv[argc]; argc++) {
  90.         argv[argc] = strdup(argv[argc]);
  91.         if (!(pop = ftpglob(argv[argc]))) {    /* globbing failed */
  92.             vv[0] = argv[argc];
  93.             vv[1] = NULL;
  94.             pop = copyblk(vv);
  95.         }
  96.         argv[argc] = (char *)pop;        /* save to free later */
  97.         while (*pop && gargc < 1000)
  98.             gargv[gargc++] = *pop++;
  99.     }
  100.     gargv[gargc] = NULL;
  101.  
  102.     iop = NULL;
  103.     switch(pid = vfork()) {
  104.     case -1:            /* error */
  105.         (void)close(pdes[0]);
  106.         (void)close(pdes[1]);
  107.         goto pfree;
  108.         /* NOTREACHED */
  109.     case 0:                /* child */
  110.         if (*type == 'r') {
  111.             if (pdes[1] != 1) {
  112.                 dup2(pdes[1], 1);
  113.                 dup2(pdes[1], 2);    /* stderr, too! */
  114.                 (void)close(pdes[1]);
  115.             }
  116.             (void)close(pdes[0]);
  117.         } else {
  118.             if (pdes[0] != 0) {
  119.                 dup2(pdes[0], 0);
  120.                 (void)close(pdes[0]);
  121.             }
  122.             (void)close(pdes[1]);
  123.         }
  124.         execv(gargv[0], gargv);
  125.         _exit(1);
  126.     }
  127.     /* parent; assume fdopen can't fail...  */
  128.     if (*type == 'r') {
  129.         iop = fdopen(pdes[0], type);
  130.         (void)close(pdes[1]);
  131.     } else {
  132.         iop = fdopen(pdes[1], type);
  133.         (void)close(pdes[0]);
  134.     }
  135.     pids[fileno(iop)] = pid;
  136.  
  137. pfree:    for (argc = 1; argv[argc] != NULL; argc++) {
  138.         blkfree((char **)argv[argc]);
  139.         free((char *)argv[argc]);
  140.     }
  141.     return(iop);
  142. }
  143.  
  144. ftpd_pclose(iop)
  145.     FILE *iop;
  146. {
  147.     register int fdes;
  148.     int omask;
  149.     union wait stat_loc;
  150.     int pid;
  151.  
  152.     /*
  153.      * pclose returns -1 if stream is not associated with a
  154.      * `popened' command, or, if already `pclosed'.
  155.      */
  156.     if (pids == 0 || pids[fdes = fileno(iop)] == 0)
  157.         return(-1);
  158.     (void)fclose(iop);
  159.     omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
  160.     while ((pid = wait((int *)&stat_loc)) != pids[fdes] && pid != -1);
  161.     (void)sigsetmask(omask);
  162.     pids[fdes] = 0;
  163.     return(pid == -1 ? -1 : stat_loc.w_status);
  164. }
  165.