utils.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * utils.c - Helper functions for dpkg
  4. *
  5. * Copyright (C) 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, but
  12. * 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
  17. * License along with dpkg; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <config.h>
  21. #include <dpkg.h>
  22. #include <string.h>
  23. /* Reimplementation of the standard ctype.h is* functions. Since gettext
  24. * has overloaded the meaning of LC_CTYPE we can't use that to force C
  25. * locale, so use these cis* functions instead.
  26. */
  27. int cisdigit(int c) {
  28. return (c>='0') && (c<='9');
  29. }
  30. int cisalpha(int c) {
  31. return ((c>='a') && (c<='z')) || ((c>='A') && (c<='Z'));
  32. }
  33. int
  34. fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn)
  35. {
  36. int l;
  37. if (!fgets(buf, bufsz, f)) {
  38. if (ferror(f))
  39. ohshite(_("read error in `%.250s'"), fn);
  40. return -1;
  41. }
  42. l = strlen(buf);
  43. if (l == 0)
  44. ohshit(_("fgets gave an empty string from `%.250s'"), fn);
  45. if (buf[--l] != '\n')
  46. ohshit(_("too-long line or missing newline in `%.250s'"), fn);
  47. buf[l] = 0;
  48. return l;
  49. }
  50. int
  51. fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn)
  52. {
  53. int l = fgets_checked(buf, bufsz, f, fn);
  54. if (l < 0)
  55. ohshit(_("unexpected eof reading `%.250s'"), fn);
  56. return l;
  57. }