1 /* $XConsortium: lndir.c,v 1.5 91/07/21 15:52:11 rws Exp $ */
2 /* Create shadow link tree (after X11R4 script of the same name)
3 Mark Reinhold (mbr@lcs.mit.edu)/3 January 1990 */
5 /* Copyright 1990, Massachusetts Institute of Technology
7 Permission to use, copy, modify, and distribute this program for any purpose
8 and without fee is hereby granted, provided that this copyright and
9 permission notice appear on all copies and supporting documentation, that
10 the name of MIT not be used in advertising or publicity pertaining to
11 distribution of this program without specific prior permission, and that
12 notice be given in supporting documentation that copying and distribution is
13 by permission of MIT. MIT makes no representations about the suitability of
14 this software for any purpose. It is provided "as is" without expressed or
18 /* From the original /bin/sh script:
20 Used to create a copy of the a directory tree that has links for all
21 non-directories (except those named RCS or SCCS). If you are
22 building the distribution on more than one machine, you should use
25 If your master sources are located in /usr/local/src/X and you would like
26 your link tree to be in /usr/local/src/new-X, do the following:
28 % mkdir /usr/local/src/new-X
29 % cd /usr/local/src/new-X
36 #include <sys/param.h>
60 quit (code, fmt, a1, a2, a3)
63 fprintf (stderr, fmt, a1, a2, a3);
80 fprintf (stderr, fmt, a1, a2, a3);
85 /* Recursively create symbolic links from the current directory to the "from"
86 directory. Assumes that files described by fs and ts are directories. */
88 dodir (fn, fs, ts, rel)
89 char *fn; /* name of "from" directory, either absolute or
91 struct stat *fs, *ts; /* stats for the "from" directory and cwd */
92 int rel; /* if true, prepend "../" to fn before using */
96 char buf[MAXPATHLEN + 1], *p;
97 char symbuf[MAXPATHLEN + 1];
101 if ((fs->st_dev == ts->st_dev) && (fs->st_ino == ts->st_ino)) {
102 msg ("%s: From and to directories are identical!", fn);
112 if (!(df = opendir (buf))) {
113 msg ("%s: Cannot opendir", buf);
117 p = buf + strlen (buf);
119 n_dirs = fs->st_nlink;
120 while (dp = readdir (df)) {
121 strcpy (p, dp->d_name);
124 if (stat (buf, &sb) < 0) {
129 if (sb.st_mode & S_IFDIR) {
132 if (dp->d_name[0] == '.' &&
133 (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' &&
134 dp->d_name[2] == '\0')))
136 if (!strcmp (dp->d_name, "RCS"))
138 if (!strcmp (dp->d_name, "SCCS"))
141 printf ("%s:\n", buf);
142 if ((stat (dp->d_name, &sc) < 0) && (errno == ENOENT)) {
143 if (mkdir (dp->d_name, 0777) < 0 ||
144 stat (dp->d_name, &sc) < 0) {
149 if (readlink (dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) {
150 msg ("%s: is a link instead of a directory\n", dp->d_name);
153 if (chdir (dp->d_name) < 0) {
157 dodir (buf, &sb, &sc, (buf[0] != '/'));
158 if (chdir ("..") < 0)
165 if (symlink (buf, dp->d_name) < 0) {
166 int saverrno = errno;
168 symlen = readlink(dp->d_name, symbuf, sizeof(symbuf) - 1);
171 symbuf[symlen] = '\0';
172 if (symlen < 0 || strcmp(symbuf, buf))
190 if (ac > 1 && !strcmp(av[1], "-silent")) {
193 if (ac < silent + 2 || ac > silent + 3)
194 quit (1, "usage: %s [-silent] fromdir [todir]", av[0]);
197 if (ac == silent + 3)
203 if (stat (tn, &ts) < 0)
205 if (!(ts.st_mode & S_IFDIR))
206 quit (2, "%s: Not a directory", tn);
211 if (stat (fn, &fs) < 0)
213 if (!(fs.st_mode & S_IFDIR))
214 quit (2, "%s: Not a directory", fn);
216 exit (dodir (fn, &fs, &ts, 0));