compat.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * compat.c - compatibility functions
  4. *
  5. * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.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= 0;
  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]= 0;
  59. return retval;
  60. }
  61. #endif
  62. #ifndef HAVE_STRERROR
  63. extern const char *const sys_errlist[];
  64. extern const int sys_nerr;
  65. const char *strerror(int e) {
  66. static char buf[100];
  67. if (e >= 0 && e < sys_nerr) return sys_errlist[e];
  68. sprintf(buf, _("System error no.%d"), e);
  69. return buf;
  70. }
  71. #endif
  72. #ifndef HAVE_STRSIGNAL
  73. extern const char *const sys_siglist[];
  74. const char *strsignal(int e) {
  75. static char buf[100];
  76. if (e >= 0 && e < NSIG) return sys_siglist[e];
  77. sprintf(buf, _("Signal no.%d"), e);
  78. return buf;
  79. }
  80. #endif
  81. #ifndef HAVE_SCANDIR
  82. static int (*scandir_comparfn)(const void*, const void*);
  83. static int scandir_compar(const void *a, const void *b) {
  84. return scandir_comparfn(*(const struct dirent**)a,*(const struct dirent**)b);
  85. }
  86. int scandir(const char *dir, struct dirent ***namelist,
  87. int (*select)(const struct dirent *),
  88. int (*compar)(const void*, const void*)) {
  89. DIR *d;
  90. int used, avail;
  91. struct dirent *e, *m;
  92. d= opendir(dir); if (!d) return -1;
  93. used=0; avail=20;
  94. *namelist= malloc(avail*sizeof(struct dirent*));
  95. if (!*namelist) return -1;
  96. while ((e= readdir(d)) != 0) {
  97. if (!select(e)) continue;
  98. m= malloc(sizeof(struct dirent) + strlen(e->d_name));
  99. if (!m) return -1;
  100. *m= *e;
  101. strcpy(m->d_name,e->d_name);
  102. if (used >= avail-1) {
  103. avail+= avail;
  104. *namelist= realloc(*namelist, avail*sizeof(struct dirent*));
  105. if (!*namelist) return -1;
  106. }
  107. (*namelist)[used]= m;
  108. used++;
  109. }
  110. (*namelist)[used]= 0;
  111. scandir_comparfn= compar;
  112. qsort(*namelist, used, sizeof(struct dirent*), scandir_compar);
  113. return used;
  114. }
  115. #endif
  116. #ifndef HAVE_ALPHASORT
  117. int alphasort(const struct dirent *a, const struct dirent *b) {
  118. return strcmp(a->d_name,b->d_name);
  119. }
  120. #endif
  121. #ifndef HAVE_UNSETENV
  122. void unsetenv(const char *p) {
  123. char *q;
  124. q= malloc(strlen(p)+3); if (!q) return;
  125. strcpy(q,p); strcat(q,"="); putenv(q);
  126. }
  127. #endif