compat.c 4.9 KB

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