home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / lndir < prev    next >
Encoding:
Internet Message Format  |  1991-08-07  |  5.4 KB

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