Module Name:    src
Committed By:   riastradh
Date:           Wed Jun 24 16:58:12 UTC 2020

Modified Files:
        src/bin/mv: mv.1 mv.c

Log Message:
New mv -h option.

`mv -h source target' just issues rename(source, target) without
discriminating on whether target resolves to a directory; this way
you can atomically replace a symlink to a directory.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/bin/mv/mv.1
cvs rdiff -u -r1.45 -r1.46 src/bin/mv/mv.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/mv/mv.1
diff -u src/bin/mv/mv.1:1.30 src/bin/mv/mv.1:1.31
--- src/bin/mv/mv.1:1.30	Tue Jul  4 06:50:04 2017
+++ src/bin/mv/mv.1	Wed Jun 24 16:58:12 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: mv.1,v 1.30 2017/07/04 06:50:04 wiz Exp $
+.\"	$NetBSD: mv.1,v 1.31 2020/06/24 16:58:12 riastradh Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -40,7 +40,7 @@
 .Nd move files
 .Sh SYNOPSIS
 .Nm
-.Op Fl fiv
+.Op Fl fhiv
 .Ar source target
 .Nm
 .Op Fl fiv
@@ -54,7 +54,9 @@ operand to the destination path named by
 .Ar target
 operand.
 This form is assumed when the last operand does not name an already
-existing directory.
+existing directory or when the
+.Fl h
+flag is specified.
 .Pp
 In its second form,
 .Nm
@@ -72,6 +74,11 @@ The following options are available:
 .It Fl f
 Do not prompt for confirmation before overwriting the destination
 path.
+.It Fl h
+Try to rename the source to the target irrespective of whether the
+target may resolve to a directory.
+This option allows atomically replacing a symbolic link to a
+directory.
 .It Fl i
 Causes
 .Nm

Index: src/bin/mv/mv.c
diff -u src/bin/mv/mv.c:1.45 src/bin/mv/mv.c:1.46
--- src/bin/mv/mv.c:1.45	Sun Feb 28 10:59:29 2016
+++ src/bin/mv/mv.c	Wed Jun 24 16:58:12 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: mv.c,v 1.45 2016/02/28 10:59:29 mrg Exp $ */
+/* $NetBSD: mv.c,v 1.46 2020/06/24 16:58:12 riastradh Exp $ */
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 19
 #if 0
 static char sccsid[] = "@(#)mv.c	8.2 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: mv.c,v 1.45 2016/02/28 10:59:29 mrg Exp $");
+__RCSID("$NetBSD: mv.c,v 1.46 2020/06/24 16:58:12 riastradh Exp $");
 #endif
 #endif /* not lint */
 
@@ -65,7 +65,7 @@ __RCSID("$NetBSD: mv.c,v 1.45 2016/02/28
 
 #include "pathnames.h"
 
-static int fflg, iflg, vflg;
+static int fflg, hflg, iflg, vflg;
 static int stdin_ok;
 static sig_atomic_t pinfo;
 
@@ -93,8 +93,11 @@ main(int argc, char *argv[])
 	setprogname(argv[0]);
 	(void)setlocale(LC_ALL, "");
 
-	while ((ch = getopt(argc, argv, "ifv")) != -1)
+	while ((ch = getopt(argc, argv, "hifv")) != -1)
 		switch (ch) {
+		case 'h':
+			hflg = 1;
+			break;
 		case 'i':
 			fflg = 0;
 			iflg = 1;
@@ -123,7 +126,7 @@ main(int argc, char *argv[])
 	 * 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 (hflg || stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
 		if (argc > 2)
 			usage();
 		exit(do_move(argv[0], argv[1]));
@@ -419,7 +422,7 @@ copy(char *from, char *to)
 static void
 usage(void)
 {
-	(void)fprintf(stderr, "usage: %s [-fiv] source target\n"
+	(void)fprintf(stderr, "usage: %s [-fhiv] source target\n"
 	    "       %s [-fiv] source ... directory\n", getprogname(),
 	    getprogname());
 	exit(1);

Reply via email to