1 /* $XConsortium: lndir.c /main/16 1996/09/28 16:16:40 rws $ */
2 /* Create shadow link tree (after X11R4 script of the same name)
3 Mark Reinhold (mbr@lcs.mit.edu)/3 January 1990 */
6 Copyright (c) 1990, X Consortium
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of the X Consortium shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings
27 in this Software without prior written authorization from the X Consortium.
31 /* From the original /bin/sh script:
33 Used to create a copy of the a directory tree that has links for all
34 non-directories (except those named RCS, SCCS or CVS.adm). If you are
35 building the distribution on more than one machine, you should use
38 If your master sources are located in /usr/local/src/X and you would like
39 your link tree to be in /usr/local/src/new-X, do the following:
41 % mkdir /usr/local/src/new-X
42 % cd /usr/local/src/new-X
46 #include "lndir-Xos.h"
49 #include <sys/param.h>
69 #define MAXPATHLEN 2048
73 #include <sys/cygwin.h>
76 #if NeedVarargsPrototypes
83 int silent = 0; /* -silent */
84 int ignore_links = 0; /* -ignorelinks */
93 #if NeedVarargsPrototypes
94 int code, char * fmt, ...)
96 code, fmt, a1, a2, a3)
100 #if NeedVarargsPrototypes
103 vfprintf (stderr, fmt, args);
106 fprintf (stderr, fmt, a1, a2, a3);
122 #if NeedVarargsPrototypes
129 #if NeedVarargsPrototypes
133 fprintf (stderr, "%s:\n", curdir);
136 #if NeedVarargsPrototypes
138 vfprintf (stderr, fmt, args);
141 fprintf (stderr, fmt, a1, a2, a3);
151 fprintf (stderr, "%s:\n", curdir);
158 int equivalent(lname, rname)
164 if (!strcmp(lname, rname))
166 for (s = lname; *s && (s = strchr(s, '/')); s++) {
170 return !strcmp(lname, rname);
174 /* Recursively create symbolic links from the current directory to the "from"
175 directory. Assumes that files described by fs and ts are directories. */
177 dodir (fn, fs, ts, rel)
178 char *fn; /* name of "from" directory, either absolute or
180 struct stat *fs, *ts; /* stats for the "from" directory and cwd */
181 int rel; /* if true, prepend "../" to fn before using */
185 char buf[MAXPATHLEN + 1], *p;
186 char symbuf[MAXPATHLEN + 1];
187 char basesym[MAXPATHLEN + 1];
194 if ((fs->st_dev == ts->st_dev) && (fs->st_ino == ts->st_ino)) {
195 msg ("%s: From and to directories are identical!", fn);
205 if (!(df = opendir (buf))) {
206 msg ("%s: Cannot opendir", buf);
210 p = buf + strlen (buf);
212 n_dirs = fs->st_nlink;
213 while (dp = readdir (df)) {
214 if (dp->d_name[strlen(dp->d_name) - 1] == '~')
216 if (dp->d_name[0] == '.' && dp->d_name[1] == '#') /* 'non-conflict files' left behind by CVS */
218 strcpy (p, dp->d_name);
221 if (stat (buf, &sb) < 0) {
227 if(S_ISDIR(sb.st_mode))
229 if (sb.st_mode & S_IFDIR)
233 #ifndef __CYGWIN32__ /* don't trust cygwin's n_dirs count */
236 if (dp->d_name[0] == '.' &&
237 (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' &&
238 dp->d_name[2] == '\0')))
240 if (!strcmp (dp->d_name, "RCS"))
242 if (!strcmp (dp->d_name, "SCCS"))
244 if (!strcmp (dp->d_name, "CVS"))
246 if (!strcmp (dp->d_name, ".svn"))
248 if (!strcmp (dp->d_name, "CVS.adm"))
252 curdir = silent ? buf : (char *)0;
254 printf ("%s:\n", buf);
255 if ((stat (dp->d_name, &sc) < 0) && (errno == ENOENT)) {
256 if (mkdir (dp->d_name, 0777) < 0 ||
257 stat (dp->d_name, &sc) < 0) {
258 mperror (dp->d_name);
259 curdir = rcurdir = ocurdir;
263 if (readlink (dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) {
264 msg ("%s: is a link instead of a directory", dp->d_name);
265 curdir = rcurdir = ocurdir;
268 if (chdir (dp->d_name) < 0) {
269 mperror (dp->d_name);
270 curdir = rcurdir = ocurdir;
273 dodir (buf, &sb, &sc, (buf[0] != '/'));
274 if (chdir ("..") < 0)
276 curdir = rcurdir = ocurdir;
282 symlen = readlink (dp->d_name, symbuf, sizeof(symbuf) - 1);
284 symbuf[symlen] = '\0';
286 /* The option to ignore links exists mostly because
287 checking for them slows us down by 10-20%.
288 But it is off by default because this really is a useful check. */
290 /* see if the file in the base tree was a symlink */
291 basesymlen = readlink(buf, basesym, sizeof(basesym) - 1);
293 basesym[basesymlen] = '\0';
297 if (!equivalent (basesymlen>=0 ? basesym : buf, symbuf)) {
300 if (symlink (basesymlen>=0 ? basesym : buf, dp->d_name) < 0)
301 mperror (dp->d_name);
303 /* Link exists in new tree. Print message if it doesn't match. */
304 msg ("%s: %s", dp->d_name, symbuf);
308 if (symlink (basesymlen>=0 ? basesym : buf, dp->d_name) < 0)
309 mperror (dp->d_name);
322 char *prog_name = av[0];
327 The lndir code assumes unix-style paths to work. cygwin
328 lets you get away with using dos'ish paths (e.g., "f:/oo")
329 in most contexts. Using them with 'lndir' will seriously
330 confuse the user though, so under-the-hood, we convert the
331 path into something POSIX-like.
333 static char fn[MAXPATHLEN+1];
339 if (strcmp(*av, "-silent") == 0)
341 else if (strcmp(*av, "-f") == 0)
343 else if (strcmp(*av, "-ignorelinks") == 0)
345 else if (strcmp(*av, "--") == 0) {
352 if (ac < 1 || ac > 2)
353 quit (1, "usage: %s [-f] [-silent] [-ignorelinks] fromdir [todir]",
357 cygwin_conv_to_full_posix_path(av[0], fn);
368 if (stat (tn, &ts) < 0) {
369 if (force && (tn[0] != '.' || tn[1] != '\0') ) {
370 mkdir(tn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
375 if (!(S_ISDIR(ts.st_mode)))
377 if (!(ts.st_mode & S_IFDIR))
379 quit (2, "%s: Not a directory", tn);
386 if (stat (fn, &fs) < 0)
389 if (!(S_ISDIR(fs.st_mode)))
391 if (!(fs.st_mode & S_IFDIR))
393 quit (2, "%s: Not a directory", fn);
395 exit (dodir (fn, &fs, &ts, 0));