Files
Arquivotheca.Solaris-2.5/cmd/ypcmd/openchild.c
seta75D 7c4988eac0 Init
2021-10-11 19:38:01 -03:00

142 lines
2.9 KiB
C
Executable File

/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */
/* The copyright notice above does not evidence any */
/* actual or intended publication of such source code. */
#ident "@(#)openchild.c 1.4 92/07/14 SMI" /* SVr4.0 1.3 */
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* PROPRIETARY NOTICE (Combined)
*
* This source code is unpublished proprietary information
* constituting, or derived under license from AT&T's UNIX(r) System V.
* In addition, portions of such source code were derived from Berkeley
* 4.3 BSD under license from the Regents of the University of
* California.
*
*
*
* Copyright Notice
*
* Notice of copyright on this source code product does not indicate
* publication.
*
* (c) 1986,1987,1988,1989,1990 Sun Microsystems, Inc
* (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
* All rights reserved.
*/
#if !defined(lint) && defined(SCCSIDS)
static char sccsid[] = "@(#)openchild.c 1.7 88/02/08 Copyr 1986 Sun Micro";
#endif
/*
* openchild.c
*
* Open two pipes to a child process, one for reading, one for writing.
* The pipes are accessed by FILE pointers. This is NOT a public
* interface, but for internal use only!
*/
#include <stdio.h>
extern void *malloc();
extern char *strrchr();
static char *basename();
static char SHELL[] = "/bin/sh";
extern int close();
extern int dup();
extern int execl();
extern void exit();
extern long fork();
extern int pipe();
extern unsigned int strlen();
/*
* returns pid, or -1 for failure
*/
_openchild(command, fto, ffrom)
char *command;
FILE **fto;
FILE **ffrom;
{
int i;
int pid;
int pdto[2];
int pdfrom[2];
char *com;
if (pipe(pdto) < 0) {
goto error1;
}
if (pipe(pdfrom) < 0) {
goto error2;
}
switch (pid = fork()) {
case -1:
goto error3;
case 0:
/*
* child: read from pdto[0], write into pdfrom[1]
*/
(void) close(0);
(void) dup(pdto[0]);
(void) close(1);
(void) dup(pdfrom[1]);
for (i = _NFILE - 1; i >= 3; i--) {
(void) close(i);
}
com = malloc((unsigned) strlen(command) + 6);
if (com == NULL) {
exit(1);
}
(void) sprintf(com, "exec %s", command);
execl(SHELL, basename(SHELL), "-c", com, NULL);
exit(1);
default:
/*
* parent: write into pdto[1], read from pdfrom[0]
*/
*fto = fdopen(pdto[1], "w");
(void) close(pdto[0]);
*ffrom = fdopen(pdfrom[0], "r");
(void) close(pdfrom[1]);
break;
}
return (pid);
/*
* error cleanup and return
*/
error3:
printf("openchild: error3");
(void) close(pdfrom[0]);
(void) close(pdfrom[1]);
error2:
printf("openchild: error2");
(void) close(pdto[0]);
(void) close(pdto[1]);
error1:
printf("openchild: error1");
return (-1);
}
static char *
basename(path)
char *path;
{
char *p;
p = strrchr(path, '/');
if (p == NULL) {
return (path);
} else {
return (p + 1);
}
}