fgets_str.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 tarball for details.
  9. */
  10. #include "libbb.h"
  11. static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int chop_off)
  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. while (1) {
  20. ch = fgetc(file);
  21. if (ch == EOF) {
  22. if (idx == 0)
  23. return linebuf; /* NULL */
  24. break;
  25. }
  26. if (idx >= linebufsz) {
  27. linebufsz += 200;
  28. linebuf = xrealloc(linebuf, linebufsz);
  29. }
  30. linebuf[idx] = ch;
  31. idx++;
  32. /* Check for terminating string */
  33. end_string_offset = idx - term_length;
  34. if (end_string_offset >= 0
  35. && memcmp(&linebuf[end_string_offset], terminating_string, term_length) == 0
  36. ) {
  37. if (chop_off)
  38. idx -= term_length;
  39. break;
  40. }
  41. }
  42. /* Grow/shrink *first*, then store NUL */
  43. linebuf = xrealloc(linebuf, idx + 1);
  44. linebuf[idx] = '\0';
  45. return linebuf;
  46. }
  47. /* Read up to TERMINATING_STRING from FILE and return it,
  48. * including terminating string.
  49. * Non-terminated string can be returned if EOF is reached.
  50. * Return NULL if EOF is reached immediately. */
  51. char *xmalloc_fgets_str(FILE *file, const char *terminating_string)
  52. {
  53. return xmalloc_fgets_internal(file, terminating_string, 0);
  54. }
  55. char *xmalloc_fgetline_str(FILE *file, const char *terminating_string)
  56. {
  57. return xmalloc_fgets_internal(file, terminating_string, 1);
  58. }