home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume02 / lndir < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  5.8 KB

  1. From mipos3!omepd!intelisc!littlei!uunet!husc6!necntc!ncoast!allbery Sat Feb  6 16:05:29 PST 1988
  2. Article 289 of comp.sources.misc:
  3. Path: td2cad!mipos3!omepd!intelisc!littlei!uunet!husc6!necntc!ncoast!allbery
  4. From: koreth@ssyx.ucsc.edu (Steven Grimm)
  5. Newsgroups: comp.sources.misc
  6. Subject: v02i039: Symbolic directory copier ("lndir")
  7. Message-ID: <7174@ncoast.UUCP>
  8. Date: 3 Feb 88 02:01:29 GMT
  9. Sender: allbery@ncoast.UUCP
  10. Organization: University of California, Santa Cruz
  11. Lines: 202
  12. Approved: allbery@ncoast.UUCP
  13. X-Archive: comp.sources.misc/8802/2
  14. Comp.sources.misc: Volume 2, Issue 39
  15. Submitted-By: "Steven Grimm" <koreth@ssyx.ucsc.edu>
  16. Archive-Name: lndir
  17.  
  18. Comp.sources.misc: Volume 2, Issue 39
  19. Submitted-By: "Steven Grimm" <koreth@ssyx.ucsc.edu>
  20. Archive-Name: lndir
  21.  
  22. This is my first posting to comp.sources.unix; I assume that a posting here
  23. will go to the moderator.  If anyone gets this and I'm doing something wrong,
  24. let me know.  Thanks.
  25.  
  26. [Yes, r$ is back, and bouncing stuff as readily as ever!  ;-)  ++bsa]
  27.  
  28.  
  29. Steven Grimm
  30.  
  31. ---------- cut here ----------
  32. # This is a shell archive.  Remove anything before this line
  33. # then unpack it by saving it in a file and typing "sh file"
  34. # (Files unpacked will be owned by you and have default permissions).
  35. # This archive contains the following files:
  36. #    ./lndir.c
  37. #    ./Makefile
  38. #
  39. if `test ! -s ./lndir.c`
  40. then
  41. echo "writing ./lndir.c"
  42. cat > ./lndir.c << '\Rogue\Monster\'
  43. #include <stdio.h>
  44. #include <sys/types.h>
  45. #include <sys/dir.h>
  46. #include <sys/stat.h>
  47. char *rindex();
  48.  
  49. /*
  50. ** LNDIR
  51. **
  52. ** Written by Steven Grimm (koreth@ssyx.ucsc.edu), 11-9-87
  53. **
  54. ** A safe way to duplicate a directory structure elsewhere on the filesystem.
  55. ** It's necessary because a cd into a straight symbolic link actually changes
  56. ** to the directory pointed to by the link, which can be dangerous if the
  57. ** link is in a sensitive area of the filesystem.  LNDIR recursively recreates
  58. ** a directory structure, making symbolic links to all the files in the
  59. ** directory.
  60. **
  61. ** This is really pretty ugly, but was the only way to solve a few problems
  62. ** we were having.
  63. **
  64. ** Usage: lndir srcdir [destdir]
  65. **   Where destdir is the name that you want the source directory to have
  66. **   in its new location -- that is, to link /usr/src/foo to /usr/a/xxx/foo,
  67. **   you'd do
  68. **   % lndir /usr/src/foo /usr/a/xxx/foo
  69. **   If no destination directory is specified, the new directory is given
  70. **   the same name as its original and placed in the current directory, so
  71. **   the above is equivalent to
  72. **   % cd /usr/a/xxx
  73. **   % lndir /usr/src/foo
  74. */
  75.  
  76. int errno;        /* System error number storage. */
  77. char errfile[1024];    /* Filename that caused the error. */
  78.  
  79. main(argc, argv)
  80. char **argv;
  81. {
  82.     char srcdir[1024], destdir[1024];
  83.  
  84.     if (argc < 2 || argc > 3)
  85.     {
  86.         printf("Usage: %s srcdir [destdir]\n", argv[0]);
  87.         exit(-1);
  88.     }
  89.  
  90.     strcpy(srcdir, argv[1]);
  91.     if (argc == 2)
  92.     {
  93.         char *sdirname;
  94.  
  95.         strcpy(destdir, "./");
  96.         sdirname = rindex(srcdir, '/');
  97.         if (! sdirname)
  98.         {
  99.             printf("Can't copy a directory to itself.\n");
  100.             exit(-1);
  101.         }
  102.         strcat(destdir, sdirname+1);
  103.     }
  104.     else
  105.         strcpy(destdir, argv[2]);
  106.     
  107.     if ((errno = copydir(srcdir, destdir)) < 0)
  108.     {
  109.         char error[2048];
  110.         sprintf(error, "File %s", errfile);
  111.         perror(error);
  112.     }
  113.     exit(errno);
  114. }
  115.  
  116. /*
  117. ** Create a copy of the source directory in the destination directory, and
  118. ** create symbolic links to all the files there.  If any of the files are
  119. ** directories themselves, recursively copy them into the destination
  120. ** directory using the same procedure (which is what recursive means,
  121. ** isn't it?)
  122. */
  123. copydir(s, d)
  124. char *s, *d;
  125. {
  126.     DIR        *dptr;
  127.     struct direct    *file;
  128.     struct stat    sbuf, sourcedir;
  129.  
  130.     printf("Copying %s to %s\n", s, d);
  131.  
  132.     if (stat(s, &sourcedir) == -1)
  133.     {
  134.         strcpy(errfile, s);
  135.         return errno;
  136.     }
  137.  
  138.     if ((dptr = opendir(s)) == NULL)
  139.     {
  140.         strcpy(errfile, s);
  141.         return errno;
  142.     }
  143.  
  144. /* The new directory is created with mode 700, so we can write to it (just in
  145.    case the source directory isn't writeable, or umask is set to some weird
  146.    value.)  It's chmodded to its original value after its contents have been
  147.    copied. */
  148.     if (mkdir(d, 0700) == -1)
  149.     {
  150.         strcpy(errfile, d);
  151.         return errno;
  152.     }
  153.  
  154.     while (file = readdir(dptr))
  155.     {
  156.         char    srcfile[1024], destfile[1024];
  157.  
  158.         strcpy(srcfile, s);
  159.         strcat(srcfile, "/");
  160.         strcat(srcfile, file->d_name);
  161.         strcpy(destfile, d);
  162.         strcat(destfile, "/");
  163.         strcat(destfile, file->d_name);
  164.         if (stat(srcfile, &sbuf) == -1)
  165.         {
  166.             strcpy(errfile, srcfile);
  167.             return errno;
  168.         }
  169.         if (sbuf.st_mode & S_IFDIR)    /* Is this a directory? */
  170.         {
  171.             if (file->d_name[0] == '.' &&    /* Is it . or ..? */
  172.                 (file->d_name[1] == '\0' ||
  173.                  (file->d_name[1] == '.' &&
  174.                   file->d_name[2] == '\0')))
  175.                 continue;
  176.             if (errno = copydir(srcfile, destfile))
  177.                 return errno;
  178.         }
  179.         else
  180.             if (symlink(srcfile, destfile) == -1)
  181.             {
  182.                 strcpy(errfile, srcfile);
  183.                 return errno;
  184.             }
  185.     }
  186.     if (chmod(d, sourcedir.st_mode & 07777) == -1)
  187.     {
  188.         strcpy(errfile, d);
  189.         return errno;
  190.     }
  191.     return 0;
  192. }
  193.  
  194. \Rogue\Monster\
  195. else
  196.   echo "will not over write ./lndir.c"
  197. fi
  198. if `test ! -s ./Makefile`
  199. then
  200. echo "writing ./Makefile"
  201. cat > ./Makefile << '\Rogue\Monster\'
  202. CFLAGS = -O
  203.  
  204. lndir: lndir.c
  205.     $(CC) $(CFLAGS) lndir.c -o lndir
  206. \Rogue\Monster\
  207. else
  208.   echo "will not over write ./Makefile"
  209. fi
  210. echo "Finished archive 1 of 1"
  211. # if you want to concatenate archives, remove anything after this line
  212. exit
  213. +New! Improved! Now 100% Artificial-+-+-----------------------------------+
  214. |#   #  @@@  ****  &&&&& $$$$$ %   %| |Steven Grimm                       |
  215. |#  #  @   @ *   * &       $   %   %+-+ ARPA: koreth@ucscb.ucsc.edu       |
  216. |###   @   @ ****  &&&&    $   %%%%%| | UUCP: ...!ucbvax!ucscc!ssyx!koreth|
  217. |#  #  @   @ * *   &       $   %   %+-+     ______________________________|
  218. |#   #  @@@  *  ** &&&&&   $   %   %| |     |"Let's see what's out there."|
  219. +-----with NutraSour(TM)!  No natural colors or preservatives!------------+
  220.  
  221.  
  222.