[TUHS] Happy birthday, Dennis Ritchie! [ really sun vs dec/apollo --> X and NeWS ]

Larry McVoy lm at mcvoy.com
Wed Sep 20 13:13:12 AEST 2017


On Tue, Sep 19, 2017 at 01:56:48PM -0400, Random832 wrote:
> On Tue, Sep 19, 2017, at 09:56, Larry McVoy wrote:
> > On Tue, Sep 19, 2017 at 03:53:59PM +0200, Steffen Nurpmeso wrote:
> > >  |void    
> > >  |my_perror(char *file, int line, char *msg)
> > >  |{
> > >  |        char    *p = 0;
> > >  |        int     save = errno;
> > >  |
> > >  |        if (p = getenv("_BK_VERSION")) {
> > >  |                if (strneq(p, "bk-", 3)) p += 3;
> > >  |                fprintf(stderr, "%s:%d (%s): ", file, line, p);
> > >  |} else {
> > >  |                fprintf(stderr, "%s:%d: ", file, line);
> > >  |}
> > >  |        if (p = strerror(errno)) {
> > >  |                fprintf(stderr, "%s: %s\n", msg, p);
> > >  |} else {
> > >  |                fprintf(stderr, "%s: errno=%d\n", msg, errno);
> > >  |}
> > >  |        errno = save;   
> > >  |}
> > >  |
> > >  |libc should do that.
> > > 
> > > That really made me wonder why "save" is not used, errno may
> > > eventually change along the way.  Ok ok, but.. well.
> > 
> > Huh?  save is set with errno and then errno is restored to save.   The 
> > point of save is to do the library calls (which do syscalls which 
> > could in theory change errno) and leave it the same as it was on
> > the way in.
> 
> I think his point was that you should be passing save (rather than
> errno) to the strerror and the last printf, because the preceding
> library calls may have changed errno.

Thanks for that insight.  That code has been working for decades but I
think the reason it works is if it works then errno doesn't change and
if it doesn't work then we're not going to see the output.  So passing
save is technically correct but not sure it is practically correct.

Anyone have an opinion?


More information about the TUHS mailing list