utils.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * utils.c - Helper functions for dpkg
  4. *
  5. * Copyright © 2001 Wichert Akkerman <wakkerma@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public
  9. * License version 2 as published by the Free Software Foundation.
  10. *
  11. * This is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <compat.h>
  21. #include <string.h>
  22. #include <dpkg/i18n.h>
  23. #include <dpkg/dpkg.h>
  24. /* Reimplementation of the standard ctype.h is* functions. Since gettext
  25. * has overloaded the meaning of LC_CTYPE we can't use that to force C
  26. * locale, so use these cis* functions instead.
  27. */
  28. int cisdigit(int c) {
  29. return (c>='0') && (c<='9');
  30. }
  31. int cisalpha(int c) {
  32. return ((c>='a') && (c<='z')) || ((c>='A') && (c<='Z'));
  33. }
  34. int
  35. cisspace(int c)
  36. {
  37. return (c == '\n' || c == '\t' || c == ' ');
  38. }
  39. int
  40. fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn)
  41. {
  42. int l;
  43. if (!fgets(buf, bufsz, f)) {
  44. if (ferror(f))
  45. ohshite(_("read error in `%.250s'"), fn);
  46. return -1;
  47. }
  48. l = strlen(buf);
  49. if (l == 0)
  50. ohshit(_("fgets gave an empty string from `%.250s'"), fn);
  51. if (buf[--l] != '\n')
  52. ohshit(_("too-long line or missing newline in `%.250s'"), fn);
  53. buf[l] = '\0';
  54. return l;
  55. }
  56. int
  57. fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn)
  58. {
  59. int l = fgets_checked(buf, bufsz, f, fn);
  60. if (l < 0)
  61. ohshit(_("unexpected eof reading `%.250s'"), fn);
  62. return l;
  63. }