📄 popen.c
字号:
#ifndef lintstatic char *sccsid = "@(#)popen.c 4.1 (ULTRIX) 7/3/90";#endif lint/************************************************************************ * * * Copyright (c) 1985 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//************************************************************************ * Modification History * * David L Ballenger, 5-Sep-1985 * 001 Allocate array of PIDs dynamically based on number on the number * of files which can be opened. * * Based on: popen.c 4.4 (Berkeley) 9/25/83 * ************************************************************************/#include <stdio.h>#include <signal.h>#define tst(a,b) (*mode == 'r'? (b) : (a))#define RDR 0#define WTR 1extern char *calloc();extern int getdtablesize();static int *popen_pid;FILE *popen(cmd,mode)char *cmd;char *mode;{ int p[2]; register myside, hisside, pid; /* Allocate the array to store the PIDs for popen()'d pipes. * The array is allocated dynamically to allow programs to move * to systems with a larger file descriptor tables without * being recompiled. */ if (popen_pid == NULL) { popen_pid = (int *)calloc(getdtablesize(),sizeof(int)); if (popen_pid == NULL) return(NULL); } if(pipe(p) < 0) return NULL; myside = tst(p[WTR], p[RDR]); hisside = tst(p[RDR], p[WTR]); if((pid = fork()) == 0) { /* myside and hisside reverse roles in child */ close(myside); if (hisside != tst(0, 1)) { dup2(hisside, tst(0, 1)); close(hisside); } execl("/bin/sh", "sh", "-c", cmd, 0); _exit(1); } if(pid == -1) { close(myside); close(hisside); return NULL; } popen_pid[myside] = pid; close(hisside); return(fdopen(myside, mode));}pclose(ptr)FILE *ptr;{ register int f, r; void (*hstat)(), (*istat)(), (*qstat)(); int status; f = fileno(ptr); fclose(ptr); /* If the pid array has not been allocated, then this wasn't * opened with popen(), so return the error status. */ if (popen_pid == NULL) return(-1); istat = signal(SIGINT, SIG_IGN); qstat = signal(SIGQUIT, SIG_IGN); hstat = signal(SIGHUP, SIG_IGN); while((r = wait(&status)) != popen_pid[f] && r != -1) ; if(r == -1) status = -1; signal(SIGINT, istat); signal(SIGQUIT, qstat); signal(SIGHUP, hstat); return(status);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -