2 * Copyright (c) 2011 Gary Mills
3 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 1992 Diomidis Spinellis.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Diomidis Spinellis of Imperial College, University of London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
38 #include <sys/param.h>
60 * Linked list of units (strings and files) to be compiled
63 struct s_compunit
*next
;
64 enum e_cut
{CU_FILE
, CU_STRING
} type
;
65 char *s
; /* Pointer to string or fname */
69 * Linked list pointer to compilation units and pointer to current
72 static struct s_compunit
*script
, **cu_nextp
= &script
;
75 * Linked list of files to be processed
83 * Linked list pointer to files and pointer to current
86 static struct s_flist
*files
, **fl_nextp
= &files
;
88 FILE *infile
; /* Current input file */
89 FILE *outfile
; /* Current output file */
91 int aflag
, eflag
, nflag
;
93 static int rval
; /* Exit status */
95 static int ispan
; /* Whether inplace editing spans across files */
98 * Current file and line number; line numbers restart across compilation
99 * units, but span across input files. The latter is optional if editing
102 const char *fname
; /* File name. */
103 const char *outfname
; /* Output file name */
104 static char oldfname
[PATH_MAX
]; /* Old file name (for in-place editing) */
105 static char tmpfname
[PATH_MAX
]; /* Temporary file name (for in-place editing) */
106 static const char *inplace
; /* Inplace edit file extension. */
109 static const struct option lopts
[] = {
110 {"in-place", optional_argument
, NULL
, 'i'},
114 static void add_compunit(enum e_cut
, char *);
115 static void add_file(char *);
116 static void usage(void);
117 static char *getln(FILE *, size_t *);
121 main(int argc
, char *argv
[])
126 (void) setlocale(LC_ALL
, "");
129 #define TEXT_DOMAIN "SYS_TEST"
131 (void) textdomain(TEXT_DOMAIN
);
136 while ((c
= getopt_long(argc
, argv
, "EI::ae:f:i::lnr", lopts
, NULL
)) !=
139 case 'r': /* Gnu sed compat */
141 rflags
= REG_EXTENDED
;
148 ispan
= 1; /* span across input files */
155 if (asprintf(&temp_arg
, "%s\n", optarg
) < 1)
157 add_compunit(CU_STRING
, temp_arg
);
161 add_compunit(CU_FILE
, optarg
);
168 ispan
= 0; /* don't span across input files */
171 /* On SunOS, setlinebuf "returns no useful value */
172 (void) setlinebuf(stdout
);
184 /* First usage case; script is the first arg */
185 if (!eflag
&& !fflag
&& *argv
) {
186 add_compunit(CU_STRING
, *argv
);
192 /* Continue with first and start second usage */
194 for (; *argv
; argv
++)
208 (void) fputs(_("usage: sed script [-Ealn] [-i[extension]] [file...]\n"
209 " sed [-Ealn] [-i[extension]] [-e script]... "
210 "[-f script_file]... [file...]\n"),
216 * Like fgets, but go through the chain of compilation units chaining them
217 * together. Empty strings and files are ignored.
220 cu_fgets(char *buf
, int n
, int *more
)
222 static enum {ST_EOF
, ST_FILE
, ST_STRING
} state
= ST_EOF
;
223 static FILE *f
; /* Current open file */
224 static char *s
; /* Current pointer inside string */
225 static char string_ident
[30];
231 if (script
== NULL
) {
237 switch (script
->type
) {
239 if ((f
= fopen(script
->s
, "r")) == NULL
)
240 err(1, "%s", script
->s
);
245 if (((size_t)snprintf(string_ident
,
246 sizeof (string_ident
), "\"%s\"", script
->s
)) >=
247 sizeof (string_ident
) - 1)
248 (void) strcpy(string_ident
+
249 sizeof (string_ident
) - 6, " ...\"");
250 fname
= string_ident
;
258 if ((p
= fgets(buf
, n
, f
)) != NULL
) {
260 if (linenum
== 1 && buf
[0] == '#' && buf
[1] == 'n')
266 script
= script
->next
;
271 if (linenum
== 0 && s
[0] == '#' && s
[1] == 'n')
285 if (s
== script
->s
) {
286 script
= script
->next
;
289 script
= script
->next
;
314 * Like fgets, but go through the list of files chaining them together.
315 * Set len to the length of the line.
318 mf_fgets(SPACE
*sp
, enum e_spflag spflag
)
324 static int firstfile
;
326 if (infile
== NULL
) {
328 if (files
->fname
== NULL
) {
331 _("-I or -i may not be used with stdin"));
341 if (infile
!= NULL
&& (c
= getc(infile
)) != EOF
) {
342 (void) ungetc(c
, infile
);
345 /* If we are here then either eof or no files are open yet */
346 if (infile
== stdin
) {
350 if (infile
!= NULL
) {
351 (void) fclose(infile
);
352 if (*oldfname
!= '\0') {
353 /* if there was a backup file, remove it */
354 (void) unlink(oldfname
);
356 * Backup the original. Note that hard links
357 * are not supported on all filesystems.
359 if ((link(fname
, oldfname
) != 0) &&
360 (rename(fname
, oldfname
) != 0)) {
363 (void) unlink(tmpfname
);
368 if (*tmpfname
!= '\0') {
369 if (outfile
!= NULL
&& outfile
!= stdout
)
370 if (fclose(outfile
) != 0) {
372 (void) unlink(tmpfname
);
376 if (rename(tmpfname
, fname
) != 0) {
377 /* this should not happen really! */
379 (void) unlink(tmpfname
);
394 fname
= files
->fname
;
395 if (inplace
!= NULL
) {
398 (void) strlcpy(bn
, fname
, sizeof (bn
));
399 (void) strlcpy(dn
, fname
, sizeof (dn
));
400 if (lstat(fname
, &sb
) != 0)
402 if (!(sb
.st_mode
& S_IFREG
))
403 fatal(_("in-place editing only "
404 "works for regular files"));
405 if (*inplace
!= '\0') {
406 (void) strlcpy(oldfname
, fname
,
408 len
= strlcat(oldfname
, inplace
,
410 if (len
> sizeof (oldfname
))
411 fatal(_("name too long"));
413 len
= snprintf(tmpfname
, sizeof (tmpfname
),
414 "%s/.!%ld!%s", dirname(dn
), (long)getpid(),
416 if (len
>= sizeof (tmpfname
))
417 fatal(_("name too long"));
418 (void) unlink(tmpfname
);
419 if ((outfile
= fopen(tmpfname
, "w")) == NULL
)
422 * Some file systems don't support chown or
423 * chmod fully. On those, the owner/group and
424 * permissions will already be set to what
427 if (fstat(fileno(outfile
), &nsb
) != 0) {
430 if (((sb
.st_uid
!= nsb
.st_uid
) ||
431 (sb
.st_gid
!= nsb
.st_gid
)) &&
432 (fchown(fileno(outfile
), sb
.st_uid
, sb
.st_gid
)
435 if ((sb
.st_mode
!= nsb
.st_mode
) &&
436 (fchmod(fileno(outfile
), sb
.st_mode
& 07777) != 0))
447 if ((infile
= fopen(fname
, "r")) == NULL
) {
454 * We are here only when infile is open and we still have something
457 * Use fgetln so that we can handle essentially infinite input data.
458 * Can't use the pointer into the stdio buffer as the process space
459 * because the ungetc() can cause it to move.
461 p
= getln(infile
, &len
);
463 errx(1, "%s: %s", fname
, strerror(errno
? errno
: EIO
));
464 if (len
!= 0 && p
[len
- 1] == '\n')
466 cspace(sp
, p
, len
, spflag
);
474 * Add a compilation unit to the linked list
477 add_compunit(enum e_cut type
, char *s
)
479 struct s_compunit
*cu
;
481 if ((cu
= malloc(sizeof (struct s_compunit
))) == NULL
)
487 cu_nextp
= &cu
->next
;
491 * Add a file to the linked list
498 if ((fp
= malloc(sizeof (struct s_flist
))) == NULL
)
503 fl_nextp
= &fp
->next
;
511 if (files
->next
!= NULL
&& (inplace
== NULL
|| ispan
))
513 if ((ch
= getc(infile
)) == EOF
)
515 (void) ungetc(ch
, infile
);
520 getln(FILE *in
, size_t *lenp
)
522 static char *buffer
= NULL
;
523 static size_t sz
= 0;
528 if (sz
<= (len
+ 1)) {
530 if ((nb
= realloc(buffer
, sz
+ LINE_MAX
)) == NULL
) {
539 if (fgets(buffer
+ len
, sz
- len
, in
) == NULL
) {
545 len
+= strlen(buffer
+ len
);
547 if (buffer
[len
- 1] == '\n') {
548 /* got the new line */