Home | History | Annotate | Line # | Download | only in sed
main.c revision 1.36
      1  1.36  christos /*	$NetBSD: main.c,v 1.36 2020/05/15 22:39:54 christos Exp $	*/
      2   1.9       tls 
      3   1.1       alm /*-
      4  1.22  christos  * Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson.
      5  1.22  christos  * Copyright (c) 1992 Diomidis Spinellis.
      6   1.5       cgd  * Copyright (c) 1992, 1993
      7   1.5       cgd  *	The Regents of the University of California.  All rights reserved.
      8   1.1       alm  *
      9   1.1       alm  * This code is derived from software contributed to Berkeley by
     10   1.1       alm  * Diomidis Spinellis of Imperial College, University of London.
     11   1.1       alm  *
     12   1.1       alm  * Redistribution and use in source and binary forms, with or without
     13   1.1       alm  * modification, are permitted provided that the following conditions
     14   1.1       alm  * are met:
     15   1.1       alm  * 1. Redistributions of source code must retain the above copyright
     16   1.1       alm  *    notice, this list of conditions and the following disclaimer.
     17   1.1       alm  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1       alm  *    notice, this list of conditions and the following disclaimer in the
     19   1.1       alm  *    documentation and/or other materials provided with the distribution.
     20  1.14       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.14       agc  *    may be used to endorse or promote products derived from this software
     22  1.14       agc  *    without specific prior written permission.
     23  1.14       agc  *
     24  1.14       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.14       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.14       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.14       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.14       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.14       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.14       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.14       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.14       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.14       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.14       agc  * SUCH DAMAGE.
     35  1.14       agc  */
     36  1.14       agc 
     37  1.17   gdamore #if HAVE_NBTOOL_CONFIG_H
     38  1.17   gdamore #include "nbtool_config.h"
     39  1.17   gdamore #endif
     40  1.17   gdamore 
     41  1.10     lukem #include <sys/cdefs.h>
     42  1.36  christos __RCSID("$NetBSD: main.c,v 1.36 2020/05/15 22:39:54 christos Exp $");
     43  1.22  christos #ifdef __FBSDID
     44  1.22  christos __FBSDID("$FreeBSD: head/usr.bin/sed/main.c 252231 2013-06-26 04:14:19Z pfg $");
     45  1.22  christos #endif
     46  1.22  christos 
     47   1.1       alm #ifndef lint
     48  1.18     lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993\
     49  1.22  christos 	The Regents of the University of California.  All rights reserved.");
     50  1.22  christos #endif
     51   1.1       alm 
     52  1.30  christos #if 0
     53  1.30  christos static const char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/3/94";
     54  1.30  christos #endif
     55  1.30  christos 
     56   1.1       alm #include <sys/types.h>
     57  1.22  christos #include <sys/mman.h>
     58  1.22  christos #include <sys/param.h>
     59  1.22  christos #include <sys/stat.h>
     60   1.1       alm 
     61  1.22  christos #include <err.h>
     62   1.1       alm #include <errno.h>
     63   1.1       alm #include <fcntl.h>
     64  1.34  christos #include <libgen.h>
     65  1.21       tnn #include <limits.h>
     66  1.22  christos #include <locale.h>
     67   1.1       alm #include <regex.h>
     68   1.1       alm #include <stddef.h>
     69  1.22  christos #define _WITH_GETLINE
     70   1.1       alm #include <stdio.h>
     71   1.1       alm #include <stdlib.h>
     72   1.1       alm #include <string.h>
     73   1.1       alm #include <unistd.h>
     74   1.1       alm 
     75   1.1       alm #include "defs.h"
     76   1.1       alm #include "extern.h"
     77   1.1       alm 
     78   1.1       alm /*
     79  1.34  christos  * Linked list of units (strings and files) to be compiled
     80  1.34  christos  */
     81  1.34  christos struct s_compunit {
     82  1.34  christos 	struct s_compunit *next;
     83  1.34  christos 	enum e_cut {CU_FILE, CU_STRING} type;
     84  1.34  christos 	char *s;			/* Pointer to string or fname */
     85  1.34  christos };
     86  1.34  christos 
     87  1.34  christos /*
     88   1.1       alm  * Linked list pointer to compilation units and pointer to current
     89   1.1       alm  * next pointer.
     90   1.1       alm  */
     91  1.34  christos static struct s_compunit *script, **cu_nextp = &script;
     92  1.34  christos 
     93  1.34  christos /*
     94  1.34  christos  * Linked list of files to be processed
     95  1.34  christos  */
     96  1.34  christos struct s_flist {
     97  1.34  christos 	char *fname;
     98  1.34  christos 	struct s_flist *next;
     99  1.34  christos };
    100   1.1       alm 
    101   1.1       alm /*
    102   1.1       alm  * Linked list pointer to files and pointer to current
    103   1.1       alm  * next pointer.
    104   1.1       alm  */
    105  1.34  christos static struct s_flist *files, **fl_nextp = &files;
    106   1.1       alm 
    107  1.34  christos FILE *infile;			/* Current input file */
    108  1.34  christos FILE *outfile;			/* Current output file */
    109  1.34  christos 
    110  1.34  christos int aflag, eflag, nflag;
    111  1.22  christos int rflags = 0;
    112  1.34  christos static int rval;		/* Exit status */
    113  1.34  christos 
    114  1.34  christos static int ispan;		/* Whether inplace editing spans across files */
    115  1.22  christos 
    116  1.34  christos /*
    117  1.34  christos  * Current file and line number; line numbers restart across compilation
    118  1.34  christos  * units, but span across input files.  The latter is optional if editing
    119  1.34  christos  * in place.
    120  1.34  christos  */
    121  1.34  christos const char *fname;		/* File name. */
    122  1.34  christos const char *outfname;		/* Output file name */
    123  1.34  christos static char oldfname[PATH_MAX];	/* Old file name (for in-place editing) */
    124  1.34  christos static char tmpfname[PATH_MAX];	/* Temporary file name (for in-place editing) */
    125  1.34  christos static const char *inplace;	/* Inplace edit file extension. */
    126  1.34  christos u_long linenum;
    127   1.1       alm 
    128  1.13       wiz static void add_compunit(enum e_cut, char *);
    129  1.13       wiz static void add_file(char *);
    130  1.24     joerg static void usage(void) __dead;
    131   1.1       alm 
    132   1.1       alm int
    133  1.13       wiz main(int argc, char *argv[])
    134   1.1       alm {
    135   1.1       alm 	int c, fflag;
    136  1.22  christos 	char *temp_arg;
    137  1.22  christos 
    138  1.22  christos 	setprogname(argv[0]);
    139  1.22  christos 	(void) setlocale(LC_ALL, "");
    140   1.1       alm 
    141   1.1       alm 	fflag = 0;
    142  1.22  christos 	inplace = NULL;
    143  1.22  christos 
    144  1.25  christos 	while ((c = getopt(argc, argv, "EI::ae:f:i::lnru")) != -1)
    145   1.1       alm 		switch (c) {
    146  1.22  christos 		case 'r':		/* Gnu sed compat */
    147  1.22  christos 		case 'E':
    148  1.22  christos 			rflags = REG_EXTENDED;
    149  1.22  christos 			break;
    150  1.22  christos 		case 'I':
    151  1.22  christos 			inplace = optarg ? optarg : __UNCONST("");
    152  1.22  christos 			ispan = 1;	/* span across input files */
    153  1.22  christos 			break;
    154   1.1       alm 		case 'a':
    155   1.1       alm 			aflag = 1;
    156   1.1       alm 			break;
    157   1.1       alm 		case 'e':
    158   1.1       alm 			eflag = 1;
    159  1.22  christos 			temp_arg = xmalloc(strlen(optarg) + 2);
    160  1.22  christos 			strcpy(temp_arg, optarg);
    161  1.22  christos 			strcat(temp_arg, "\n");
    162  1.22  christos 			add_compunit(CU_STRING, temp_arg);
    163   1.1       alm 			break;
    164   1.1       alm 		case 'f':
    165   1.1       alm 			fflag = 1;
    166   1.1       alm 			add_compunit(CU_FILE, optarg);
    167   1.1       alm 			break;
    168  1.22  christos 		case 'i':
    169  1.22  christos 			inplace = optarg ? optarg : __UNCONST("");
    170  1.22  christos 			ispan = 0;	/* don't span across input files */
    171  1.22  christos 			break;
    172  1.22  christos 		case 'l':
    173  1.23  christos #ifdef _IOLBF
    174  1.23  christos 			c = setvbuf(stdout, NULL, _IOLBF, 0);
    175  1.23  christos #else
    176  1.23  christos 			c = setlinebuf(stdout);
    177  1.23  christos #endif
    178  1.23  christos 			if (c)
    179  1.23  christos 				warn("setting line buffered output failed");
    180  1.22  christos 			break;
    181   1.1       alm 		case 'n':
    182   1.1       alm 			nflag = 1;
    183   1.1       alm 			break;
    184  1.25  christos 		case 'u':
    185  1.25  christos #ifdef _IONBF
    186  1.25  christos 			c = setvbuf(stdout, NULL, _IONBF, 0);
    187  1.25  christos #else
    188  1.25  christos 			c = -1;
    189  1.25  christos 			errno = EOPNOTSUPP;
    190  1.25  christos #endif
    191  1.25  christos 			if (c)
    192  1.25  christos 				warn("setting unbuffered output failed");
    193  1.25  christos 			break;
    194   1.1       alm 		default:
    195   1.1       alm 		case '?':
    196  1.22  christos 			usage();
    197   1.1       alm 		}
    198   1.1       alm 	argc -= optind;
    199   1.1       alm 	argv += optind;
    200   1.1       alm 
    201   1.1       alm 	/* First usage case; script is the first arg */
    202   1.1       alm 	if (!eflag && !fflag && *argv) {
    203   1.1       alm 		add_compunit(CU_STRING, *argv);
    204   1.1       alm 		argv++;
    205   1.1       alm 	}
    206   1.1       alm 
    207   1.1       alm 	compile();
    208   1.1       alm 
    209   1.1       alm 	/* Continue with first and start second usage */
    210   1.1       alm 	if (*argv)
    211   1.1       alm 		for (; *argv; argv++)
    212   1.1       alm 			add_file(*argv);
    213   1.1       alm 	else
    214   1.1       alm 		add_file(NULL);
    215  1.34  christos 	process();
    216  1.34  christos 	cfclose(prog, NULL);
    217   1.1       alm 	if (fclose(stdout))
    218  1.22  christos 		err(1, "stdout");
    219  1.22  christos 	exit(rval);
    220  1.22  christos }
    221  1.22  christos 
    222  1.22  christos static void
    223  1.22  christos usage(void)
    224  1.22  christos {
    225  1.25  christos 	(void)fprintf(stderr,
    226  1.26       wiz 	    "Usage:  %s [-aElnru] command [file ...]\n"
    227  1.27  christos 	    "\t%s [-aElnru] [-e command] [-f command_file] [-I[extension]]\n"
    228  1.27  christos 	    "\t    [-i[extension]] [file ...]\n", getprogname(), getprogname());
    229  1.22  christos 	exit(1);
    230   1.1       alm }
    231   1.1       alm 
    232   1.1       alm /*
    233  1.34  christos  * Like fgets, but go through the chain of compilation units chaining them
    234  1.34  christos  * together.  Empty strings and files are ignored.
    235  1.34  christos  */
    236  1.34  christos char *
    237  1.34  christos cu_fgets(char *buf, int n, int *more)
    238  1.34  christos {
    239  1.34  christos 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
    240  1.34  christos 	static FILE *f;		/* Current open file */
    241  1.34  christos 	static char *s;		/* Current pointer inside string */
    242  1.34  christos 	static char string_ident[30];
    243  1.34  christos 	char *p;
    244  1.34  christos 
    245  1.34  christos again:
    246  1.34  christos 	switch (state) {
    247  1.34  christos 	case ST_EOF:
    248  1.34  christos 		if (script == NULL) {
    249  1.34  christos 			if (more != NULL)
    250  1.34  christos 				*more = 0;
    251  1.34  christos 			return (NULL);
    252  1.34  christos 		}
    253  1.34  christos 		linenum = 0;
    254  1.34  christos 		switch (script->type) {
    255  1.34  christos 		case CU_FILE:
    256  1.34  christos 			if ((f = fopen(script->s, "r")) == NULL)
    257  1.34  christos 				err(1, "%s", script->s);
    258  1.34  christos 			fname = script->s;
    259  1.34  christos 			state = ST_FILE;
    260  1.34  christos 			goto again;
    261  1.34  christos 		case CU_STRING:
    262  1.34  christos 			if (((size_t)snprintf(string_ident,
    263  1.34  christos 			    sizeof(string_ident), "\"%s\"", script->s)) >=
    264  1.34  christos 			    sizeof(string_ident) - 1)
    265  1.34  christos 				(void)strcpy(string_ident +
    266  1.34  christos 				    sizeof(string_ident) - 6, " ...\"");
    267  1.34  christos 			fname = string_ident;
    268  1.34  christos 			s = script->s;
    269  1.34  christos 			state = ST_STRING;
    270  1.34  christos 			goto again;
    271  1.35  christos 		default:
    272  1.35  christos 			abort();
    273  1.34  christos 		}
    274  1.34  christos 	case ST_FILE:
    275  1.34  christos 		if ((p = fgets(buf, n, f)) != NULL) {
    276  1.34  christos 			linenum++;
    277  1.34  christos 			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
    278  1.34  christos 				nflag = 1;
    279  1.34  christos 			if (more != NULL)
    280  1.34  christos 				*more = !feof(f);
    281  1.34  christos 			return (p);
    282  1.34  christos 		}
    283  1.34  christos 		script = script->next;
    284  1.34  christos 		(void)fclose(f);
    285  1.34  christos 		state = ST_EOF;
    286  1.34  christos 		goto again;
    287  1.34  christos 	case ST_STRING:
    288  1.34  christos 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
    289  1.34  christos 			nflag = 1;
    290  1.34  christos 		p = buf;
    291  1.34  christos 		for (;;) {
    292  1.34  christos 			if (n-- <= 1) {
    293  1.34  christos 				*p = '\0';
    294  1.34  christos 				linenum++;
    295  1.34  christos 				if (more != NULL)
    296  1.34  christos 					*more = 1;
    297  1.34  christos 				return (buf);
    298  1.34  christos 			}
    299  1.34  christos 			switch (*s) {
    300  1.34  christos 			case '\0':
    301  1.34  christos 				state = ST_EOF;
    302  1.34  christos 				if (s == script->s) {
    303  1.34  christos 					script = script->next;
    304  1.34  christos 					goto again;
    305  1.34  christos 				} else {
    306  1.34  christos 					script = script->next;
    307  1.34  christos 					*p = '\0';
    308  1.34  christos 					linenum++;
    309  1.34  christos 					if (more != NULL)
    310  1.34  christos 						*more = 0;
    311  1.34  christos 					return (buf);
    312  1.34  christos 				}
    313  1.34  christos 			case '\n':
    314  1.34  christos 				*p++ = '\n';
    315  1.34  christos 				*p = '\0';
    316  1.34  christos 				s++;
    317  1.34  christos 				linenum++;
    318  1.34  christos 				if (more != NULL)
    319  1.34  christos 					*more = 0;
    320  1.34  christos 				return (buf);
    321  1.34  christos 			default:
    322  1.34  christos 				*p++ = *s++;
    323  1.34  christos 			}
    324  1.34  christos 		}
    325  1.34  christos 	}
    326  1.34  christos 	/* NOTREACHED */
    327  1.34  christos 	return (NULL);
    328  1.34  christos }
    329  1.34  christos 
    330  1.34  christos /*
    331  1.34  christos  * Like fgets, but go through the list of files chaining them together.
    332  1.34  christos  * Set len to the length of the line.
    333  1.34  christos  */
    334  1.34  christos int
    335  1.34  christos mf_fgets(SPACE *sp, enum e_spflag spflag)
    336  1.34  christos {
    337  1.34  christos 	struct stat sb;
    338  1.34  christos 	size_t len;
    339  1.34  christos 	static char *p = NULL;
    340  1.34  christos 	static size_t plen = 0;
    341  1.34  christos 	int c;
    342  1.34  christos 	static int firstfile;
    343  1.34  christos 
    344  1.34  christos 	if (infile == NULL) {
    345  1.34  christos 		/* stdin? */
    346  1.34  christos 		if (files->fname == NULL) {
    347  1.34  christos 			if (inplace != NULL)
    348  1.34  christos 				errx(1, "-I or -i may not be used with stdin");
    349  1.34  christos 			infile = stdin;
    350  1.34  christos 			fname = "stdin";
    351  1.34  christos 			outfile = stdout;
    352  1.34  christos 			outfname = "stdout";
    353  1.34  christos 		}
    354  1.34  christos 		firstfile = 1;
    355  1.34  christos 	}
    356  1.34  christos 
    357  1.34  christos 	for (;;) {
    358  1.34  christos 		if (infile != NULL && (c = getc(infile)) != EOF) {
    359  1.34  christos 			(void)ungetc(c, infile);
    360  1.34  christos 			break;
    361  1.34  christos 		}
    362  1.34  christos 		/* If we are here then either eof or no files are open yet */
    363  1.34  christos 		if (infile == stdin) {
    364  1.34  christos 			sp->len = 0;
    365  1.34  christos 			return (0);
    366  1.34  christos 		}
    367  1.34  christos 		if (infile != NULL) {
    368  1.34  christos 			fclose(infile);
    369  1.34  christos 			if (*oldfname != '\0') {
    370  1.34  christos 				/* if there was a backup file, remove it */
    371  1.34  christos 				unlink(oldfname);
    372  1.34  christos 				/*
    373  1.34  christos 				 * Backup the original.  Note that hard links
    374  1.34  christos 				 * are not supported on all filesystems.
    375  1.34  christos 				 */
    376  1.34  christos 				if ((link(fname, oldfname) != 0) &&
    377  1.34  christos 				   (rename(fname, oldfname) != 0)) {
    378  1.34  christos 					warn("rename()");
    379  1.34  christos 					if (*tmpfname)
    380  1.34  christos 						unlink(tmpfname);
    381  1.34  christos 					exit(1);
    382  1.34  christos 				}
    383  1.34  christos 				*oldfname = '\0';
    384  1.34  christos 			}
    385  1.34  christos 			if (*tmpfname != '\0') {
    386  1.34  christos 				if (outfile != NULL && outfile != stdout)
    387  1.34  christos 					if (fclose(outfile) != 0) {
    388  1.34  christos 						warn("fclose()");
    389  1.34  christos 						unlink(tmpfname);
    390  1.34  christos 						exit(1);
    391  1.34  christos 					}
    392  1.34  christos 				outfile = NULL;
    393  1.34  christos 				if (rename(tmpfname, fname) != 0) {
    394  1.34  christos 					/* this should not happen really! */
    395  1.34  christos 					warn("rename()");
    396  1.34  christos 					unlink(tmpfname);
    397  1.34  christos 					exit(1);
    398  1.34  christos 				}
    399  1.34  christos 				*tmpfname = '\0';
    400  1.34  christos 			}
    401  1.34  christos 			outfname = NULL;
    402  1.34  christos 		}
    403  1.34  christos 		if (firstfile == 0)
    404  1.34  christos 			files = files->next;
    405  1.34  christos 		else
    406  1.34  christos 			firstfile = 0;
    407  1.34  christos 		if (files == NULL) {
    408  1.34  christos 			sp->len = 0;
    409  1.34  christos 			return (0);
    410  1.34  christos 		}
    411  1.34  christos 		fname = files->fname;
    412  1.34  christos 		if (inplace != NULL) {
    413  1.34  christos 			if (lstat(fname, &sb) != 0)
    414  1.34  christos 				err(1, "%s", fname);
    415  1.34  christos 			if (!(sb.st_mode & S_IFREG))
    416  1.34  christos 				errx(1, "%s: %s %s", fname,
    417  1.34  christos 				    "in-place editing only",
    418  1.34  christos 				    "works for regular files");
    419  1.34  christos 			if (*inplace != '\0') {
    420  1.34  christos 				strlcpy(oldfname, fname,
    421  1.34  christos 				    sizeof(oldfname));
    422  1.34  christos 				len = strlcat(oldfname, inplace,
    423  1.34  christos 				    sizeof(oldfname));
    424  1.34  christos 				if (len > sizeof(oldfname))
    425  1.34  christos 					errx(1, "%s: name too long", fname);
    426  1.34  christos 			}
    427  1.34  christos 			char d_name[PATH_MAX], f_name[PATH_MAX];
    428  1.34  christos 			(void)strlcpy(d_name, fname, sizeof(d_name));
    429  1.34  christos 			(void)strlcpy(f_name, fname, sizeof(f_name));
    430  1.34  christos 			len = (size_t)snprintf(tmpfname, sizeof(tmpfname),
    431  1.34  christos 			    "%s/.!%ld!%s", dirname(d_name), (long)getpid(),
    432  1.34  christos 			    basename(f_name));
    433  1.34  christos 			if (len >= sizeof(tmpfname))
    434  1.34  christos 				errx(1, "%s: name too long", fname);
    435  1.34  christos 			unlink(tmpfname);
    436  1.34  christos 			if (outfile != NULL && outfile != stdout)
    437  1.34  christos 				fclose(outfile);
    438  1.34  christos 			if ((outfile = fopen(tmpfname, "w")) == NULL)
    439  1.34  christos 				err(1, "%s", fname);
    440  1.34  christos 			fchown(fileno(outfile), sb.st_uid, sb.st_gid);
    441  1.34  christos 			fchmod(fileno(outfile), sb.st_mode & ALLPERMS);
    442  1.34  christos 			outfname = tmpfname;
    443  1.34  christos 			if (!ispan) {
    444  1.34  christos 				linenum = 0;
    445  1.34  christos 				resetstate();
    446  1.34  christos 			}
    447  1.34  christos 		} else {
    448  1.34  christos 			outfile = stdout;
    449  1.34  christos 			outfname = "stdout";
    450  1.34  christos 		}
    451  1.34  christos 		if ((infile = fopen(fname, "r")) == NULL) {
    452  1.34  christos 			warn("%s", fname);
    453  1.34  christos 			rval = 1;
    454  1.34  christos 			continue;
    455  1.34  christos 		}
    456  1.34  christos 	}
    457  1.34  christos 	/*
    458  1.34  christos 	 * We are here only when infile is open and we still have something
    459  1.34  christos 	 * to read from it.
    460  1.34  christos 	 *
    461  1.34  christos 	 * Use getline() so that we can handle essentially infinite input
    462  1.34  christos 	 * data.  The p and plen are static so each invocation gives
    463  1.34  christos 	 * getline() the same buffer which is expanded as needed.
    464  1.34  christos 	 */
    465  1.34  christos 	ssize_t slen = getline(&p, &plen, infile);
    466  1.34  christos 	if (slen == -1)
    467  1.34  christos 		err(1, "%s", fname);
    468  1.36  christos 	if (slen != 0 && p[slen - 1] == '\n') {
    469  1.36  christos 		sp->append_newline = 1;
    470  1.34  christos 		slen--;
    471  1.36  christos 	} else if (!lastline()) {
    472  1.36  christos 		sp->append_newline = 1;
    473  1.36  christos 	} else {
    474  1.36  christos 		sp->append_newline = 0;
    475  1.36  christos 	}
    476  1.34  christos 	cspace(sp, p, (size_t)slen, spflag);
    477  1.34  christos 
    478  1.34  christos 	linenum++;
    479  1.34  christos 
    480  1.34  christos 	return (1);
    481  1.34  christos }
    482  1.34  christos 
    483  1.34  christos /*
    484   1.1       alm  * Add a compilation unit to the linked list
    485   1.1       alm  */
    486   1.1       alm static void
    487  1.13       wiz add_compunit(enum e_cut type, char *s)
    488   1.1       alm {
    489   1.1       alm 	struct s_compunit *cu;
    490   1.1       alm 
    491   1.1       alm 	cu = xmalloc(sizeof(struct s_compunit));
    492   1.1       alm 	cu->type = type;
    493   1.1       alm 	cu->s = s;
    494   1.1       alm 	cu->next = NULL;
    495   1.1       alm 	*cu_nextp = cu;
    496   1.1       alm 	cu_nextp = &cu->next;
    497   1.1       alm }
    498   1.1       alm 
    499   1.1       alm /*
    500   1.1       alm  * Add a file to the linked list
    501   1.1       alm  */
    502   1.1       alm static void
    503  1.13       wiz add_file(char *s)
    504   1.1       alm {
    505   1.1       alm 	struct s_flist *fp;
    506   1.1       alm 
    507   1.1       alm 	fp = xmalloc(sizeof(struct s_flist));
    508   1.1       alm 	fp->next = NULL;
    509   1.1       alm 	*fl_nextp = fp;
    510   1.1       alm 	fp->fname = s;
    511   1.1       alm 	fl_nextp = &fp->next;
    512   1.1       alm }
    513  1.34  christos 
    514  1.36  christos static int
    515  1.36  christos next_files_have_lines(void)
    516  1.36  christos {
    517  1.36  christos 	struct s_flist *file;
    518  1.36  christos 	FILE *file_fd;
    519  1.36  christos 	int ch;
    520  1.36  christos 
    521  1.36  christos 	file = files;
    522  1.36  christos 	while ((file = file->next) != NULL) {
    523  1.36  christos 		if ((file_fd = fopen(file->fname, "r")) == NULL)
    524  1.36  christos 			continue;
    525  1.36  christos 
    526  1.36  christos 		if ((ch = getc(file_fd)) != EOF) {
    527  1.36  christos 			/*
    528  1.36  christos 			 * This next file has content, therefore current
    529  1.36  christos 			 * file doesn't contains the last line.
    530  1.36  christos 			 */
    531  1.36  christos 			ungetc(ch, file_fd);
    532  1.36  christos 			fclose(file_fd);
    533  1.36  christos 			return (1);
    534  1.36  christos 		}
    535  1.36  christos 
    536  1.36  christos 		fclose(file_fd);
    537  1.36  christos 	}
    538  1.36  christos 
    539  1.36  christos 	return (0);
    540  1.36  christos }
    541  1.36  christos 
    542  1.34  christos int
    543  1.34  christos lastline(void)
    544  1.34  christos {
    545  1.34  christos 	int ch;
    546  1.34  christos 
    547  1.36  christos 	if (feof(infile)) {
    548  1.36  christos 		return !(
    549  1.36  christos 		    (inplace == NULL || ispan) &&
    550  1.36  christos 		    next_files_have_lines());
    551  1.36  christos 	}
    552  1.36  christos 	if ((ch = getc(infile)) == EOF) {
    553  1.36  christos 		return !(
    554  1.36  christos 		    (inplace == NULL || ispan) &&
    555  1.36  christos 		    next_files_have_lines());
    556  1.36  christos 	}
    557  1.34  christos 	ungetc(ch, infile);
    558  1.34  christos 	return (0);
    559  1.34  christos }
    560