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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 9 | */ |
Matt Kraai | bcca331 | 2001-10-18 17:04:22 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 12 | static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off, size_t *maxsz_p) |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 13 | { |
| 14 | char *linebuf = NULL; |
| 15 | const int term_length = strlen(terminating_string); |
| 16 | int end_string_offset; |
| 17 | int linebufsz = 0; |
| 18 | int idx = 0; |
| 19 | int ch; |
Ron Yorston | 6ad38d6 | 2021-01-23 13:22:33 +0000 | [diff] [blame] | 20 | size_t maxsz = maxsz_p ? *maxsz_p : INT_MAX - 4095; |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 33 | if (idx >= maxsz) { |
| 34 | linebuf[idx] = ch; |
| 35 | idx++; |
| 36 | break; |
| 37 | } |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | linebuf[idx] = ch; |
| 41 | idx++; |
| 42 | |
| 43 | /* Check for terminating string */ |
| 44 | end_string_offset = idx - term_length; |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 45 | if (end_string_offset >= 0 |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 46 | && memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0 |
| 47 | ) { |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 48 | if (chop_off) |
| 49 | idx -= term_length; |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 50 | break; |
| 51 | } |
| 52 | } |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 53 | /* Grow/shrink *first*, then store NUL */ |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 54 | linebuf = xrealloc(linebuf, idx + 1); |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 55 | linebuf[idx] = '\0'; |
Ron Yorston | 6ad38d6 | 2021-01-23 13:22:33 +0000 | [diff] [blame] | 56 | if (maxsz_p) |
| 57 | *maxsz_p = idx; |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 58 | return linebuf; |
Glenn L McGrath | 17822cd | 2001-06-13 07:34:03 +0000 | [diff] [blame] | 59 | } |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 60 | |
| 61 | /* Read up to TERMINATING_STRING from FILE and return it, |
| 62 | * including terminating string. |
| 63 | * Non-terminated string can be returned if EOF is reached. |
| 64 | * Return NULL if EOF is reached immediately. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 65 | char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string) |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 66 | { |
Ron Yorston | 6ad38d6 | 2021-01-23 13:22:33 +0000 | [diff] [blame] | 67 | return xmalloc_fgets_internal(file, terminating_string, 0, NULL); |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | char* FAST_FUNC xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p) |
| 71 | { |
Denis Vlasenko | 9f57cf6 | 2009-03-18 17:32:44 +0000 | [diff] [blame] | 72 | return xmalloc_fgets_internal(file, terminating_string, 0, maxsz_p); |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 75 | char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string) |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 76 | { |
Ron Yorston | 6ad38d6 | 2021-01-23 13:22:33 +0000 | [diff] [blame] | 77 | return xmalloc_fgets_internal(file, terminating_string, 1, NULL); |
Denis Vlasenko | abee3d0 | 2007-12-26 20:44:45 +0000 | [diff] [blame] | 78 | } |