Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/refer/findline.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "refer.h"
#include <errno.h>

#ifndef D1
#define D1 0
#endif

#if D1
#define	D(x) x
#else
#define	D(x)
#endif

int findline(char *argin, char *out, int outlen, long indexdate)
{
	static char	name[DIRLEN] = "";
	char *p, *in;
	static FILE *fa = NULL;
	long lp, llen;
	int len = 0, k, nofil;

	D(fprintf(stderr, "findline: %s\n", argin));
	in = (char *)zalloc(strlen(argin) + 1, 1);
	strcpy(in, argin);
	nofil = in[0] == 0;
	for (p = in; *p && *p != ':' && *p != ';'; p++)
		;
	if (*p) 
		*p++ = 0;
	else 
		p = in;
	k = sscanf(p, "%ld,%ld", &lp, &llen);
	D(fprintf(stderr, "p %s k %d lp %ld llen %ld\n", p, k, lp, llen));
	if (k < 2) {
		lp = 0;
		llen = outlen;
	}
	D(fprintf(stderr, "lp %ld llen %ld\n", lp, llen));
	D(fprintf(stderr, "fa now %o, p %o in %o %s\n", fa, p, in, in));
	if (nofil) {
		D(fprintf(stderr, "set fa to stdin\n"));
		fa = stdin;
	} else if (strcmp (name, in) != 0 || 1) {
		D(fprintf(stderr, "old: %s new %s not equal\n", name, in));
		if (fa != NULL)
			fa = freopen(in, "r", fa);
		else
			fa = fopen(in, "r");
		if (fa == NULL) {	/* should this be a fatal error? */
			warn(0, "failed to (re)open %s.  %s\n", in, strerror(errno));
			free(in);
			return 0;
		}
		strcpy(name, in);
		if (gdate(fa) > indexdate && indexdate != 0)
			err("index files %s.i[abc] out of date - rerun pubindex", name);
	} else {
		D(fprintf(stderr, "old %s new %s same fa %o\n", name, in, fa));
	}
	if (fa != NULL) {
		fseek (fa, lp, 0);
		len = fread (out, 1, llen>=outlen? outlen-1: llen, fa);
		out[len] = 0;
		D(fprintf(stderr, "length as read is %d\n", len));
	}
	free(in);
	return len;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].