diff options
| author | Roy Marples <roy@marples.name> | 2008-01-07 18:14:51 +0000 |
|---|---|---|
| committer | Roy Marples <roy@marples.name> | 2008-01-07 18:14:51 +0000 |
| commit | b5d0d901da7cd53ea7f2b3a21c4ef6cbb4ce69c0 (patch) | |
| tree | 47d7768dae056651578ba693e4c8ddfafbaaac78 /common.c | |
| parent | 52a76582a60d360f74124028ea1f42ea446c1535 (diff) | |
| download | dhcpcd-b5d0d901da7cd53ea7f2b3a21c4ef6cbb4ce69c0.tar.xz | |
Stop using static inflexable buffers when cleaning metas and reading files.
Diffstat (limited to 'common.c')
| -rw-r--r-- | common.c | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -41,6 +41,33 @@ #include "common.h" #include "logger.h" +/* Handy routine to read very long lines in text files. + * This means we read the whole line and avoid any nasty buffer overflows. */ +char *getline (FILE *fp) +{ + char *line = NULL; + char *p; + size_t len = 0; + size_t last = 0; + + if (feof (fp)) + return (NULL); + + do { + len += BUFSIZ; + line = xrealloc (line, sizeof (char) * len); + p = line + last; + fgets (p, BUFSIZ, fp); + last += strlen (p); + } while (! feof (fp) && line[last - 1] != '\n'); + + /* Trim the trailing newline */ + if (line[--last] == '\n') + line[last] = '\0'; + + return (line); +} + /* OK, this should be in dhcpcd.c * It's here to make dhcpcd more readable */ #ifdef __linux__ @@ -174,6 +201,17 @@ void *xmalloc (size_t s) exit (EXIT_FAILURE); } +void *xrealloc (void *ptr, size_t s) +{ + void *value = realloc (ptr, s); + + if (value) + return (value); + + logger (LOG_ERR, "memory exhausted"); + exit (EXIT_FAILURE); +} + char *xstrdup (const char *str) { char *value; |
