fgets_str.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) many different people.
  6. * If you wrote this, please acknowledge your work.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. #include "libbb.h"
  11. static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off, size_t *maxsz_p)
  12. {
  13. char *linebuf = NULL;
  14. const int term_length = strlen(terminating_string);
  15. int end_string_offset;
  16. int linebufsz = 0;
  17. int idx = 0;
  18. int ch;
  19. size_t maxsz = *maxsz_p;
  20. while (1) {
  21. ch = fgetc(file);
  22. if (ch == EOF) {
  23. if (idx == 0)
  24. return linebuf; /* NULL */
  25. break;
  26. }
  27. if (idx >= linebufsz) {
  28. linebufsz += 200;
  29. linebuf = xrealloc(linebuf, linebufsz);
  30. if (idx >= maxsz) {
  31. linebuf[idx] = ch;
  32. idx++;
  33. break;
  34. }
  35. }
  36. linebuf[idx] = ch;
  37. idx++;
  38. /* Check for terminating string */
  39. end_string_offset = idx - term_length;
  40. if (end_string_offset >= 0
  41. && memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0
  42. ) {
  43. if (chop_off)
  44. idx -= term_length;
  45. break;
  46. }
  47. }
  48. /* Grow/shrink *first*, then store NUL */
  49. linebuf = xrealloc(linebuf, idx + 1);
  50. linebuf[idx] = '\0';
  51. *maxsz_p = idx;
  52. return linebuf;
  53. }
  54. /* Read up to TERMINATING_STRING from FILE and return it,
  55. * including terminating string.
  56. * Non-terminated string can be returned if EOF is reached.
  57. * Return NULL if EOF is reached immediately. */
  58. char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string)
  59. {
  60. size_t maxsz = INT_MAX - 4095;
  61. return xmalloc_fgets_internal(file, terminating_string, 0, &maxsz);
  62. }
  63. char* FAST_FUNC xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p)
  64. {
  65. size_t maxsz;
  66. if (!maxsz_p) {
  67. maxsz = INT_MAX - 4095;
  68. maxsz_p = &maxsz;
  69. }
  70. return xmalloc_fgets_internal(file, terminating_string, 0, maxsz_p);
  71. }
  72. char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string)
  73. {
  74. size_t maxsz = INT_MAX - 4095;
  75. return xmalloc_fgets_internal(file, terminating_string, 1, &maxsz);
  76. }