diff options
author | Roy Marples <roy@marples.name> | 2008-01-06 19:52:43 +0000 |
---|---|---|
committer | Roy Marples <roy@marples.name> | 2008-01-06 19:52:43 +0000 |
commit | b1540cba5b88a1ef625b66990af66fbb6aa15e62 (patch) | |
tree | 8c4d960d90e10464472b8105219fe9e7b8901800 /src | |
parent | 8afb86a5deb7187e87f35a62959e3944348eb0d9 (diff) |
Use a dynamically expanding buffer for reading config files.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.mk | 2 | ||||
-rw-r--r-- | src/librc/librc-misc.c | 144 | ||||
-rw-r--r-- | src/rc/Makefile | 2 |
3 files changed, 74 insertions, 74 deletions
@@ -48,7 +48,7 @@ ${SHLIB_NAME}: ${SOBJS} -o $@ -Wl,-soname,${SONAME} \ ${SOBJS} ${LDADD} -install: +install: all ${INSTALL} -d ${DESTDIR}${SHLIBDIR} ${INSTALL} -m ${LIBMODE} ${SHLIB_NAME} ${DESTDIR}${SHLIBDIR} ln -fs ${SHLIB_NAME} ${DESTDIR}${SHLIBDIR}/${SHLIB_LINK} diff --git a/src/librc/librc-misc.c b/src/librc/librc-misc.c index dcecc293..b8f59936 100644 --- a/src/librc/librc-misc.c +++ b/src/librc/librc-misc.c @@ -105,73 +105,109 @@ char *rc_strcatpaths (const char *path1, const char *paths, ...) } librc_hidden_def(rc_strcatpaths) - -char **rc_config_load (const char *file) +char **rc_config_list (const char *file) { - char **list = NULL; FILE *fp; char *buffer; char *p; char *token; - char *line; - char *linep; - char *linetok; - int i = 0; - bool replaced; - char *entry; - char *newline; + char **list = NULL; + size_t buflen = BUFSIZ; + size_t last; - if (! (fp = fopen (file, "r"))) + if (! (fp = fopen (file, "r"))) return (NULL); - buffer = xmalloc (sizeof (char) * RC_LINEBUFFER); - while (fgets (buffer, RC_LINEBUFFER, fp)) { - p = buffer; + buffer = xmalloc (sizeof (char) * buflen); + buffer[buflen - 1] = '\0'; + while (fgets (buffer, buflen, fp)) { + /* Increase the buffer to read the rest of the line if needed */ + last = strlen (buffer) - 1; + while (! feof (fp) && buffer[last] != '\n') { + buflen += BUFSIZ; + buffer = xrealloc (buffer, sizeof (char *) * buflen); + fgets (buffer + last, BUFSIZ, fp); + last = strlen (buffer) - 1; + } /* Strip leading spaces/tabs */ + p = buffer; while ((*p == ' ') || (*p == '\t')) p++; - if (! p || strlen (p) < 3 || p[0] == '#') - continue; + /* Get entry - we do not want comments */ + token = strsep (&p, "#"); + if (token && (strlen (token) > 1)) { + /* Stip the newline if present */ + if (token[strlen (token) - 1] == '\n') + token[strlen (token) - 1] = 0; - /* Get entry */ - token = strsep (&p, "="); + rc_strlist_add (&list, token); + } + } + free (buffer); + fclose (fp); + + return (list); +} +librc_hidden_def(rc_config_list) - if (! token) +char **rc_config_load (const char *file) +{ + char **list = NULL; + char **config = NULL; + char *token; + char *line; + char *linep; + char *linetok; + int i = 0; + int j; + bool replaced; + char *entry; + char *newline; + + list = rc_config_list (file); + STRLIST_FOREACH (list, line, j) { + /* Get entry */ + if (! (token = strsep (&line, "="))) continue; entry = xstrdup (token); - /* Preserve shell coloring */ - if (*p == '$') - token = p; + if (*line == '$') + token = line; else do { /* Bash variables are usually quoted */ - token = strsep (&p, "\"\'"); + token = strsep (&line, "\"\'"); } while ((token) && (strlen (token) == 0)); /* Drop a newline if that's all we have */ - i = strlen (token) - 1; - if (token[i] == 10) - token[i] = 0; - - i = strlen (entry) + strlen (token) + 2; - newline = xmalloc (i); - snprintf (newline, i, "%s=%s", entry, token); + if (token) { + i = strlen (token) - 1; + if (token[i] == '\n') + token[i] = 0; + + i = strlen (entry) + strlen (token) + 2; + newline = xmalloc (sizeof (char) * i); + snprintf (newline, i, "%s=%s", entry, token); + } else { + i = strlen (entry) + 2; + newline = xmalloc (sizeof (char) * i); + snprintf (newline, i, "%s=", entry); + } replaced = false; /* In shells the last item takes precedence, so we need to remove any prior values we may already have */ - STRLIST_FOREACH (list, line, i) { + STRLIST_FOREACH (config, line, i) { char *tmp = xstrdup (line); linep = tmp; linetok = strsep (&linep, "="); if (strcmp (linetok, entry) == 0) { /* We have a match now - to save time we directly replace it */ - free (list[i - 1]); - list[i - 1] = newline; + free (config[i - 1]); + config[i - 1] = newline; replaced = true; free (tmp); break; @@ -180,15 +216,14 @@ char **rc_config_load (const char *file) } if (! replaced) { - rc_strlist_addsort (&list, newline); + rc_strlist_addsort (&config, newline); free (newline); } free (entry); } - free (buffer); - fclose (fp); + rc_strlist_free (list); - return (list); + return (config); } librc_hidden_def(rc_config_load) @@ -208,38 +243,3 @@ char *rc_config_value (char **list, const char *entry) } librc_hidden_def(rc_config_value) -char **rc_config_list (const char *file) -{ - FILE *fp; - char *buffer; - char *p; - char *token; - char **list = NULL; - - if (! (fp = fopen (file, "r"))) - return (NULL); - - buffer = xmalloc (sizeof (char) * RC_LINEBUFFER); - while (fgets (buffer, RC_LINEBUFFER, fp)) { - p = buffer; - - /* Strip leading spaces/tabs */ - while ((*p == ' ') || (*p == '\t')) - p++; - - /* Get entry - we do not want comments */ - token = strsep (&p, "#"); - if (token && (strlen (token) > 1)) { - /* Stip the newline if present */ - if (token[strlen (token) - 1] == '\n') - token[strlen (token) - 1] = 0; - - rc_strlist_add (&list, token); - } - } - free (buffer); - fclose (fp); - - return (list); -} -librc_hidden_def(rc_config_list) diff --git a/src/rc/Makefile b/src/rc/Makefile index 6d3d7dd5..da0c95ed 100644 --- a/src/rc/Makefile +++ b/src/rc/Makefile @@ -48,7 +48,7 @@ version.h: echo "#define BRANDING \"${BRANDING}\"" >> version.h; \ fi -install: +install: all ${INSTALL} -d ${DESTDIR}${BINDIR} ${INSTALL} ${PROG} ${DESTDIR}${BINDIR} ${INSTALL} -d ${DESTDIR}/bin |