home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / Fresco / build / Unix / config / util / lndir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-12  |  7.1 KB  |  326 lines

  1. /* $XConsortium: lndir.c,v 1.14 95/01/09 20:08:20 kaleb Exp $ */
  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 <X11/Xos.h>
  47. #include <X11/Xfuncproto.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. #if NeedVarargsPrototypes
  74. #include <stdarg.h>
  75. #endif
  76.  
  77. #ifdef X_NOT_STDC_ENV
  78. extern int errno;
  79. #endif
  80. int silent;
  81.  
  82. char *rcurdir;
  83. char *curdir;
  84.  
  85. void
  86. quit (
  87. #if NeedVarargsPrototypes
  88.     int code, char * fmt, ...)
  89. #else
  90.     code, fmt, a1, a2, a3)
  91.     char *fmt;
  92. #endif
  93. {
  94. #if NeedVarargsPrototypes
  95.     va_list args;
  96.     va_start(args, fmt);
  97.     vfprintf (stderr, fmt, args);
  98.     va_end(args);
  99. #else
  100.     fprintf (stderr, fmt, a1, a2, a3);
  101. #endif
  102.     putc ('\n', stderr);
  103.     exit (code);
  104. }
  105.  
  106. void
  107. quiterr (code, s)
  108.     char *s;
  109. {
  110.     perror (s);
  111.     exit (code);
  112. }
  113.  
  114. void
  115. msg (
  116. #if NeedVarargsPrototypes
  117.     char * fmt, ...)
  118. #else
  119.     fmt, a1, a2, a3)
  120.     char *fmt;
  121. #endif
  122. {
  123. #if NeedVarargsPrototypes
  124.     va_list args;
  125. #endif
  126.     if (curdir) {
  127.     fprintf (stderr, "%s:\n", curdir);
  128.     curdir = 0;
  129.     }
  130. #if NeedVarargsPrototypes
  131.     va_start(args, fmt);
  132.     vfprintf (stderr, fmt, args);
  133.     va_end(args);
  134. #else
  135.     fprintf (stderr, fmt, a1, a2, a3);
  136. #endif
  137.     putc ('\n', stderr);
  138. }
  139.  
  140. void
  141. mperror (s)
  142.     char *s;
  143. {
  144.     if (curdir) {
  145.     fprintf (stderr, "%s:\n", curdir);
  146.     curdir = 0;
  147.     }
  148.     perror (s);
  149. }
  150.  
  151.  
  152. int equivalent(lname, rname)
  153.     char *lname;
  154.     char *rname;
  155. {
  156.     char *s;
  157.  
  158.     if (!strcmp(lname, rname))
  159.     return 1;
  160.     for (s = lname; *s && (s = strchr(s, '/')); s++) {
  161.     while (s[1] == '/')
  162.         strcpy(s+1, s+2);
  163.     }
  164.     return !strcmp(lname, rname);
  165. }
  166.  
  167.  
  168. /* Recursively create symbolic links from the current directory to the "from"
  169.    directory.  Assumes that files described by fs and ts are directories. */
  170.  
  171. dodir (fn, fs, ts, rel)
  172. char *fn;            /* name of "from" directory, either absolute or
  173.                    relative to cwd */
  174. struct stat *fs, *ts;        /* stats for the "from" directory and cwd */
  175. int rel;            /* if true, prepend "../" to fn before using */
  176. {
  177.     DIR *df;
  178.     struct dirent *dp;
  179.     char buf[MAXPATHLEN + 1], *p;
  180.     char symbuf[MAXPATHLEN + 1];
  181.     struct stat sb, sc;
  182.     int n_dirs;
  183.     int symlen;
  184.     char *ocurdir;
  185.  
  186.     if ((fs->st_dev == ts->st_dev) && (fs->st_ino == ts->st_ino)) {
  187.     msg ("%s: From and to directories are identical!", fn);
  188.     return 1;
  189.     }
  190.  
  191.     if (rel)
  192.     strcpy (buf, "../");
  193.     else
  194.     buf[0] = '\0';
  195.     strcat (buf, fn);
  196.     
  197.     if (!(df = opendir (buf))) {
  198.     msg ("%s: Cannot opendir", buf);
  199.     return 1;
  200.     }
  201.  
  202.     p = buf + strlen (buf);
  203.     *p++ = '/';
  204.     n_dirs = fs->st_nlink;
  205.     while (dp = readdir (df)) {
  206.     if (dp->d_name[strlen(dp->d_name) - 1] == '~')
  207.         continue;
  208.     strcpy (p, dp->d_name);
  209.  
  210.     if (n_dirs > 0) {
  211.         if (stat (buf, &sb) < 0) {
  212.         mperror (buf);
  213.         continue;
  214.         }
  215.  
  216. #ifdef S_ISDIR
  217.         if(S_ISDIR(sb.st_mode))
  218. #else
  219.         if (sb.st_mode & S_IFDIR) 
  220. #endif
  221.         {
  222.         /* directory */
  223.         n_dirs--;
  224.         if (dp->d_name[0] == '.' &&
  225.             (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' &&
  226.                            dp->d_name[2] == '\0')))
  227.             continue;
  228.         if (!strcmp (dp->d_name, "RCS"))
  229.             continue;
  230.         if (!strcmp (dp->d_name, "SCCS"))
  231.             continue;
  232.         if (!strcmp (dp->d_name, "CVS"))
  233.             continue;
  234.         if (!strcmp (dp->d_name, "CVS.adm"))
  235.             continue;
  236.         ocurdir = rcurdir;
  237.         rcurdir = buf;
  238.         curdir = silent ? buf : (char *)0;
  239.         if (!silent)
  240.             printf ("%s:\n", buf);
  241.         if ((stat (dp->d_name, &sc) < 0) && (errno == ENOENT)) {
  242.             if (mkdir (dp->d_name, 0777) < 0 ||
  243.             stat (dp->d_name, &sc) < 0) {
  244.             mperror (dp->d_name);
  245.             curdir = rcurdir = ocurdir;
  246.             continue;
  247.             }
  248.         }
  249.         if (readlink (dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) {
  250.             msg ("%s: is a link instead of a directory", dp->d_name);
  251.             curdir = rcurdir = ocurdir;
  252.             continue;
  253.         }
  254.         if (chdir (dp->d_name) < 0) {
  255.             mperror (dp->d_name);
  256.             curdir = rcurdir = ocurdir;
  257.             continue;
  258.         }
  259.         dodir (buf, &sb, &sc, (buf[0] != '/'));
  260.         if (chdir ("..") < 0)
  261.             quiterr (1, "..");
  262.         curdir = rcurdir = ocurdir;
  263.         continue;
  264.         }
  265.     }
  266.  
  267.     /* non-directory */
  268.     symlen = readlink (dp->d_name, symbuf, sizeof(symbuf) - 1);
  269.     if (symlen >= 0) {
  270.         symbuf[symlen] = '\0';
  271.         if (!equivalent (symbuf, buf))
  272.         msg ("%s: %s", dp->d_name, symbuf);
  273.     } else if (symlink (buf, dp->d_name) < 0)
  274.         mperror (dp->d_name);
  275.     }
  276.  
  277.     closedir (df);
  278.     return 0;
  279. }
  280.  
  281.  
  282. main (ac, av)
  283. int ac;
  284. char **av;
  285. {
  286.     char *fn, *tn;
  287.     struct stat fs, ts;
  288.  
  289.     silent = 0;
  290.     if (ac > 1 && !strcmp(av[1], "-silent")) {
  291.     silent = 1;
  292.     }
  293.     if (ac < silent + 2 || ac > silent + 3)
  294.     quit (1, "usage: %s [-silent] fromdir [todir]", av[0]);
  295.  
  296.     fn = av[silent + 1];
  297.     if (ac == silent + 3)
  298.     tn = av[silent + 2];
  299.     else
  300.     tn = ".";
  301.  
  302.     /* to directory */
  303.     if (stat (tn, &ts) < 0)
  304.     quiterr (1, tn);
  305. #ifdef S_ISDIR
  306.     if (!(S_ISDIR(ts.st_mode)))
  307. #else
  308.     if (!(ts.st_mode & S_IFDIR))
  309. #endif
  310.     quit (2, "%s: Not a directory", tn);
  311.     if (chdir (tn) < 0)
  312.     quiterr (1, tn);
  313.  
  314.     /* from directory */
  315.     if (stat (fn, &fs) < 0)
  316.     quiterr (1, fn);
  317. #ifdef S_ISDIR
  318.     if (!(S_ISDIR(fs.st_mode)))
  319. #else
  320.     if (!(fs.st_mode & S_IFDIR))
  321. #endif
  322.     quit (2, "%s: Not a directory", fn);
  323.  
  324.     exit (dodir (fn, &fs, &ts, 0));
  325. }
  326.