pathsub.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. ** Pathname subroutines.
  43. **
  44. ** Brendan Eich, 8/29/95
  45. */
  46. #include <assert.h>
  47. #include <dirent.h>
  48. #include <errno.h>
  49. #include <stdarg.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <unistd.h>
  54. #include <sys/types.h>
  55. #include <sys/stat.h>
  56. #include "pathsub.h"
  57. #ifdef USE_REENTRANT_LIBC
  58. #include <libc_r.h>
  59. #endif /* USE_REENTRANT_LIBC */
  60. char *program;
  61. void
  62. fail(char *format, ...)
  63. {
  64. int error;
  65. va_list ap;
  66. #ifdef USE_REENTRANT_LIBC
  67. R_STRERROR_INIT_R();
  68. #endif
  69. error = errno;
  70. fprintf(stderr, "%s: ", program);
  71. va_start(ap, format);
  72. vfprintf(stderr, format, ap);
  73. va_end(ap);
  74. if (error)
  75. #ifdef USE_REENTRANT_LIBC
  76. R_STRERROR_R(errno);
  77. fprintf(stderr, ": %s", r_strerror_r);
  78. #else
  79. fprintf(stderr, ": %s", strerror(errno));
  80. #endif
  81. putc('\n', stderr);
  82. exit(1);
  83. }
  84. char *
  85. getcomponent(char *path, char *name)
  86. {
  87. if (*path == '\0')
  88. return 0;
  89. if (*path == '/') {
  90. *name++ = '/';
  91. } else {
  92. do {
  93. *name++ = *path++;
  94. } while (*path != '/' && *path != '\0');
  95. }
  96. *name = '\0';
  97. while (*path == '/')
  98. path++;
  99. return path;
  100. }
  101. #ifdef UNIXWARE
  102. /* Sigh. The static buffer in Unixware's readdir is too small. */
  103. struct dirent * readdir(DIR *d)
  104. {
  105. static struct dirent *buf = NULL;
  106. #define MAX_PATH_LEN 1024
  107. if(buf == NULL)
  108. buf = (struct dirent *) malloc(sizeof(struct dirent) + MAX_PATH_LEN)
  109. ;
  110. return(readdir_r(d, buf));
  111. }
  112. #endif
  113. char *
  114. ino2name(ino_t ino, char *dir)
  115. {
  116. DIR *dp;
  117. struct dirent *ep;
  118. char *name;
  119. dp = opendir("..");
  120. if (!dp)
  121. fail("cannot read parent directory");
  122. for (;;) {
  123. if (!(ep = readdir(dp)))
  124. fail("cannot find current directory");
  125. if (ep->d_ino == ino)
  126. break;
  127. }
  128. name = xstrdup(ep->d_name);
  129. closedir(dp);
  130. return name;
  131. }
  132. void *
  133. xmalloc(size_t size)
  134. {
  135. void *p = malloc(size);
  136. if (!p)
  137. fail("cannot allocate %u bytes", size);
  138. return p;
  139. }
  140. char *
  141. xstrdup(char *s)
  142. {
  143. return strcpy(xmalloc(strlen(s) + 1), s);
  144. }
  145. char *
  146. xbasename(char *path)
  147. {
  148. char *cp;
  149. while ((cp = strrchr(path, '/')) && cp[1] == '\0')
  150. *cp = '\0';
  151. if (!cp) return path;
  152. return cp + 1;
  153. }
  154. void
  155. xchdir(char *dir)
  156. {
  157. if (chdir(dir) < 0)
  158. fail("cannot change directory to %s", dir);
  159. }
  160. int
  161. relatepaths(char *from, char *to, char *outpath)
  162. {
  163. char *cp, *cp2;
  164. int len;
  165. char buf[NAME_MAX];
  166. assert(*from == '/' && *to == '/');
  167. for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
  168. if (*cp == '\0')
  169. break;
  170. while (cp[-1] != '/')
  171. cp--, cp2--;
  172. if (cp - 1 == to) {
  173. /* closest common ancestor is /, so use full pathname */
  174. len = strlen(strcpy(outpath, to));
  175. if (outpath[len] != '/') {
  176. outpath[len++] = '/';
  177. outpath[len] = '\0';
  178. }
  179. } else {
  180. len = 0;
  181. while ((cp2 = getcomponent(cp2, buf)) != 0) {
  182. strcpy(outpath + len, "../");
  183. len += 3;
  184. }
  185. while ((cp = getcomponent(cp, buf)) != 0) {
  186. sprintf(outpath + len, "%s/", buf);
  187. len += strlen(outpath + len);
  188. }
  189. }
  190. return len;
  191. }
  192. void
  193. reversepath(char *inpath, char *name, int len, char *outpath)
  194. {
  195. char *cp, *cp2;
  196. char buf[NAME_MAX];
  197. struct stat sb;
  198. cp = strcpy(outpath + PATH_MAX - (len + 1), name);
  199. cp2 = inpath;
  200. while ((cp2 = getcomponent(cp2, buf)) != 0) {
  201. if (strcmp(buf, ".") == 0)
  202. continue;
  203. if (strcmp(buf, "..") == 0) {
  204. if (stat(".", &sb) < 0)
  205. fail("cannot stat current directory");
  206. name = ino2name(sb.st_ino, "..");
  207. len = strlen(name);
  208. cp -= len + 1;
  209. strcpy(cp, name);
  210. cp[len] = '/';
  211. free(name);
  212. xchdir("..");
  213. } else {
  214. cp -= 3;
  215. strncpy(cp, "../", 3);
  216. xchdir(buf);
  217. }
  218. }
  219. strcpy(outpath, cp);
  220. }