tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: tac (was Re: GNU shellery)



    Date:        Wed, 4 Oct 2017 06:15:20 -0500 (CDT)
    From:        "Jeremy C. Reed" <reed%reedmedia.net@localhost>
    Message-ID:  <alpine.NEB.2.20.1710040607190.7325%t1.m.reedmedia.net@localhost>

  | I put the README, source, and SCCS files here:
  | https://ftp.netbsd.org/pub/NetBSD/misc/reed/tac/local/

In that version of tac.c I notice this wonderful example of C code ...

    if ferror(stdout) {
	perror("tac: fwrite/putc");
	exit(++numerr > 1 ? numerr : 1);
    }

unnoticed after all this time ... which compiles purely because ferror()
expands to a string enclosed by () and so makes valid C code (that ferror()
doesn't set errno probably doesn't matter here, as it is likely that
whatever happened to cause ferror() to be true would have left an errno
value that at least might be reasonable.)

And this:

prterr(s)
    char *s;
{

    fprintf(stderr, "tac: ");
    perror(s);
}

which relies upon stderr being (line) buffered, so that fprintf() is
effectively just a memcpy() and so does not alter errno, which remains
valid (or as valid as it was previously, which isn't it isn't always
reliable the way prterr() is called) for the perror() call.

It also uses bcopy() with potentially overlapping src & dest.

Boy did we get away with some trash code in the 80's !

kre



Home | Main Index | Thread Index | Old Index