psystem(3) - shell command with pipes

Stephen J. Muir stephen at dcl-cs.UUCP
Thu Jun 20 17:48:03 AEST 1985


#!/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 <signal.h>
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 at ucl-cs	| Post: University of Lancaster,
JANET:	stephen at uk.ac.lancs.comp	|	Department of Computing,
Phone:	+44 524 65201 Ext. 4599		|	Bailrigg, Lancaster, UK.
Project:Alvey ECLIPSE Distribution	|	LA1 4YR



More information about the Comp.sources.unix mailing list