genericunixprotocols/ftpsrv/src/popen.cpp
changeset 0 c6b0df440bee
equal deleted inserted replaced
-1:000000000000 0:c6b0df440bee
       
     1 //
       
     2 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 // All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 //
       
     9 // Initial Contributors:
       
    10 // Nokia Corporation - initial contribution.
       
    11 //
       
    12 // Contributors:
       
    13 //
       
    14 // Description:
       
    15 //
       
    16 /*
       
    17  * Copyright (c) 1988, 1993, 1994
       
    18  *	The Regents of the University of California.  All rights reserved.
       
    19  *
       
    20  * This code is derived from software written by Ken Arnold and
       
    21  * published in UNIX Review, Vol. 6, No. 8.
       
    22  *
       
    23  * Redistribution and use in source and binary forms, with or without
       
    24  * modification, are permitted provided that the following conditions
       
    25  * are met:
       
    26  * 1. Redistributions of source code must retain the above copyright
       
    27  *    notice, this list of conditions and the following disclaimer.
       
    28  * 2. Redistributions in binary form must reproduce the above copyright
       
    29  *    notice, this list of conditions and the following disclaimer in the
       
    30  *    documentation and/or other materials provided with the distribution.
       
    31  * 3. All advertising materials mentioning features or use of this software
       
    32  *    must display the following acknowledgement:
       
    33  *	This product includes software developed by the University of
       
    34  *	California, Berkeley and its contributors.
       
    35  * 4. Neither the name of the University nor the names of its contributors
       
    36  *    may be used to endorse or promote products derived from this software
       
    37  *    without specific prior written permission.
       
    38  *
       
    39  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
       
    40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
       
    43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
       
    44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
       
    45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
       
    47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
       
    48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
       
    49  * SUCH DAMAGE.
       
    50  *
       
    51  */
       
    52 
       
    53 /*
       
    54  * From: @(#)popen.c	8.3 (Berkeley) 4/6/94
       
    55  * From: NetBSD: popen.c,v 1.5 1995/04/11 02:45:00 cgd Exp
       
    56  * From: OpenBSD: popen.c,v 1.8 1996/12/07 10:52:06 bitblt Exp
       
    57  * From: OpenBSD: popen.c,v 1.10 1999/02/26 00:15:54 art Exp
       
    58  */
       
    59 char popen_rcsid[] = 
       
    60   "$Id: popen.c,v 1.6 1999/07/16 01:12:54 dholland Exp $";
       
    61 
       
    62 #include <sys/types.h>
       
    63 #include <sys/wait.h>
       
    64 #include <sys/param.h>
       
    65 #include <sys/stat.h>
       
    66 
       
    67 #include <arpa/ftp.h>
       
    68 
       
    69 #include <errno.h>
       
    70 #include <glob.h>
       
    71 #include <signal.h>
       
    72 #include <stdio.h>
       
    73 #include <stdlib.h>
       
    74 #include <string.h>
       
    75 #include <unistd.h>
       
    76 #include <dirent.h>
       
    77 
       
    78 #include "extern.h"
       
    79 
       
    80 
       
    81 //#define SIG_BLOCK 1	/* set of signals to block */
       
    82 //#define SIG_SETMASK 0	/* set mask with sigprocmask() */
       
    83 
       
    84 /*
       
    85  * Special version of popen which avoids call to shell.  This ensures noone
       
    86  * may create a pipe to a hidden program as a side effect of a list or dir
       
    87  * command.
       
    88  *
       
    89  * Note: destroys "program".
       
    90  */
       
    91 static int *pids;
       
    92 
       
    93 
       
    94 extern int root;
       
    95 extern int type;
       
    96 extern off_t	byte_count;
       
    97 
       
    98 
       
    99 #define MAX_ARGV	100
       
   100 #define MAX_GARGV	1000
       
   101 
       
   102 
       
   103 FILE *
       
   104 ftpd_popen(char *program, const char *type)
       
   105 {
       
   106 	FILE *volatile iop;
       
   107 	int pdes[2];
       
   108 
       
   109 	if ((*type != 'r' && *type != 'w') || type[1])
       
   110 		return (NULL);
       
   111 
       
   112 
       
   113 	if (pipe(pdes) < 0)
       
   114 		return (NULL);
       
   115 	
       
   116 	
       
   117 	 if (( iop = popen(program, "r")) == NULL)
       
   118 		    {
       
   119 			 return (NULL);
       
   120 		    }
       
   121 	
       
   122 	if (*type == 'r') {
       
   123 	iop = fdopen(pdes[0], type);
       
   124 	(void)close(pdes[1]);
       
   125  	} else {
       
   126 	iop = fdopen(pdes[1], type);
       
   127 	(void)close(pdes[0]);
       
   128  	}
       
   129 	
       
   130 
       
   131 	return (iop);
       
   132 }
       
   133 
       
   134 int
       
   135 ftpd_pclose(FILE *iop)
       
   136 {
       
   137 	int fdes, status;
       
   138 	pid_t pid;
       
   139 	
       
   140 	/*
       
   141 	 * pclose returns -1 if stream is not associated with a
       
   142 	 * `popened' command, or, if already `pclosed'.
       
   143 	 */
       
   144 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
       
   145 		return (-1);
       
   146 	(void)fclose(iop);
       
   147 
       
   148 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
       
   149 		continue;
       
   150 	pids[fdes] = 0;
       
   151 	if (pid < 0)
       
   152 		return (pid);
       
   153 	if (WIFEXITED(status))
       
   154 		return (WEXITSTATUS(status));
       
   155 	return (1);
       
   156 }
       
   157 
       
   158 
       
   159 
       
   160 
       
   161 
       
   162 
       
   163