PageRenderTime 42ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 481 lines | 379 code | 47 blank | 55 comment | 123 complexity | 6d7a8781f57d867dc440b5a4884dda5e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. ** Netscape portable install command.
  39. **
  40. ** Brendan Eich, 7/20/95
  41. */
  42. #include <stdio.h> /* OSF/1 requires this before grp.h, so put it first */
  43. #include <assert.h>
  44. #include <fcntl.h>
  45. #include <errno.h>
  46. #include <dirent.h>
  47. #include <limits.h>
  48. #include <grp.h>
  49. #include <pwd.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <unistd.h>
  54. #include <utime.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #include "pathsub.h"
  58. #ifdef HAVE_GETOPT_H
  59. #include <getopt.h>
  60. #endif
  61. #ifdef SUNOS4
  62. #include "sunos4.h"
  63. #endif
  64. #ifdef NEXTSTEP
  65. #include <bsd/libc.h>
  66. #endif
  67. #ifdef __QNX__
  68. #include <unix.h>
  69. #endif
  70. #ifdef NEED_S_ISLNK
  71. #if !defined(S_ISLNK) && defined(S_IFLNK)
  72. #define S_ISLNK(a) (((a) & S_IFMT) == S_IFLNK)
  73. #endif
  74. #endif
  75. #ifndef _DIRECTORY_SEPARATOR
  76. #define _DIRECTORY_SEPARATOR "/"
  77. #endif /* _DIRECTORY_SEPARATOR */
  78. #ifdef NEED_FCHMOD_PROTO
  79. extern int fchmod(int fildes, mode_t mode);
  80. #endif
  81. static void
  82. usage(void)
  83. {
  84. fprintf(stderr,
  85. "usage: %s [-C cwd] [-L linkprefix] [-m mode] [-o owner] [-g group]\n"
  86. " %*s [-DdltR] file [file ...] directory\n",
  87. program, (int) strlen(program), "");
  88. exit(2);
  89. }
  90. static int
  91. mkdirs(char *path, mode_t mode)
  92. {
  93. char *cp;
  94. struct stat sb;
  95. int res;
  96. int l;
  97. /* strip trailing "/." */
  98. l = strlen(path);
  99. if(l > 1 && path[l - 1] == '.' && path[l - 2] == '/')
  100. path[l - 2] = 0;
  101. while (*path == '/' && path[1] == '/')
  102. path++;
  103. for (cp = strrchr(path, '/'); cp && cp != path && *(cp - 1) == '/'; cp--);
  104. if (cp && cp != path) {
  105. *cp = '\0';
  106. if ((lstat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
  107. mkdirs(path, mode) < 0) {
  108. return -1;
  109. }
  110. *cp = '/';
  111. }
  112. res = mkdir(path, mode);
  113. if ((res != 0) && (errno == EEXIST))
  114. return 0;
  115. else
  116. return res;
  117. }
  118. static uid_t
  119. touid(char *owner)
  120. {
  121. struct passwd *pw;
  122. uid_t uid;
  123. char *cp;
  124. pw = getpwnam(owner);
  125. if (pw)
  126. return pw->pw_uid;
  127. uid = strtol(owner, &cp, 0);
  128. if (uid == 0 && cp == owner)
  129. fail("cannot find uid for %s", owner);
  130. return uid;
  131. }
  132. static gid_t
  133. togid(char *group)
  134. {
  135. struct group *gr;
  136. gid_t gid;
  137. char *cp;
  138. gr = getgrnam(group);
  139. if (gr)
  140. return gr->gr_gid;
  141. gid = strtol(group, &cp, 0);
  142. if (gid == 0 && cp == group)
  143. fail("cannot find gid for %s", group);
  144. return gid;
  145. }
  146. static void
  147. copyfile( char *name, char *toname, mode_t mode, char *group, char *owner,
  148. int dotimes, uid_t uid, gid_t gid )
  149. {
  150. int fromfd, tofd = -1, cc, wc, exists;
  151. char buf[BUFSIZ], *bp;
  152. struct stat sb, tosb;
  153. struct utimbuf utb;
  154. exists = (lstat(toname, &tosb) == 0);
  155. fromfd = open(name, O_RDONLY);
  156. if (fromfd < 0 || fstat(fromfd, &sb) < 0)
  157. fail("cannot access %s", name);
  158. if (exists) {
  159. if (S_ISREG(tosb.st_mode)) {
  160. /* See if we can open it. This is more reliable than 'access'. */
  161. tofd = open(toname, O_CREAT | O_WRONLY, 0666);
  162. }
  163. if (tofd < 0) {
  164. (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
  165. }
  166. }
  167. if (tofd < 0) {
  168. tofd = open(toname, O_CREAT | O_WRONLY, 0666);
  169. if (tofd < 0)
  170. fail("cannot create %s", toname);
  171. }
  172. bp = buf;
  173. while ((cc = read(fromfd, bp, sizeof buf)) > 0)
  174. {
  175. while ((wc = write(tofd, bp, (unsigned int)cc)) > 0)
  176. {
  177. if ((cc -= wc) == 0)
  178. break;
  179. bp += wc;
  180. }
  181. if (wc < 0)
  182. fail("cannot write to %s", toname);
  183. }
  184. if (cc < 0)
  185. fail("cannot read from %s", name);
  186. if (ftruncate(tofd, sb.st_size) < 0)
  187. fail("cannot truncate %s", toname);
  188. #if !defined(VMS)
  189. if (dotimes)
  190. {
  191. utb.actime = sb.st_atime;
  192. utb.modtime = sb.st_mtime;
  193. if (utime(toname, &utb) < 0)
  194. fail("cannot set times of %s", toname);
  195. }
  196. #ifdef HAVE_FCHMOD
  197. if (fchmod(tofd, mode) < 0)
  198. #else
  199. if (chmod(toname, mode) < 0)
  200. #endif
  201. fail("cannot change mode of %s", toname);
  202. #endif
  203. if ((owner || group) && fchown(tofd, uid, gid) < 0)
  204. fail("cannot change owner of %s", toname);
  205. /* Must check for delayed (NFS) write errors on close. */
  206. if (close(tofd) < 0)
  207. fail("cannot write to %s", toname);
  208. close(fromfd);
  209. #if defined(VMS)
  210. if (chmod(toname, (mode & (S_IREAD | S_IWRITE))) < 0)
  211. fail("cannot change mode of %s", toname);
  212. if (dotimes)
  213. {
  214. utb.actime = sb.st_atime;
  215. utb.modtime = sb.st_mtime;
  216. if (utime(toname, &utb) < 0)
  217. fail("cannot set times of %s", toname);
  218. }
  219. #endif
  220. }
  221. static void
  222. copydir( char *from, char *to, mode_t mode, char *group, char *owner,
  223. int dotimes, uid_t uid, gid_t gid)
  224. {
  225. int i;
  226. DIR *dir;
  227. struct dirent *ep;
  228. struct stat sb;
  229. char *base, *destdir, *direntry, *destentry;
  230. base = xbasename(from);
  231. /* create destination directory */
  232. destdir = xmalloc((unsigned int)(strlen(to) + 1 + strlen(base) + 1));
  233. sprintf(destdir, "%s%s%s", to, _DIRECTORY_SEPARATOR, base);
  234. if (mkdirs(destdir, mode) != 0) {
  235. fail("cannot make directory %s\n", destdir);
  236. return;
  237. }
  238. dir = opendir(from);
  239. direntry = xmalloc((unsigned int)PATH_MAX);
  240. destentry = xmalloc((unsigned int)PATH_MAX);
  241. while ((ep = readdir(dir)))
  242. {
  243. if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
  244. continue;
  245. sprintf(direntry, "%s/%s", from, ep->d_name);
  246. sprintf(destentry, "%s%s%s", destdir, _DIRECTORY_SEPARATOR, ep->d_name);
  247. if (stat(direntry, &sb) == 0 && S_ISDIR(sb.st_mode))
  248. copydir( direntry, destdir, mode, group, owner, dotimes, uid, gid );
  249. else
  250. copyfile( direntry, destentry, mode, group, owner, dotimes, uid, gid );
  251. }
  252. free(direntry);
  253. free(destentry);
  254. closedir(dir);
  255. }
  256. int
  257. main(int argc, char **argv)
  258. {
  259. int onlydir, dodir, dolink, dorelsymlink, dotimes, opt, len, lplen, tdlen, bnlen, exists, fromfd, tofd, cc, wc;
  260. mode_t mode = 0755;
  261. char *linkprefix, *owner, *group, *cp, *cwd, *todir, *toname, *name, *base, *linkname, *bp, buf[BUFSIZ];
  262. uid_t uid;
  263. gid_t gid;
  264. struct stat sb, tosb, fromsb;
  265. struct utimbuf utb;
  266. program = argv[0];
  267. cwd = linkname = linkprefix = owner = group = 0;
  268. onlydir = dodir = dolink = dorelsymlink = dotimes = lplen = 0;
  269. while ((opt = getopt(argc, argv, "C:DdlL:Rm:o:g:t")) != EOF) {
  270. switch (opt) {
  271. case 'C':
  272. cwd = optarg;
  273. break;
  274. case 'D':
  275. onlydir = 1;
  276. break;
  277. case 'd':
  278. dodir = 1;
  279. break;
  280. case 'l':
  281. dolink = 1;
  282. break;
  283. case 'L':
  284. linkprefix = optarg;
  285. lplen = strlen(linkprefix);
  286. dolink = 1;
  287. break;
  288. case 'R':
  289. dolink = dorelsymlink = 1;
  290. break;
  291. case 'm':
  292. mode = strtoul(optarg, &cp, 8);
  293. if (mode == 0 && cp == optarg)
  294. usage();
  295. break;
  296. case 'o':
  297. owner = optarg;
  298. break;
  299. case 'g':
  300. group = optarg;
  301. break;
  302. case 't':
  303. dotimes = 1;
  304. break;
  305. default:
  306. usage();
  307. }
  308. }
  309. argc -= optind;
  310. argv += optind;
  311. if (argc < 2 - onlydir)
  312. usage();
  313. todir = argv[argc-1];
  314. if ((stat(todir, &sb) < 0 || !S_ISDIR(sb.st_mode)) &&
  315. mkdirs(todir, 0777) < 0) {
  316. fail("cannot make directory %s", todir);
  317. }
  318. if (onlydir)
  319. return 0;
  320. if (!cwd) {
  321. #ifndef NEEDS_GETCWD
  322. #ifndef GETCWD_CANT_MALLOC
  323. cwd = getcwd(0, PATH_MAX);
  324. #else
  325. cwd = malloc(PATH_MAX + 1);
  326. cwd = getcwd(cwd, PATH_MAX);
  327. #endif
  328. #else
  329. cwd = malloc(PATH_MAX + 1);
  330. cwd = getwd(cwd);
  331. #endif
  332. }
  333. xchdir(todir);
  334. #ifndef NEEDS_GETCWD
  335. #ifndef GETCWD_CANT_MALLOC
  336. todir = getcwd(0, PATH_MAX);
  337. #else
  338. todir = malloc(PATH_MAX + 1);
  339. todir = getcwd(todir, PATH_MAX);
  340. #endif
  341. #else
  342. todir = malloc(PATH_MAX + 1);
  343. todir = getwd(todir);
  344. #endif
  345. tdlen = strlen(todir);
  346. xchdir(cwd);
  347. tdlen = strlen(todir);
  348. uid = owner ? touid(owner) : (uid_t)(-1);
  349. gid = group ? togid(group) : (gid_t)(-1);
  350. while (--argc > 0) {
  351. name = *argv++;
  352. len = strlen(name);
  353. base = xbasename(name);
  354. bnlen = strlen(base);
  355. toname = xmalloc((unsigned int)(tdlen + 1 + bnlen + 1));
  356. sprintf(toname, "%s%s%s", todir, _DIRECTORY_SEPARATOR, base);
  357. exists = (lstat(toname, &tosb) == 0);
  358. if (dodir) {
  359. /* -d means create a directory, always */
  360. if (exists && !S_ISDIR(tosb.st_mode)) {
  361. (void) unlink(toname);
  362. exists = 0;
  363. }
  364. if (!exists && mkdir(toname, mode) < 0)
  365. fail("cannot make directory %s", toname);
  366. if ((owner || group) && chown(toname, uid, gid) < 0)
  367. fail("cannot change owner of %s", toname);
  368. } else if (dolink) {
  369. if (access(name, R_OK) != 0) {
  370. fail("cannot access %s", name);
  371. }
  372. if (*name == '/') {
  373. /* source is absolute pathname, link to it directly */
  374. linkname = 0;
  375. } else {
  376. if (linkprefix) {
  377. /* -L implies -l and prefixes names with a $cwd arg. */
  378. len += lplen + 1;
  379. linkname = xmalloc((unsigned int)(len + 1));
  380. sprintf(linkname, "%s/%s", linkprefix, name);
  381. } else if (dorelsymlink) {
  382. /* Symlink the relative path from todir to source name. */
  383. linkname = xmalloc(PATH_MAX);
  384. if (*todir == '/') {
  385. /* todir is absolute: skip over common prefix. */
  386. lplen = relatepaths(todir, cwd, linkname);
  387. strcpy(linkname + lplen, name);
  388. } else {
  389. /* todir is named by a relative path: reverse it. */
  390. reversepath(todir, name, len, linkname);
  391. xchdir(cwd);
  392. }
  393. len = strlen(linkname);
  394. }
  395. name = linkname;
  396. }
  397. /* Check for a pre-existing symlink with identical content. */
  398. if ((exists && (!S_ISLNK(tosb.st_mode) ||
  399. readlink(toname, buf, sizeof buf) != len ||
  400. strncmp(buf, name, (unsigned int)len) != 0)) ||
  401. ((stat(name, &fromsb) == 0) &&
  402. (fromsb.st_mtime > tosb.st_mtime))) {
  403. (void) (S_ISDIR(tosb.st_mode) ? rmdir : unlink)(toname);
  404. exists = 0;
  405. }
  406. if (!exists && symlink(name, toname) < 0)
  407. fail("cannot make symbolic link %s", toname);
  408. #ifdef HAVE_LCHOWN
  409. if ((owner || group) && lchown(toname, uid, gid) < 0)
  410. fail("cannot change owner of %s", toname);
  411. #endif
  412. if (linkname) {
  413. free(linkname);
  414. linkname = 0;
  415. }
  416. } else {
  417. /* Copy from name to toname, which might be the same file. */
  418. if( stat(name, &sb) == 0 && S_IFDIR & sb.st_mode )
  419. {
  420. /* then is directory: must explicitly create destination dir */
  421. /* and manually copy files over */
  422. copydir( name, todir, mode, group, owner, dotimes, uid, gid );
  423. }
  424. else
  425. {
  426. copyfile(name, toname, mode, group, owner, dotimes, uid, gid);
  427. }
  428. }
  429. free(toname);
  430. }
  431. free(cwd);
  432. free(todir);
  433. return 0;
  434. }