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

extern long atol();
void fatal();

main(argc, argv)
int argc; char *argv[];
{
	register FILE *ifp;
	register int c;
	long nchars, size;

	if (argc < 2 || argc > 3)
		fatal("Usage: fseek file [ nchars ]");
	nchars = (argc == 3) ? atol(argv[2]) : 100L;
	if ((ifp = fopen(argv[1], "r")) == NULL)
		fatal("cannot open input file");
	/* Seek to end */
	if (fseek(ifp, 0L, 2) == -1)
		fatal("seek error");
	/* Find current size */
	size = ftell(ifp);
	size = (size < nchars) ? 0L : size - nchars;

	/* Seek to point */
	if (fseek(ifp, size, 0) == -1)
		fatal("seek error");
	while ((c = getc(ifp)) != EOF)
		/* Copy rest to stdout */
		putchar(c);
	if (fclose(ifp) == EOF)
		fatal("cannot close");
	exit(0);
}

void fatal(message)
char *message;
{
	fprintf(stderr, "fseek: %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.