File:  [MW Coherent from dump] / coherent / d / usr / src / examples / fopen.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>

char buf[BUFSIZ];
void fatal();

main(argc, argv)
int argc; 
char *argv[];
{
	register FILE *ifp, *ofp;
	register unsigned int n;

	if (argc != 3)
		fatal("Usage: fopen source destination");
	if ((ifp = fopen(argv[1], "r")) == NULL)
		fatal("cannot open input file");
	if ((ofp = fopen(argv[2], "w")) == NULL)
		fatal("cannot open output file");

	while ((n = fread(buf, 1, BUFSIZ, ifp)) != 0) {
		if (fwrite(buf, 1, n, ofp) != n)
			fatal("write error");
	}

	if (!feof(ifp))
		fatal("read error");
	if (fclose(ifp) == EOF || fclose(ofp) == EOF)
		fatal("cannot close");

	exit(0);
}

void fatal(message)
char *message;
{
	fprintf(stderr, "fatal: %s\n", message);
	exit(1);
}

unix.superglobalmegacorp.com

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