Preload sysctl lines even if longer than stdio buffer

by using getline(3) to use a dynamically increased buffer
if required by the input found in sysctl configuration files.

Signed-off-by: Werner Fink <werner@suse.de>
This commit is contained in:
Werner Fink 2018-01-18 11:06:55 +01:00 committed by Craig Small
parent 3497521d63
commit 6559450503

View File

@ -511,18 +511,21 @@ static int pattern_match(const char *string, const char *pat)
*/ */
static int Preload(const char *restrict const filename) static int Preload(const char *restrict const filename)
{ {
char oneline[LINELEN]; char *oneline;
char buffer[LINELEN];
FILE *fp; FILE *fp;
char *t; char *t;
int n = 0; int n = 0;
int rc = 0; int rc = 0;
size_t blen = LINELEN;
ssize_t rlen;
char *name, *value; char *name, *value;
glob_t globbuf; glob_t globbuf;
int globerr; int globerr;
int globflg; int globflg;
int j; int j;
oneline = xmalloc(blen);
globflg = GLOB_NOCHECK; globflg = GLOB_NOCHECK;
#ifdef GLOB_BRACE #ifdef GLOB_BRACE
globflg |= GLOB_BRACE; globflg |= GLOB_BRACE;
@ -544,13 +547,19 @@ static int Preload(const char *restrict const filename)
? stdin : fopen(globbuf.gl_pathv[j], "r"); ? stdin : fopen(globbuf.gl_pathv[j], "r");
if (!fp) { if (!fp) {
xwarn(_("cannot open \"%s\""), globbuf.gl_pathv[j]); xwarn(_("cannot open \"%s\""), globbuf.gl_pathv[j]);
return -1; rc = -1;
goto out;
} }
while (fgets(oneline, sizeof oneline, fp)) { while ((rlen = getline(&oneline, &blen, fp)) != -1) {
n++; size_t offset;
t = StripLeadingAndTrailingSpaces(oneline);
n++;
if (rlen < 2)
continue;
t = StripLeadingAndTrailingSpaces(oneline);
if (strlen(t) < 2) if (strlen(t) < 2)
continue; continue;
@ -569,6 +578,10 @@ static int Preload(const char *restrict const filename)
if (pattern && !pattern_match(name, pattern)) if (pattern && !pattern_match(name, pattern))
continue; continue;
offset = strlen(name);
memmove(&oneline[0], name, offset);
oneline[offset++] = '=';
value = strtok(NULL, "\n\r"); value = strtok(NULL, "\n\r");
if (!value || !*value) { if (!value || !*value) {
xwarnx(_("%s(%d): invalid syntax, continuing..."), xwarnx(_("%s(%d): invalid syntax, continuing..."),
@ -580,12 +593,16 @@ static int Preload(const char *restrict const filename)
value++; value++;
/* should NameOnly affect this? */ /* should NameOnly affect this? */
sprintf(buffer, "%s=%s", name, value); memmove(&oneline[offset], value, strlen(value));
rc |= WriteSetting(buffer); offset += strlen(value);
oneline[offset] = '\0';
rc |= WriteSetting(oneline);
} }
fclose(fp); fclose(fp);
} }
out:
return rc; return rc;
} }