lndir.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these librararies and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: lndir.c /main/2 1996/11/01 10:11:58 drk $ */
  24. /* Create shadow link tree (after X11R4 script of the same name)
  25. Mark Reinhold (mbr@lcs.mit.edu)/3 January 1990 */
  26. /* Copyright 1990, Massachusetts Institute of Technology
  27. Permission to use, copy, modify, and distribute this program for any purpose
  28. and without fee is hereby granted, provided that this copyright and
  29. permission notice appear on all copies and supporting documentation, that
  30. the name of MIT not be used in advertising or publicity pertaining to
  31. distribution of this program without specific prior permission, and that
  32. notice be given in supporting documentation that copying and distribution is
  33. by permission of MIT. MIT makes no representations about the suitability of
  34. this software for any purpose. It is provided "as is" without expressed or
  35. implied warranty.
  36. */
  37. /* From the original /bin/sh script:
  38. Used to create a copy of the a directory tree that has links for all
  39. non-directories (except those named RCS or SCCS). If you are
  40. building the distribution on more than one machine, you should use
  41. this script.
  42. If your master sources are located in /usr/local/src/X and you would like
  43. your link tree to be in /usr/local/src/new-X, do the following:
  44. % mkdir /usr/local/src/new-X
  45. % cd /usr/local/src/new-X
  46. % lndir ../X
  47. */
  48. #include <X11/Xos.h>
  49. #include <stdio.h>
  50. #include <sys/stat.h>
  51. #include <sys/param.h>
  52. #include <errno.h>
  53. #ifndef X_NOT_POSIX
  54. #include <dirent.h>
  55. #else
  56. #ifdef SYSV
  57. #include <dirent.h>
  58. #else
  59. #ifdef USG
  60. #include <dirent.h>
  61. #else
  62. #include <sys/dir.h>
  63. #ifndef dirent
  64. #define dirent direct
  65. #endif
  66. #endif
  67. #endif
  68. #endif
  69. #ifdef X_NOT_STDC_ENV
  70. extern int errno;
  71. #endif
  72. int silent;
  73. void
  74. quit (code, fmt, a1, a2, a3)
  75. char *fmt;
  76. {
  77. fprintf (stderr, fmt, a1, a2, a3);
  78. putc ('\n', stderr);
  79. exit (code);
  80. }
  81. void
  82. quiterr (code, s)
  83. char *s;
  84. {
  85. perror (s);
  86. exit (code);
  87. }
  88. void
  89. msg (fmt, a1, a2, a3)
  90. char *fmt;
  91. {
  92. fprintf (stderr, fmt, a1, a2, a3);
  93. putc ('\n', stderr);
  94. }
  95. /* Recursively create symbolic links from the current directory to the "from"
  96. directory. Assumes that files described by fs and ts are directories. */
  97. dodir (fn, fs, ts, rel)
  98. char *fn; /* name of "from" directory, either absolute or
  99. relative to cwd */
  100. struct stat *fs, *ts; /* stats for the "from" directory and cwd */
  101. int rel; /* if true, prepend "../" to fn before using */
  102. {
  103. DIR *df;
  104. struct dirent *dp;
  105. char buf[MAXPATHLEN + 1], *p;
  106. char symbuf[MAXPATHLEN + 1];
  107. struct stat sb, sc;
  108. int n_dirs;
  109. if ((fs->st_dev == ts->st_dev) && (fs->st_ino == ts->st_ino)) {
  110. msg ("%s: From and to directories are identical!", fn);
  111. return 1;
  112. }
  113. if (rel)
  114. strcpy (buf, "../");
  115. else
  116. buf[0] = '\0';
  117. strcat (buf, fn);
  118. if (!(df = opendir (buf))) {
  119. msg ("%s: Cannot opendir", buf);
  120. return 1;
  121. }
  122. p = buf + strlen (buf);
  123. *p++ = '/';
  124. n_dirs = fs->st_nlink;
  125. while (dp = readdir (df)) {
  126. strcpy (p, dp->d_name);
  127. if (n_dirs > 0) {
  128. if (stat (buf, &sb) < 0) {
  129. perror (buf);
  130. continue;
  131. }
  132. if (sb.st_mode & S_IFDIR) {
  133. /* directory */
  134. n_dirs--;
  135. if (dp->d_name[0] == '.' &&
  136. (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' &&
  137. dp->d_name[2] == '\0')))
  138. continue;
  139. if (!strcmp (dp->d_name, "RCS"))
  140. continue;
  141. if (!strcmp (dp->d_name, "SCCS"))
  142. continue;
  143. if (!silent)
  144. printf ("%s:\n", buf);
  145. if ((stat (dp->d_name, &sc) < 0) && (errno == ENOENT)) {
  146. if (mkdir (dp->d_name, 0777) < 0 ||
  147. stat (dp->d_name, &sc) < 0) {
  148. perror (dp->d_name);
  149. continue;
  150. }
  151. }
  152. if (readlink (dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) {
  153. msg ("%s: is a link instead of a directory\n", dp->d_name);
  154. continue;
  155. }
  156. if (chdir (dp->d_name) < 0) {
  157. perror (dp->d_name);
  158. continue;
  159. }
  160. dodir (buf, &sb, &sc, (buf[0] != '/'));
  161. if (chdir ("..") < 0)
  162. quiterr (1, "..");
  163. continue;
  164. }
  165. }
  166. /* non-directory */
  167. if (symlink (buf, dp->d_name) < 0) {
  168. int saverrno = errno;
  169. int symlen;
  170. symlen = readlink(dp->d_name, symbuf, sizeof(symbuf) - 1);
  171. errno = saverrno;
  172. if (symlen > 0)
  173. symbuf[symlen] = '\0';
  174. if (symlen < 0 || strcmp(symbuf, buf))
  175. perror (dp->d_name);
  176. }
  177. }
  178. closedir (df);
  179. return 0;
  180. }
  181. main (ac, av)
  182. int ac;
  183. char **av;
  184. {
  185. char *fn, *tn;
  186. struct stat fs, ts;
  187. silent = 0;
  188. if (ac > 1 && !strcmp(av[1], "-silent")) {
  189. silent = 1;
  190. }
  191. if (ac < silent + 2 || ac > silent + 3)
  192. quit (1, "usage: %s [-silent] fromdir [todir]", av[0]);
  193. fn = av[silent + 1];
  194. if (ac == silent + 3)
  195. tn = av[silent + 2];
  196. else
  197. tn = ".";
  198. /* to directory */
  199. if (stat (tn, &ts) < 0)
  200. quiterr (1, tn);
  201. if (!(ts.st_mode & S_IFDIR))
  202. quit (2, "%s: Not a directory", tn);
  203. if (chdir (tn) < 0)
  204. quiterr (1, tn);
  205. /* from directory */
  206. if (stat (fn, &fs) < 0)
  207. quiterr (1, fn);
  208. if (!(fs.st_mode & S_IFDIR))
  209. quit (2, "%s: Not a directory", fn);
  210. exit (dodir (fn, &fs, &ts, 0));
  211. }