Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/libbb/get_line_from_file.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/libbb/get_line_from_file.c
r2725 r3232 12 12 #include "libbb.h" 13 13 14 /* This function reads an entire line from a text file, up to a newline 15 * or NUL byte, inclusive. It returns a malloc'ed char * which 16 * must be free'ed by the caller. If end is NULL '\n' isn't considered 17 * end of line. If end isn't NULL, length of the chunk is stored in it. 18 * If lineno is not NULL, *lineno is incremented for each line, 19 * and also trailing '\' is recognized as line continuation. 20 * 21 * Returns NULL if EOF/error. */ 22 char* FAST_FUNC bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno) 14 char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end) 23 15 { 24 16 int ch; 25 intidx = 0;17 unsigned idx = 0; 26 18 char *linebuf = NULL; 27 int linebufsz = 0;28 19 29 20 while ((ch = getc(file)) != EOF) { 30 21 /* grow the line buffer as necessary */ 31 if (idx >= linebufsz) { 32 linebufsz += 256; 33 linebuf = xrealloc(linebuf, linebufsz); 34 } 22 if (!(idx & 0xff)) 23 linebuf = xrealloc(linebuf, idx + 0x100); 35 24 linebuf[idx++] = (char) ch; 36 if ( !ch)25 if (ch == '\0') 37 26 break; 38 if (end && ch == '\n') { 39 if (lineno == NULL) 40 break; 41 (*lineno)++; 42 if (idx < 2 || linebuf[idx-2] != '\\') 43 break; 44 idx -= 2; 45 } 27 if (end && ch == '\n') 28 break; 46 29 } 47 30 if (end) … … 58 41 } 59 42 return linebuf; 60 }61 62 char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)63 {64 return bb_get_chunk_with_continuation(file, end, NULL);65 43 } 66 44
Note:
See TracChangeset
for help on using the changeset viewer.