mlib.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * mlib.c - ‘must’ library: routines will succeed or longjmp
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2006-2013, 2015 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <string.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <dpkg/i18n.h>
  30. #include <dpkg/dpkg.h>
  31. static inline void *
  32. must_alloc(void *ptr)
  33. {
  34. if (ptr)
  35. return ptr;
  36. onerr_abort++;
  37. ohshite(_("failed to allocate memory"));
  38. }
  39. void *m_malloc(size_t amount) {
  40. #ifdef MDEBUG
  41. unsigned short *ptr_canary, canary;
  42. #endif
  43. void *ptr;
  44. ptr = must_alloc(malloc(amount));
  45. #ifdef MDEBUG
  46. ptr_canary = ptr;
  47. canary = (unsigned short)amount ^ 0xf000;
  48. while (amount >= 2) {
  49. *ptr_canary++ = canary;
  50. amount -= 2;
  51. }
  52. #endif
  53. return ptr;
  54. }
  55. void *
  56. m_calloc(size_t nmemb, size_t size)
  57. {
  58. return must_alloc(calloc(nmemb, size));
  59. }
  60. void *m_realloc(void *r, size_t amount) {
  61. return must_alloc(realloc(r, amount));
  62. }
  63. char *
  64. m_strdup(const char *str)
  65. {
  66. return must_alloc(strdup(str));
  67. }
  68. char *
  69. m_strndup(const char *str, size_t n)
  70. {
  71. return must_alloc(strndup(str, n));
  72. }
  73. int
  74. m_vasprintf(char **strp, const char *fmt, va_list args)
  75. {
  76. int n;
  77. n = vasprintf(strp, fmt, args);
  78. if (n >= 0)
  79. return n;
  80. onerr_abort++;
  81. ohshite(_("failed to allocate memory"));
  82. }
  83. int
  84. m_asprintf(char **strp, const char *fmt, ...)
  85. {
  86. va_list args;
  87. int n;
  88. va_start(args, fmt);
  89. n = m_vasprintf(strp, fmt, args);
  90. va_end(args);
  91. return n;
  92. }
  93. void m_dup2(int oldfd, int newfd) {
  94. const char *const stdstrings[]= { "in", "out", "err" };
  95. if (dup2(oldfd,newfd) == newfd) return;
  96. onerr_abort++;
  97. if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
  98. ohshite(_("failed to dup for fd %d"),newfd);
  99. }
  100. void m_pipe(int *fds) {
  101. if (!pipe(fds)) return;
  102. onerr_abort++;
  103. ohshite(_("failed to create pipe"));
  104. }
  105. void
  106. m_output(FILE *f, const char *name)
  107. {
  108. fflush(f);
  109. if (ferror(f))
  110. ohshite(_("error writing to '%s'"), name);
  111. }
  112. void
  113. setcloexec(int fd, const char *fn)
  114. {
  115. int f;
  116. f = fcntl(fd, F_GETFD);
  117. if (f == -1)
  118. ohshite(_("unable to read filedescriptor flags for %.250s"),fn);
  119. if (fcntl(fd, F_SETFD, (f|FD_CLOEXEC))==-1)
  120. ohshite(_("unable to set close-on-exec flag for %.250s"),fn);
  121. }