File:  [MW Coherent from dump] / coherent / d / usr / src / examples / system.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed May 29 04:56:38 2019 UTC (7 years, 2 months ago) by root
Branches: MarkWilliams, MAIN
CVS tags: relic, HEAD
coherent

#include <stdio.h>

FILE *newfp;
int oldstdout;

main()
{
	extern int system();

	ropen("list.fil");
	system("ls *.c");
	rclose();
}

/*
 * Redirect stdout prior to system() call.
 * You can't redirect child process's I/O
 * but you can redirect main()'s and let the child inherit it.
 */
ropen(tofile)
char *tofile;
{
	if ((newfp = fopen(tofile, "wr")) == NULL)
		fatal("cannot open output file \"%s\"", tofile);

	/* Duplicate stdout so it can be restored later */
	if ((oldstdout = dup(fileno(stdout))) == -1)
		fatal("dup failed");

	/* Force duplication of new file handle as stdout */
	if (dup2(fileno(newfp), fileno(stdout)) == -1)
		fatal("dup2 failed");
}

/*
 * Terminate redirection
 */
rclose()
{
	/* Restore old stdout */
	if (dup2(oldstdout, fileno(stdout)) == -1)
		fatal("dup2 failed");
	/* Close the extra handle */
	if (close(oldstdout) != 0)
		fatal("cannot close old stdout");
	fclose(newfp);
}

/*
 * Fatal error
 */
fatal(p)
char *p;
{
	fprintf(stderr, "system: %r\n", &p);
	exit(1);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.