🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

7 total messages Started by vsh Thu, 17 Oct 1985 21:18
Fix to sed
#478
Author: vsh
Date: Thu, 17 Oct 1985 21:18
197 lines
4280 bytes
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
Re: Fix to sed
#479
Author: Doug Gwyn <gwyn>
Date: Fri, 18 Oct 1985 01:52
6 lines
847 bytes
> There is a bug in sed:  when processing files which do not end with a newline
> character, the last line is discarded.

Many UNIX text-file utilities will discard a (necessarily final)
text line that does not end in a newline.  Quite simply, such a
file is not a proper UNIX text file.
Re: Fix to sed (what's a text file?)
#481
Author: Kay Dekker
Date: Sun, 20 Oct 1985 19:00
16 lines
1325 bytes
In article <2235@brl-tgr.ARPA> gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) writes:
>Many UNIX text-file utilities will discard a (necessarily final)
>text line that does not end in a newline.  Quite simply, such a
>file is not a proper UNIX text file.

Who says?  Where's the definition of a 'proper' UNIX text file?

Maybe the "many UNIX text-file utilities" could do with fixing: discarding
lines that don't end in a newline seems bogus to me.

						Kay.
-- 
"A boy does not put his hand into his pocket until every other means of
gaining his end has failed."		_Tommy_, by J. M. Barrie.
			
			... mcvax!ukc!warwick!flame!kay
"proper UNIX text file" ???
#480
Author: Curtis Jackson
Date: Mon, 21 Oct 1985 14:46
21 lines
1564 bytes
In article <2235@brl-tgr.ARPA> gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) writes:
>Many UNIX text-file utilities will discard a (necessarily final)
>text line that does not end in a newline.  Quite simply, such a
>file is not a proper UNIX text file.

I think that the \User Guide to the UNIX System/ rebutts this as well as
I could:

"In the UNIX system, files have no internal structure; they are simply
a finite sequence of arbitrary characters."

A Unix file is a series of bytes, nothing more is needed to make it a
'proper' UNIX text file.  The reason that sed and some (few) others discard
the last few bytes after the last newline is because these utilities work
on 'lines' of input -- and the definition used by all (most?) of them for
a line is zero or more non-newline characters followed by a newline.
-- 

The MAD Programmer -- 919-228-3313 (Cornet 291)
alias: Curtis Jackson	...![ ihnp4 ulysses cbosgd mgnetp ]!burl!rcj
			...![ ihnp4 cbosgd akgua masscomp ]!clyde!rcj
Re: Fix to sed (what's a text file?)
#482
Author: Doug Gwyn <gwyn>
Date: Tue, 22 Oct 1985 00:01
42 lines
2648 bytes
> >Many UNIX text-file utilities will discard a (necessarily final)
> >text line that does not end in a newline.  Quite simply, such a
> >file is not a proper UNIX text file.
> 
> Who says?  Where's the definition of a 'proper' UNIX text file?

The problem is, there are several interpretations of such a file,
depending on the utility involved.  Perhaps there should be a
well-defined standard interpretation, but there isn't currently.

"A file of text consists simply of a string of characters, with
lines demarcated by the newline character."  -- from "The UNIX
Time-Sharing System" by Ritchie & Thompson

"text file, ASCII file -- a file, the bytes of which are understood
to be in ASCII code"  -- from "Glossary" in "UNIX Time-Sharing
System Programmer's Manual", 8th Ed.

"A text stream is an ordered sequence of bytes composed into lines,
each line consisting of zero or more characters plus a terminating
new-line character.  ...  The sequentially last character read in
from a text stream will, however, always be sequentially the last
character that was earlier written out to the text stream, if that
character was a new-line."  -- from ANSI X3J11/85-045

My personal choice would be similar to Ritchie & Thompson, where
newlines delimit (NOT "terminate") text lines, so that the last
character in a text file would not need to be a newline.  However,
this raises the question of what utilities should do with the
null line at the end of every text file that DOES end with a
newline; this will still be utility-dependent (and should be
documented whenever it is handled differently from other text
lines in the file).

X3J11/85-045 botched it anyhow, since they intended that ALL UNIX
files qualify as "text streams" under stdio (vs. "binary streams",
which have to be handled differently on some non-UNIX OSes).

So, how do we establish a standard interpretation for non-newline-
terminated UNIX text files?

(Discussion should move to net.unix.)
Re: "proper UNIX text file" ???
#483
Author: Doug Gwyn <gwyn>
Date: Wed, 23 Oct 1985 13:51
25 lines
1796 bytes
> In article <2235@brl-tgr.ARPA> gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) writes:
> >Many UNIX text-file utilities will discard a (necessarily final)
> >text line that does not end in a newline.  Quite simply, such a
> >file is not a proper UNIX text file.
> 
> I think that the \User Guide to the UNIX System/ rebutts this as well as
> I could:
> 
> "In the UNIX system, files have no internal structure; they are simply
> a finite sequence of arbitrary characters."
> 
> A Unix file is a series of bytes, nothing more is needed to make it a
> 'proper' UNIX text file.

Tell that to "ed".  This is the second response I have gotten
this morning that shows lack of care in reading.  The magic
word "text" in front of "file" is what is called an adjective.
It qualifies (i.e., modifies) the general meaning of the noun
to produce a more specific meaning.  There are UNIX files and
there is a subset of these called UNIX text files.  Many UNIX
utilities are designed to work only with text files, which DO
have internal structure (line-oriented).  These and only these
are the files under discussion.

P.S.  I didn't realize Thomas & Yates was considered authoritative.
Re: "proper UNIX text file" ???
#485
Author: Mikel Manitius
Date: Fri, 25 Oct 1985 20:56
40 lines
2633 bytes
> > In article <2235@brl-tgr.ARPA> gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) writes:
> > >Many UNIX text-file utilities will discard a (necessarily final)
> > >text line that does not end in a newline.  Quite simply, such a
> > >file is not a proper UNIX text file.
> > 
> > "In the UNIX system, files have no internal structure; they are simply
> > a finite sequence of arbitrary characters."
> > 
> > A Unix file is a series of bytes, nothing more is needed to make it a
> > 'proper' UNIX text file.
> 
> Tell that to "ed".  This is the second response I have gotten
> this morning that shows lack of care in reading.  The magic
> word "text" in front of "file" is what is called an adjective.
> It qualifies (i.e., modifies) the general meaning of the noun
> to produce a more specific meaning.  There are UNIX files and
> there is a subset of these called UNIX text files.  Many UNIX
> utilities are designed to work only with text files, which DO
> have internal structure (line-oriented).  These and only these
> are the files under discussion.

That still isn't entierly the point, Unix (the operating system)
doesn't know the difference between a "text" file and anything
else, the only thing it knows about are files and directories,
it does not concern itself with what is within the file.

The aplication program, such as "ed" chooses to determine that a
file that does not contain printable characters is not a "text"
file, this is purly abstract.

And just becuase the last character in the file is not a newline,
does mean that all of the characters preceding it all the way to
the previous newline are not text. Utilities such as "fgets"
determine a line when data is delimitered by a newline, if it
gets an EOF before a newline it flushes that data. Note that
fgets(3) is a library function and not a system call, thusly
it is a utility and not part of the operating system.
-- 
			Mikel Manitius @ AT&T-IS Altamonte Springs, FL
			...{ihnp4|akguc|attmail|indra!koura}!codas!mikel
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads