GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/sed/main.c Lines: 206 245 84.1 %
Date: 2017-11-07 Branches: 126 183 68.9 %

Line Branch Exec Source
1
/*	$OpenBSD: main.c,v 1.35 2017/08/01 18:05:53 martijn Exp $	*/
2
3
/*-
4
 * Copyright (c) 1992 Diomidis Spinellis.
5
 * Copyright (c) 1992, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * This code is derived from software contributed to Berkeley by
9
 * Diomidis Spinellis of Imperial College, University of London.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
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
 * 3. 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.
22
 *
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
33
 * SUCH DAMAGE.
34
 */
35
36
#include <sys/types.h>
37
#include <sys/ioctl.h>
38
#include <sys/stat.h>
39
40
#include <ctype.h>
41
#include <errno.h>
42
#include <fcntl.h>
43
#include <limits.h>
44
#include <regex.h>
45
#include <stddef.h>
46
#include <stdio.h>
47
#include <stdlib.h>
48
#include <string.h>
49
#include <unistd.h>
50
#include <libgen.h>
51
52
#include "defs.h"
53
#include "extern.h"
54
55
/*
56
 * Linked list of units (strings and files) to be compiled
57
 */
58
struct s_compunit {
59
	struct s_compunit *next;
60
	enum e_cut {CU_FILE, CU_STRING} type;
61
	char *s;			/* Pointer to string or fname */
62
};
63
64
/*
65
 * Linked list pointer to compilation units and pointer to current
66
 * next pointer.
67
 */
68
static struct s_compunit *script, **cu_nextp = &script;
69
70
/*
71
 * Linked list of files to be processed
72
 */
73
struct s_flist {
74
	char *fname;
75
	struct s_flist *next;
76
};
77
78
/*
79
 * Linked list pointer to files and pointer to current
80
 * next pointer.
81
 */
82
static struct s_flist *files, **fl_nextp = &files;
83
84
FILE *infile;			/* Current input file */
85
FILE *outfile;			/* Current output file */
86
87
int Eflag, aflag, eflag, nflag;
88
static int rval;	/* Exit status */
89
90
/*
91
 * Current file and line number; line numbers restart across compilation
92
 * units, but span across input files.  The latter is optional if editing
93
 * in place.
94
 */
95
const char *fname;		/* File name. */
96
const char *outfname;		/* Output file name */
97
static char oldfname[PATH_MAX];	/* Old file name (for in-place editing) */
98
static char tmpfname[PATH_MAX];	/* Temporary file name (for in-place editing) */
99
char *inplace;			/* Inplace edit file extension */
100
u_long linenum;
101
102
static void add_compunit(enum e_cut, char *);
103
static void add_file(char *);
104
static int next_files_have_lines(void);
105
106
int termwidth;
107
108
int pledge_wpath, pledge_rpath;
109
110
int
111
main(int argc, char *argv[])
112
{
113
19030
	struct winsize win;
114
	int c, fflag;
115
	char *p;
116
117
	fflag = 0;
118
9515
	inplace = NULL;
119
25690
	while ((c = getopt(argc, argv, "Eae:f:i::nru")) != -1)
120


6681
		switch (c) {
121
		case 'E':
122
		case 'r':
123
4301
			Eflag = 1;
124
4301
			break;
125
		case 'a':
126
			aflag = 1;
127
			break;
128
		case 'e':
129
1584
			eflag = 1;
130
1584
			add_compunit(CU_STRING, optarg);
131
1584
			break;
132
		case 'f':
133
			fflag = 1;
134
249
			add_compunit(CU_FILE, optarg);
135
249
			break;
136
		case 'i':
137
21
			inplace = optarg ? optarg : "";
138
21
			break;
139
		case 'n':
140
505
			nflag = 1;
141
505
			break;
142
		case 'u':
143
			setvbuf(stdout, NULL, _IOLBF, 0);
144
			break;
145
		default:
146
		case '?':
147
			(void)fprintf(stderr,
148
			    "usage: sed [-aEnru] [-i[extension]] command [file ...]\n"
149
			    "       sed [-aEnru] [-e command] [-f command_file] [-i[extension]] [file ...]\n");
150
			exit(1);
151
		}
152
9494
	argc -= optind;
153
9494
	argv += optind;
154
155
9494
	termwidth = 0;
156
9494
	if ((p = getenv("COLUMNS")) != NULL)
157
		termwidth = strtonum(p, 0, INT_MAX, NULL);
158

19010
	if (termwidth == 0 && ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
159
22
	    win.ws_col > 0)
160
22
		termwidth = win.ws_col;
161
9494
	if (termwidth == 0)
162
9472
		termwidth = 80;
163
164
9494
	if (inplace != NULL) {
165
21
		if (pledge("stdio rpath wpath cpath fattr chown flock", NULL) == -1)
166
			error(FATAL, "pledge: %s", strerror(errno));
167
	} else {
168
9473
		if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
169
			error(FATAL, "pledge: %s", strerror(errno));
170
	}
171
172
	/* First usage case; script is the first arg */
173

17359
	if (!eflag && !fflag && *argv) {
174
7865
		add_compunit(CU_STRING, *argv);
175
7865
		argv++;
176
7865
	}
177
178
9312
	compile();
179
180
	/* Continue with first and start second usage */
181
9312
	if (*argv) {
182
1080
		if (!pledge_wpath && inplace == NULL) {
183
1059
			if (pledge("stdio rpath flock cpath wpath", NULL) == -1)
184
				error(FATAL, "pledge: %s", strerror(errno));
185
		}
186
3510
		for (; *argv; argv++)
187
1215
			add_file(*argv);
188
	} else {
189
8232
		if (!pledge_wpath && !pledge_rpath) {
190
8187
			if (pledge("stdio flock rpath cpath wpath", NULL) == -1)
191
				error(FATAL, "pledge: %s", strerror(errno));
192
45
		} else if (pledge_rpath) {
193
45
			if (pledge("stdio rpath flock cpath wpath", NULL) == -1)
194
				error(FATAL, "pledge: %s", strerror(errno));
195
		} else if (pledge_wpath) {
196
			if (pledge("stdio wpath cpath flock rpath", NULL) == -1)
197
				error(FATAL, "pledge: %s", strerror(errno));
198
		}
199
8232
		add_file(NULL);
200
	}
201
9087
	process();
202
9087
	cfclose(prog, NULL);
203
9087
	if (fclose(stdout))
204
		error(FATAL, "stdout: %s", strerror(errno));
205
	exit (rval);
206
}
207
208
/*
209
 * Like fgets, but go through the chain of compilation units chaining them
210
 * together.  Empty strings and files are ignored.
211
 */
212
char *
213
cu_fgets(char **outbuf, size_t *outsize)
214
{
215
	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
216
	static FILE *f;		/* Current open file */
217
	static char *s;		/* Current pointer inside string */
218
	static char string_ident[30];
219
81392
	size_t len;
220
	char *p;
221
222
40696
	if (*outbuf == NULL)
223
9659
		*outsize = 0;
224
225
again:
226

50629
	switch (state) {
227
	case ST_EOF:
228
19045
		if (script == NULL)
229
9347
			return (NULL);
230
9698
		linenum = 0;
231
9698
		switch (script->type) {
232
		case CU_FILE:
233
249
			if ((f = fopen(script->s, "r")) == NULL)
234
				error(FATAL,
235
				    "%s: %s", script->s, strerror(errno));
236
242
			fname = script->s;
237
242
			state = ST_FILE;
238
242
			goto again;
239
		case CU_STRING:
240
18898
			if ((snprintf(string_ident,
241
18898
			    sizeof(string_ident), "\"%s\"", script->s)) >=
242
			    sizeof(string_ident))
243
3707
				strlcpy(string_ident +
244
				    sizeof(string_ident) - 6, " ...\"", 5);
245
9449
			fname = string_ident;
246
9449
			s = script->s;
247
9449
			state = ST_STRING;
248
9449
			goto again;
249
		}
250
	case ST_FILE:
251
17806
		if ((p = fgetln(f, &len)) != NULL) {
252
17571
			linenum++;
253
17571
			if (len >= *outsize) {
254
298
				free(*outbuf);
255
298
				*outsize = ROUNDLEN(len + 1);
256
298
				*outbuf = xmalloc(*outsize);
257
298
			}
258
17571
			memcpy(*outbuf, p, len);
259
17571
			(*outbuf)[len] = '\0';
260

17848
			if (linenum == 1 && p[0] == '#' && p[1] == 'n')
261
7
				nflag = 1;
262
17571
			return (*outbuf);
263
		}
264
235
		script = script->next;
265
235
		(void)fclose(f);
266
235
		state = ST_EOF;
267
235
		goto again;
268
	case ST_STRING:
269

23227
		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
270
			nflag = 1;
271
13778
		p = *outbuf;
272
13778
		len = *outsize;
273
250243
		for (;;) {
274
250243
			if (len <= 1) {
275
18680
				*outbuf = xrealloc(*outbuf,
276
9340
				    *outsize + _POSIX2_LINE_MAX);
277
9340
				p = *outbuf + *outsize - len;
278
9340
				len += _POSIX2_LINE_MAX;
279
9340
				*outsize += _POSIX2_LINE_MAX;
280
9340
			}
281
250243
			switch (*s) {
282
			case '\0':
283
9449
				state = ST_EOF;
284
9449
				if (s == script->s) {
285
					script = script->next;
286
					goto again;
287
				} else {
288
					script = script->next;
289
9442
					*p = '\0';
290
9442
					linenum++;
291
9442
					return (*outbuf);
292
				}
293
			case '\n':
294
4329
				*p++ = '\n';
295
4329
				*p = '\0';
296
4329
				s++;
297
4329
				linenum++;
298
4329
				return (*outbuf);
299
			default:
300
236465
				*p++ = *s++;
301
236465
				len--;
302
			}
303
		}
304
	}
305
306
	return (NULL);
307
40689
}
308
309
/*
310
 * Like fgets, but go through the list of files chaining them together.
311
 * Set len to the length of the line.
312
 */
313
int
314
mf_fgets(SPACE *sp, enum e_spflag spflag)
315
{
316
1876072
	struct stat sb;
317
938036
	size_t len;
318
	char *p;
319
	int c, fd;
320
	static int firstfile;
321
322
938036
	if (infile == NULL) {
323
		/* stdin? */
324
9312
		if (files->fname == NULL) {
325
8232
			if (inplace != NULL)
326
				error(FATAL, "-i may not be used with stdin");
327
8232
			infile = stdin;
328
8232
			fname = "stdin";
329
8232
			outfile = stdout;
330
8232
			outfname = "stdout";
331
8232
		}
332
333
9312
		firstfile = 1;
334
9312
	}
335
336
	for (;;) {
337


4691907
		if (infile != NULL && (c = getc(infile)) != EOF) {
338
928942
			(void)ungetc(c, infile);
339
			break;
340
		}
341
		/* If we are here then either eof or no files are open yet */
342
10309
		if (infile == stdin) {
343
8212
			sp->len = 0;
344
8212
			return (0);
345
		}
346
2097
		if (infile != NULL) {
347
1010
			fclose(infile);
348
1010
			if (*oldfname != '\0') {
349
				if (rename(fname, oldfname) != 0) {
350
					warning("rename()");
351
					unlink(tmpfname);
352
					exit(1);
353
				}
354
				*oldfname = '\0';
355
			}
356
1010
			if (*tmpfname != '\0') {
357
21
				if (outfile != NULL && outfile != stdout)
358
21
					fclose(outfile);
359
21
				outfile = NULL;
360
21
				rename(tmpfname, fname);
361
21
				*tmpfname = '\0';
362
21
			}
363
1010
			outfname = NULL;
364
1010
		}
365
2097
		if (firstfile == 0)
366
1017
			files = files->next;
367
		else
368
1080
			firstfile = 0;
369
2097
		if (files == NULL) {
370
882
			sp->len = 0;
371
882
			return (0);
372
		}
373
1215
		fname = files->fname;
374
1215
		if (inplace != NULL) {
375
21
			if (lstat(fname, &sb) != 0)
376
				error(FATAL, "%s: %s", fname,
377
				    strerror(errno ? errno : EIO));
378
21
			if (!S_ISREG(sb.st_mode))
379
				error(FATAL, "%s: %s %s", fname,
380
				    "in-place editing only",
381
				    "works for regular files");
382
21
			if (*inplace != '\0') {
383
				strlcpy(oldfname, fname,
384
				    sizeof(oldfname));
385
				len = strlcat(oldfname, inplace,
386
				    sizeof(oldfname));
387
				if (len > sizeof(oldfname))
388
					error(FATAL, "%s: name too long", fname);
389
			}
390
21
			len = snprintf(tmpfname, sizeof(tmpfname), "%s/sedXXXXXXXXXX",
391
21
			    dirname(fname));
392
21
			if (len >= sizeof(tmpfname))
393
				error(FATAL, "%s: name too long", fname);
394
21
			if ((fd = mkstemp(tmpfname)) == -1)
395
				error(FATAL, "%s: %s", fname, strerror(errno));
396
21
			if ((outfile = fdopen(fd, "w")) == NULL) {
397
				unlink(tmpfname);
398
				error(FATAL, "%s", fname);
399
			}
400
63
			fchown(fileno(outfile), sb.st_uid, sb.st_gid);
401
63
			fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
402
21
			outfname = tmpfname;
403
21
			linenum = 0;
404
21
			resetranges();
405
21
		} else {
406
1194
			outfile = stdout;
407
1194
			outfname = "stdout";
408
		}
409
1215
		if ((infile = fopen(fname, "r")) == NULL) {
410
7
			warning("%s", strerror(errno));
411
7
			rval = 1;
412
7
			continue;
413
		}
414
	}
415
416
	/*
417
	 * We are here only when infile is open and we still have something
418
	 * to read from it.
419
	 *
420
	 * Use fgetln so that we can handle essentially infinite input data.
421
	 * Can't use the pointer into the stdio buffer as the process space
422
	 * because the ungetc() can cause it to move.
423
	 */
424
928942
	p = fgetln(infile, &len);
425

1857884
	if (ferror(infile))
426
		error(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
427

1857884
	if (len != 0 && p[len - 1] == '\n') {
428
927912
		sp->append_newline = 1;
429
927912
		len--;
430
928942
	} else if (!lastline()) {
431
		sp->append_newline = 1;
432
	} else {
433
		sp->append_newline = 0;
434
	}
435
928942
	cspace(sp, p, len, spflag);
436
437
928942
	linenum++;
438
439
928942
	return (1);
440
938036
}
441
442
/*
443
 * Add a compilation unit to the linked list
444
 */
445
static void
446
add_compunit(enum e_cut type, char *s)
447
{
448
	struct s_compunit *cu;
449
450
19396
	cu = xmalloc(sizeof(struct s_compunit));
451
9698
	cu->type = type;
452
9698
	cu->s = s;
453
9698
	cu->next = NULL;
454
9698
	*cu_nextp = cu;
455
9698
	cu_nextp = &cu->next;
456
9698
}
457
458
/*
459
 * Add a file to the linked list
460
 */
461
static void
462
add_file(char *s)
463
{
464
	struct s_flist *fp;
465
466
18894
	fp = xmalloc(sizeof(struct s_flist));
467
9447
	fp->next = NULL;
468
9447
	*fl_nextp = fp;
469
9447
	fp->fname = s;
470
9447
	fl_nextp = &fp->next;
471
9447
}
472
473
474
static int
475
next_files_have_lines()
476
{
477
	struct s_flist *file;
478
	FILE *file_fd;
479
	int ch;
480
481
2290
	file = files;
482
2318
	while ((file = file->next) != NULL) {
483
42
		if ((file_fd = fopen(file->fname, "r")) == NULL)
484
			continue;
485
486

168
		if ((ch = getc(file_fd)) != EOF) {
487
			/*
488
			 * This next file has content, therefore current
489
			 * file doesn't contains the last line.
490
			 */
491
28
			ungetc(ch, file_fd);
492
28
			fclose(file_fd);
493
28
			return (1);
494
		}
495
14
		fclose(file_fd);
496
	}
497
1117
	return (0);
498
1145
}
499
500
int
501
lastline(void)
502
{
503
	int ch;
504
505

6162
	if (feof(infile)) {
506
1030
		return !(
507
2060
		    (inplace == NULL) &&
508
1030
		    next_files_have_lines());
509
	}
510

4096
	if ((ch = getc(infile)) == EOF) {
511
115
		return !(
512
230
		    (inplace == NULL) &&
513
115
		    next_files_have_lines());
514
	}
515
909
	ungetc(ch, infile);
516
909
	return (0);
517
2054
}