-/* $Id: manpath.c,v 1.27 2015/10/11 21:12:55 schwarze Exp $ */
+/* $Id: manpath.c,v 1.30 2016/05/28 13:44:13 schwarze Exp $ */
/*
* Copyright (c) 2011, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
#include <sys/stat.h>
#include <ctype.h>
+#if HAVE_ERR
#include <err.h>
+#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
char manpath_default[] = MANPATH_DEFAULT;
FILE *stream;
- char *cp, *ep;
- size_t len, tok;
+ char *line, *cp, *ep;
+ size_t linesz, tok, toklen;
+ ssize_t linelen;
if ((stream = fopen(file, "r")) == NULL)
goto out;
- while ((cp = fgetln(stream, &len)) != NULL) {
- ep = cp + len;
- if (ep[-1] != '\n')
- break;
- *--ep = '\0';
+ line = NULL;
+ linesz = 0;
+
+ while ((linelen = getline(&line, &linesz, stream)) != -1) {
+ cp = line;
+ ep = cp + linelen - 1;
+ while (ep > cp && isspace((unsigned char)*ep))
+ *ep-- = '\0';
while (isspace((unsigned char)*cp))
cp++;
- if (*cp == '#')
+ if (cp == ep || *cp == '#')
continue;
for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
- len = strlen(toks[tok]);
- if (cp + len < ep &&
- isspace((unsigned char)cp[len]) &&
- !strncmp(cp, toks[tok], len)) {
- cp += len;
+ toklen = strlen(toks[tok]);
+ if (cp + toklen < ep &&
+ isspace((unsigned char)cp[toklen]) &&
+ strncmp(cp, toks[tok], toklen) == 0) {
+ cp += toklen;
while (isspace((unsigned char)*cp))
cp++;
break;
break;
}
}
+ free(line);
fclose(stream);
out: