/*
**  A program to test the popen/pclose pair.  It takes two arguments:
**  the first is either "r" or "w", and the second is a command string.
**  It calls popen with the indicated "r" or "w" mode and passes it the
**  command string.  If the mode was "r", then it reads from the pipe
**  until eof and writes to stdout.  If the mode was "w", then it writes
**  10 simple lines TO the pipe.  In either case, pclose is called and
**  the exit status of the command is displayed.  You should be able to
**  pass nearly any command line for execution...just be sure to enclose
**  the command argument in quotes!  Standard Amiga programs that
**  READ from stdin are kind of rare so use the "r" mode for most
**  things.  Examples:
**		pipetst w cat
**		pipetst r "List c:"
**		pipetst r "Dir sys:"
**		pipetst r "Type pipetst.c"
*/

#include <stdio.h>
#include <fcntl.h>

main(argc,argv)
int	argc;
char	*argv[];
{
	char sline[80];
	FILE *pipein,*pipeout,*popen();
	int		rc;
	short	i;

	if (argc < 2) {
		printf("Need command to run\n");
		exit(1);
		}
	if (argv[1][0] == 'r') {
		pipein = popen(argv[2],"r");
		if (pipein == NULL) {
			printf("Couldn't open pipein file\n");
			exit(1);
			}
		while (fgets(sline,80,pipein) != NULL)
			printf("pipetst: %s",sline);
		rc = pclose(pipein);
		}
	else if (argv[1][0] == 'w') {
		pipeout = popen(argv[2],"w");
		if (pipeout == NULL) {
			printf("Couldn't open pipeout file\n");
			exit(1);
			}
		for (i=0; i<10; i++)
			fprintf(pipeout,"Line %d from pipetst\n",i);
		rc = pclose(pipeout);
		}
	else {
		printf("pipetst needs first parm of r or w\n");
		exit(1);
		}
	printf("pipetst: Return code was %d\n",rc);
}
