Make "runghc -f path-to-ghc Main.hs" work
[ghc-hetmet.git] / utils / lndir / lndir.c
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 */
4
5 /* 
6 Copyright (c) 1990,  X Consortium
7
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:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
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.
24
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.
28
29 */
30
31 /* From the original /bin/sh script:
32
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
36   this technique.
37
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:
40
41         %  mkdir /usr/local/src/new-X
42         %  cd /usr/local/src/new-X
43         %  lndir ../X
44 */
45
46 #include "lndir-Xos.h"
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <sys/stat.h>
50 #include <sys/param.h>
51 #include <errno.h>
52
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 #ifndef MAXPATHLEN
70 #define MAXPATHLEN 2048
71 #endif
72
73 #ifdef __CYGWIN32__
74 #include <sys/cygwin.h>
75 #endif
76
77 #if NeedVarargsPrototypes
78 #include <stdarg.h>
79 #endif
80
81 #ifdef X_NOT_STDC_ENV
82 extern int errno;
83 #endif
84 int silent = 0;                 /* -silent */
85 int copy = 0;                   /* -copy */
86 int ignore_links = 0;           /* -ignorelinks */
87
88 char *rcurdir;
89 char *curdir;
90
91 int force=0;
92
93 #ifdef WIN32
94 #define mymkdir(X, Y) mkdir(X)
95 #else
96 #define mymkdir(X, Y) mkdir(X, Y)
97 #endif
98
99 #ifndef WIN32
100 #define SYMLINKS
101 #define BELIEVE_ST_NLINK
102 #endif
103
104 void
105 quit (
106 #if NeedVarargsPrototypes
107     int code, char * fmt, ...)
108 #else
109     code, fmt, a1, a2, a3)
110     char *fmt;
111 #endif
112 {
113 #if NeedVarargsPrototypes
114     va_list args;
115     va_start(args, fmt);
116     vfprintf (stderr, fmt, args);
117     va_end(args);
118 #else
119     fprintf (stderr, fmt, a1, a2, a3);
120 #endif
121     putc ('\n', stderr);
122     exit (code);
123 }
124
125 void
126 quiterr (code, s)
127     char *s;
128 {
129     perror (s);
130     exit (code);
131 }
132
133 void
134 msg (
135 #if NeedVarargsPrototypes
136     char * fmt, ...)
137 #else
138     fmt, a1, a2, a3)
139     char *fmt;
140 #endif
141 {
142 #if NeedVarargsPrototypes
143     va_list args;
144 #endif
145     if (curdir) {
146         fprintf (stderr, "%s:\n", curdir);
147         curdir = 0;
148     }
149 #if NeedVarargsPrototypes
150     va_start(args, fmt);
151     vfprintf (stderr, fmt, args);
152     va_end(args);
153 #else
154     fprintf (stderr, fmt, a1, a2, a3);
155 #endif
156     putc ('\n', stderr);
157 }
158
159 void
160 mperror (s)
161     char *s;
162 {
163     if (curdir) {
164         fprintf (stderr, "%s:\n", curdir);
165         curdir = 0;
166     }
167     perror (s);
168 }
169
170 #define BUFSIZE 1024
171 int copyfile(const char *oldpath, const char *newpath) {
172     FILE *f_old;
173     FILE *f_new;
174     int e;
175     ssize_t s;
176     char buf[BUFSIZE];
177
178 #ifdef SYMLINKS
179     if (copy) {
180         return symlink(oldpath, newpath);
181     } else {
182 #endif
183         f_old = fopen(oldpath, "rb");
184         if (f_old == NULL) {
185             return -1;
186         }
187         f_new = fopen(newpath, "wbx");
188         if (f_new == NULL) {
189             e = errno;
190             fclose(f_old);
191             errno = e;
192             return -1;
193         }
194         while ((s = fread(buf, 1, BUFSIZE, f_old)) > 0) {
195             if (fwrite(buf, 1, s, f_new) < s) {
196                 e = errno;
197                 fclose(f_old);
198                 fclose(f_new);
199                 errno = e;
200                 return -1;
201             }
202         }
203         if (!feof(f_old)) {
204             e = errno;
205             fclose(f_old);
206             fclose(f_new);
207             errno = e;
208             return -1;
209         }
210         if (fclose(f_new) == EOF) {
211             e = errno;
212             fclose(f_old);
213             errno = e;
214             return -1;
215         }
216         fclose(f_old);
217         return 0;
218 #ifdef SYMLINKS
219     }
220 #endif
221 }
222
223 int equivalent(lname, rname)
224     char *lname;
225     char *rname;
226 {
227     char *s;
228
229     if (!strcmp(lname, rname))
230         return 1;
231     for (s = lname; *s && (s = strchr(s, '/')); s++) {
232         while (s[1] == '/')
233             strcpy(s+1, s+2);
234     }
235     return !strcmp(lname, rname);
236 }
237
238
239 /* Recursively create symbolic links from the current directory to the "from"
240    directory.  Assumes that files described by fs and ts are directories. */
241
242 dodir (fn, fs, ts, rel)
243 char *fn;                       /* name of "from" directory, either absolute or
244                                    relative to cwd */
245 struct stat *fs, *ts;           /* stats for the "from" directory and cwd */
246 int rel;                        /* if true, prepend "../" to fn before using */
247 {
248     DIR *df;
249     struct dirent *dp;
250     char buf[MAXPATHLEN + 1], *p;
251     char symbuf[MAXPATHLEN + 1];
252     char basesym[MAXPATHLEN + 1];
253     struct stat sb, sc;
254     int n_dirs;
255     int symlen = -1;
256     int basesymlen = -1;
257     char *ocurdir;
258
259     if ((fs->st_dev == ts->st_dev) &&
260         (fs->st_ino == ts->st_ino) &&
261         /* inode is always 0 on Windows; we don't want to fail in that case */
262         (fs->st_ino != 0)
263        ) {
264         msg ("%s: From and to directories are identical!", fn);
265         return 1;
266     }
267
268     if (rel)
269         strcpy (buf, "../");
270     else
271         buf[0] = '\0';
272     strcat (buf, fn);
273     
274     if (!(df = opendir (buf))) {
275         msg ("%s: Cannot opendir", buf);
276         return 1;
277     }
278
279     p = buf + strlen (buf);
280     *p++ = '/';
281     n_dirs = fs->st_nlink;
282     while (dp = readdir (df)) {
283         if (dp->d_name[strlen(dp->d_name) - 1] == '~')
284             continue;
285         if (dp->d_name[0] == '.' && dp->d_name[1] == '#') /* 'non-conflict files' left behind by CVS */
286             continue;
287         strcpy (p, dp->d_name);
288
289         if (
290 #ifdef BELIEVE_ST_NLINK
291             n_dirs > 0
292 #else
293             /* st_nlink is 1 on Windows, so we have to keep looking for
294              * directories forever */
295             1
296 #endif
297            ) {
298             if (stat (buf, &sb) < 0) {
299                 mperror (buf);
300                 continue;
301             }
302
303 #ifdef S_ISDIR
304             if(S_ISDIR(sb.st_mode))
305 #else
306             if (sb.st_mode & S_IFDIR) 
307 #endif
308             {
309                 /* directory */
310 #ifndef __CYGWIN32__   /* don't trust cygwin's n_dirs count */
311                 n_dirs--;
312 #endif
313                 if (dp->d_name[0] == '.' &&
314                     (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' &&
315                                                dp->d_name[2] == '\0')))
316                     continue;
317                 if (!strcmp (dp->d_name, "RCS"))
318                     continue;
319                 if (!strcmp (dp->d_name, "SCCS"))
320                     continue;
321                 if (!strcmp (dp->d_name, "CVS"))
322                     continue;
323                 if (!strcmp (dp->d_name, ".svn"))
324                     continue;
325                 if (!strcmp (dp->d_name, "_darcs"))
326                     continue;
327                 if (!strcmp (dp->d_name, "CVS.adm"))
328                     continue;
329                 ocurdir = rcurdir;
330                 rcurdir = buf;
331                 curdir = silent ? buf : (char *)0;
332                 if (!silent)
333                     printf ("%s:\n", buf);
334                 if ((stat (dp->d_name, &sc) < 0) && (errno == ENOENT)) {
335                     if (mymkdir (dp->d_name, 0777) < 0 ||
336                         stat (dp->d_name, &sc) < 0) {
337                         mperror (dp->d_name);
338                         curdir = rcurdir = ocurdir;
339                         continue;
340                     }
341                 }
342 #ifdef SYMLINKS
343                 if (readlink (dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) {
344                     msg ("%s: is a link instead of a directory", dp->d_name);
345                     curdir = rcurdir = ocurdir;
346                     continue;
347                 }
348 #endif
349                 if (chdir (dp->d_name) < 0) {
350                     mperror (dp->d_name);
351                     curdir = rcurdir = ocurdir;
352                     continue;
353                 }
354                 rel = (fn[0] != '/') && ((fn[0] == '\0') || (fn[1] != ':'));
355                 dodir (buf, &sb, &sc, rel);
356                 if (chdir ("..") < 0)
357                     quiterr (1, "..");
358                 curdir = rcurdir = ocurdir;
359                 continue;
360             }
361         }
362
363         /* non-directory */
364 #ifdef SYMLINKS
365         symlen = readlink (dp->d_name, symbuf, sizeof(symbuf) - 1);
366         if (symlen >= 0)
367             symbuf[symlen] = '\0';
368
369         /* The option to ignore links exists mostly because
370            checking for them slows us down by 10-20%.
371            But it is off by default because this really is a useful check. */
372         if (!ignore_links) {
373             /* see if the file in the base tree was a symlink */
374             basesymlen = readlink(buf, basesym, sizeof(basesym) - 1);
375             if (basesymlen >= 0)
376                 basesym[basesymlen] = '\0';
377         }
378 #endif
379
380         if (symlen >= 0) {
381           if (!equivalent (basesymlen>=0 ? basesym : buf, symbuf)) {
382             if (force) {
383               unlink(dp->d_name);
384               if (copyfile (basesymlen>=0 ? basesym : buf, dp->d_name) < 0)
385                 mperror (dp->d_name);
386             } else {
387               /* Link exists in new tree.  Print message if it doesn't match. */
388               msg ("%s: %s", dp->d_name, symbuf);
389             }
390           }
391         } else {
392           if (copyfile (basesymlen>=0 ? basesym : buf, dp->d_name) < 0)
393             mperror (dp->d_name);
394         }
395     }
396     
397     closedir (df);
398     return 0;
399 }
400
401 main (ac, av)
402 int ac;
403 char **av;
404 {
405     char *prog_name = av[0];
406     char* tn;
407     struct stat fs, ts;
408 #ifdef __CYGWIN32__
409     /*   
410     The lndir code assumes unix-style paths to work. cygwin
411     lets you get away with using dos'ish paths (e.g., "f:/oo")
412     in most contexts. Using them with 'lndir' will seriously
413     confuse the user though, so under-the-hood, we convert the
414     path into something POSIX-like.
415     */
416     static char fn[MAXPATHLEN+1];
417 #else
418     char *fn;
419 #endif
420
421     while (++av, --ac) {
422       if (strcmp(*av, "-silent") == 0)
423           silent = 1;
424       else if (strcmp(*av, "-f") == 0)
425           force = 1;
426       else if (strcmp(*av, "-ignorelinks") == 0)
427           ignore_links = 1;
428       else if (strcmp(*av, "-copy") == 0)
429           copy = 1;
430       else if (strcmp(*av, "--") == 0) {
431           ++av, --ac;
432           break;
433       } else
434           break;
435     }
436
437     if (ac < 1 || ac > 2)
438         quit (1, "usage: %s [-f] [-silent] [-ignorelinks] fromdir [todir]",
439               prog_name);
440
441 #ifdef __CYGWIN32__
442     cygwin_conv_to_full_posix_path(av[0], fn);
443 #else
444     fn = av[0];
445 #endif
446
447     if (ac == 2)
448         tn = av[1];
449     else
450         tn = ".";
451
452     /* to directory */
453     if (stat (tn, &ts) < 0) {
454       if (force && (tn[0] != '.' || tn[1] != '\0') ) {
455          mymkdir(tn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
456       } 
457       else {
458         quiterr (1, tn);
459 #ifdef S_ISDIR
460         if (!(S_ISDIR(ts.st_mode)))
461 #else
462         if (!(ts.st_mode & S_IFDIR))
463 #endif
464            quit (2, "%s: Not a directory", tn);
465       }
466     }
467     if (chdir (tn) < 0)
468         quiterr (1, tn);
469
470     /* from directory */
471     if (stat (fn, &fs) < 0)
472         quiterr (1, fn);
473 #ifdef S_ISDIR
474     if (!(S_ISDIR(fs.st_mode)))
475 #else
476     if (!(fs.st_mode & S_IFDIR))
477 #endif
478         quit (2, "%s: Not a directory", fn);
479
480     exit (dodir (fn, &fs, &ts, 0));
481 }