xref: /freebsd/usr.bin/sed/main.c (revision 3af4dcb22376f1053d03f68d15aa7d00d3bb92e4)
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  * 3. All advertising materials mentioning features or use of this software
189b50d902SRodney W. Grimes  *    must display the following acknowledgement:
199b50d902SRodney W. Grimes  *	This product includes software developed by the University of
209b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
219b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
229b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
239b50d902SRodney W. Grimes  *    without specific prior written permission.
249b50d902SRodney W. Grimes  *
259b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
269b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
279b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
289b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
299b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
309b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
319b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
329b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
339b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
349b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
359b50d902SRodney W. Grimes  * SUCH DAMAGE.
369b50d902SRodney W. Grimes  */
379b50d902SRodney W. Grimes 
38e74bf75fSMark Murray #include <sys/cdefs.h>
39e74bf75fSMark Murray __FBSDID("$FreeBSD$");
40e74bf75fSMark Murray 
419b50d902SRodney W. Grimes #ifndef lint
4273a08bb2SPhilippe Charnier static const char copyright[] =
439b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\
449b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
45e74bf75fSMark Murray #endif
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #ifndef lint
48e74bf75fSMark Murray static const char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/3/94";
4973a08bb2SPhilippe Charnier #endif
509b50d902SRodney W. Grimes 
51839af0c1SJuli Mallett #include <sys/param.h>
52839af0c1SJuli Mallett #include <sys/stat.h>
539b50d902SRodney W. Grimes 
5473a08bb2SPhilippe Charnier #include <err.h>
559b50d902SRodney W. Grimes #include <errno.h>
569b50d902SRodney W. Grimes #include <fcntl.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 
979b50d902SRodney W. Grimes int aflag, eflag, nflag;
98175de1e6SBrian Feldman int rflags = 0;
993af4dcb2STim J. Robbins static int rval;		/* Exit status */
1009b50d902SRodney W. Grimes 
1019b50d902SRodney W. Grimes /*
1029b50d902SRodney W. Grimes  * Current file and line number; line numbers restart across compilation
1039b50d902SRodney W. Grimes  * units, but span across input files.
1049b50d902SRodney W. Grimes  */
105e74bf75fSMark Murray const char *fname;		/* File name. */
106839af0c1SJuli Mallett const char *inplace;		/* Inplace edit file extension. */
1079b50d902SRodney W. Grimes u_long linenum;
1089b50d902SRodney W. Grimes int lastline;			/* TRUE on the last line of the last file */
1099b50d902SRodney W. Grimes 
1103f330d7dSWarner Losh static void add_compunit(enum e_cut, char *);
1113f330d7dSWarner Losh static void add_file(char *);
112839af0c1SJuli Mallett static int inplace_edit(char **);
1133f330d7dSWarner Losh static void usage(void);
1149b50d902SRodney W. Grimes 
1159b50d902SRodney W. Grimes int
1169b50d902SRodney W. Grimes main(argc, argv)
1179b50d902SRodney W. Grimes 	int argc;
1189b50d902SRodney W. Grimes 	char *argv[];
1199b50d902SRodney W. Grimes {
1209b50d902SRodney W. Grimes 	int c, fflag;
121e6121e72SNick Sayer 	char *temp_arg;
1229b50d902SRodney W. Grimes 
123726aebe5SAndrey A. Chernov 	(void) setlocale(LC_ALL, "");
124726aebe5SAndrey A. Chernov 
1259b50d902SRodney W. Grimes 	fflag = 0;
126839af0c1SJuli Mallett 	inplace = NULL;
127839af0c1SJuli Mallett 
128839af0c1SJuli Mallett 	while ((c = getopt(argc, argv, "Eae:f:i:n")) != -1)
1299b50d902SRodney W. Grimes 		switch (c) {
130175de1e6SBrian Feldman 		case 'E':
131175de1e6SBrian Feldman 			rflags = REG_EXTENDED;
132175de1e6SBrian Feldman 			break;
1339b50d902SRodney W. Grimes 		case 'a':
1349b50d902SRodney W. Grimes 			aflag = 1;
1359b50d902SRodney W. Grimes 			break;
1369b50d902SRodney W. Grimes 		case 'e':
1379b50d902SRodney W. Grimes 			eflag = 1;
1388e33c0a0SDavid E. O'Brien 			if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL)
1398e33c0a0SDavid E. O'Brien 				err(1, "malloc");
140e6121e72SNick Sayer 			strcpy(temp_arg, optarg);
141e6121e72SNick Sayer 			strcat(temp_arg, "\n");
142e6121e72SNick Sayer 			add_compunit(CU_STRING, temp_arg);
1439b50d902SRodney W. Grimes 			break;
1449b50d902SRodney W. Grimes 		case 'f':
1459b50d902SRodney W. Grimes 			fflag = 1;
1469b50d902SRodney W. Grimes 			add_compunit(CU_FILE, optarg);
1479b50d902SRodney W. Grimes 			break;
148839af0c1SJuli Mallett 		case 'i':
149839af0c1SJuli Mallett 			inplace = optarg;
150839af0c1SJuli Mallett 			break;
1519b50d902SRodney W. Grimes 		case 'n':
1529b50d902SRodney W. Grimes 			nflag = 1;
1539b50d902SRodney W. Grimes 			break;
1549b50d902SRodney W. Grimes 		default:
1559b50d902SRodney W. Grimes 		case '?':
15673a08bb2SPhilippe Charnier 			usage();
1579b50d902SRodney W. Grimes 		}
1589b50d902SRodney W. Grimes 	argc -= optind;
1599b50d902SRodney W. Grimes 	argv += optind;
1609b50d902SRodney W. Grimes 
1619b50d902SRodney W. Grimes 	/* First usage case; script is the first arg */
1629b50d902SRodney W. Grimes 	if (!eflag && !fflag && *argv) {
1639b50d902SRodney W. Grimes 		add_compunit(CU_STRING, *argv);
1649b50d902SRodney W. Grimes 		argv++;
1659b50d902SRodney W. Grimes 	}
1669b50d902SRodney W. Grimes 
1679b50d902SRodney W. Grimes 	compile();
1689b50d902SRodney W. Grimes 
1699b50d902SRodney W. Grimes 	/* Continue with first and start second usage */
1709b50d902SRodney W. Grimes 	if (*argv)
1719b50d902SRodney W. Grimes 		for (; *argv; argv++)
1729b50d902SRodney W. Grimes 			add_file(*argv);
1739b50d902SRodney W. Grimes 	else
1749b50d902SRodney W. Grimes 		add_file(NULL);
1759b50d902SRodney W. Grimes 	process();
1769b50d902SRodney W. Grimes 	cfclose(prog, NULL);
1779b50d902SRodney W. Grimes 	if (fclose(stdout))
17873a08bb2SPhilippe Charnier 		err(1, "stdout");
1793af4dcb2STim J. Robbins 	exit(rval);
1809b50d902SRodney W. Grimes }
1819b50d902SRodney W. Grimes 
18273a08bb2SPhilippe Charnier static void
18373a08bb2SPhilippe Charnier usage()
18473a08bb2SPhilippe Charnier {
18573a08bb2SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n",
18690f7fe63SJuli Mallett 		"usage: sed script [-Ean] [-i extension] [file ...]",
18790f7fe63SJuli Mallett 		"       sed [-an] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
18873a08bb2SPhilippe Charnier 	exit(1);
18973a08bb2SPhilippe Charnier }
19073a08bb2SPhilippe Charnier 
1919b50d902SRodney W. Grimes /*
1929b50d902SRodney W. Grimes  * Like fgets, but go through the chain of compilation units chaining them
1939b50d902SRodney W. Grimes  * together.  Empty strings and files are ignored.
1949b50d902SRodney W. Grimes  */
1959b50d902SRodney W. Grimes char *
196c56690efSArchie Cobbs cu_fgets(buf, n, more)
1979b50d902SRodney W. Grimes 	char *buf;
1989b50d902SRodney W. Grimes 	int n;
199c56690efSArchie Cobbs 	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
2959b50d902SRodney W. Grimes mf_fgets(sp, spflag)
2969b50d902SRodney W. Grimes 	SPACE *sp;
2979b50d902SRodney W. Grimes 	enum e_spflag spflag;
2989b50d902SRodney W. Grimes {
2999b50d902SRodney W. Grimes 	static FILE *f;		/* Current open file */
3009b50d902SRodney W. Grimes 	size_t len;
3015f4cf81eSWolfram Schneider 	char *p;
3025f4cf81eSWolfram Schneider 	int c;
3039b50d902SRodney W. Grimes 
3049b50d902SRodney W. Grimes 	if (f == NULL)
3059b50d902SRodney W. Grimes 		/* Advance to first non-empty file */
3069b50d902SRodney W. Grimes 		for (;;) {
3079b50d902SRodney W. Grimes 			if (files == NULL) {
3089b50d902SRodney W. Grimes 				lastline = 1;
3099b50d902SRodney W. Grimes 				return (0);
3109b50d902SRodney W. Grimes 			}
3119b50d902SRodney W. Grimes 			if (files->fname == NULL) {
312839af0c1SJuli Mallett 				if (inplace != NULL)
313839af0c1SJuli Mallett 					errx(1, "-i may not be used with stdin");
3149b50d902SRodney W. Grimes 				f = stdin;
3159b50d902SRodney W. Grimes 				fname = "stdin";
3169b50d902SRodney W. Grimes 			} else {
317839af0c1SJuli Mallett 				if (inplace != NULL) {
318839af0c1SJuli Mallett 					if (inplace_edit(&files->fname) == -1)
319839af0c1SJuli Mallett 						continue;
320839af0c1SJuli Mallett 				}
3219b50d902SRodney W. Grimes 				fname = files->fname;
3223af4dcb2STim J. Robbins 				if ((f = fopen(fname, "r")) == NULL) {
3233af4dcb2STim J. Robbins 					warn("%s", fname);
3243af4dcb2STim J. Robbins 					rval = 1;
3253af4dcb2STim J. Robbins 					files = files->next;
3263af4dcb2STim J. Robbins 					continue;
3273af4dcb2STim J. Robbins 				}
3285d16412dSJuli Mallett 				if (inplace != NULL && *inplace == '\0')
3295d16412dSJuli Mallett 					unlink(fname);
3309b50d902SRodney W. Grimes 			}
3315f4cf81eSWolfram Schneider 			if ((c = getc(f)) != EOF) {
3325f4cf81eSWolfram Schneider 				(void)ungetc(c, f);
3339b50d902SRodney W. Grimes 				break;
3349b50d902SRodney W. Grimes 			}
3359b50d902SRodney W. Grimes 			(void)fclose(f);
3369b50d902SRodney W. Grimes 			files = files->next;
3379b50d902SRodney W. Grimes 		}
3389b50d902SRodney W. Grimes 
3399b50d902SRodney W. Grimes 	if (lastline) {
3409b50d902SRodney W. Grimes 		sp->len = 0;
3419b50d902SRodney W. Grimes 		return (0);
3429b50d902SRodney W. Grimes 	}
3439b50d902SRodney W. Grimes 
3449b50d902SRodney W. Grimes 	/*
3459b50d902SRodney W. Grimes 	 * Use fgetln so that we can handle essentially infinite input data.
3465f4cf81eSWolfram Schneider 	 * Can't use the pointer into the stdio buffer as the process space
3475f4cf81eSWolfram Schneider 	 * because the ungetc() can cause it to move.
3489b50d902SRodney W. Grimes 	 */
3499b50d902SRodney W. Grimes 	p = fgetln(f, &len);
3509b50d902SRodney W. Grimes 	if (ferror(f))
35173a08bb2SPhilippe Charnier 		errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
3529b50d902SRodney W. Grimes 	cspace(sp, p, len, spflag);
3539b50d902SRodney W. Grimes 
3549b50d902SRodney W. Grimes 	linenum++;
3559b50d902SRodney W. Grimes 	/* Advance to next non-empty file */
3565f4cf81eSWolfram Schneider 	while ((c = getc(f)) == EOF) {
3579b50d902SRodney W. Grimes 		(void)fclose(f);
3583af4dcb2STim J. Robbins next:		files = files->next;
3599b50d902SRodney W. Grimes 		if (files == NULL) {
3609b50d902SRodney W. Grimes 			lastline = 1;
3619b50d902SRodney W. Grimes 			return (1);
3629b50d902SRodney W. Grimes 		}
3639b50d902SRodney W. Grimes 		if (files->fname == NULL) {
364839af0c1SJuli Mallett 			if (inplace != NULL)
365839af0c1SJuli Mallett 				errx(1, "-i may not be used with stdin");
3669b50d902SRodney W. Grimes 			f = stdin;
3679b50d902SRodney W. Grimes 			fname = "stdin";
3689b50d902SRodney W. Grimes 		} else {
369839af0c1SJuli Mallett 			if (inplace != NULL) {
370839af0c1SJuli Mallett 				if (inplace_edit(&files->fname) == -1)
371839af0c1SJuli Mallett 					continue;
372839af0c1SJuli Mallett 			}
3739b50d902SRodney W. Grimes 			fname = files->fname;
3743af4dcb2STim J. Robbins 			if ((f = fopen(fname, "r")) == NULL) {
3753af4dcb2STim J. Robbins 				warn("%s", fname);
3763af4dcb2STim J. Robbins 				rval = 1;
3773af4dcb2STim J. Robbins 				goto next;
3783af4dcb2STim J. Robbins 			}
3795d16412dSJuli Mallett 			if (inplace != NULL && *inplace == '\0')
3805d16412dSJuli Mallett 				unlink(fname);
3819b50d902SRodney W. Grimes 		}
3829b50d902SRodney W. Grimes 	}
3835f4cf81eSWolfram Schneider 	(void)ungetc(c, f);
3849b50d902SRodney W. Grimes 	return (1);
3859b50d902SRodney W. Grimes }
3869b50d902SRodney W. Grimes 
3879b50d902SRodney W. Grimes /*
3889b50d902SRodney W. Grimes  * Add a compilation unit to the linked list
3899b50d902SRodney W. Grimes  */
3909b50d902SRodney W. Grimes static void
3919b50d902SRodney W. Grimes add_compunit(type, s)
3929b50d902SRodney W. Grimes 	enum e_cut type;
3939b50d902SRodney W. Grimes 	char *s;
3949b50d902SRodney W. Grimes {
3959b50d902SRodney W. Grimes 	struct s_compunit *cu;
3969b50d902SRodney W. Grimes 
3978e33c0a0SDavid E. O'Brien 	if ((cu = malloc(sizeof(struct s_compunit))) == NULL)
3988e33c0a0SDavid E. O'Brien 		err(1, "malloc");
3999b50d902SRodney W. Grimes 	cu->type = type;
4009b50d902SRodney W. Grimes 	cu->s = s;
4019b50d902SRodney W. Grimes 	cu->next = NULL;
4029b50d902SRodney W. Grimes 	*cu_nextp = cu;
4039b50d902SRodney W. Grimes 	cu_nextp = &cu->next;
4049b50d902SRodney W. Grimes }
4059b50d902SRodney W. Grimes 
4069b50d902SRodney W. Grimes /*
4079b50d902SRodney W. Grimes  * Add a file to the linked list
4089b50d902SRodney W. Grimes  */
4099b50d902SRodney W. Grimes static void
4109b50d902SRodney W. Grimes add_file(s)
4119b50d902SRodney W. Grimes 	char *s;
4129b50d902SRodney W. Grimes {
4139b50d902SRodney W. Grimes 	struct s_flist *fp;
4149b50d902SRodney W. Grimes 
4158e33c0a0SDavid E. O'Brien 	if ((fp = malloc(sizeof(struct s_flist))) == NULL)
4168e33c0a0SDavid E. O'Brien 		err(1, "malloc");
4179b50d902SRodney W. Grimes 	fp->next = NULL;
4189b50d902SRodney W. Grimes 	*fl_nextp = fp;
4199b50d902SRodney W. Grimes 	fp->fname = s;
4209b50d902SRodney W. Grimes 	fl_nextp = &fp->next;
4219b50d902SRodney W. Grimes }
422839af0c1SJuli Mallett 
423839af0c1SJuli Mallett /*
424839af0c1SJuli Mallett  * Modify a pointer to a filename for inplace editing and reopen stdout
425839af0c1SJuli Mallett  */
426839af0c1SJuli Mallett static int
427d88e9d84SJuli Mallett inplace_edit(filename)
428d88e9d84SJuli Mallett 	char **filename;
429839af0c1SJuli Mallett {
430839af0c1SJuli Mallett 	struct stat orig;
431839af0c1SJuli Mallett 	int input, output;
432839af0c1SJuli Mallett 	char backup[MAXPATHLEN];
433839af0c1SJuli Mallett 	char *buffer;
434839af0c1SJuli Mallett 
435d88e9d84SJuli Mallett 	if (lstat(*filename, &orig) == -1)
436839af0c1SJuli Mallett 		err(1, "lstat");
437839af0c1SJuli Mallett 	if ((orig.st_mode & S_IFREG) == 0) {
438d88e9d84SJuli Mallett 		warnx("cannot inplace edit %s, not a regular file", *filename);
439839af0c1SJuli Mallett 		return -1;
440839af0c1SJuli Mallett 	}
441839af0c1SJuli Mallett 
4425d16412dSJuli Mallett 	if (*inplace == '\0') {
4435d16412dSJuli Mallett 		char template[] = "/tmp/sed.XXXXXXXXXX";
4445d16412dSJuli Mallett 
445e7b663efSBrian Feldman 		output = mkstemp(template);
446e7b663efSBrian Feldman 		if (output == -1)
447e7b663efSBrian Feldman 			err(1, "mkstemp");
4485d16412dSJuli Mallett 		strlcpy(backup, template, MAXPATHLEN);
4495d16412dSJuli Mallett 	} else {
450d88e9d84SJuli Mallett 		strlcpy(backup, *filename, MAXPATHLEN);
451839af0c1SJuli Mallett 		strlcat(backup, inplace, MAXPATHLEN);
452e7b663efSBrian Feldman 		output = open(backup, O_WRONLY | O_CREAT | O_EXCL);
453e7b663efSBrian Feldman 		if (output == -1)
454e7b663efSBrian Feldman 			err(1, "open(%s)", backup);
4555d16412dSJuli Mallett 	}
456839af0c1SJuli Mallett 
457d88e9d84SJuli Mallett 	input = open(*filename, O_RDONLY);
458839af0c1SJuli Mallett 	if (input == -1)
459d88e9d84SJuli Mallett 		err(1, "open(%s)", *filename);
460839af0c1SJuli Mallett 	if (fchmod(output, orig.st_mode & ~S_IFMT) == -1)
461839af0c1SJuli Mallett 		err(1, "chmod");
462839af0c1SJuli Mallett 	buffer = malloc(orig.st_size);
463839af0c1SJuli Mallett 	if (buffer == NULL)
464839af0c1SJuli Mallett 		errx(1, "malloc failed");
465839af0c1SJuli Mallett 	if (read(input, buffer, orig.st_size) == -1)
466839af0c1SJuli Mallett 		err(1, "read");
467839af0c1SJuli Mallett 	if (write(output, buffer, orig.st_size) == -1)
468839af0c1SJuli Mallett 		err(1, "write");
469839af0c1SJuli Mallett 	close(input);
470839af0c1SJuli Mallett 	close(output);
471d88e9d84SJuli Mallett 	freopen(*filename, "w", stdout);
472d88e9d84SJuli Mallett 	*filename = strdup(backup);
473839af0c1SJuli Mallett 	return 0;
474839af0c1SJuli Mallett }
475