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 518701af62SMaxim Sobolev #include <sys/types.h> 528701af62SMaxim Sobolev #include <sys/mman.h> 53839af0c1SJuli Mallett #include <sys/param.h> 54839af0c1SJuli Mallett #include <sys/stat.h> 559b50d902SRodney W. Grimes 5673a08bb2SPhilippe Charnier #include <err.h> 579b50d902SRodney W. Grimes #include <errno.h> 589b50d902SRodney W. Grimes #include <fcntl.h> 59726aebe5SAndrey A. Chernov #include <locale.h> 609b50d902SRodney W. Grimes #include <regex.h> 619b50d902SRodney W. Grimes #include <stddef.h> 629b50d902SRodney W. Grimes #include <stdio.h> 639b50d902SRodney W. Grimes #include <stdlib.h> 649b50d902SRodney W. Grimes #include <string.h> 659b50d902SRodney W. Grimes #include <unistd.h> 669b50d902SRodney W. Grimes 679b50d902SRodney W. Grimes #include "defs.h" 689b50d902SRodney W. Grimes #include "extern.h" 699b50d902SRodney W. Grimes 709b50d902SRodney W. Grimes /* 719b50d902SRodney W. Grimes * Linked list of units (strings and files) to be compiled 729b50d902SRodney W. Grimes */ 739b50d902SRodney W. Grimes struct s_compunit { 749b50d902SRodney W. Grimes struct s_compunit *next; 759b50d902SRodney W. Grimes enum e_cut {CU_FILE, CU_STRING} type; 769b50d902SRodney W. Grimes char *s; /* Pointer to string or fname */ 779b50d902SRodney W. Grimes }; 789b50d902SRodney W. Grimes 799b50d902SRodney W. Grimes /* 809b50d902SRodney W. Grimes * Linked list pointer to compilation units and pointer to current 819b50d902SRodney W. Grimes * next pointer. 829b50d902SRodney W. Grimes */ 839b50d902SRodney W. Grimes static struct s_compunit *script, **cu_nextp = &script; 849b50d902SRodney W. Grimes 859b50d902SRodney W. Grimes /* 869b50d902SRodney W. Grimes * Linked list of files to be processed 879b50d902SRodney W. Grimes */ 889b50d902SRodney W. Grimes struct s_flist { 899b50d902SRodney W. Grimes char *fname; 909b50d902SRodney W. Grimes struct s_flist *next; 919b50d902SRodney W. Grimes }; 929b50d902SRodney W. Grimes 939b50d902SRodney W. Grimes /* 949b50d902SRodney W. Grimes * Linked list pointer to files and pointer to current 959b50d902SRodney W. Grimes * next pointer. 969b50d902SRodney W. Grimes */ 979b50d902SRodney W. Grimes static struct s_flist *files, **fl_nextp = &files; 989b50d902SRodney W. Grimes 999b50d902SRodney W. Grimes int aflag, eflag, nflag; 100175de1e6SBrian Feldman int rflags = 0; 1013af4dcb2STim J. Robbins static int rval; /* Exit status */ 1029b50d902SRodney W. Grimes 1039b50d902SRodney W. Grimes /* 1049b50d902SRodney W. Grimes * Current file and line number; line numbers restart across compilation 1059b50d902SRodney W. Grimes * units, but span across input files. 1069b50d902SRodney W. Grimes */ 107e74bf75fSMark Murray const char *fname; /* File name. */ 108839af0c1SJuli Mallett const char *inplace; /* Inplace edit file extension. */ 1099b50d902SRodney W. Grimes u_long linenum; 1109b50d902SRodney W. Grimes int lastline; /* TRUE on the last line of the last file */ 1119b50d902SRodney W. Grimes 1123f330d7dSWarner Losh static void add_compunit(enum e_cut, char *); 1133f330d7dSWarner Losh static void add_file(char *); 114839af0c1SJuli Mallett static int inplace_edit(char **); 1153f330d7dSWarner Losh static void usage(void); 1169b50d902SRodney W. Grimes 1179b50d902SRodney W. Grimes int 1189b50d902SRodney W. Grimes main(argc, argv) 1199b50d902SRodney W. Grimes int argc; 1209b50d902SRodney W. Grimes char *argv[]; 1219b50d902SRodney W. Grimes { 1229b50d902SRodney W. Grimes int c, fflag; 123e6121e72SNick Sayer char *temp_arg; 1249b50d902SRodney W. Grimes 125726aebe5SAndrey A. Chernov (void) setlocale(LC_ALL, ""); 126726aebe5SAndrey A. Chernov 1279b50d902SRodney W. Grimes fflag = 0; 128839af0c1SJuli Mallett inplace = NULL; 129839af0c1SJuli Mallett 130839af0c1SJuli Mallett while ((c = getopt(argc, argv, "Eae:f:i:n")) != -1) 1319b50d902SRodney W. Grimes switch (c) { 132175de1e6SBrian Feldman case 'E': 133175de1e6SBrian Feldman rflags = REG_EXTENDED; 134175de1e6SBrian Feldman break; 1359b50d902SRodney W. Grimes case 'a': 1369b50d902SRodney W. Grimes aflag = 1; 1379b50d902SRodney W. Grimes break; 1389b50d902SRodney W. Grimes case 'e': 1399b50d902SRodney W. Grimes eflag = 1; 1408e33c0a0SDavid E. O'Brien if ((temp_arg = malloc(strlen(optarg) + 2)) == NULL) 1418e33c0a0SDavid E. O'Brien err(1, "malloc"); 142e6121e72SNick Sayer strcpy(temp_arg, optarg); 143e6121e72SNick Sayer strcat(temp_arg, "\n"); 144e6121e72SNick Sayer add_compunit(CU_STRING, temp_arg); 1459b50d902SRodney W. Grimes break; 1469b50d902SRodney W. Grimes case 'f': 1479b50d902SRodney W. Grimes fflag = 1; 1489b50d902SRodney W. Grimes add_compunit(CU_FILE, optarg); 1499b50d902SRodney W. Grimes break; 150839af0c1SJuli Mallett case 'i': 151839af0c1SJuli Mallett inplace = optarg; 152839af0c1SJuli Mallett break; 1539b50d902SRodney W. Grimes case 'n': 1549b50d902SRodney W. Grimes nflag = 1; 1559b50d902SRodney W. Grimes break; 1569b50d902SRodney W. Grimes default: 1579b50d902SRodney W. Grimes case '?': 15873a08bb2SPhilippe Charnier usage(); 1599b50d902SRodney W. Grimes } 1609b50d902SRodney W. Grimes argc -= optind; 1619b50d902SRodney W. Grimes argv += optind; 1629b50d902SRodney W. Grimes 1639b50d902SRodney W. Grimes /* First usage case; script is the first arg */ 1649b50d902SRodney W. Grimes if (!eflag && !fflag && *argv) { 1659b50d902SRodney W. Grimes add_compunit(CU_STRING, *argv); 1669b50d902SRodney W. Grimes argv++; 1679b50d902SRodney W. Grimes } 1689b50d902SRodney W. Grimes 1699b50d902SRodney W. Grimes compile(); 1709b50d902SRodney W. Grimes 1719b50d902SRodney W. Grimes /* Continue with first and start second usage */ 1729b50d902SRodney W. Grimes if (*argv) 1739b50d902SRodney W. Grimes for (; *argv; argv++) 1749b50d902SRodney W. Grimes add_file(*argv); 1759b50d902SRodney W. Grimes else 1769b50d902SRodney W. Grimes add_file(NULL); 1779b50d902SRodney W. Grimes process(); 1789b50d902SRodney W. Grimes cfclose(prog, NULL); 1799b50d902SRodney W. Grimes if (fclose(stdout)) 18073a08bb2SPhilippe Charnier err(1, "stdout"); 1813af4dcb2STim J. Robbins exit(rval); 1829b50d902SRodney W. Grimes } 1839b50d902SRodney W. Grimes 18473a08bb2SPhilippe Charnier static void 18573a08bb2SPhilippe Charnier usage() 18673a08bb2SPhilippe Charnier { 18773a08bb2SPhilippe Charnier (void)fprintf(stderr, "%s\n%s\n", 18890f7fe63SJuli Mallett "usage: sed script [-Ean] [-i extension] [file ...]", 18990f7fe63SJuli Mallett " sed [-an] [-i extension] [-e script] ... [-f script_file] ... [file ...]"); 19073a08bb2SPhilippe Charnier exit(1); 19173a08bb2SPhilippe Charnier } 19273a08bb2SPhilippe Charnier 1939b50d902SRodney W. Grimes /* 1949b50d902SRodney W. Grimes * Like fgets, but go through the chain of compilation units chaining them 1959b50d902SRodney W. Grimes * together. Empty strings and files are ignored. 1969b50d902SRodney W. Grimes */ 1979b50d902SRodney W. Grimes char * 198c56690efSArchie Cobbs cu_fgets(buf, n, more) 1999b50d902SRodney W. Grimes char *buf; 2009b50d902SRodney W. Grimes int n; 201c56690efSArchie Cobbs int *more; 2029b50d902SRodney W. Grimes { 2039b50d902SRodney W. Grimes static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 2049b50d902SRodney W. Grimes static FILE *f; /* Current open file */ 2059b50d902SRodney W. Grimes static char *s; /* Current pointer inside string */ 2069b50d902SRodney W. Grimes static char string_ident[30]; 2079b50d902SRodney W. Grimes char *p; 2089b50d902SRodney W. Grimes 2099b50d902SRodney W. Grimes again: 2109b50d902SRodney W. Grimes switch (state) { 2119b50d902SRodney W. Grimes case ST_EOF: 212c56690efSArchie Cobbs if (script == NULL) { 213c56690efSArchie Cobbs if (more != NULL) 214c56690efSArchie Cobbs *more = 0; 2159b50d902SRodney W. Grimes return (NULL); 216c56690efSArchie Cobbs } 2179b50d902SRodney W. Grimes linenum = 0; 2189b50d902SRodney W. Grimes switch (script->type) { 2199b50d902SRodney W. Grimes case CU_FILE: 2209b50d902SRodney W. Grimes if ((f = fopen(script->s, "r")) == NULL) 22173a08bb2SPhilippe Charnier err(1, "%s", script->s); 2229b50d902SRodney W. Grimes fname = script->s; 2239b50d902SRodney W. Grimes state = ST_FILE; 2249b50d902SRodney W. Grimes goto again; 2259b50d902SRodney W. Grimes case CU_STRING: 2269b50d902SRodney W. Grimes if ((snprintf(string_ident, 2279b50d902SRodney W. Grimes sizeof(string_ident), "\"%s\"", script->s)) >= 2289b50d902SRodney W. Grimes sizeof(string_ident) - 1) 2299b50d902SRodney W. Grimes (void)strcpy(string_ident + 2309b50d902SRodney W. Grimes sizeof(string_ident) - 6, " ...\""); 2319b50d902SRodney W. Grimes fname = string_ident; 2329b50d902SRodney W. Grimes s = script->s; 2339b50d902SRodney W. Grimes state = ST_STRING; 2349b50d902SRodney W. Grimes goto again; 2359b50d902SRodney W. Grimes } 2369b50d902SRodney W. Grimes case ST_FILE: 2379b50d902SRodney W. Grimes if ((p = fgets(buf, n, f)) != NULL) { 2389b50d902SRodney W. Grimes linenum++; 2399b50d902SRodney W. Grimes if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') 2409b50d902SRodney W. Grimes nflag = 1; 241c56690efSArchie Cobbs if (more != NULL) 242c56690efSArchie Cobbs *more = !feof(f); 2439b50d902SRodney W. Grimes return (p); 2449b50d902SRodney W. Grimes } 2459b50d902SRodney W. Grimes script = script->next; 2469b50d902SRodney W. Grimes (void)fclose(f); 2479b50d902SRodney W. Grimes state = ST_EOF; 2489b50d902SRodney W. Grimes goto again; 2499b50d902SRodney W. Grimes case ST_STRING: 2509b50d902SRodney W. Grimes if (linenum == 0 && s[0] == '#' && s[1] == 'n') 2519b50d902SRodney W. Grimes nflag = 1; 2529b50d902SRodney W. Grimes p = buf; 2539b50d902SRodney W. Grimes for (;;) { 2549b50d902SRodney W. Grimes if (n-- <= 1) { 2559b50d902SRodney W. Grimes *p = '\0'; 2569b50d902SRodney W. Grimes linenum++; 257c56690efSArchie Cobbs if (more != NULL) 258c56690efSArchie Cobbs *more = 1; 2599b50d902SRodney W. Grimes return (buf); 2609b50d902SRodney W. Grimes } 2619b50d902SRodney W. Grimes switch (*s) { 2629b50d902SRodney W. Grimes case '\0': 2639b50d902SRodney W. Grimes state = ST_EOF; 2649b50d902SRodney W. Grimes if (s == script->s) { 2659b50d902SRodney W. Grimes script = script->next; 2669b50d902SRodney W. Grimes goto again; 2679b50d902SRodney W. Grimes } else { 2689b50d902SRodney W. Grimes script = script->next; 2699b50d902SRodney W. Grimes *p = '\0'; 2709b50d902SRodney W. Grimes linenum++; 271c56690efSArchie Cobbs if (more != NULL) 272c56690efSArchie Cobbs *more = 0; 2739b50d902SRodney W. Grimes return (buf); 2749b50d902SRodney W. Grimes } 2759b50d902SRodney W. Grimes case '\n': 2769b50d902SRodney W. Grimes *p++ = '\n'; 2779b50d902SRodney W. Grimes *p = '\0'; 2789b50d902SRodney W. Grimes s++; 2799b50d902SRodney W. Grimes linenum++; 280c56690efSArchie Cobbs if (more != NULL) 281c56690efSArchie Cobbs *more = 0; 2829b50d902SRodney W. Grimes return (buf); 2839b50d902SRodney W. Grimes default: 2849b50d902SRodney W. Grimes *p++ = *s++; 2859b50d902SRodney W. Grimes } 2869b50d902SRodney W. Grimes } 2879b50d902SRodney W. Grimes } 2889b50d902SRodney W. Grimes /* NOTREACHED */ 28973a08bb2SPhilippe Charnier return (NULL); 2909b50d902SRodney W. Grimes } 2919b50d902SRodney W. Grimes 2929b50d902SRodney W. Grimes /* 2939b50d902SRodney W. Grimes * Like fgets, but go through the list of files chaining them together. 2949b50d902SRodney W. Grimes * Set len to the length of the line. 2959b50d902SRodney W. Grimes */ 2969b50d902SRodney W. Grimes int 2979b50d902SRodney W. Grimes mf_fgets(sp, spflag) 2989b50d902SRodney W. Grimes SPACE *sp; 2999b50d902SRodney W. Grimes enum e_spflag spflag; 3009b50d902SRodney W. Grimes { 3019b50d902SRodney W. Grimes static FILE *f; /* Current open file */ 3029b50d902SRodney W. Grimes size_t len; 3035f4cf81eSWolfram Schneider char *p; 3045f4cf81eSWolfram Schneider int c; 3058701af62SMaxim Sobolev static int firstfile; 3069b50d902SRodney W. Grimes 3078701af62SMaxim Sobolev if (f == NULL) { 3088701af62SMaxim Sobolev /* stdin? */ 3099b50d902SRodney W. Grimes if (files->fname == NULL) { 310839af0c1SJuli Mallett if (inplace != NULL) 311839af0c1SJuli Mallett errx(1, "-i may not be used with stdin"); 3129b50d902SRodney W. Grimes f = stdin; 3139b50d902SRodney W. Grimes fname = "stdin"; 3148701af62SMaxim Sobolev } 3158701af62SMaxim Sobolev firstfile = 1; 3168701af62SMaxim Sobolev } 3178701af62SMaxim Sobolev 3188701af62SMaxim Sobolev for (;;) { 3198701af62SMaxim Sobolev if (f != NULL && (c = getc(f)) != EOF) { 3208701af62SMaxim Sobolev (void)ungetc(c, f); 3218701af62SMaxim Sobolev break; 3228701af62SMaxim Sobolev } 3238701af62SMaxim Sobolev /* If we are here then either eof or no files are open yet */ 3248701af62SMaxim Sobolev if (f == stdin) { 325254fac85STim J. Robbins sp->len = 0; 3268701af62SMaxim Sobolev lastline = 1; 3278701af62SMaxim Sobolev return (0); 3288701af62SMaxim Sobolev } 3298701af62SMaxim Sobolev if (f != NULL) { 3308701af62SMaxim Sobolev fclose(f); 3318701af62SMaxim Sobolev } 3328701af62SMaxim Sobolev if (firstfile == 0) { 3338701af62SMaxim Sobolev files = files->next; 3348701af62SMaxim Sobolev } else 3358701af62SMaxim Sobolev firstfile = 0; 3368701af62SMaxim Sobolev if (files == NULL) { 337254fac85STim J. Robbins sp->len = 0; 3388701af62SMaxim Sobolev lastline = 1; 3398701af62SMaxim Sobolev return (0); 3408701af62SMaxim Sobolev } 341839af0c1SJuli Mallett if (inplace != NULL) { 342839af0c1SJuli Mallett if (inplace_edit(&files->fname) == -1) 343839af0c1SJuli Mallett continue; 344839af0c1SJuli Mallett } 3459b50d902SRodney W. Grimes fname = files->fname; 3463af4dcb2STim J. Robbins if ((f = fopen(fname, "r")) == NULL) { 3473af4dcb2STim J. Robbins warn("%s", fname); 3486689fb2bSTim J. Robbins rval = 1; 3493af4dcb2STim J. Robbins continue; 3503af4dcb2STim J. Robbins } 3515d16412dSJuli Mallett if (inplace != NULL && *inplace == '\0') 3525d16412dSJuli Mallett unlink(fname); 3539b50d902SRodney W. Grimes } 3549b50d902SRodney W. Grimes /* 3558701af62SMaxim Sobolev * We are here only when f is open and we still have something to 3568701af62SMaxim Sobolev * read from it. 3578701af62SMaxim Sobolev * 3589b50d902SRodney W. Grimes * Use fgetln so that we can handle essentially infinite input data. 3595f4cf81eSWolfram Schneider * Can't use the pointer into the stdio buffer as the process space 3605f4cf81eSWolfram Schneider * because the ungetc() can cause it to move. 3619b50d902SRodney W. Grimes */ 3629b50d902SRodney W. Grimes p = fgetln(f, &len); 3639b50d902SRodney W. Grimes if (ferror(f)) 36473a08bb2SPhilippe Charnier errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); 365ed92199dSTim J. Robbins if (len != 0 && p[len - 1] == '\n') 366ed92199dSTim J. Robbins len--; 3679b50d902SRodney W. Grimes cspace(sp, p, len, spflag); 3689b50d902SRodney W. Grimes 3699b50d902SRodney W. Grimes linenum++; 3708701af62SMaxim Sobolev if (files->next == NULL) { 3718701af62SMaxim Sobolev if ((c = getc(f)) != EOF) { 3725f4cf81eSWolfram Schneider (void)ungetc(c, f); 3738701af62SMaxim Sobolev } else { 3748701af62SMaxim Sobolev lastline = 1; 3758701af62SMaxim Sobolev } 3768701af62SMaxim Sobolev } 3778701af62SMaxim Sobolev 3789b50d902SRodney W. Grimes return (1); 3799b50d902SRodney W. Grimes } 3809b50d902SRodney W. Grimes 3819b50d902SRodney W. Grimes /* 3829b50d902SRodney W. Grimes * Add a compilation unit to the linked list 3839b50d902SRodney W. Grimes */ 3849b50d902SRodney W. Grimes static void 3859b50d902SRodney W. Grimes add_compunit(type, s) 3869b50d902SRodney W. Grimes enum e_cut type; 3879b50d902SRodney W. Grimes char *s; 3889b50d902SRodney W. Grimes { 3899b50d902SRodney W. Grimes struct s_compunit *cu; 3909b50d902SRodney W. Grimes 3918e33c0a0SDavid E. O'Brien if ((cu = malloc(sizeof(struct s_compunit))) == NULL) 3928e33c0a0SDavid E. O'Brien err(1, "malloc"); 3939b50d902SRodney W. Grimes cu->type = type; 3949b50d902SRodney W. Grimes cu->s = s; 3959b50d902SRodney W. Grimes cu->next = NULL; 3969b50d902SRodney W. Grimes *cu_nextp = cu; 3979b50d902SRodney W. Grimes cu_nextp = &cu->next; 3989b50d902SRodney W. Grimes } 3999b50d902SRodney W. Grimes 4009b50d902SRodney W. Grimes /* 4019b50d902SRodney W. Grimes * Add a file to the linked list 4029b50d902SRodney W. Grimes */ 4039b50d902SRodney W. Grimes static void 4049b50d902SRodney W. Grimes add_file(s) 4059b50d902SRodney W. Grimes char *s; 4069b50d902SRodney W. Grimes { 4079b50d902SRodney W. Grimes struct s_flist *fp; 4089b50d902SRodney W. Grimes 4098e33c0a0SDavid E. O'Brien if ((fp = malloc(sizeof(struct s_flist))) == NULL) 4108e33c0a0SDavid E. O'Brien err(1, "malloc"); 4119b50d902SRodney W. Grimes fp->next = NULL; 4129b50d902SRodney W. Grimes *fl_nextp = fp; 4139b50d902SRodney W. Grimes fp->fname = s; 4149b50d902SRodney W. Grimes fl_nextp = &fp->next; 4159b50d902SRodney W. Grimes } 416839af0c1SJuli Mallett 417839af0c1SJuli Mallett /* 418839af0c1SJuli Mallett * Modify a pointer to a filename for inplace editing and reopen stdout 419839af0c1SJuli Mallett */ 420839af0c1SJuli Mallett static int 421d88e9d84SJuli Mallett inplace_edit(filename) 422d88e9d84SJuli Mallett char **filename; 423839af0c1SJuli Mallett { 424839af0c1SJuli Mallett struct stat orig; 425839af0c1SJuli Mallett int input, output; 426839af0c1SJuli Mallett char backup[MAXPATHLEN]; 427839af0c1SJuli Mallett char *buffer; 428839af0c1SJuli Mallett 429d88e9d84SJuli Mallett if (lstat(*filename, &orig) == -1) 430839af0c1SJuli Mallett err(1, "lstat"); 431839af0c1SJuli Mallett if ((orig.st_mode & S_IFREG) == 0) { 432d88e9d84SJuli Mallett warnx("cannot inplace edit %s, not a regular file", *filename); 433839af0c1SJuli Mallett return -1; 434839af0c1SJuli Mallett } 435839af0c1SJuli Mallett 4365d16412dSJuli Mallett if (*inplace == '\0') { 4375d16412dSJuli Mallett char template[] = "/tmp/sed.XXXXXXXXXX"; 4385d16412dSJuli Mallett 439e7b663efSBrian Feldman output = mkstemp(template); 440e7b663efSBrian Feldman if (output == -1) 441e7b663efSBrian Feldman err(1, "mkstemp"); 4425d16412dSJuli Mallett strlcpy(backup, template, MAXPATHLEN); 4435d16412dSJuli Mallett } else { 444d88e9d84SJuli Mallett strlcpy(backup, *filename, MAXPATHLEN); 445839af0c1SJuli Mallett strlcat(backup, inplace, MAXPATHLEN); 4468701af62SMaxim Sobolev output = open(backup, O_WRONLY | O_CREAT | O_TRUNC); 447e7b663efSBrian Feldman if (output == -1) 448e7b663efSBrian Feldman err(1, "open(%s)", backup); 4495d16412dSJuli Mallett } 450839af0c1SJuli Mallett 451d88e9d84SJuli Mallett input = open(*filename, O_RDONLY); 452839af0c1SJuli Mallett if (input == -1) 453d88e9d84SJuli Mallett err(1, "open(%s)", *filename); 454839af0c1SJuli Mallett if (fchmod(output, orig.st_mode & ~S_IFMT) == -1) 455839af0c1SJuli Mallett err(1, "chmod"); 4568701af62SMaxim Sobolev buffer = (char *)mmap(0, orig.st_size, PROT_READ, MAP_SHARED, input, 0); 4578701af62SMaxim Sobolev if (buffer == MAP_FAILED) 4588701af62SMaxim Sobolev err(1, "mmap(%s)", *filename); 459839af0c1SJuli Mallett if (write(output, buffer, orig.st_size) == -1) 4608701af62SMaxim Sobolev err(1, "write(%s)", backup); 4618701af62SMaxim Sobolev if (munmap(buffer, orig.st_size) == -1) 4628701af62SMaxim Sobolev err(1, "munmap(%s)", *filename); 463839af0c1SJuli Mallett close(input); 464839af0c1SJuli Mallett close(output); 465d88e9d84SJuli Mallett freopen(*filename, "w", stdout); 466d88e9d84SJuli Mallett *filename = strdup(backup); 467839af0c1SJuli Mallett return 0; 468839af0c1SJuli Mallett } 469