xref: /freebsd/usr.bin/sed/main.c (revision a3eb24c68be6b1e6b46e7171e6783aea7e5ecfac)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1992 Diomidis Spinellis.
39b50d902SRodney W. Grimes  * Copyright (c) 1992, 1993
49b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
59b50d902SRodney W. Grimes  *
69b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
79b50d902SRodney W. Grimes  * Diomidis Spinellis of Imperial College, University of London.
89b50d902SRodney W. Grimes  *
99b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
109b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
119b50d902SRodney W. Grimes  * are met:
129b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
149b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
159b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
169b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
34e74bf75fSMark Murray #include <sys/cdefs.h>
35e74bf75fSMark Murray __FBSDID("$FreeBSD$");
36e74bf75fSMark Murray 
379b50d902SRodney W. Grimes #ifndef lint
3873a08bb2SPhilippe Charnier static const char copyright[] =
399b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\
409b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
41e74bf75fSMark Murray #endif
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #ifndef lint
44e74bf75fSMark Murray static const char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/3/94";
4573a08bb2SPhilippe Charnier #endif
469b50d902SRodney W. Grimes 
478701af62SMaxim Sobolev #include <sys/types.h>
488701af62SMaxim Sobolev #include <sys/mman.h>
49839af0c1SJuli Mallett #include <sys/param.h>
50839af0c1SJuli Mallett #include <sys/stat.h>
519b50d902SRodney W. Grimes 
5273a08bb2SPhilippe Charnier #include <err.h>
539b50d902SRodney W. Grimes #include <errno.h>
549b50d902SRodney W. Grimes #include <fcntl.h>
55a3eb24c6SStefan Farfeleder #include <libgen.h>
5681a8648aSTim J. Robbins #include <limits.h>
57726aebe5SAndrey A. Chernov #include <locale.h>
589b50d902SRodney W. Grimes #include <regex.h>
599b50d902SRodney W. Grimes #include <stddef.h>
609b50d902SRodney W. Grimes #include <stdio.h>
619b50d902SRodney W. Grimes #include <stdlib.h>
629b50d902SRodney W. Grimes #include <string.h>
639b50d902SRodney W. Grimes #include <unistd.h>
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes #include "defs.h"
669b50d902SRodney W. Grimes #include "extern.h"
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes /*
699b50d902SRodney W. Grimes  * Linked list of units (strings and files) to be compiled
709b50d902SRodney W. Grimes  */
719b50d902SRodney W. Grimes struct s_compunit {
729b50d902SRodney W. Grimes 	struct s_compunit *next;
739b50d902SRodney W. Grimes 	enum e_cut {CU_FILE, CU_STRING} type;
749b50d902SRodney W. Grimes 	char *s;			/* Pointer to string or fname */
759b50d902SRodney W. Grimes };
769b50d902SRodney W. Grimes 
779b50d902SRodney W. Grimes /*
789b50d902SRodney W. Grimes  * Linked list pointer to compilation units and pointer to current
799b50d902SRodney W. Grimes  * next pointer.
809b50d902SRodney W. Grimes  */
819b50d902SRodney W. Grimes static struct s_compunit *script, **cu_nextp = &script;
829b50d902SRodney W. Grimes 
839b50d902SRodney W. Grimes /*
849b50d902SRodney W. Grimes  * Linked list of files to be processed
859b50d902SRodney W. Grimes  */
869b50d902SRodney W. Grimes struct s_flist {
879b50d902SRodney W. Grimes 	char *fname;
889b50d902SRodney W. Grimes 	struct s_flist *next;
899b50d902SRodney W. Grimes };
909b50d902SRodney W. Grimes 
919b50d902SRodney W. Grimes /*
929b50d902SRodney W. Grimes  * Linked list pointer to files and pointer to current
939b50d902SRodney W. Grimes  * next pointer.
949b50d902SRodney W. Grimes  */
959b50d902SRodney W. Grimes static struct s_flist *files, **fl_nextp = &files;
969b50d902SRodney W. Grimes 
97f54cda14SDag-Erling Smørgrav FILE *infile;			/* Current input file */
98f54cda14SDag-Erling Smørgrav FILE *outfile;			/* Current output file */
998523e9a6STim J. Robbins 
1009b50d902SRodney W. Grimes int aflag, eflag, nflag;
101175de1e6SBrian Feldman int rflags = 0;
1023af4dcb2STim J. Robbins static int rval;		/* Exit status */
1039b50d902SRodney W. Grimes 
1049b50d902SRodney W. Grimes /*
1059b50d902SRodney W. Grimes  * Current file and line number; line numbers restart across compilation
1069b50d902SRodney W. Grimes  * units, but span across input files.
1079b50d902SRodney W. Grimes  */
108e74bf75fSMark Murray const char *fname;		/* File name. */
109f54cda14SDag-Erling Smørgrav const char *outfname;		/* Output file name */
110f54cda14SDag-Erling Smørgrav static char oldfname[PATH_MAX];	/* Old file name (for in-place editing) */
111f54cda14SDag-Erling Smørgrav static char tmpfname[PATH_MAX];	/* Temporary file name (for in-place editing) */
112839af0c1SJuli Mallett const char *inplace;		/* Inplace edit file extension. */
1139b50d902SRodney W. Grimes u_long linenum;
1149b50d902SRodney W. Grimes 
1153f330d7dSWarner Losh static void add_compunit(enum e_cut, char *);
1163f330d7dSWarner Losh static void add_file(char *);
117839af0c1SJuli Mallett static int inplace_edit(char **);
1183f330d7dSWarner Losh static void usage(void);
1199b50d902SRodney W. Grimes 
1209b50d902SRodney W. Grimes int
121e6478125SDag-Erling Smørgrav main(int argc, char *argv[])
1229b50d902SRodney W. Grimes {
1239b50d902SRodney W. Grimes 	int c, fflag;
124e6121e72SNick Sayer 	char *temp_arg;
1259b50d902SRodney W. Grimes 
126726aebe5SAndrey A. Chernov 	(void) setlocale(LC_ALL, "");
127726aebe5SAndrey A. Chernov 
1289b50d902SRodney W. Grimes 	fflag = 0;
129839af0c1SJuli Mallett 	inplace = NULL;
130839af0c1SJuli Mallett 
131839af0c1SJuli Mallett 	while ((c = getopt(argc, argv, "Eae:f:i:n")) != -1)
1329b50d902SRodney W. Grimes 		switch (c) {
133175de1e6SBrian Feldman 		case 'E':
134175de1e6SBrian Feldman 			rflags = REG_EXTENDED;
135175de1e6SBrian Feldman 			break;
1369b50d902SRodney W. Grimes 		case 'a':
1379b50d902SRodney W. Grimes 			aflag = 1;
1389b50d902SRodney W. Grimes 			break;
1399b50d902SRodney W. Grimes 		case 'e':
1409b50d902SRodney W. Grimes 			eflag = 1;
1418e33c0a0SDavid E. O'Brien 			if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
1428e33c0a0SDavid E. O'Brien 				err(1, "malloc");
143e6121e72SNick Sayer 			strcpy(temp_arg, optarg);
144e6121e72SNick Sayer 			strcat(temp_arg, "\n");
145e6121e72SNick Sayer 			add_compunit(CU_STRING, temp_arg);
1469b50d902SRodney W. Grimes 			break;
1479b50d902SRodney W. Grimes 		case 'f':
1489b50d902SRodney W. Grimes 			fflag = 1;
1499b50d902SRodney W. Grimes 			add_compunit(CU_FILE, optarg);
1509b50d902SRodney W. Grimes 			break;
151839af0c1SJuli Mallett 		case 'i':
152839af0c1SJuli Mallett 			inplace = optarg;
153839af0c1SJuli Mallett 			break;
1549b50d902SRodney W. Grimes 		case 'n':
1559b50d902SRodney W. Grimes 			nflag = 1;
1569b50d902SRodney W. Grimes 			break;
1579b50d902SRodney W. Grimes 		default:
1589b50d902SRodney W. Grimes 		case '?':
15973a08bb2SPhilippe Charnier 			usage();
1609b50d902SRodney W. Grimes 		}
1619b50d902SRodney W. Grimes 	argc -= optind;
1629b50d902SRodney W. Grimes 	argv += optind;
1639b50d902SRodney W. Grimes 
1649b50d902SRodney W. Grimes 	/* First usage case; script is the first arg */
1659b50d902SRodney W. Grimes 	if (!eflag && !fflag && *argv) {
1669b50d902SRodney W. Grimes 		add_compunit(CU_STRING, *argv);
1679b50d902SRodney W. Grimes 		argv++;
1689b50d902SRodney W. Grimes 	}
1699b50d902SRodney W. Grimes 
1709b50d902SRodney W. Grimes 	compile();
1719b50d902SRodney W. Grimes 
1729b50d902SRodney W. Grimes 	/* Continue with first and start second usage */
1739b50d902SRodney W. Grimes 	if (*argv)
1749b50d902SRodney W. Grimes 		for (; *argv; argv++)
1759b50d902SRodney W. Grimes 			add_file(*argv);
1769b50d902SRodney W. Grimes 	else
1779b50d902SRodney W. Grimes 		add_file(NULL);
1789b50d902SRodney W. Grimes 	process();
1799b50d902SRodney W. Grimes 	cfclose(prog, NULL);
1809b50d902SRodney W. Grimes 	if (fclose(stdout))
18173a08bb2SPhilippe Charnier 		err(1, "stdout");
1823af4dcb2STim J. Robbins 	exit(rval);
1839b50d902SRodney W. Grimes }
1849b50d902SRodney W. Grimes 
18573a08bb2SPhilippe Charnier static void
186e6478125SDag-Erling Smørgrav usage(void)
18773a08bb2SPhilippe Charnier {
18873a08bb2SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n",
18990f7fe63SJuli Mallett 		"usage: sed script [-Ean] [-i extension] [file ...]",
19090f7fe63SJuli Mallett 		"       sed [-an] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
19173a08bb2SPhilippe Charnier 	exit(1);
19273a08bb2SPhilippe Charnier }
19373a08bb2SPhilippe Charnier 
1949b50d902SRodney W. Grimes /*
1959b50d902SRodney W. Grimes  * Like fgets, but go through the chain of compilation units chaining them
1969b50d902SRodney W. Grimes  * together.  Empty strings and files are ignored.
1979b50d902SRodney W. Grimes  */
1989b50d902SRodney W. Grimes char *
199e6478125SDag-Erling Smørgrav cu_fgets(char *buf, int n, int *more)
2009b50d902SRodney W. Grimes {
2019b50d902SRodney W. Grimes 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
2029b50d902SRodney W. Grimes 	static FILE *f;		/* Current open file */
2039b50d902SRodney W. Grimes 	static char *s;		/* Current pointer inside string */
2049b50d902SRodney W. Grimes 	static char string_ident[30];
2059b50d902SRodney W. Grimes 	char *p;
2069b50d902SRodney W. Grimes 
2079b50d902SRodney W. Grimes again:
2089b50d902SRodney W. Grimes 	switch (state) {
2099b50d902SRodney W. Grimes 	case ST_EOF:
210c56690efSArchie Cobbs 		if (script == NULL) {
211c56690efSArchie Cobbs 			if (more != NULL)
212c56690efSArchie Cobbs 				*more = 0;
2139b50d902SRodney W. Grimes 			return (NULL);
214c56690efSArchie Cobbs 		}
2159b50d902SRodney W. Grimes 		linenum = 0;
2169b50d902SRodney W. Grimes 		switch (script->type) {
2179b50d902SRodney W. Grimes 		case CU_FILE:
2189b50d902SRodney W. Grimes 			if ((f = fopen(script->s, "r")) == NULL)
21973a08bb2SPhilippe Charnier 				err(1, "%s", script->s);
2209b50d902SRodney W. Grimes 			fname = script->s;
2219b50d902SRodney W. Grimes 			state = ST_FILE;
2229b50d902SRodney W. Grimes 			goto again;
2239b50d902SRodney W. Grimes 		case CU_STRING:
2249b50d902SRodney W. Grimes 			if ((snprintf(string_ident,
2259b50d902SRodney W. Grimes 			    sizeof(string_ident), "\"%s\"", script->s)) >=
2269b50d902SRodney W. Grimes 			    sizeof(string_ident) - 1)
2279b50d902SRodney W. Grimes 				(void)strcpy(string_ident +
2289b50d902SRodney W. Grimes 				    sizeof(string_ident) - 6, " ...\"");
2299b50d902SRodney W. Grimes 			fname = string_ident;
2309b50d902SRodney W. Grimes 			s = script->s;
2319b50d902SRodney W. Grimes 			state = ST_STRING;
2329b50d902SRodney W. Grimes 			goto again;
2339b50d902SRodney W. Grimes 		}
2349b50d902SRodney W. Grimes 	case ST_FILE:
2359b50d902SRodney W. Grimes 		if ((p = fgets(buf, n, f)) != NULL) {
2369b50d902SRodney W. Grimes 			linenum++;
2379b50d902SRodney W. Grimes 			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
2389b50d902SRodney W. Grimes 				nflag = 1;
239c56690efSArchie Cobbs 			if (more != NULL)
240c56690efSArchie Cobbs 				*more = !feof(f);
2419b50d902SRodney W. Grimes 			return (p);
2429b50d902SRodney W. Grimes 		}
2439b50d902SRodney W. Grimes 		script = script->next;
2449b50d902SRodney W. Grimes 		(void)fclose(f);
2459b50d902SRodney W. Grimes 		state = ST_EOF;
2469b50d902SRodney W. Grimes 		goto again;
2479b50d902SRodney W. Grimes 	case ST_STRING:
2489b50d902SRodney W. Grimes 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
2499b50d902SRodney W. Grimes 			nflag = 1;
2509b50d902SRodney W. Grimes 		p = buf;
2519b50d902SRodney W. Grimes 		for (;;) {
2529b50d902SRodney W. Grimes 			if (n-- <= 1) {
2539b50d902SRodney W. Grimes 				*p = '\0';
2549b50d902SRodney W. Grimes 				linenum++;
255c56690efSArchie Cobbs 				if (more != NULL)
256c56690efSArchie Cobbs 					*more = 1;
2579b50d902SRodney W. Grimes 				return (buf);
2589b50d902SRodney W. Grimes 			}
2599b50d902SRodney W. Grimes 			switch (*s) {
2609b50d902SRodney W. Grimes 			case '\0':
2619b50d902SRodney W. Grimes 				state = ST_EOF;
2629b50d902SRodney W. Grimes 				if (s == script->s) {
2639b50d902SRodney W. Grimes 					script = script->next;
2649b50d902SRodney W. Grimes 					goto again;
2659b50d902SRodney W. Grimes 				} else {
2669b50d902SRodney W. Grimes 					script = script->next;
2679b50d902SRodney W. Grimes 					*p = '\0';
2689b50d902SRodney W. Grimes 					linenum++;
269c56690efSArchie Cobbs 					if (more != NULL)
270c56690efSArchie Cobbs 						*more = 0;
2719b50d902SRodney W. Grimes 					return (buf);
2729b50d902SRodney W. Grimes 				}
2739b50d902SRodney W. Grimes 			case '\n':
2749b50d902SRodney W. Grimes 				*p++ = '\n';
2759b50d902SRodney W. Grimes 				*p = '\0';
2769b50d902SRodney W. Grimes 				s++;
2779b50d902SRodney W. Grimes 				linenum++;
278c56690efSArchie Cobbs 				if (more != NULL)
279c56690efSArchie Cobbs 					*more = 0;
2809b50d902SRodney W. Grimes 				return (buf);
2819b50d902SRodney W. Grimes 			default:
2829b50d902SRodney W. Grimes 				*p++ = *s++;
2839b50d902SRodney W. Grimes 			}
2849b50d902SRodney W. Grimes 		}
2859b50d902SRodney W. Grimes 	}
2869b50d902SRodney W. Grimes 	/* NOTREACHED */
28773a08bb2SPhilippe Charnier 	return (NULL);
2889b50d902SRodney W. Grimes }
2899b50d902SRodney W. Grimes 
2909b50d902SRodney W. Grimes /*
2919b50d902SRodney W. Grimes  * Like fgets, but go through the list of files chaining them together.
2929b50d902SRodney W. Grimes  * Set len to the length of the line.
2939b50d902SRodney W. Grimes  */
2949b50d902SRodney W. Grimes int
295e6478125SDag-Erling Smørgrav mf_fgets(SPACE *sp, enum e_spflag spflag)
2969b50d902SRodney W. Grimes {
297f54cda14SDag-Erling Smørgrav 	struct stat sb;
2989b50d902SRodney W. Grimes 	size_t len;
2995f4cf81eSWolfram Schneider 	char *p;
3005f4cf81eSWolfram Schneider 	int c;
3018701af62SMaxim Sobolev 	static int firstfile;
3029b50d902SRodney W. Grimes 
303f54cda14SDag-Erling Smørgrav 	if (infile == NULL) {
3048701af62SMaxim Sobolev 		/* stdin? */
3059b50d902SRodney W. Grimes 		if (files->fname == NULL) {
306839af0c1SJuli Mallett 			if (inplace != NULL)
307839af0c1SJuli Mallett 				errx(1, "-i may not be used with stdin");
308f54cda14SDag-Erling Smørgrav 			infile = stdin;
3099b50d902SRodney W. Grimes 			fname = "stdin";
310f54cda14SDag-Erling Smørgrav 			outfile = stdout;
311f54cda14SDag-Erling Smørgrav 			outfname = "stdout";
3128701af62SMaxim Sobolev 		}
3138701af62SMaxim Sobolev 		firstfile = 1;
3148701af62SMaxim Sobolev 	}
3158701af62SMaxim Sobolev 
3168701af62SMaxim Sobolev 	for (;;) {
317f54cda14SDag-Erling Smørgrav 		if (infile != NULL && (c = getc(infile)) != EOF) {
318f54cda14SDag-Erling Smørgrav 			(void)ungetc(c, infile);
3198701af62SMaxim Sobolev 			break;
3208701af62SMaxim Sobolev 		}
3218701af62SMaxim Sobolev 		/* If we are here then either eof or no files are open yet */
322f54cda14SDag-Erling Smørgrav 		if (infile == stdin) {
323254fac85STim J. Robbins 			sp->len = 0;
3248701af62SMaxim Sobolev 			return (0);
3258701af62SMaxim Sobolev 		}
326f54cda14SDag-Erling Smørgrav 		if (infile != NULL) {
327f54cda14SDag-Erling Smørgrav 			fclose(infile);
3287bbdbe14SBrian Somers 			if (*oldfname != '\0') {
3297bbdbe14SBrian Somers 				if (rename(fname, oldfname) != 0) {
330f54cda14SDag-Erling Smørgrav 					warn("rename()");
331f54cda14SDag-Erling Smørgrav 					unlink(tmpfname);
332f54cda14SDag-Erling Smørgrav 					exit(1);
3338701af62SMaxim Sobolev 				}
3347bbdbe14SBrian Somers 				*oldfname = '\0';
3357bbdbe14SBrian Somers 			}
3367bbdbe14SBrian Somers 			if (*tmpfname != '\0') {
3377bbdbe14SBrian Somers 				if (outfile != NULL && outfile != stdout)
3387bbdbe14SBrian Somers 					fclose(outfile);
3397bbdbe14SBrian Somers 				outfile = NULL;
340f54cda14SDag-Erling Smørgrav 				rename(tmpfname, fname);
3417bbdbe14SBrian Somers 				*tmpfname = '\0';
3427bbdbe14SBrian Somers 			}
343f54cda14SDag-Erling Smørgrav 			outfname = NULL;
344f54cda14SDag-Erling Smørgrav 		}
345f54cda14SDag-Erling Smørgrav 		if (firstfile == 0)
3468701af62SMaxim Sobolev 			files = files->next;
347f54cda14SDag-Erling Smørgrav 		else
3488701af62SMaxim Sobolev 			firstfile = 0;
3498701af62SMaxim Sobolev 		if (files == NULL) {
350254fac85STim J. Robbins 			sp->len = 0;
3518701af62SMaxim Sobolev 			return (0);
3528701af62SMaxim Sobolev 		}
3539b50d902SRodney W. Grimes 		fname = files->fname;
354f54cda14SDag-Erling Smørgrav 		if (inplace != NULL) {
355f54cda14SDag-Erling Smørgrav 			if (lstat(fname, &sb) != 0)
356f54cda14SDag-Erling Smørgrav 				err(1, "%s", fname);
357f54cda14SDag-Erling Smørgrav 			if (!(sb.st_mode & S_IFREG))
358f54cda14SDag-Erling Smørgrav 				errx(1, "%s: %s %s", fname,
359f54cda14SDag-Erling Smørgrav 				    "in-place editing only",
360f54cda14SDag-Erling Smørgrav 				    "works for regular files");
361f54cda14SDag-Erling Smørgrav 			if (*inplace != '\0') {
362f54cda14SDag-Erling Smørgrav 				strlcpy(oldfname, fname,
363f54cda14SDag-Erling Smørgrav 				    sizeof(oldfname));
364f54cda14SDag-Erling Smørgrav 				len = strlcat(oldfname, inplace,
365f54cda14SDag-Erling Smørgrav 				    sizeof(oldfname));
366f54cda14SDag-Erling Smørgrav 				if (len > sizeof(oldfname))
367f54cda14SDag-Erling Smørgrav 					errx(1, "%s: name too long", fname);
368f54cda14SDag-Erling Smørgrav 			}
369f54cda14SDag-Erling Smørgrav 			len = snprintf(tmpfname, sizeof(tmpfname),
370591c337eSDag-Erling Smørgrav 			    "%s/.!%ld!%s", dirname(fname), (long)getpid(),
371591c337eSDag-Erling Smørgrav 			    basename(fname));
372f54cda14SDag-Erling Smørgrav 			if (len >= sizeof(tmpfname))
373f54cda14SDag-Erling Smørgrav 				errx(1, "%s: name too long", fname);
374f54cda14SDag-Erling Smørgrav 			unlink(tmpfname);
375f54cda14SDag-Erling Smørgrav 			if ((outfile = fopen(tmpfname, "w")) == NULL)
376f54cda14SDag-Erling Smørgrav 				err(1, "%s", fname);
377f54cda14SDag-Erling Smørgrav 			fchown(fileno(outfile), sb.st_uid, sb.st_gid);
378f54cda14SDag-Erling Smørgrav 			fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
379f54cda14SDag-Erling Smørgrav 			outfname = tmpfname;
380f54cda14SDag-Erling Smørgrav 		} else {
381f54cda14SDag-Erling Smørgrav 			outfile = stdout;
382f54cda14SDag-Erling Smørgrav 			outfname = "stdout";
383f54cda14SDag-Erling Smørgrav 		}
384f54cda14SDag-Erling Smørgrav 		if ((infile = fopen(fname, "r")) == NULL) {
3853af4dcb2STim J. Robbins 			warn("%s", fname);
3866689fb2bSTim J. Robbins 			rval = 1;
3873af4dcb2STim J. Robbins 			continue;
3883af4dcb2STim J. Robbins 		}
3899b50d902SRodney W. Grimes 	}
3909b50d902SRodney W. Grimes 	/*
391f54cda14SDag-Erling Smørgrav 	 * We are here only when infile is open and we still have something
3928523e9a6STim J. Robbins 	 * to read from it.
3938701af62SMaxim Sobolev 	 *
3949b50d902SRodney W. Grimes 	 * Use fgetln so that we can handle essentially infinite input data.
3955f4cf81eSWolfram Schneider 	 * Can't use the pointer into the stdio buffer as the process space
3965f4cf81eSWolfram Schneider 	 * because the ungetc() can cause it to move.
3979b50d902SRodney W. Grimes 	 */
398f54cda14SDag-Erling Smørgrav 	p = fgetln(infile, &len);
399f54cda14SDag-Erling Smørgrav 	if (ferror(infile))
40073a08bb2SPhilippe Charnier 		errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
401ed92199dSTim J. Robbins 	if (len != 0 && p[len - 1] == '\n')
402ed92199dSTim J. Robbins 		len--;
4039b50d902SRodney W. Grimes 	cspace(sp, p, len, spflag);
4049b50d902SRodney W. Grimes 
4059b50d902SRodney W. Grimes 	linenum++;
4068701af62SMaxim Sobolev 
4079b50d902SRodney W. Grimes 	return (1);
4089b50d902SRodney W. Grimes }
4099b50d902SRodney W. Grimes 
4109b50d902SRodney W. Grimes /*
4119b50d902SRodney W. Grimes  * Add a compilation unit to the linked list
4129b50d902SRodney W. Grimes  */
4139b50d902SRodney W. Grimes static void
414e6478125SDag-Erling Smørgrav add_compunit(enum e_cut type, char *s)
4159b50d902SRodney W. Grimes {
4169b50d902SRodney W. Grimes 	struct s_compunit *cu;
4179b50d902SRodney W. Grimes 
4188e33c0a0SDavid E. O'Brien 	if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
4198e33c0a0SDavid E. O'Brien 		err(1, "malloc");
4209b50d902SRodney W. Grimes 	cu->type = type;
4219b50d902SRodney W. Grimes 	cu->s = s;
4229b50d902SRodney W. Grimes 	cu->next = NULL;
4239b50d902SRodney W. Grimes 	*cu_nextp = cu;
4249b50d902SRodney W. Grimes 	cu_nextp = &cu->next;
4259b50d902SRodney W. Grimes }
4269b50d902SRodney W. Grimes 
4279b50d902SRodney W. Grimes /*
4289b50d902SRodney W. Grimes  * Add a file to the linked list
4299b50d902SRodney W. Grimes  */
4309b50d902SRodney W. Grimes static void
431e6478125SDag-Erling Smørgrav add_file(char *s)
4329b50d902SRodney W. Grimes {
4339b50d902SRodney W. Grimes 	struct s_flist *fp;
4349b50d902SRodney W. Grimes 
4358e33c0a0SDavid E. O'Brien 	if ((fp = malloc(sizeof(struct s_flist))) == NULL)
4368e33c0a0SDavid E. O'Brien 		err(1, "malloc");
4379b50d902SRodney W. Grimes 	fp->next = NULL;
4389b50d902SRodney W. Grimes 	*fl_nextp = fp;
4399b50d902SRodney W. Grimes 	fp->fname = s;
4409b50d902SRodney W. Grimes 	fl_nextp = &fp->next;
4419b50d902SRodney W. Grimes }
442839af0c1SJuli Mallett 
4438523e9a6STim J. Robbins int
4448523e9a6STim J. Robbins lastline(void)
4458523e9a6STim J. Robbins {
4468523e9a6STim J. Robbins 	int ch;
4478523e9a6STim J. Robbins 
4488523e9a6STim J. Robbins 	if (files->next != NULL)
4498523e9a6STim J. Robbins 		return (0);
450f54cda14SDag-Erling Smørgrav 	if ((ch = getc(infile)) == EOF)
4518523e9a6STim J. Robbins 		return (1);
452f54cda14SDag-Erling Smørgrav 	ungetc(ch, infile);
4538523e9a6STim J. Robbins 	return (0);
4548523e9a6STim J. Robbins }
455