compat.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * compat.c - compatibility functions
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@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
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * 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
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <errno.h>
  23. #include <signal.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <dirent.h>
  28. #include <stdarg.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #include <dpkg.h>
  33. #include <gettext.h>
  34. #define _(str) gettext(str)
  35. #ifndef HAVE_VSNPRINTF
  36. int vsnprintf (char *buf, size_t maxsize, const char *fmt, va_list al) {
  37. static FILE *file= NULL;
  38. struct stat stab;
  39. unsigned long want, nr;
  40. int retval;
  41. if (maxsize == 0) return -1;
  42. if (!file) {
  43. file= tmpfile(); if (!file) ohshite(_("unable to open tmpfile for vsnprintf"));
  44. } else {
  45. if (fseek(file,0,0)) ohshite(_("unable to rewind at start of vsnprintf"));
  46. if (ftruncate(fileno(file),0)) ohshite(_("unable to truncate in vsnprintf"));
  47. }
  48. if (vfprintf(file,fmt,al) == EOF) ohshite(_("write error in vsnprintf"));
  49. if (fflush(file)) ohshite(_("unable to flush in vsnprintf"));
  50. if (fstat(fileno(file),&stab)) ohshite(_("unable to stat in vsnprintf"));
  51. if (fseek(file,0,0)) ohshite(_("unable to rewind in vsnprintf"));
  52. want= stab.st_size;
  53. if (want >= maxsize) {
  54. want= maxsize-1; retval= -1;
  55. } else {
  56. retval= want;
  57. }
  58. nr= fread(buf,1,want-1,file);
  59. if (nr != want-1) ohshite(_("read error in vsnprintf truncated"));
  60. buf[want]= NULL;
  61. return retval;
  62. }
  63. #endif
  64. #ifndef HAVE_SNPRINTF
  65. int
  66. snprintf (char *str, size_t n, char const *format, ...)
  67. {
  68. va_list ap;
  69. int i;
  70. (void)n;
  71. va_start (ap, format);
  72. i = vsprintf (str, format, ap);
  73. va_end (ap);
  74. return i;
  75. }
  76. #endif
  77. #ifndef HAVE_STRERROR
  78. extern const char *const sys_errlist[];
  79. extern const int sys_nerr;
  80. const char *strerror(int e) {
  81. static char buf[100];
  82. if (e >= 0 && e < sys_nerr) return sys_errlist[e];
  83. sprintf(buf, _("System error no.%d"), e);
  84. return buf;
  85. }
  86. #endif
  87. #ifndef HAVE_STRSIGNAL
  88. extern const char *const sys_siglist[];
  89. const char *strsignal(int e) {
  90. static char buf[100];
  91. if (e >= 0 && e < NSIG) return sys_siglist[e];
  92. sprintf(buf, _("Signal no.%d"), e);
  93. return buf;
  94. }
  95. #endif
  96. #ifndef HAVE_DECL_SYS_SIGLIST
  97. const char *const sys_siglist[32] = {
  98. "SIGHUP", /*1*/
  99. "SIGINT", /*2*/
  100. "SIGQUIT", /*3*/
  101. "SIGILL", /*4*/
  102. "SIGTRAP", /*5*/
  103. "SIGABRT", /*6*/
  104. "SIGEMT", /*7*/
  105. "SIGFPE", /*8*/
  106. "SIGKILL", /*9*/
  107. "SIGUSR1", /*10*/
  108. "SIGSEGV", /*11*/
  109. "SIGUSR2", /*12*/
  110. "SIGPIPE", /*13*/
  111. "SIGALRM", /*14*/
  112. "SIGTERM", /*15*/
  113. "SIGSTKFLT", /*16*/
  114. "SIGCHLD", /*17*/
  115. "SIGCONT", /*18*/
  116. "SIGSTOP", /*19*/
  117. "SIGTSTP", /*20*/
  118. "SIGTTIN", /*21*/
  119. "SIGTTOU", /*22*/
  120. "SIGXXX", /*23*/
  121. "SIGXXX", /*24*/
  122. "SIGXXX", /*25*/
  123. "SIGXXX", /*26*/
  124. "SIGXXX", /*27*/
  125. "SIGXXX", /*28*/
  126. "SIGXXX", /*29*/
  127. "SIGXXX", /*30*/
  128. "SIGXXX", /*31*/
  129. "SIGXXX" /*32*/
  130. };
  131. #endif
  132. #ifndef HAVE_SCANDIR
  133. static int (*scandir_comparfn)(const void*, const void*);
  134. static int scandir_compar(const void *a, const void *b) {
  135. return scandir_comparfn(*(const struct dirent**)a,*(const struct dirent**)b);
  136. }
  137. int scandir(const char *dir, struct dirent ***namelist,
  138. int (*select)(const struct dirent *),
  139. int (*compar)(const void*, const void*)) {
  140. DIR *d;
  141. int used, avail;
  142. struct dirent *e, *m;
  143. d= opendir(dir); if (!d) return -1;
  144. used=0; avail=20;
  145. *namelist= malloc(avail*sizeof(struct dirent*));
  146. if (!*namelist) return -1;
  147. while ((e= readdir(d)) != NULL) {
  148. if (!select(e)) continue;
  149. m= malloc(sizeof(struct dirent) + strlen(e->d_name));
  150. if (!m) return -1;
  151. *m= *e;
  152. strcpy(m->d_name,e->d_name);
  153. if (used >= avail-1) {
  154. avail+= avail;
  155. *namelist= realloc(*namelist, avail*sizeof(struct dirent*));
  156. if (!*namelist) return -1;
  157. }
  158. (*namelist)[used]= m;
  159. used++;
  160. }
  161. (*namelist)[used]= NULL;
  162. scandir_comparfn= compar;
  163. qsort(*namelist, used, sizeof(struct dirent*), scandir_compar);
  164. return used;
  165. }
  166. #endif
  167. #ifndef HAVE_ALPHASORT
  168. int alphasort(const struct dirent *a, const struct dirent *b) {
  169. return strcmp(a->d_name,b->d_name);
  170. }
  171. #endif
  172. #ifndef HAVE_UNSETENV
  173. void unsetenv(const char *p) {
  174. char *q;
  175. q= malloc(strlen(p)+3); if (!q) return;
  176. strcpy(q,p); strcat(q,"="); putenv(q);
  177. }
  178. #endif