get_line_from_file.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2005, 2006 Rob Landley <rob@landley.net>
  6. * Copyright (C) 2004 Erik Andersen <andersen@codepoet.org>
  7. * Copyright (C) 2001 Matt Krai
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  10. */
  11. #include "libbb.h"
  12. /* This function reads an entire line from a text file, up to a newline
  13. * or NUL byte, inclusive. It returns a malloc'ed char * which
  14. * must be free'ed by the caller. If end is NULL '\n' isn't considered
  15. * end of line. If end isn't NULL, length of the chunk is stored in it.
  16. * If lineno is not NULL, *lineno is incremented for each line,
  17. * and also trailing '\' is recognized as line continuation.
  18. *
  19. * Returns NULL if EOF/error. */
  20. char* FAST_FUNC bb_get_chunk_with_continuation(FILE *file, int *end, int *lineno)
  21. {
  22. int ch;
  23. int idx = 0;
  24. char *linebuf = NULL;
  25. int linebufsz = 0;
  26. while ((ch = getc(file)) != EOF) {
  27. /* grow the line buffer as necessary */
  28. if (idx >= linebufsz) {
  29. linebufsz += 256;
  30. linebuf = xrealloc(linebuf, linebufsz);
  31. }
  32. linebuf[idx++] = (char) ch;
  33. if (!ch)
  34. break;
  35. if (end && ch == '\n') {
  36. if (lineno == NULL)
  37. break;
  38. (*lineno)++;
  39. if (idx < 2 || linebuf[idx-2] != '\\')
  40. break;
  41. idx -= 2;
  42. }
  43. }
  44. if (end)
  45. *end = idx;
  46. if (linebuf) {
  47. // huh, does fgets discard prior data on error like this?
  48. // I don't think so....
  49. //if (ferror(file)) {
  50. // free(linebuf);
  51. // return NULL;
  52. //}
  53. linebuf = xrealloc(linebuf, idx + 1);
  54. linebuf[idx] = '\0';
  55. }
  56. return linebuf;
  57. }
  58. char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)
  59. {
  60. return bb_get_chunk_with_continuation(file, end, NULL);
  61. }
  62. /* Get line, including trailing \n if any */
  63. char* FAST_FUNC xmalloc_fgets(FILE *file)
  64. {
  65. int i;
  66. return bb_get_chunk_from_file(file, &i);
  67. }
  68. /* Get line. Remove trailing \n */
  69. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  70. {
  71. int i;
  72. char *c = bb_get_chunk_from_file(file, &i);
  73. if (i && c[--i] == '\n')
  74. c[i] = '\0';
  75. return c;
  76. }
  77. #if 0
  78. /* GNUism getline() should be faster (not tested) than a loop with fgetc */
  79. /* Get line, including trailing \n if any */
  80. char* FAST_FUNC xmalloc_fgets(FILE *file)
  81. {
  82. char *res_buf = NULL;
  83. size_t res_sz;
  84. if (getline(&res_buf, &res_sz, file) == -1) {
  85. free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */
  86. res_buf = NULL;
  87. }
  88. //TODO: trimming to res_sz?
  89. return res_buf;
  90. }
  91. /* Get line. Remove trailing \n */
  92. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  93. {
  94. char *res_buf = NULL;
  95. size_t res_sz;
  96. res_sz = getline(&res_buf, &res_sz, file);
  97. if ((ssize_t)res_sz != -1) {
  98. if (res_buf[res_sz - 1] == '\n')
  99. res_buf[--res_sz] = '\0';
  100. //TODO: trimming to res_sz?
  101. } else {
  102. free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */
  103. res_buf = NULL;
  104. }
  105. return res_buf;
  106. }
  107. #endif
  108. #if 0
  109. /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07.
  110. *
  111. * NB: they stop at NUL byte too.
  112. * Performance is important here. Think "grep 50gigabyte_file"...
  113. * Ironically, grep can't use it because of NUL issue.
  114. * We sorely need C lib to provide fgets which reports size!
  115. *
  116. * Update:
  117. * Actually, uclibc and glibc have it. man getline. It's GNUism,
  118. * but very useful one (if it's as fast as this code).
  119. * TODO:
  120. * - currently, sed and sort use bb_get_chunk_from_file and heavily
  121. * depend on its "stop on \n or \0" behavior, and STILL they fail
  122. * to handle all cases with embedded NULs correctly. So:
  123. * - audit sed and sort; convert them to getline FIRST.
  124. * - THEN ditch bb_get_chunk_from_file, replace it with getline.
  125. * - provide getline implementation for non-GNU systems.
  126. */
  127. static char* xmalloc_fgets_internal(FILE *file, int *sizep)
  128. {
  129. int len;
  130. int idx = 0;
  131. char *linebuf = NULL;
  132. while (1) {
  133. char *r;
  134. linebuf = xrealloc(linebuf, idx + 0x100);
  135. r = fgets(&linebuf[idx], 0x100, file);
  136. if (!r) {
  137. /* need to terminate in case this is error
  138. * (EOF puts NUL itself) */
  139. linebuf[idx] = '\0';
  140. break;
  141. }
  142. /* stupid. fgets knows the len, it should report it somehow */
  143. len = strlen(&linebuf[idx]);
  144. idx += len;
  145. if (len != 0xff || linebuf[idx - 1] == '\n')
  146. break;
  147. }
  148. *sizep = idx;
  149. if (idx) {
  150. /* xrealloc(linebuf, idx + 1) is up to caller */
  151. return linebuf;
  152. }
  153. free(linebuf);
  154. return NULL;
  155. }
  156. /* Get line, remove trailing \n */
  157. char* FAST_FUNC xmalloc_fgetline_fast(FILE *file)
  158. {
  159. int sz;
  160. char *r = xmalloc_fgets_internal(file, &sz);
  161. if (r && r[sz - 1] == '\n')
  162. r[--sz] = '\0';
  163. return r; /* not xrealloc(r, sz + 1)! */
  164. }
  165. char* FAST_FUNC xmalloc_fgets(FILE *file)
  166. {
  167. int sz;
  168. return xmalloc_fgets_internal(file, &sz);
  169. }
  170. /* Get line, remove trailing \n */
  171. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  172. {
  173. int sz;
  174. char *r = xmalloc_fgets_internal(file, &sz);
  175. if (!r)
  176. return r;
  177. if (r[sz - 1] == '\n')
  178. r[--sz] = '\0';
  179. return xrealloc(r, sz + 1);
  180. }
  181. #endif