pathsub.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. ** Pathname subroutines.
  7. **
  8. ** Brendan Eich, 8/29/95
  9. */
  10. #include <assert.h>
  11. #include <sys/types.h>
  12. #include <dirent.h>
  13. #include <errno.h>
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <sys/stat.h>
  20. #include "pathsub.h"
  21. #ifdef USE_REENTRANT_LIBC
  22. #include <libc_r.h>
  23. #endif
  24. #ifdef SUNOS4
  25. #include "sunos4.h"
  26. #endif
  27. #ifndef D_INO
  28. #define D_INO d_ino
  29. #endif
  30. char *program;
  31. void
  32. fail(const char *format, ...)
  33. {
  34. int error;
  35. va_list ap;
  36. #ifdef USE_REENTRANT_LIBC
  37. R_STRERROR_INIT_R();
  38. #endif
  39. error = errno;
  40. fprintf(stderr, "%s: ", program);
  41. va_start(ap, format);
  42. vfprintf(stderr, format, ap);
  43. va_end(ap);
  44. if (error) {
  45. #ifdef USE_REENTRANT_LIBC
  46. R_STRERROR_R(errno);
  47. fprintf(stderr, ": %s", r_strerror_r);
  48. #else
  49. fprintf(stderr, ": %s", strerror(errno));
  50. #endif
  51. }
  52. putc('\n', stderr);
  53. exit(1);
  54. }
  55. char *
  56. getcomponent(char *path, char *name)
  57. {
  58. if (*path == '\0')
  59. return 0;
  60. if (*path == '/') {
  61. *name++ = '/';
  62. } else {
  63. do {
  64. *name++ = *path++;
  65. } while (*path != '/' && *path != '\0');
  66. }
  67. *name = '\0';
  68. while (*path == '/')
  69. path++;
  70. return path;
  71. }
  72. #ifdef LAME_READDIR
  73. #include <sys/param.h>
  74. /*
  75. ** The static buffer in Unixware's readdir is too small.
  76. */
  77. struct dirent *readdir(DIR *d)
  78. {
  79. static struct dirent *buf = NULL;
  80. if(buf == NULL)
  81. buf = (struct dirent *) malloc(sizeof(struct dirent) + MAXPATHLEN);
  82. return(readdir_r(d, buf));
  83. }
  84. #endif
  85. char *
  86. ino2name(ino_t ino)
  87. {
  88. DIR *dp;
  89. struct dirent *ep;
  90. char *name;
  91. dp = opendir("..");
  92. if (!dp)
  93. fail("cannot read parent directory");
  94. for (;;) {
  95. if (!(ep = readdir(dp)))
  96. fail("cannot find current directory");
  97. if (ep->D_INO == ino)
  98. break;
  99. }
  100. name = xstrdup(ep->d_name);
  101. closedir(dp);
  102. return name;
  103. }
  104. void *
  105. xmalloc(size_t size)
  106. {
  107. void *p = malloc(size);
  108. if (!p)
  109. fail("cannot allocate %u bytes", size);
  110. return p;
  111. }
  112. char *
  113. xstrdup(char *s)
  114. {
  115. return strcpy(xmalloc(strlen(s) + 1), s);
  116. }
  117. char *
  118. xbasename(char *path)
  119. {
  120. char *cp;
  121. while ((cp = strrchr(path, '/')) && cp[1] == '\0')
  122. *cp = '\0';
  123. if (!cp) return path;
  124. return cp + 1;
  125. }
  126. void
  127. xchdir(const char *dir)
  128. {
  129. if (chdir(dir) < 0)
  130. fail("cannot change directory to %s", dir);
  131. }
  132. int
  133. relatepaths(char *from, char *to, char *outpath)
  134. {
  135. char *cp, *cp2;
  136. int len;
  137. char buf[NAME_MAX];
  138. assert(*from == '/' && *to == '/');
  139. for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
  140. if (*cp == '\0')
  141. break;
  142. while (cp[-1] != '/')
  143. cp--, cp2--;
  144. if (cp - 1 == to) {
  145. /* closest common ancestor is /, so use full pathname */
  146. len = strlen(strcpy(outpath, to));
  147. if (outpath[len] != '/') {
  148. outpath[len++] = '/';
  149. outpath[len] = '\0';
  150. }
  151. } else {
  152. len = 0;
  153. while ((cp2 = getcomponent(cp2, buf)) != 0) {
  154. strcpy(outpath + len, "../");
  155. len += 3;
  156. }
  157. while ((cp = getcomponent(cp, buf)) != 0) {
  158. sprintf(outpath + len, "%s/", buf);
  159. len += strlen(outpath + len);
  160. }
  161. }
  162. return len;
  163. }
  164. void
  165. reversepath(char *inpath, char *name, int len, char *outpath)
  166. {
  167. char *cp, *cp2;
  168. char buf[NAME_MAX];
  169. struct stat sb;
  170. cp = strcpy(outpath + PATH_MAX - (len + 1), name);
  171. cp2 = inpath;
  172. while ((cp2 = getcomponent(cp2, buf)) != 0) {
  173. if (strcmp(buf, ".") == 0)
  174. continue;
  175. if (strcmp(buf, "..") == 0) {
  176. if (stat(".", &sb) < 0)
  177. fail("cannot stat current directory");
  178. name = ino2name(sb.st_ino);
  179. len = strlen(name);
  180. cp -= len + 1;
  181. strcpy(cp, name);
  182. cp[len] = '/';
  183. free(name);
  184. xchdir("..");
  185. } else {
  186. cp -= 3;
  187. strncpy(cp, "../", 3);
  188. xchdir(buf);
  189. }
  190. }
  191. strcpy(outpath, cp);
  192. }