add SIGINFO for mv(1), largely based upon the support in cp(1).

This commit is contained in:
mrg 2016-02-28 10:59:29 +00:00
parent 52ae8af955
commit 0f6eb69275

View File

@ -1,4 +1,4 @@
/* $NetBSD: mv.c,v 1.44 2015/03/02 03:17:24 enami Exp $ */
/* $NetBSD: mv.c,v 1.45 2016/02/28 10:59:29 mrg Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
#if 0
static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
#else
__RCSID("$NetBSD: mv.c,v 1.44 2015/03/02 03:17:24 enami Exp $");
__RCSID("$NetBSD: mv.c,v 1.45 2016/02/28 10:59:29 mrg Exp $");
#endif
#endif /* not lint */
@ -67,12 +67,20 @@ __RCSID("$NetBSD: mv.c,v 1.44 2015/03/02 03:17:24 enami Exp $");
static int fflg, iflg, vflg;
static int stdin_ok;
static sig_atomic_t pinfo;
static int copy(char *, char *);
static int do_move(char *, char *);
static int fastcopy(char *, char *, struct stat *);
__dead static void usage(void);
static void
progress(int sig __unused)
{
pinfo++;
}
int
main(int argc, char *argv[])
{
@ -109,6 +117,8 @@ main(int argc, char *argv[])
stdin_ok = isatty(STDIN_FILENO);
(void)signal(SIGINFO, progress);
/*
* 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.
@ -262,7 +272,9 @@ fastcopy(char *from, char *to, struct stat *sbp)
#endif
static blksize_t blen;
static char *bp;
int nread, from_fd, to_fd;
int from_fd, to_fd;
ssize_t nread;
off_t total = 0;
if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
warn("%s", from);
@ -281,11 +293,21 @@ fastcopy(char *from, char *to, struct stat *sbp)
(void)close(to_fd);
return (1);
}
while ((nread = read(from_fd, bp, blen)) > 0)
while ((nread = read(from_fd, bp, blen)) > 0) {
if (write(to_fd, bp, nread) != nread) {
warn("%s", to);
goto err;
}
total += nread;
if (pinfo) {
int pcent = (int)((100.0 * total) / sbp->st_size);
pinfo = 0;
(void)fprintf(stderr, "%s => %s %llu/%llu bytes %d%% "
"written\n", from, to, (unsigned long long)total,
(unsigned long long)sbp->st_size, pcent);
}
}
if (nread < 0) {
warn("%s", from);
err: if (unlink(to))