utils.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * utils.c - helper functions for dpkg
  4. *
  5. * Copyright © 1995, 2008 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <string.h>
  23. #include <dpkg/i18n.h>
  24. #include <dpkg/dpkg.h>
  25. int
  26. fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn)
  27. {
  28. int l;
  29. if (!fgets(buf, bufsz, f)) {
  30. if (ferror(f))
  31. ohshite(_("read error in '%.250s'"), fn);
  32. return -1;
  33. }
  34. l = strlen(buf);
  35. if (l == 0)
  36. ohshit(_("fgets gave an empty string from '%.250s'"), fn);
  37. if (buf[--l] != '\n')
  38. ohshit(_("too-long line or missing newline in '%.250s'"), fn);
  39. buf[l] = '\0';
  40. return l;
  41. }
  42. int
  43. fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn)
  44. {
  45. int l = fgets_checked(buf, bufsz, f, fn);
  46. if (l < 0)
  47. ohshit(_("unexpected end of file reading '%.250s'"), fn);
  48. return l;
  49. }