586 sed -i should take an "optional" argument
authorYuri Pankov <yuri.pankov@nexenta.com>
Fri, 04 Nov 2011 12:59:45 +0400
changeset 13535 aaf06453aff9
parent 13534 e75aae650e7f
child 13536 bf0e4028ac3a
586 sed -i should take an "optional" argument Reviewed by: Andrew Stormont <[email protected]> Reviewed by: Kartik Mistry <[email protected]> Approved by: Gordon Ross <[email protected]>
usr/src/cmd/sed/main.c
usr/src/man/man1/sed.1
--- a/usr/src/cmd/sed/main.c	Mon May 16 06:46:51 2011 +0100
+++ b/usr/src/cmd/sed/main.c	Fri Nov 04 12:59:45 2011 +0400
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2011 Gary Mills
- * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
+ * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
  * Copyright (c) 1992 Diomidis Spinellis.
  * Copyright (c) 1992, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -41,7 +41,9 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <libgen.h>
+#include <libintl.h>
 #include <limits.h>
 #include <locale.h>
 #include <regex.h>
@@ -50,7 +52,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <libintl.h>
 
 #include "defs.h"
 #include "extern.h"
@@ -105,6 +106,11 @@
 static const char *inplace;	/* Inplace edit file extension. */
 ulong_t linenum;
 
+static const struct option lopts[] = {
+	{"in-place",	optional_argument,	NULL,	'i'},
+	{NULL,		0,			NULL,	0}
+};
+
 static void add_compunit(enum e_cut, char *);
 static void add_file(char *);
 static void usage(void);
@@ -127,14 +133,18 @@
 	fflag = 0;
 	inplace = NULL;
 
-	while ((c = getopt(argc, argv, "EI:ae:f:i:lnr")) != -1)
+	while ((c = getopt_long(argc, argv, "EI::ae:f:i::lnr", lopts, NULL)) !=
+	    -1)
 		switch (c) {
 		case 'r':		/* Gnu sed compat */
 		case 'E':
 			rflags = REG_EXTENDED;
 			break;
 		case 'I':
-			inplace = optarg;
+			if (optarg != NULL)
+				inplace = optarg;
+			else
+				inplace = "";
 			ispan = 1;	/* span across input files */
 			break;
 		case 'a':
@@ -151,7 +161,10 @@
 			add_compunit(CU_FILE, optarg);
 			break;
 		case 'i':
-			inplace = optarg;
+			if (optarg != NULL)
+				inplace = optarg;
+			else
+				inplace = "";
 			ispan = 0;	/* don't span across input files */
 			break;
 		case 'l':
@@ -192,8 +205,8 @@
 static void
 usage(void)
 {
-	(void) fputs(_("usage: sed script [-Ealn] [-i extension] [file...]\n"
-	    "       sed [-Ealn] [-i extension] [-e script]... "
+	(void) fputs(_("usage: sed script [-Ealn] [-i[extension]] [file...]\n"
+	    "       sed [-Ealn] [-i[extension]] [-e script]... "
 	    "[-f script_file]... [file...]\n"),
 	    stderr);
 	exit(1);
--- a/usr/src/man/man1/sed.1	Mon May 16 06:46:51 2011 +0100
+++ b/usr/src/man/man1/sed.1	Fri Nov 04 12:59:45 2011 +0400
@@ -1,6 +1,8 @@
 .\" Copyright (c) 1992, 1993
 .\"     The Regents of the University of California.  All rights reserved.
 .\"
+.\" Copyright 2011 Nexenta Systems, Inc. All rights reserved.
+.\"
 .\" This code is derived from software contributed to Berkeley by
 .\" the Institute of Electrical and Electronics Engineers, Inc.
 .\"
@@ -28,7 +30,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.TH SED 1 "May 24, 2009"
+.TH SED 1 "Nov 2, 2011"
 .SH NAME
 \fBsed\fP
 \- stream editor
@@ -43,8 +45,7 @@
 [\fB\-Ealnr\fP]
 [\fB\-e\fP \fIcommand\fP]
 [\fB\-f\fP \fIcommand_file\fP]
-[\fB\-I\fP \fIextension\fP]
-[\fB\-i\fP \fIextension\fP]
+[\fB\-I\fP[\fIextension\fP] | \fB\-i\fP[\fIextension\fP]]
 [\fIfile ...\fP]
 .SH DESCRIPTION
 The
@@ -97,16 +98,11 @@
 to the list of commands.
 The editing commands should each be listed on a separate line.
 .TP
-\fB\-I\fP \fIextension\fP
-Edit files in-place, saving backups with the specified
-\fIextension\fP.
-If a zero-length
-\fIextension\fP
-is given, no backup will be saved.
-It is not recommended to give a zero-length
-\fIextension\fP
-when in-place editing files, as you risk corruption or partial content
-in situations where disk space is exhausted, etc.
+\fB\-I\fP[\fIextension\fP]
+Edit files in-place, saving backups if \fIextension\fP was specified.
+It is not recommended to omit saving backups when in-place editing files,
+as you risk corruption or partial content in situations where disk
+space is exhausted, etc.
 
 Note that in-place editing with
 \fB\-I\fP
@@ -124,7 +120,7 @@
 \fB\-i\fP
 is desired.
 .TP
-\fB\-i\fP \fIextension\fP
+\fB\-i\fP[\fIextension\fP]
 Edit files in-place similarly to
 \fB\-I\fP,
 but treat each file independently from other files.