[net.sources] psystem

stephen@dcl-cs.UUCP (Stephen J. Muir) (06/20/85)

#!/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@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

stephen@dcl-cs.UUCP (Stephen J. Muir) (06/25/85)

I was asked for a program to demonstrate the use of the (recently posted)
"psystem(3)".  Here it is.
----------------------------------- cut here ----------------------------------
/* This is NOT an example of a well written program */

main ()
	{ int	input, output;
	  static char	buf [512];
		/* you should really check the return value */
	  psystem ("dd conv=ucase", &input, &output, 0, 0);
		/* pump input into the command */
	  write (input, "this is my input\n", 17);
		/* indicate end-of-file to command */
	  close (input);
		/* collect output - you should really use the return value */
	  read (output, buf, 512);
		/* tidy up */
	  close (output);
		/* collect our zombie */
	  wait (0);
	  printf ("The output was \"%s\".\n", buf);
	  exit (0);
	}
-- 
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