Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/libbb/get_line_from_file.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/libbb/get_line_from_file.c
r821 r1770 10 10 */ 11 11 12 #include <stdio.h>13 #include <stdlib.h>14 12 #include "libbb.h" 15 13 16 /* get_line_from_file() - This function reads an entire line from a text file, 17 * up to a newline or NUL byte. It returns a malloc'ed char * which must be 18 * stored and free'ed by the caller. If end is null '\n' isn't considered 19 * and of line. If end isn't null, length of the chunk read is stored in it. */ 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 must be 16 * stored and 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 read is stored in it. 18 * Return NULL if EOF/error */ 20 19 21 char *bb_get_chunk_from_file(FILE * 20 char *bb_get_chunk_from_file(FILE *file, int *end) 22 21 { 23 22 int ch; … … 28 27 while ((ch = getc(file)) != EOF) { 29 28 /* grow the line buffer as necessary */ 30 if (idx > linebufsz - 2) { 31 linebuf = xrealloc(linebuf, linebufsz += 80); 29 if (idx >= linebufsz) { 30 linebufsz += 80; 31 linebuf = xrealloc(linebuf, linebufsz); 32 32 } 33 33 linebuf[idx++] = (char) ch; … … 38 38 *end = idx; 39 39 if (linebuf) { 40 if (ferror(file)) { 41 free(linebuf); 42 return NULL; 43 } 44 linebuf[idx] = 0; 40 // huh, does fgets discard prior data on error like this? 41 // I don't think so.... 42 //if (ferror(file)) { 43 // free(linebuf); 44 // return NULL; 45 //} 46 linebuf = xrealloc(linebuf, idx+1); 47 linebuf[idx] = '\0'; 45 48 } 46 49 return linebuf; 47 50 } 48 51 49 /* Get line, including trailing /n if any */50 char * bb_get_line_from_file(FILE *file)52 /* Get line, including trailing \n if any */ 53 char *xmalloc_fgets(FILE *file) 51 54 { 52 55 int i; … … 55 58 } 56 59 57 /* Get line. Remove trailing /n */58 char * bb_get_chomped_line_from_file(FILE *file)60 /* Get line. Remove trailing \n */ 61 char *xmalloc_getline(FILE *file) 59 62 { 60 63 int i; … … 62 65 63 66 if (i && c[--i] == '\n') 64 c[i] = 0;67 c[i] = '\0'; 65 68 66 69 return c;
Note:
See TracChangeset
for help on using the changeset viewer.