pathsub.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /*
  39. ** Pathname subroutines.
  40. **
  41. ** Brendan Eich, 8/29/95
  42. */
  43. #include <assert.h>
  44. #include <dirent.h>
  45. #include <errno.h>
  46. #include <stdarg.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <unistd.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include "pathsub.h"
  54. #ifdef USE_REENTRANT_LIBC
  55. #include <libc_r.h>
  56. #endif /* USE_REENTRANT_LIBC */
  57. char *program;
  58. void
  59. fail(char *format, ...)
  60. {
  61. int error;
  62. va_list ap;
  63. #ifdef USE_REENTRANT_LIBC
  64. R_STRERROR_INIT_R();
  65. #endif
  66. error = errno;
  67. fprintf(stderr, "%s: ", program);
  68. va_start(ap, format);
  69. vfprintf(stderr, format, ap);
  70. va_end(ap);
  71. if (error)
  72. #ifdef USE_REENTRANT_LIBC
  73. R_STRERROR_R(errno);
  74. fprintf(stderr, ": %s", r_strerror_r);
  75. #else
  76. fprintf(stderr, ": %s", strerror(errno));
  77. #endif
  78. putc('\n', stderr);
  79. exit(1);
  80. }
  81. char *
  82. getcomponent(char *path, char *name)
  83. {
  84. if (*path == '\0')
  85. return 0;
  86. if (*path == '/') {
  87. *name++ = '/';
  88. } else {
  89. do {
  90. *name++ = *path++;
  91. } while (*path != '/' && *path != '\0');
  92. }
  93. *name = '\0';
  94. while (*path == '/')
  95. path++;
  96. return path;
  97. }
  98. #ifdef UNIXWARE
  99. /* Sigh. The static buffer in Unixware's readdir is too small. */
  100. struct dirent * readdir(DIR *d)
  101. {
  102. static struct dirent *buf = NULL;
  103. #define MAX_PATH_LEN 1024
  104. if(buf == NULL)
  105. buf = (struct dirent *) malloc(sizeof(struct dirent) + MAX_PATH_LEN)
  106. ;
  107. return(readdir_r(d, buf));
  108. }
  109. #endif
  110. char *
  111. ino2name(ino_t ino, char *dir)
  112. {
  113. DIR *dp;
  114. struct dirent *ep;
  115. char *name;
  116. dp = opendir("..");
  117. if (!dp)
  118. fail("cannot read parent directory");
  119. for (;;) {
  120. if (!(ep = readdir(dp)))
  121. fail("cannot find current directory");
  122. if (ep->d_ino == ino)
  123. break;
  124. }
  125. name = xstrdup(ep->d_name);
  126. closedir(dp);
  127. return name;
  128. }
  129. void *
  130. xmalloc(size_t size)
  131. {
  132. void *p = malloc(size);
  133. if (!p)
  134. fail("cannot allocate %u bytes", size);
  135. return p;
  136. }
  137. char *
  138. xstrdup(char *s)
  139. {
  140. return strcpy(xmalloc(strlen(s) + 1), s);
  141. }
  142. char *
  143. xbasename(char *path)
  144. {
  145. char *cp;
  146. while ((cp = strrchr(path, '/')) && cp[1] == '\0')
  147. *cp = '\0';
  148. if (!cp) return path;
  149. return cp + 1;
  150. }
  151. void
  152. xchdir(char *dir)
  153. {
  154. if (chdir(dir) < 0)
  155. fail("cannot change directory to %s", dir);
  156. }
  157. int
  158. relatepaths(char *from, char *to, char *outpath)
  159. {
  160. char *cp, *cp2;
  161. int len;
  162. char buf[NAME_MAX];
  163. assert(*from == '/' && *to == '/');
  164. for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
  165. if (*cp == '\0')
  166. break;
  167. while (cp[-1] != '/')
  168. cp--, cp2--;
  169. if (cp - 1 == to) {
  170. /* closest common ancestor is /, so use full pathname */
  171. len = strlen(strcpy(outpath, to));
  172. if (outpath[len] != '/') {
  173. outpath[len++] = '/';
  174. outpath[len] = '\0';
  175. }
  176. } else {
  177. len = 0;
  178. while ((cp2 = getcomponent(cp2, buf)) != 0) {
  179. strcpy(outpath + len, "../");
  180. len += 3;
  181. }
  182. while ((cp = getcomponent(cp, buf)) != 0) {
  183. sprintf(outpath + len, "%s/", buf);
  184. len += strlen(outpath + len);
  185. }
  186. }
  187. return len;
  188. }
  189. void
  190. reversepath(char *inpath, char *name, int len, char *outpath)
  191. {
  192. char *cp, *cp2;
  193. char buf[NAME_MAX];
  194. struct stat sb;
  195. cp = strcpy(outpath + PATH_MAX - (len + 1), name);
  196. cp2 = inpath;
  197. while ((cp2 = getcomponent(cp2, buf)) != 0) {
  198. if (strcmp(buf, ".") == 0)
  199. continue;
  200. if (strcmp(buf, "..") == 0) {
  201. if (stat(".", &sb) < 0)
  202. fail("cannot stat current directory");
  203. name = ino2name(sb.st_ino, "..");
  204. len = strlen(name);
  205. cp -= len + 1;
  206. strcpy(cp, name);
  207. cp[len] = '/';
  208. free(name);
  209. xchdir("..");
  210. } else {
  211. cp -= 3;
  212. strncpy(cp, "../", 3);
  213. xchdir(buf);
  214. }
  215. }
  216. strcpy(outpath, cp);
  217. }