static char *sccsid
= "@(#)mv.c 4.13 (Berkeley) 83/06/30";
#define ISDIR(st) (((st).st_mode&S_IFMT) == S_IFDIR)
#define ISLNK(st) (((st).st_mode&S_IFMT) == S_IFLNK)
#define ISREG(st) (((st).st_mode&S_IFMT) == S_IFREG)
(((st).st_mode&S_IFMT) == S_IFCHR || ((st).st_mode&S_IFMT) == S_IFBLK)
int iflag
= 0; /* interactive mode */
int fflag
= 0; /* force overwriting */
while (argc
> 1 && *argv
[1] == '-') {
* all files following a null option
* are considered file names
while (*++arg
!= '\0') switch (*arg
) {
if (stat(dest
, &s2
) >= 0 && ISDIR(s2
)) {
for (i
= 1; i
< argc
-1; i
++)
r
|= movewithshortname(argv
[i
], dest
);
r
= move(argv
[1], argv
[2]);
"usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (`fn' is a file or directory)\n");
movewithshortname(src
, dest
)
register char *shortname
;
char target
[MAXPATHLEN
+ 1];
if (strlen(dest
) + strlen(shortname
) > MAXPATHLEN
- 1) {
error("%s/%s: pathname too long", dest
,
sprintf(target
, "%s/%s", dest
, shortname
);
return (move(src
, target
));
if (lstat(source
, &s1
) < 0) {
error("cannot access %s", source
);
* First, try to rename source to destination.
* The only reason we continue on failure is if
* the move is on a nondirectory and not across
targetexists
= lstat(target
, &s2
) >= 0;
if (iflag
&& !fflag
&& query("remove %s? ", target
) == 0)
if (s1
.st_dev
== s2
.st_dev
&& s1
.st_ino
== s2
.st_ino
) {
error("%s and %s are identical", source
, target
);
if (access(target
, 2) < 0 && !fflag
&& isatty(fileno(stdin
))) {
if (query("override protection %o for %s? ",
s2
.st_mode
& MODEBITS
, target
) == 0)
if (rename(source
, target
) >= 0)
Perror2(source
, "rename");
error("can't mv directories across file systems");
if (targetexists
&& unlink(target
) < 0) {
error("cannot unlink %s", target
);
* File can't be renamed, try to recreate the symbolic
* link or special device, or copy the file wholesale
if (readlink(source
, symln
, sizeof (symln
)) < 0) {
m
= umask(~(s1
.st_mode
& MODEBITS
));
if (symlink(symln
, target
) < 0) {
if (mknod(target
, s1
.st_mode
, s1
.st_rdev
) < 0) {
/* kludge prior to utimes */
(void) utime(target
, tv
);
execl("/bin/cp", "cp", source
, target
, 0);
error("cannot exec /bin/cp");
while ((c
= wait(&status
)) != i
&& c
!= -1)
/* kludge prior to utimes */
(void) utime(target
, tv
);
error("%s: unknown file type %o", source
, s1
.st_mode
);
if (unlink(source
) < 0) {
error("cannot unlink %s", source
);
fprintf(stderr
, prompt
, a1
, a2
);
while (c
!= '\n' && c
!= EOF
)
fprintf(stderr
, fmt
, a1
, a2
);
char buf
[MAXPATHLEN
+ 10];
sprintf(buf
, "mv: %s", s
);
char buf
[MAXPATHLEN
+ 20];
sprintf(buf
, "mv: %s: %s", s1
, s2
);