Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 7 May 2002 21:46:11 +0000
From:      "J. Mallett" <jmallett@FreeBSD.ORG>
To:        "J. Mallett" <jmallett@FreeBSD.org>
Cc:        cvs-committers@FreeBSD.org, cvs-all@FreeBSD.org
Subject:   Re: cvs commit: src/usr.bin/sed main.c sed.1
Message-ID:  <20020507214610.GA30780@FreeBSD.ORG>
In-Reply-To: <200205071832.g47IWJO26790@freefall.freebsd.org>
References:  <200205071832.g47IWJO26790@freefall.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, May 07, 2002 at 11:32:18AM -0700, J. Mallett wrote:
> jmallett    2002/05/07 11:32:18 PDT
> 
>   Modified files:
>     usr.bin/sed          main.c sed.1 
>   Log:
>   Add a -i option to sed(1) to do inplace editing, to give us an alternative to
>   Perl for such things.  The key difference to Perl is that a backup extension
>   *MUST* be specified, because on one hand it isn't recommended to have options
>   which optionally take a parameter, and on the other hand, it'd be slightly
>   unpleasent to implement proper handling for that.
>   
>   The difference between this and the version posted to developers@ is that it
>   does handle multiple files in argv after the getopt(3) handling "correctly",
>   in that the inplace editing-specific code has been moved out to a function,
>   and that function is used beyond the first file in our linked list.
>   
>   This option has been documented as FreeBSD-specific in the manpage.
>   
>   Reviewed by:    developers@ (got feedback from: des, fanf, sobomax, roberto,
>                   obrien)
>   MFC after:      1 week

Patch to remove the "droppings" as jhb put it, if a nil extension is specified.

I'll implement a -I option which takes no arguments on top of this, if it looks
kosher.

freefall% cvs diff -u
cvs diff: Diffing .
Index: main.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/sed/main.c,v
retrieving revision 1.16
diff -u -r1.16 main.c
--- main.c      7 May 2002 18:32:18 -0000       1.16
+++ main.c      7 May 2002 21:36:18 -0000
@@ -320,6 +320,8 @@
                                fname = files->fname;
                                if ((f = fopen(fname, "r")) == NULL)
                                        err(1, "%s", fname);
+                               if (inplace != NULL && *inplace == '\0')
+                                       unlink(fname);
                        }
                        if ((c = getc(f)) != EOF) {
                                (void)ungetc(c, f);
@@ -366,6 +368,8 @@
                        fname = files->fname;
                        if ((f = fopen(fname, "r")) == NULL)
                                err(1, "%s", fname);
+                       if (inplace != NULL && *inplace == '\0')
+                               unlink(fname);
                }
        }
        (void)ungetc(c, f);
@@ -427,8 +431,16 @@
                return -1;
        }

-       strlcpy(backup, *fname, MAXPATHLEN);
-       strlcat(backup, inplace, MAXPATHLEN);
+       if (*inplace == '\0') {
+               char template[] = "/tmp/sed.XXXXXXXXXX";
+
+               if (mktemp(template) == NULL)
+                       err(1, "mktemp");
+               strlcpy(backup, template, MAXPATHLEN);
+       } else {
+               strlcpy(backup, *fname, MAXPATHLEN);
+               strlcat(backup, inplace, MAXPATHLEN);
+       }

        input = open(*fname, O_RDONLY);
        if (input == -1)
cvs diff: Diffing TEST
cvs diff: Diffing USD.doc

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020507214610.GA30780>