Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84 +2.11; site dcl-cs.UUCP Path: utzoo!watmath!clyde!bonnie!akgua!whuxlm!whuxl!houxm!ihnp4!mhuxn!mhuxr!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!seismo!mcvax!ukc!icdoc!dcl-cs!stephen From: stephen@dcl-cs.UUCP (Stephen J. Muir) Newsgroups: net.sources Subject: psystem(3) - shell command with pipes Message-ID: <260@dcl-cs.UUCP> Date: Thu, 20-Jun-85 03:48:03 EDT Article-I.D.: dcl-cs.260 Posted: Thu Jun 20 03:48:03 1985 Date-Received: Sun, 23-Jun-85 04:36:17 EDT Reply-To: stephen@dcl-cs.UUCP (Stephen J. Muir) Organization: Department of Computing at Lancaster University. Lines: 132 Xpath: icdoc ivax #!/bin/sh echo 'Start of pack.out, part 01 of 01:' echo 'x - psystem.3' sed 's/^X//' > psystem.3 << '/' X.TH PSYSTEM 3 "18 June 1985" X.SH NAME Xpsystem \- issue a shell command with pipes X.SH SYNOPSIS X.nf X.B "int psystem (string, p0, p1, p2, child)" X.B char *string; X.B int *p0, *p1, *p2; X.B void (*child) (); X.fi X.SH DESCRIPTION X.I Psystem Xcauses the X.I string Xto be given to X.IR sh (1) Xas input as if the string had been typed as a command at a terminal. XIf X.IR p0 , X.I p1 Xor X.I p2 Xare given, Xpipes are set up to the X.I standard input, X.I standard output Xor X.I standard error Xrespectively; Xthe file descriptors being passed back in the appropriate pointer. XIf X.I child Xis given (as non zero), Xthat routine is called just before the command is executed. X.SH NOTE XThe flag X.I \-lsjm Xneeds to be given to X.IR cc (1) Xor X.IR ld (1). XThe child isn't waited for; you have to do this yourself. X.SH "RETURN VALUE" XThe process ID of the child is returned. X.SH "SEE ALSO" Xsystem (3), wait (2), sh (1) X.SH AUTHOR XStephen J. Muir, Lancaster University. / echo 'x - psystem.c' sed 's/^X//' > psystem.c << '/' X/* Written by Stephen J. Muir, Computing Dept., Lancaster University */ X X# include X Xstatic psyspid; X Xpsystem (com, fd0, fd1, fd2, child) X char *com; X int *fd0, *fd1, *fd2; X void (*child) (); X { int p0 [2], p1 [2], p2 [2]; X if (fd0 && pipe (p0) == -1) X return (-1); X if (fd1 && pipe (p1) == -1) X { if (fd0) X { close (p0 [0]); X close (p0 [1]); X } X return (-1); X } X if (fd2 && pipe (p2) == -1) X { if (fd0) X { close (p0 [0]); X close (p0 [1]); X } X if (fd1) X { close (p1 [0]); X close (p1 [1]); X } X return (-1); X } X while ((psyspid = fork ()) == -1); X if (psyspid == 0) X { if (fd0) X { close (p0 [1]); X dup2 (p0 [0], 0); X close (p0 [0]); X } X if (fd1) X { close (p1 [0]); X dup2 (p1 [1], 1); X close (p1 [1]); X } X if (fd2) X { close (p2 [0]); X dup2 (p2 [1], 2); X close (p2 [1]); X } X signal (SIGQUIT, SIG_IGN); X if (child) X (*child) (); X execl ("/bin/sh", "sh", "-c", com, 0); X _exit (127); X } X if (fd0) X { close (p0 [0]); X *fd0 = p0 [1]; X } X if (fd1) X { close (p1 [1]); X *fd1 = p1 [0]; X } X if (fd2) X { close (p2 [1]); X *fd2 = p2 [0]; X } X return (psyspid); X } / echo 'Part 01 of pack.out complete.' exit -- UUCP: ...!seismo!mcvax!ukc!icdoc!dcl-cs!stephen DARPA: stephen%lancs.comp@ucl-cs | Post: University of Lancaster, JANET: stephen@uk.ac.lancs.comp | Department of Computing, Phone: +44 524 65201 Ext. 4599 | Bailrigg, Lancaster, UK. Project:Alvey ECLIPSE Distribution | LA1 4YR