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

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


#include "inv.h"

void getargs(char *s, Alist *al)
{
	al->n = 0;
	for (;;) {
		while (*s == ' ' || *s == '\t' || *s == '\n')
			s++;
		if (*s == 0)
			break;
		if (al->lp == 0) {
			al->avail = 50;
			al->lp = (char **)malloc(al->avail*sizeof(char*));
		} else if (al->n >= al->avail) {
			al->avail += 50;
			al->lp = (char **)realloc(al->lp, al->avail*sizeof(char*));
		}
		if (al->lp == 0)
			err("not enough memory for keyword list");
		al->lp[al->n] = s;
		while (*s != 0 && *s != ' ' && *s != '\t' && *s != '\n')
			s++;
		if (al->lp[al->n] != s)
			al->n++;
		if (*s == 0) 
			break;
		*s++ = 0;
	}
}

# define LINESIZ 1024
# define MAXLINE (20*LINESIZ)

char *getln(FILE *fp)
{
	static char *line;
	static unsigned linesize = LINESIZ;
	unsigned offset;

	if (line==0)
		if ((line = (char *)malloc(linesize)) == 0)
			err("not enough memory for index line");
	for (offset = 0;;) {
		if (fgets(line+offset, linesize - offset, fp) == 0)
			return offset==0? 0: line;
		offset += strlen(line+offset);
		if (line[--offset] == '\n')
			return line;
		if ((linesize += LINESIZ) >= MAXLINE)
			err("index line too long (>%d chars)", MAXLINE);
		if ((line = (char *)realloc(line, linesize)) == 0)
			err("not enough memory for index line");
	}
	return 0;	/* for compiler */
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.