mv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Copyright (C) 1986 Free Software Foundation, Inc.
  2. NO WARRANTY
  3. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  4. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  5. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  6. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  7. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  8. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  10. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  11. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  12. CORRECTION.
  13. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  14. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  15. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  16. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  17. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  18. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  19. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  20. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  21. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  22. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  23. GENERAL PUBLIC LICENSE TO COPY
  24. 1. You may copy and distribute verbatim copies of this source file
  25. as you receive it, in any medium, provided that you conspicuously
  26. and appropriately publish on each copy a valid copyright notice
  27. "Copyright (C) 1986 Free Software Foundation, Inc"; and include following the
  28. copyright notice a verbatim copy of the above disclaimer of warranty
  29. and of this License.
  30. 2. You may modify your copy or copies of this source file or
  31. any portion of it, and copy and distribute such modifications under
  32. the terms of Paragraph 1 above, provided that you also do the following:
  33. a) cause the modified files to carry prominent notices stating
  34. that you changed the files and the date of any change; and
  35. b) cause the whole of any work that you distribute or publish,
  36. that in whole or in part contains or is a derivative of this
  37. program or any part thereof, to be freely distributed
  38. and licensed to all third parties on terms identical to those
  39. contained in this License Agreement (except that you may choose
  40. to grant more extensive warranty protection to third parties,
  41. at your option).
  42. 3. You may copy and distribute this program or any portion of it in
  43. compiled, executable or object code form under the terms of Paragraphs
  44. 1 and 2 above provided that you do the following:
  45. a) cause each such copy to be accompanied by the
  46. corresponding machine-readable source code, which must
  47. be distributed under the terms of Paragraphs 1 and 2 above; or,
  48. b) cause each such copy to be accompanied by a
  49. written offer, with no time limit, to give any third party
  50. free (except for a nominal shipping charge) a machine readable
  51. copy of the corresponding source code, to be distributed
  52. under the terms of Paragraphs 1 and 2 above; or,
  53. c) in the case of a recipient of this program in compiled, executable
  54. or object code form (without the corresponding source code) you
  55. shall cause copies you distribute to be accompanied by a copy
  56. of the written offer of source code which you received along
  57. with the copy you received.
  58. 4. You may not copy, sublicense, distribute or transfer this program
  59. except as expressly provided under this License Agreement. Any attempt
  60. otherwise to copy, sublicense, distribute or transfer this program is void and
  61. your rights to use the program under this License agreement shall be
  62. automatically terminated. However, parties who have received computer
  63. software programs from you with this License Agreement will not have
  64. their licenses terminated so long as such parties remain in full compliance.
  65. */
  66. /*
  67. * Mv. Usage is either `mv f1 f2' or `mv f1 f2 ... dir'. In either case,
  68. * two options, -i and -f, which must be placed before all files, may be
  69. * specified. The null option, -, indicates that no options, only files,
  70. * follow and may be used to specify file names beginning with a - sign.
  71. * The -i option causes mv to require confirmation from the user before
  72. * performing any move which would destroy an existing file. The -f option,
  73. * overriding the -i option, causes mv to assume a 'y' answer to all
  74. * questions it would normally ask (and not ask the questions). There are
  75. * two questions:
  76. *
  77. * Move f1 (removing f2)?
  78. * Override mode 0nnn for fn?
  79. *
  80. * The first one is caused by the -i option, the second by a move which would
  81. * destroy an existing file when the existing file's mode prohibits writing.
  82. * This test is slightly different from the test 4.2bsd mv makes; 4.2 mv
  83. * merely checks whether the user can write the file. This actually checks
  84. * the write bits in the mode. The difference shows when super-user tries
  85. * to mv onto a file with no write bits set.
  86. *
  87. * Author Mike Parker
  88. */
  89. /*
  90. * This program assumes the following:
  91. *
  92. * errno, stat(), access(), rename() semantics as in 4.2
  93. */
  94. #include <stdio.h>
  95. #include <errno.h>
  96. #include <sys/types.h>
  97. #include <sys/stat.h>
  98. #include <sys/file.h>
  99. char *pgm;
  100. int interactive; /* the -i option */
  101. int force; /* the -f option */
  102. int nargs; /* number of arguments to process */
  103. char **args; /* the remaining arguments */
  104. /* strip off (and interpret) any leading options from the arguments */
  105. get_options()
  106. {
  107. interactive = 0;
  108. force = 0;
  109. while (1)
  110. {
  111. if (**args != '-')
  112. return;
  113. if (*++*args == '\0')
  114. {
  115. args ++;
  116. nargs --;
  117. return;
  118. }
  119. for (; **args; ++*args)
  120. switch (**args)
  121. {
  122. case 'f':
  123. force = 1;
  124. break;
  125. case 'i':
  126. interactive = 1;
  127. break;
  128. default:
  129. fprintf (stderr, "%s: bad flag -%c\n", pgm, **args);
  130. break;
  131. }
  132. args ++;
  133. nargs --;
  134. }
  135. }
  136. /* check whether the file is a directory */
  137. int
  138. isdir (fn)
  139. char *fn;
  140. {
  141. struct stat stb;
  142. return (stat (fn, &stb) >= 0 &&
  143. (stb.st_mode & S_IFMT) == S_IFDIR);
  144. }
  145. main (ac, av)
  146. int ac;
  147. char **av;
  148. {
  149. pgm = *av++;
  150. ac --;
  151. if (*av == 0)
  152. {
  153. usage ();
  154. exit (0);
  155. }
  156. args = av;
  157. nargs = ac;
  158. get_options ();
  159. if (nargs < 2) /* too few arguments */
  160. {
  161. usage ();
  162. exit (1);
  163. }
  164. if ((nargs > 2) && !isdir (args[nargs-1])) /* >2 args and last not dir */
  165. {
  166. fprintf (stderr,
  167. "%s: when moving multiple files, last argument must be a directory",
  168. pgm);
  169. exit (1);
  170. }
  171. for (; nargs > 1; args++, nargs--) /* move each arg but last onto the last */
  172. movefile (args[0], args[nargs-1]);
  173. }
  174. /* Move `from' onto `to'. Handles case when to is a directory. */
  175. movefile (from, to)
  176. char *from;
  177. char *to;
  178. {
  179. if (isdir (to)) /* target is a directory, build full target filename */
  180. {
  181. char *cp;
  182. char *newto;
  183. char *rindex();
  184. int l;
  185. cp = rindex (from, '/');
  186. if (cp)
  187. cp++;
  188. else
  189. cp = from;
  190. l = strlen (to) + 1 + strlen (cp) + 1;
  191. newto = (char *) alloca (l);
  192. sprintf (newto, "%s/%s", to, cp);
  193. to = newto;
  194. }
  195. do_move (from, to); /* perform the move */
  196. }
  197. /* Used for confirmation of the move in various cases. Reads one line and
  198. returns true iff that line begins with y or Y. */
  199. int
  200. yes()
  201. {
  202. char c;
  203. int rv;
  204. c = getchar ();
  205. rv = (c == 'y') || (c == 'Y');
  206. while (c != '\n')
  207. c = getchar();
  208. return rv;
  209. }
  210. /* Perform a move. Handles cross-filesystem moves. */
  211. do_move (from, to)
  212. char *from;
  213. char *to;
  214. {
  215. extern int errno;
  216. if (access (to, F_OK) >= 0) /* target exists */
  217. {
  218. struct stat stb;
  219. if (interactive && !force)
  220. {
  221. printf ("Move %s (removing %s)? ", from, to);
  222. if (! yes ())
  223. return;
  224. }
  225. if (stat (to, &stb) < 0) /* we can't stat it?!? */
  226. {
  227. fprintf (stderr, "%s: strange error in stat() on %s (%s not moved): ",
  228. pgm, to, from);
  229. perror (0);
  230. return;
  231. }
  232. /* check for write access to target */
  233. if ((access (to, W_OK) < 0 ||
  234. (stb.st_mode & ((S_IWRITE>>6)*0111)) == 0)
  235. && !force )
  236. {
  237. printf ("Override mode %04o for %s? ", stb.st_mode & 0777f ,to);
  238. if (! yes ())
  239. return;
  240. }
  241. }
  242. if (rename (from, to) < 0)
  243. {
  244. if (errno == EXDEV) /* rename() failed on cross-filesystem link */
  245. {
  246. int ifd;
  247. int ofd;
  248. struct stat stb;
  249. char buf[1024];
  250. int len;
  251. /* do the following to perform the move:
  252. unlink the target
  253. open both files
  254. copy the permission bits from the source to the target
  255. copy the source to the target
  256. close both files
  257. if no error so far, unlink the source
  258. */
  259. if (unlink (to) < 0)
  260. {
  261. fprintf (stderr, "%s: cannot remove %s: ", pgm, to);
  262. perror (0);
  263. return;
  264. }
  265. ifd = open (from, O_RDONLY, 0);
  266. if (ifd < 0 )
  267. {
  268. fprintf (stderr, "%s: cannot read %s: ", pgm, from);
  269. perror (0);
  270. return;
  271. }
  272. ofd = open (to, O_WRONLY|O_CREAT|O_TRUNC, 0777);
  273. if (ofd < 0 )
  274. {
  275. close (ifd);
  276. fprintf (stderr, "%s: cannot create %s: ", pgm, to);
  277. perror (0);
  278. return;
  279. }
  280. fstat (ifd, &stb); /* can't fail if given good args */
  281. fchmod (ofd, stb.st_mode & 0777); /* again, can't fail */
  282. while ((len = read (ifd, buf, sizeof (buf))) > 0)
  283. {
  284. int wrote = 0;
  285. char *bp = buf;
  286. do
  287. {
  288. wrote = write (ofd, bp += wrote, len);
  289. if (wrote < 0)
  290. {
  291. fprintf (stderr,
  292. "%s: error writing %s while copying from %s, move not completed: ",
  293. pgm, to, from);
  294. perror (0);
  295. close (ifd);
  296. close (ofd);
  297. return;
  298. }
  299. } while ((len -= wrote) > 0);
  300. }
  301. if (len < 0)
  302. {
  303. fprintf (stderr,
  304. "%s: error reading %s while copying to %s, original not removed: ",
  305. pgm, from, to);
  306. perror (0);
  307. close (ifd);
  308. close (ofd);
  309. return;
  310. }
  311. close (ifd);
  312. close (ofd);
  313. if (unlink (from) < 0)
  314. {
  315. fprintf (stderr,
  316. "%s: cannot remove %s after successful copy to %s: ",
  317. pgm, from, to);
  318. perror (0);
  319. return;
  320. }
  321. }
  322. else
  323. {
  324. fprintf(stderr,"%s: %s not moved to %s: ",pgm,from,to);
  325. perror(0);
  326. }
  327. }
  328. }
  329. /* called to print out the usage message */
  330. usage()
  331. {
  332. fprintf (stderr,
  333. "Usage: %s [-if] [-] f1 f2 or %s [-if] [-] f1 ... fn dir\n",
  334. pgm, pgm);
  335. }