Fix to sed

vsh vsh at pixel.UUCP
Fri Oct 18 07:18:10 AEST 1985


There is a bug in sed:  when processing files which do not end with a newline
character, the last line is discarded.  For example, execute the following:

	echo aaaaa > foo1 ; echo bbbbb > foo2 ; echo -n ccccc > foo3 
	echo ddddd > foo4 ; echo eeeee > foo5 ; echo -n fffff >> foo5
	cat foo? ; sed '' foo?
	cat foo5 ; sed '' foo5

In both cases, the 'sed' should be identical with the 'cat', but is not.

This shell script creates two 'diff' files, 'd0' and 'd1', which can be
applied to the System V, Release 2 versions of 'sed0.c' and 'sed1.c' to fix
this shortcoming.  In addition, sed will recognize '-' on the command
line as representing standard input.

These changes have been tested at Pixel and appear to work okay.
Please let me know (by e-mail) if you encounter any problems, or if any
other features should be added.

Steve Harris
: ---------------- bourne shell script starts here --------------------------
:
sed 's/^X//' << ---aaaaaaaaaaaaaa--- > d0
X138a
X	execute();
X.
X134,137c
X	if(eargc <= 0) {
X		eargc = 1;
X		*eargv = "-";
X.
---aaaaaaaaaaaaaa---
sed 's/^X//' << ---bbbbbbbbbbbbbb--- > d1
X549a
X}
X
Xgbuf ()
X{
X	extern int	eargc;
X	extern char	**eargv;
X
X	register int c;
X	static int not_first = 0;
X
X	for (;;) {
X		if ((not_first) && ((c = read (f, ibuf, 512))) > 0)
X			return (c);
X		else {
X			if (not_first && f != 0)
X				close (f);
X			while (eargc-- > 0) {
X				if (! strcmp (*eargv, "-"))
X					f = 0;
X				else if ((f = open (*eargv, 0)) < 0) {
X					fprintf (stderr, "Can't open %s\n", *eargv);
X					eargv++;
X					continue;
X				}
X				not_first = 1;
X				eargv++;
X				break;
X			}
X			if (eargc < 0)
X				return (0);
X		}
X	}
X.
X541,543c
X		else if (c)
X			if (p1 < lbend) {
X				*p1++ = c;
X				lnlflag = 0;
X			}
X.
X538a
X			else {
X				dolflag = 1;
X				break;
X			}
X		if ((c = *p2++) == '\n') {
X			lnlflag++;
X			if (p2 >= ebp)
X				if (c = gbuf()) {
X					p2 = ibuf;
X					ebp = ibuf + c;
X				}
X				else
X					dolflag = 1;
X.
X535a
X	for (;;) {
X		if (p2 >= ebp)
X			if (c = gbuf()) {
X.
X520,534d
X517a
X
X	int gbuf ();
X
X	sflag = 0;	/* BUGFIX, usenet 5/16/85 */
X.
X515a
X
X.
X513,514c
Xchar *gline(addr)
Xchar *addr;
X.
X488c
X			fprintf(ipc->r1.fcode, "%s", linebuf);
X			if (lnlflag)
X				fprintf(ipc->r1.fcode, "\n");
X.
X472c
X					if (lnlflag)
X						putc('\n', stdout);
X.
X450c
X				if (lnlflag)
X					putc('\n', stdout);
X.
X443c
X			if (lnlflag)
X				putc('\n', stdout);
X.
X437c
X			if (lnlflag)
X				putc('\n', stdout);
X.
X426,430c
X			execp = gline(spend);
X.
X422a
X			if (dolflag)
X				break;
X.
X414,418c
X			execp = gline(linebuf);
X.
X409c
X				if (lnlflag)
X					putc('\n', stdout);
X.
X405a
X			if (dolflag)
X				break;
X.
X402c
X			fprintf(stdout, "%s", genbuf);
X			if (lnlflag)
X				fprintf(stdout, "\n");
X.
X395c
X							fprintf(stdout, "%s", genbuf);
X							if (lnlflag)
X								fprintf(stdout, "\n");
X.
X387c
X						fprintf(stdout, "%s", genbuf);
X						if (lnlflag)
X							fprintf(stdout, "\n");
X.
X377c
X								fprintf(stdout, "%s", genbuf);
X								if (lnlflag)
X									fprintf(stdout, "\n");
X.
X299c
X				if (lnlflag)
X					putc('\n', stdout);
X.
X170c
X			if (lnlflag)
X				putc('\n', stdout);
X.
X87,90c
X		if (dolflag)
X			return (0);
X		execp = gline(linebuf);
X.
X80,85d
X70,76d
X62,63c
Xexecute()
X.
X17a
Xint	lnlflag = 1;
X.
X14c
Xint     dolflag = 0;
X.
---bbbbbbbbbbbbbb---
-- 
Steve Harris  
Pixel Systems Inc.; 300 Wildwood St.; Woburn, MA.  01801
(617) 933-7735 x2314 (work)   (617) 664-0099 (home)
{allegra|ihnp4|cbosgd|ima|genrad|amd|harvard}!wjh12!pixel!vsh



More information about the Net.bugs mailing list