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 #define NeedVarargsPrototypes 1
48 #include "lndir-Xos.h"
52 #include <sys/param.h>
72 #define MAXPATHLEN 2048
76 #include <sys/cygwin.h>
79 #if NeedVarargsPrototypes
86 int silent = 0; /* -silent */
87 int copy = 0; /* -copy */
88 int ignore_links = 0; /* -ignorelinks */
96 #define mymkdir(X, Y) mkdir(X)
98 #define mymkdir(X, Y) mkdir(X, Y)
103 #define BELIEVE_ST_NLINK
108 #if NeedVarargsPrototypes
109 int code, char * fmt, ...)
111 code, fmt, a1, a2, a3)
115 #if NeedVarargsPrototypes
118 vfprintf (stderr, fmt, args);
121 fprintf (stderr, fmt, a1, a2, a3);
137 #if NeedVarargsPrototypes
144 #if NeedVarargsPrototypes
148 fprintf (stderr, "%s:\n", curdir);
151 #if NeedVarargsPrototypes
153 vfprintf (stderr, fmt, args);
156 fprintf (stderr, fmt, a1, a2, a3);
166 fprintf (stderr, "%s:\n", curdir);
173 int copyfile(const char *oldpath, const char *newpath) {
182 return symlink(oldpath, newpath);
185 f_old = fopen(oldpath, "rb");
189 f_new = fopen(newpath, "wbx");
196 while ((s = fread(buf, 1, BUFSIZE, f_old)) > 0) {
197 if (fwrite(buf, 1, s, f_new) < s) {
212 if (fclose(f_new) == EOF) {
225 int equivalent(lname, rname)
231 if (!strcmp(lname, rname))
233 for (s = lname; *s && (s = strchr(s, '/')); s++) {
237 return !strcmp(lname, rname);
241 /* Recursively create symbolic links from the current directory to the "from"
242 directory. Assumes that files described by fs and ts are directories. */
244 dodir (fn, fs, ts, rel)
245 char *fn; /* name of "from" directory, either absolute or
247 struct stat *fs, *ts; /* stats for the "from" directory and cwd */
248 int rel; /* if true, prepend "../" to fn before using */
252 char buf[MAXPATHLEN + 1], *p;
253 char symbuf[MAXPATHLEN + 1];
254 char basesym[MAXPATHLEN + 1];
261 if ((fs->st_dev == ts->st_dev) &&
262 (fs->st_ino == ts->st_ino) &&
263 /* inode is always 0 on Windows; we don't want to fail in that case */
266 msg ("%s: From and to directories are identical!", fn);
276 if (!(df = opendir (buf))) {
277 msg ("%s: Cannot opendir", buf);
281 p = buf + strlen (buf);
283 n_dirs = fs->st_nlink;
284 while (dp = readdir (df)) {
285 if (dp->d_name[strlen(dp->d_name) - 1] == '~')
287 if (dp->d_name[0] == '.' && dp->d_name[1] == '#') /* 'non-conflict files' left behind by CVS */
289 strcpy (p, dp->d_name);
292 #ifdef BELIEVE_ST_NLINK
295 /* st_nlink is 1 on Windows, so we have to keep looking for
296 * directories forever */
300 if (stat (buf, &sb) < 0) {
306 if(S_ISDIR(sb.st_mode))
308 if (sb.st_mode & S_IFDIR)
312 #ifndef __CYGWIN32__ /* don't trust cygwin's n_dirs count */
315 if (dp->d_name[0] == '.' &&
316 (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' &&
317 dp->d_name[2] == '\0')))
319 if (!strcmp (dp->d_name, "RCS"))
321 if (!strcmp (dp->d_name, "SCCS"))
323 if (!strcmp (dp->d_name, "CVS"))
325 if (!strcmp (dp->d_name, ".svn"))
327 if (!strcmp (dp->d_name, "_darcs"))
329 if (!strcmp (dp->d_name, "CVS.adm"))
333 curdir = silent ? buf : (char *)0;
335 printf ("%s:\n", buf);
336 if ((stat (dp->d_name, &sc) < 0) && (errno == ENOENT)) {
337 if (mymkdir (dp->d_name, 0777) < 0 ||
338 stat (dp->d_name, &sc) < 0) {
339 mperror (dp->d_name);
340 curdir = rcurdir = ocurdir;
345 if (readlink (dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) {
346 msg ("%s: is a link instead of a directory", dp->d_name);
347 curdir = rcurdir = ocurdir;
351 if (chdir (dp->d_name) < 0) {
352 mperror (dp->d_name);
353 curdir = rcurdir = ocurdir;
356 rel = (fn[0] != '/') && ((fn[0] == '\0') || (fn[1] != ':'));
357 dodir (buf, &sb, &sc, rel);
358 if (chdir ("..") < 0)
360 curdir = rcurdir = ocurdir;
367 symlen = readlink (dp->d_name, symbuf, sizeof(symbuf) - 1);
369 symbuf[symlen] = '\0';
371 /* The option to ignore links exists mostly because
372 checking for them slows us down by 10-20%.
373 But it is off by default because this really is a useful check. */
375 /* see if the file in the base tree was a symlink */
376 basesymlen = readlink(buf, basesym, sizeof(basesym) - 1);
378 basesym[basesymlen] = '\0';
383 if (!equivalent (basesymlen>=0 ? basesym : buf, symbuf)) {
386 if (copyfile (basesymlen>=0 ? basesym : buf, dp->d_name) < 0)
387 mperror (dp->d_name);
389 /* Link exists in new tree. Print message if it doesn't match. */
390 msg ("%s: %s", dp->d_name, symbuf);
394 if (copyfile (basesymlen>=0 ? basesym : buf, dp->d_name) < 0)
395 mperror (dp->d_name);
407 char *prog_name = av[0];
412 The lndir code assumes unix-style paths to work. cygwin
413 lets you get away with using dos'ish paths (e.g., "f:/oo")
414 in most contexts. Using them with 'lndir' will seriously
415 confuse the user though, so under-the-hood, we convert the
416 path into something POSIX-like.
418 static char fn[MAXPATHLEN+1];
424 if (strcmp(*av, "-silent") == 0)
426 else if (strcmp(*av, "-f") == 0)
428 else if (strcmp(*av, "-ignorelinks") == 0)
430 else if (strcmp(*av, "-copy") == 0)
432 else if (strcmp(*av, "--") == 0) {
439 if (ac < 1 || ac > 2)
440 quit (1, "usage: %s [-f] [-silent] [-ignorelinks] fromdir [todir]",
444 cygwin_conv_to_full_posix_path(av[0], fn);
455 if (stat (tn, &ts) < 0) {
456 if (force && (tn[0] != '.' || tn[1] != '\0') ) {
457 mymkdir(tn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
462 if (!(S_ISDIR(ts.st_mode)))
464 if (!(ts.st_mode & S_IFDIR))
466 quit (2, "%s: Not a directory", tn);
473 if (stat (fn, &fs) < 0)
476 if (!(S_ISDIR(fs.st_mode)))
478 if (!(fs.st_mode & S_IFDIR))
480 quit (2, "%s: Not a directory", fn);
482 exit (dodir (fn, &fs, &ts, 0));