3
0

get_line_from_file.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 source tree.
  10. */
  11. #include "libbb.h"
  12. char* FAST_FUNC bb_get_chunk_from_file(FILE *file, int *end)
  13. {
  14. int ch;
  15. unsigned idx = 0;
  16. char *linebuf = NULL;
  17. while ((ch = getc(file)) != EOF) {
  18. /* grow the line buffer as necessary */
  19. if (!(idx & 0xff))
  20. linebuf = xrealloc(linebuf, idx + 0x100);
  21. linebuf[idx++] = (char) ch;
  22. if (ch == '\0')
  23. break;
  24. if (end && ch == '\n')
  25. break;
  26. }
  27. if (end)
  28. *end = idx;
  29. if (linebuf) {
  30. // huh, does fgets discard prior data on error like this?
  31. // I don't think so....
  32. //if (ferror(file)) {
  33. // free(linebuf);
  34. // return NULL;
  35. //}
  36. linebuf = xrealloc(linebuf, idx + 1);
  37. linebuf[idx] = '\0';
  38. }
  39. return linebuf;
  40. }
  41. /* Get line, including trailing \n if any */
  42. char* FAST_FUNC xmalloc_fgets(FILE *file)
  43. {
  44. int i;
  45. return bb_get_chunk_from_file(file, &i);
  46. }
  47. /* Get line. Remove trailing \n */
  48. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  49. {
  50. int i;
  51. char *c = bb_get_chunk_from_file(file, &i);
  52. if (i && c[--i] == '\n')
  53. c[i] = '\0';
  54. return c;
  55. }
  56. #if 0
  57. /* GNUism getline() should be faster (not tested) than a loop with fgetc */
  58. /* Get line, including trailing \n if any */
  59. char* FAST_FUNC xmalloc_fgets(FILE *file)
  60. {
  61. char *res_buf = NULL;
  62. size_t res_sz;
  63. if (getline(&res_buf, &res_sz, file) == -1) {
  64. free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */
  65. res_buf = NULL;
  66. }
  67. //TODO: trimming to res_sz?
  68. return res_buf;
  69. }
  70. /* Get line. Remove trailing \n */
  71. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  72. {
  73. char *res_buf = NULL;
  74. size_t res_sz;
  75. res_sz = getline(&res_buf, &res_sz, file);
  76. if ((ssize_t)res_sz != -1) {
  77. if (res_buf[res_sz - 1] == '\n')
  78. res_buf[--res_sz] = '\0';
  79. //TODO: trimming to res_sz?
  80. } else {
  81. free(res_buf); /* uclibc allocates a buffer even on EOF. WTF? */
  82. res_buf = NULL;
  83. }
  84. return res_buf;
  85. }
  86. #endif
  87. #if 0
  88. /* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07.
  89. *
  90. * NB: they stop at NUL byte too.
  91. * Performance is important here. Think "grep 50gigabyte_file"...
  92. * Ironically, grep can't use it because of NUL issue.
  93. * We sorely need C lib to provide fgets which reports size!
  94. *
  95. * Update:
  96. * Actually, uclibc and glibc have it. man getline. It's GNUism,
  97. * but very useful one (if it's as fast as this code).
  98. * TODO:
  99. * - currently, sed and sort use bb_get_chunk_from_file and heavily
  100. * depend on its "stop on \n or \0" behavior, and STILL they fail
  101. * to handle all cases with embedded NULs correctly. So:
  102. * - audit sed and sort; convert them to getline FIRST.
  103. * - THEN ditch bb_get_chunk_from_file, replace it with getline.
  104. * - provide getline implementation for non-GNU systems.
  105. */
  106. static char* xmalloc_fgets_internal(FILE *file, int *sizep)
  107. {
  108. int len;
  109. int idx = 0;
  110. char *linebuf = NULL;
  111. while (1) {
  112. char *r;
  113. linebuf = xrealloc(linebuf, idx + 0x100);
  114. r = fgets(&linebuf[idx], 0x100, file);
  115. if (!r) {
  116. /* need to terminate in case this is error
  117. * (EOF puts NUL itself) */
  118. linebuf[idx] = '\0';
  119. break;
  120. }
  121. /* stupid. fgets knows the len, it should report it somehow */
  122. len = strlen(&linebuf[idx]);
  123. idx += len;
  124. if (len != 0xff || linebuf[idx - 1] == '\n')
  125. break;
  126. }
  127. *sizep = idx;
  128. if (idx) {
  129. /* xrealloc(linebuf, idx + 1) is up to caller */
  130. return linebuf;
  131. }
  132. free(linebuf);
  133. return NULL;
  134. }
  135. /* Get line, remove trailing \n */
  136. char* FAST_FUNC xmalloc_fgetline_fast(FILE *file)
  137. {
  138. int sz;
  139. char *r = xmalloc_fgets_internal(file, &sz);
  140. if (r && r[sz - 1] == '\n')
  141. r[--sz] = '\0';
  142. return r; /* not xrealloc(r, sz + 1)! */
  143. }
  144. char* FAST_FUNC xmalloc_fgets(FILE *file)
  145. {
  146. int sz;
  147. return xmalloc_fgets_internal(file, &sz);
  148. }
  149. /* Get line, remove trailing \n */
  150. char* FAST_FUNC xmalloc_fgetline(FILE *file)
  151. {
  152. int sz;
  153. char *r = xmalloc_fgets_internal(file, &sz);
  154. if (!r)
  155. return r;
  156. if (r[sz - 1] == '\n')
  157. r[--sz] = '\0';
  158. return xrealloc(r, sz + 1);
  159. }
  160. #endif