NetBSD/bin/mv/mv.c

382 lines
9.5 KiB
C
Raw Normal View History

/* $NetBSD: mv.c,v 1.28 2002/12/26 21:37:17 jrf Exp $ */
1995-03-21 12:01:59 +03:00
1993-03-21 12:45:37 +03:00
/*
1994-09-22 13:24:46 +04:00
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* This code is derived from software contributed to Berkeley by
* Ken Smith of The State University of New York at Buffalo.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-07-20 23:07:49 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1997-07-20 23:07:49 +04:00
__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
1995-03-21 12:01:59 +03:00
#if 0
static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: mv.c,v 1.28 2002/12/26 21:37:17 jrf Exp $");
1995-03-21 12:01:59 +03:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/stat.h>
1994-09-22 13:24:46 +04:00
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <errno.h>
1994-09-22 13:24:46 +04:00
#include <fcntl.h>
#include <grp.h>
#include <locale.h>
#include <pwd.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1994-09-22 13:24:46 +04:00
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#include "pathnames.h"
int fflg, iflg, vflg;
int stdin_ok;
1993-03-21 12:45:37 +03:00
int copy(char *, char *);
int do_move(char *, char *);
int fastcopy(char *, char *, struct stat *);
void usage(void);
int main(int, char *[]);
1994-09-22 13:24:46 +04:00
int
main(int argc, char *argv[])
1993-03-21 12:45:37 +03:00
{
int baselen, ch, len, rval;
1997-01-09 19:44:06 +03:00
char *p, *endp;
1993-03-21 12:45:37 +03:00
struct stat sb;
char path[MAXPATHLEN + 1];
setprogname(argv[0]);
1998-07-28 08:01:03 +04:00
(void)setlocale(LC_ALL, "");
while ((ch = getopt(argc, argv, "ifv")) != -1)
1994-09-22 13:24:46 +04:00
switch (ch) {
1993-03-21 12:45:37 +03:00
case 'i':
fflg = 0;
1993-03-21 12:45:37 +03:00
iflg = 1;
break;
case 'f':
iflg = 0;
1993-03-21 12:45:37 +03:00
fflg = 1;
break;
case 'v':
vflg = 1;
break;
1993-03-21 12:45:37 +03:00
case '?':
default:
usage();
}
argc -= optind;
1993-03-21 12:45:37 +03:00
argv += optind;
if (argc < 2)
usage();
stdin_ok = isatty(STDIN_FILENO);
1993-03-21 12:45:37 +03:00
/*
* If the stat on the target fails or the target isn't a directory,
* try the move. More than 2 arguments is an error in this case.
*/
if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
if (argc > 2)
usage();
exit(do_move(argv[0], argv[1]));
}
/* It's a directory, move each file into it. */
(void)strcpy(path, argv[argc - 1]);
baselen = strlen(path);
endp = &path[baselen];
*endp++ = '/';
++baselen;
1994-09-22 13:24:46 +04:00
for (rval = 0; --argc; ++argv) {
p = *argv + strlen(*argv) - 1;
while (*p == '/' && p != *argv)
*p-- = '\0';
1994-09-22 13:24:46 +04:00
if ((p = strrchr(*argv, '/')) == NULL)
1993-03-21 12:45:37 +03:00
p = *argv;
else
++p;
1994-09-22 13:24:46 +04:00
if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
warnx("%s: destination pathname too long", *argv);
rval = 1;
} else {
memmove(endp, p, len + 1);
if (do_move(*argv, path))
rval = 1;
1993-03-21 12:45:37 +03:00
}
}
1994-09-22 13:24:46 +04:00
exit(rval);
1998-07-28 09:31:22 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
1994-09-22 13:24:46 +04:00
int
do_move(char *from, char *to)
1993-03-21 12:45:37 +03:00
{
struct stat sb;
1994-09-22 13:24:46 +04:00
char modep[15];
1993-03-21 12:45:37 +03:00
1994-09-22 13:24:46 +04:00
/*
* (1) If the destination path exists, the -f option is not specified
* and either of the following conditions are true:
*
1999-12-14 20:30:11 +03:00
* (a) The permissions of the destination path do not permit
* writing and the standard input is a terminal.
* (b) The -i option is specified.
*
1994-09-22 13:24:46 +04:00
* the mv utility shall write a prompt to standard error and
* read a line from standard input. If the response is not
* affirmative, mv shall do nothing more with the current
* source file...
1993-03-21 12:45:37 +03:00
*/
if (!fflg && !access(to, F_OK)) {
1994-09-22 13:24:46 +04:00
int ask = 1;
int ch;
1993-03-21 12:45:37 +03:00
if (iflg) {
if (access(from, F_OK)) {
warn("rename %s", from);
return (1);
}
1993-03-21 12:45:37 +03:00
(void)fprintf(stderr, "overwrite %s? ", to);
} else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
if (access(from, F_OK)) {
warn("rename %s", from);
return (1);
}
1994-09-22 13:24:46 +04:00
strmode(sb.st_mode, modep);
(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
modep + 1, modep[9] == ' ' ? "" : " ",
user_from_uid(sb.st_uid, 0),
group_from_gid(sb.st_gid, 0), to);
} else
ask = 0;
1993-03-21 12:45:37 +03:00
if (ask) {
if ((ch = getchar()) != EOF && ch != '\n')
while (getchar() != '\n');
if (ch != 'y' && ch != 'Y')
1994-09-22 13:24:46 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
}
1994-09-22 13:24:46 +04:00
/*
* (2) If rename() succeeds, mv shall do nothing more with the
* current source file. If it fails for any other reason than
* EXDEV, mv shall write a diagnostic message to the standard
* error and do nothing more with the current source file.
*
* (3) If the destination path exists, and it is a file of type
* directory and source_file is not a file of type directory,
* or it is a file not of type directory, and source file is
1994-09-22 13:24:46 +04:00
* a file of type directory, mv shall write a diagnostic
* message to standard error, and do nothing more with the
* current source file...
*/
if (!rename(from, to)) {
if (vflg)
printf("%s -> %s\n", from, to);
1994-09-22 13:24:46 +04:00
return (0);
}
1993-03-21 12:45:37 +03:00
if (errno != EXDEV) {
1994-09-22 13:24:46 +04:00
warn("rename %s to %s", from, to);
return (1);
1993-03-21 12:45:37 +03:00
}
1994-09-22 13:24:46 +04:00
/*
* (4) If the destination path exists, mv shall attempt to remove it.
* If this fails for any reason, mv shall write a diagnostic
* message to the standard error and do nothing more with the
* current source file...
*/
if (!lstat(to, &sb)) {
if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
1994-09-22 13:24:46 +04:00
warn("can't remove %s", to);
return (1);
}
}
1994-09-22 13:24:46 +04:00
/*
* (5) The file hierarchy rooted in source_file shall be duplicated
* as a file hierarchy rooted in the destination path...
1993-03-21 12:45:37 +03:00
*/
if (lstat(from, &sb)) {
1994-09-22 13:24:46 +04:00
warn("%s", from);
return (1);
1993-03-21 12:45:37 +03:00
}
1994-09-22 13:24:46 +04:00
return (S_ISREG(sb.st_mode) ?
1993-03-21 12:45:37 +03:00
fastcopy(from, to, &sb) : copy(from, to));
}
1994-09-22 13:24:46 +04:00
int
fastcopy(char *from, char *to, struct stat *sbp)
1993-03-21 12:45:37 +03:00
{
struct timeval tval[2];
static u_int blen;
static char *bp;
1997-01-09 19:44:06 +03:00
int nread, from_fd, to_fd;
1993-03-21 12:45:37 +03:00
if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
1994-09-22 13:24:46 +04:00
warn("%s", from);
return (1);
1993-03-21 12:45:37 +03:00
}
1994-09-22 13:24:46 +04:00
if ((to_fd =
open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
warn("%s", to);
1993-03-21 12:45:37 +03:00
(void)close(from_fd);
1994-09-22 13:24:46 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
warn(NULL);
1994-09-22 13:24:46 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
while ((nread = read(from_fd, bp, blen)) > 0)
if (write(to_fd, bp, nread) != nread) {
1994-09-22 13:24:46 +04:00
warn("%s", to);
1993-03-21 12:45:37 +03:00
goto err;
}
if (nread < 0) {
1994-09-22 13:24:46 +04:00
warn("%s", from);
err: if (unlink(to))
warn("%s: remove", to);
1993-03-21 12:45:37 +03:00
(void)close(from_fd);
(void)close(to_fd);
1994-09-22 13:24:46 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
(void)close(from_fd);
#ifdef BSD4_4
TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
#else
tval[0].tv_sec = sbp->st_atime;
tval[1].tv_sec = sbp->st_mtime;
tval[0].tv_usec = 0;
tval[1].tv_usec = 0;
#endif
1998-11-04 21:56:53 +03:00
#ifdef __SVR4
if (utimes(to, tval))
#else
if (futimes(to_fd, tval))
1998-11-04 21:56:53 +03:00
#endif
warn("%s: set times", to);
if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
if (errno != EPERM)
warn("%s: set owner/group", to);
sbp->st_mode &= ~(S_ISUID | S_ISGID);
}
1994-09-22 13:24:46 +04:00
if (fchmod(to_fd, sbp->st_mode))
warn("%s: set mode", to);
if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
1993-03-21 12:45:37 +03:00
1994-09-22 13:24:46 +04:00
if (close(to_fd)) {
warn("%s", to);
return (1);
}
if (unlink(from)) {
warn("%s: remove", from);
return (1);
}
if (vflg)
printf("%s -> %s\n", from, to);
1994-09-22 13:24:46 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1994-09-22 13:24:46 +04:00
int
copy(char *from, char *to)
1993-03-21 12:45:37 +03:00
{
int pid, status;
1994-09-22 13:24:46 +04:00
if ((pid = vfork()) == 0) {
execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", from, to, NULL);
1994-09-22 13:24:46 +04:00
warn("%s", _PATH_CP);
1993-03-21 12:45:37 +03:00
_exit(1);
}
1994-09-22 13:24:46 +04:00
if (waitpid(pid, &status, 0) == -1) {
warn("%s: waitpid", _PATH_CP);
return (1);
}
if (!WIFEXITED(status)) {
warn("%s: did not terminate normally", _PATH_CP);
return (1);
}
if (WEXITSTATUS(status)) {
warn("%s: terminated with %d (non-zero) status",
_PATH_CP, WEXITSTATUS(status));
return (1);
}
1993-03-21 12:45:37 +03:00
if (!(pid = vfork())) {
execl(_PATH_RM, "mv", "-rf", from, NULL);
1994-09-22 13:24:46 +04:00
warn("%s", _PATH_RM);
1993-03-21 12:45:37 +03:00
_exit(1);
}
1994-09-22 13:24:46 +04:00
if (waitpid(pid, &status, 0) == -1) {
warn("%s: waitpid", _PATH_RM);
return (1);
}
if (!WIFEXITED(status)) {
warn("%s: did not terminate normally", _PATH_RM);
return (1);
}
if (WEXITSTATUS(status)) {
warn("%s: terminated with %d (non-zero) status",
_PATH_RM, WEXITSTATUS(status));
return (1);
}
return (0);
1993-03-21 12:45:37 +03:00
}
1994-09-22 13:24:46 +04:00
void
usage(void)
1993-03-21 12:45:37 +03:00
{
(void)fprintf(stderr, "usage: %s [-fiv] source target\n"
" %s [-fiv] source ... directory\n", getprogname(),
getprogname());
1993-03-21 12:45:37 +03:00
exit(1);
1998-07-28 09:31:22 +04:00
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}