ln.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* `ln' program to create links among files.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously
  27. and appropriately publish on each copy a valid copyright notice
  28. "Copyright (C) 1986 Free Software Foundation, Inc"; and include following the
  29. copyright notice a verbatim copy of the above disclaimer of warranty
  30. and of this License.
  31. 2. You may modify your copy or copies of this source file or
  32. any portion of it, and copy and distribute such modifications under
  33. the terms of Paragraph 1 above, provided that you also do the following:
  34. a) cause the modified files to carry prominent notices stating
  35. that you changed the files and the date of any change; and
  36. b) cause the whole of any work that you distribute or publish,
  37. that in whole or in part contains or is a derivative of this
  38. program or any part thereof, to be freely distributed
  39. and licensed to all third parties on terms identical to those
  40. contained in this License Agreement (except that you may choose
  41. to grant more extensive warranty protection to third parties,
  42. at your option).
  43. 3. You may copy and distribute this program or any portion of it in
  44. compiled, executable or object code form under the terms of Paragraphs
  45. 1 and 2 above provided that you do the following:
  46. a) cause each such copy to be accompanied by the
  47. corresponding machine-readable source code, which must
  48. be distributed under the terms of Paragraphs 1 and 2 above; or,
  49. b) cause each such copy to be accompanied by a
  50. written offer, with no time limit, to give any third party
  51. free (except for a nominal shipping charge) a machine readable
  52. copy of the corresponding source code, to be distributed
  53. under the terms of Paragraphs 1 and 2 above; or,
  54. c) in the case of a recipient of this program in compiled, executable
  55. or object code form (without the corresponding source code) you
  56. shall cause copies you distribute to be accompanied by a copy
  57. of the written offer of source code which you received along
  58. with the copy you received.
  59. 4. You may not copy, sublicense, distribute or transfer this program
  60. except as expressly provided under this License Agreement. Any attempt
  61. otherwise to copy, sublicense, distribute or transfer this program is void and
  62. your rights to use the program under this License agreement shall be
  63. automatically terminated. However, parties who have received computer
  64. software programs from you with this License Agreement will not have
  65. their licenses terminated so long as such parties remain in full compliance.
  66. */
  67. /*
  68. * Ln. Usage is either `ln file1 file2' or `ln file1 file2 ... destdir'. In
  69. * either case, if the -s option is inserted just after ln then ln makes
  70. * soft links instead of hard links. In the first form, file1 is the
  71. * existing file and file2 is the name under which the link is to be
  72. * created. If file2 is omitted, the link is made in the current directory
  73. * with the same name as the last component of file1. In the second form,
  74. * the fileNs are existing files and destdir is a directory in which to
  75. * create the links. The links all have the same names as the last
  76. * component of the linked-to files.
  77. *
  78. * This shares one problem with 4.2bsd ln which it would be very nearly
  79. * impossible to remove in the general case, this problem being that if you
  80. * make soft links to multiple files where the target is a directory you can
  81. * lose, for example:
  82. *
  83. * % ln -s foo bar baz subdir
  84. * % cat subdir/foo
  85. * cat: subdir/foo: Too many levels of symbolic links
  86. * % ls -l subdir
  87. * total 3
  88. * -rw-r--r-- 1 mouse 3 Apr 10 17:46 foo -> foo
  89. * -rw-r--r-- 1 mouse 3 Apr 16 05:08 bar -> bar
  90. * -rw-r--r-- 1 mouse 3 Apr 19 03:34 baz -> baz
  91. */
  92. /*
  93. * This program assumes the following:
  94. *
  95. * stat () as in 4.2bsd, at least for determining whether or not a file
  96. * is a directory.
  97. */
  98. /* Author Mike Parker */
  99. #include <stdio.h>
  100. #include <sys/types.h>
  101. #include <sys/stat.h>
  102. int dosoft; /* true -> make soft links, false -> make hard links */
  103. char *pgm; /* name by which we were invoked (for error messages) */
  104. main (ac, av)
  105. int ac;
  106. char **av;
  107. {
  108. pgm = *av++;
  109. ac --;
  110. if (dosoft = (strcmp (*av, "-s") == 0))
  111. {
  112. av ++;
  113. ac --;
  114. }
  115. if (ac < 1)
  116. {
  117. fprintf (stderr, "Usage: %s [ -s ] old\n\
  118. or: %s [ -s ] old new\n\
  119. or: %s [ -s ] old1 ... oldN dir\n", pgm, pgm, pgm);
  120. exit (1);
  121. }
  122. if (ac == 1)
  123. dolink (*av, ".");
  124. else if (ac == 2)
  125. dolink (av[0], av[1]);
  126. else
  127. {
  128. char *where = av[--ac];
  129. if (! isdir (where))
  130. {
  131. fprintf (stderr, "%s: target must be a directory for multiple files\n",
  132. pgm);
  133. exit (1);
  134. }
  135. for (; ac; ac--, av++)
  136. dolink (*av, where);
  137. }
  138. }
  139. /* `old' should exist; link named `new' is made. Handles directory issues. */
  140. dolink (old, new)
  141. char *old;
  142. char *new;
  143. {
  144. char *malloc ();
  145. char *rindex ();
  146. int link ();
  147. int symlink ();
  148. if (isdir (new)) /* target is directory, build full filename */
  149. {
  150. char *cp;
  151. char *new_new;
  152. int l;
  153. cp = rindex (old, '/');
  154. if (cp)
  155. cp++;
  156. else
  157. cp = old;
  158. l = strlen (cp) + 1 + strlen (new) + 1;
  159. new_new = (char *) alloca (l);
  160. sprintf (new_new, "%s/%s", new, cp);
  161. new = new_new;
  162. }
  163. if ((*(dosoft ? symlink : link)) (old, new) < 0)
  164. {
  165. fprintf (stderr, "%s: cannot %slink %s to %s: ",
  166. pgm, dosoft ? "soft " : "",
  167. new, old);
  168. perror (0);
  169. }
  170. }
  171. /* check whether the file named is a directory */
  172. int
  173. isdir (fn)
  174. char *fn;
  175. {
  176. struct stat stb;
  177. if (stat (fn, &stb) < 0)
  178. return (0);
  179. return ((stb.st_mode & S_IFMT) == S_IFDIR);
  180. }