Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 3 | * Utility routines. |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) many different people. |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 6 | * If you wrote this, please acknowledge your work. |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 7 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Matt Kraai | bcca331 | 2001-10-18 17:04:22 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
| 12 | |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 13 | static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off) |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 14 | { |
| 15 | char *linebuf = NULL; |
| 16 | const int term_length = strlen(terminating_string); |
| 17 | int end_string_offset; |
| 18 | int linebufsz = 0; |
| 19 | int idx = 0; |
| 20 | int ch; |
| 21 | |
| 22 | while (1) { |
| 23 | ch = fgetc(file); |
| 24 | if (ch == EOF) { |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 25 | if (idx == 0) |
| 26 | return linebuf; /* NULL */ |
| 27 | break; |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 30 | if (idx >= linebufsz) { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 31 | linebufsz += 200; |
| 32 | linebuf = xrealloc(linebuf, linebufsz); |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | linebuf[idx] = ch; |
| 36 | idx++; |
| 37 | |
| 38 | /* Check for terminating string */ |
| 39 | end_string_offset = idx - term_length; |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 40 | if (end_string_offset >= 0 |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 41 | && memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0 |
| 42 | ) { |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 43 | if (chop_off) |
| 44 | idx -= term_length; |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 45 | break; |
| 46 | } |
| 47 | } |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 48 | /* Grow/shrink *first*, then store NUL */ |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 49 | linebuf = xrealloc(linebuf, idx + 1); |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 50 | linebuf[idx] = '\0'; |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 51 | return linebuf; |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 52 | } |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 53 | |
| 54 | /* Read up to TERMINATING_STRING from FILE and return it, |
| 55 | * including terminating string. |
| 56 | * Non-terminated string can be returned if EOF is reached. |
| 57 | * Return NULL if EOF is reached immediately. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 58 | char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string) |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 59 | { |
| 60 | return xmalloc_fgets_internal(file, terminating_string, 0); |
| 61 | } |
| 62 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 63 | char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string) |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 64 | { |
| 65 | return xmalloc_fgets_internal(file, terminating_string, 1); |
| 66 | } |