/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/config/pathsub.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · C · 247 lines · 179 code · 23 blank · 45 comment · 51 complexity · 3d813c60ea26de7e4e6a097321b1ab6f MD5 · raw file

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either of the GNU General Public License Version 2 or later (the "GPL"),
  26. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /*
  38. ** Pathname subroutines.
  39. **
  40. ** Brendan Eich, 8/29/95
  41. */
  42. #include <assert.h>
  43. #include <sys/types.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/stat.h>
  52. #include "pathsub.h"
  53. #ifdef USE_REENTRANT_LIBC
  54. #include <libc_r.h>
  55. #endif
  56. #ifdef SUNOS4
  57. #include "sunos4.h"
  58. #endif
  59. #ifndef D_INO
  60. #define D_INO d_ino
  61. #endif
  62. char *program;
  63. void
  64. fail(char *format, ...)
  65. {
  66. int error;
  67. va_list ap;
  68. #ifdef USE_REENTRANT_LIBC
  69. R_STRERROR_INIT_R();
  70. #endif
  71. error = errno;
  72. fprintf(stderr, "%s: ", program);
  73. va_start(ap, format);
  74. vfprintf(stderr, format, ap);
  75. va_end(ap);
  76. if (error) {
  77. #ifdef USE_REENTRANT_LIBC
  78. R_STRERROR_R(errno);
  79. fprintf(stderr, ": %s", r_strerror_r);
  80. #else
  81. fprintf(stderr, ": %s", strerror(errno));
  82. #endif
  83. }
  84. putc('\n', stderr);
  85. exit(1);
  86. }
  87. char *
  88. getcomponent(char *path, char *name)
  89. {
  90. if (*path == '\0')
  91. return 0;
  92. if (*path == '/') {
  93. *name++ = '/';
  94. } else {
  95. do {
  96. *name++ = *path++;
  97. } while (*path != '/' && *path != '\0');
  98. }
  99. *name = '\0';
  100. while (*path == '/')
  101. path++;
  102. return path;
  103. }
  104. #ifdef LAME_READDIR
  105. #include <sys/param.h>
  106. /*
  107. ** The static buffer in Unixware's readdir is too small.
  108. */
  109. struct dirent *readdir(DIR *d)
  110. {
  111. static struct dirent *buf = NULL;
  112. if(buf == NULL)
  113. buf = (struct dirent *) malloc(sizeof(struct dirent) + MAXPATHLEN);
  114. return(readdir_r(d, buf));
  115. }
  116. #endif
  117. char *
  118. ino2name(ino_t ino, char *dir)
  119. {
  120. DIR *dp;
  121. struct dirent *ep;
  122. char *name;
  123. dp = opendir("..");
  124. if (!dp)
  125. fail("cannot read parent directory");
  126. for (;;) {
  127. if (!(ep = readdir(dp)))
  128. fail("cannot find current directory");
  129. if (ep->D_INO == ino)
  130. break;
  131. }
  132. name = xstrdup(ep->d_name);
  133. closedir(dp);
  134. return name;
  135. }
  136. void *
  137. xmalloc(size_t size)
  138. {
  139. void *p = malloc(size);
  140. if (!p)
  141. fail("cannot allocate %u bytes", size);
  142. return p;
  143. }
  144. char *
  145. xstrdup(char *s)
  146. {
  147. return strcpy(xmalloc(strlen(s) + 1), s);
  148. }
  149. char *
  150. xbasename(char *path)
  151. {
  152. char *cp;
  153. while ((cp = strrchr(path, '/')) && cp[1] == '\0')
  154. *cp = '\0';
  155. if (!cp) return path;
  156. return cp + 1;
  157. }
  158. void
  159. xchdir(char *dir)
  160. {
  161. if (chdir(dir) < 0)
  162. fail("cannot change directory to %s", dir);
  163. }
  164. int
  165. relatepaths(char *from, char *to, char *outpath)
  166. {
  167. char *cp, *cp2;
  168. int len;
  169. char buf[NAME_MAX];
  170. assert(*from == '/' && *to == '/');
  171. for (cp = to, cp2 = from; *cp == *cp2; cp++, cp2++)
  172. if (*cp == '\0')
  173. break;
  174. while (cp[-1] != '/')
  175. cp--, cp2--;
  176. if (cp - 1 == to) {
  177. /* closest common ancestor is /, so use full pathname */
  178. len = strlen(strcpy(outpath, to));
  179. if (outpath[len] != '/') {
  180. outpath[len++] = '/';
  181. outpath[len] = '\0';
  182. }
  183. } else {
  184. len = 0;
  185. while ((cp2 = getcomponent(cp2, buf)) != 0) {
  186. strcpy(outpath + len, "../");
  187. len += 3;
  188. }
  189. while ((cp = getcomponent(cp, buf)) != 0) {
  190. sprintf(outpath + len, "%s/", buf);
  191. len += strlen(outpath + len);
  192. }
  193. }
  194. return len;
  195. }
  196. void
  197. reversepath(char *inpath, char *name, int len, char *outpath)
  198. {
  199. char *cp, *cp2;
  200. char buf[NAME_MAX];
  201. struct stat sb;
  202. cp = strcpy(outpath + PATH_MAX - (len + 1), name);
  203. cp2 = inpath;
  204. while ((cp2 = getcomponent(cp2, buf)) != 0) {
  205. if (strcmp(buf, ".") == 0)
  206. continue;
  207. if (strcmp(buf, "..") == 0) {
  208. if (stat(".", &sb) < 0)
  209. fail("cannot stat current directory");
  210. name = ino2name(sb.st_ino, "..");
  211. len = strlen(name);
  212. cp -= len + 1;
  213. strcpy(cp, name);
  214. cp[len] = '/';
  215. free(name);
  216. xchdir("..");
  217. } else {
  218. cp -= 3;
  219. strncpy(cp, "../", 3);
  220. xchdir(buf);
  221. }
  222. }
  223. strcpy(outpath, cp);
  224. }