Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Oct 1998 09:06:58 -0700 (PDT)
From:      Matthew Dillon <dillon@apollo.backplane.com>
To:        "David O'Brien" <obrien@nuxi.com>
Cc:        Peter Wemm <peter@netplex.com.au>, cvs-committers@FreeBSD.ORG
Subject:   Re: cvs commit: src/lib/libc/stdio mktemp.c
Message-ID:  <199810211606.JAA06226@apollo.backplane.com>
References:  <199810202134.PAA28899@harmony.village.org> <199810210231.KAA14549@spinner.netplex.com.au> <19981021025928.A21068@nuxi.com>

next in thread | previous in thread | raw e-mail | index | archive | help
:> -			    open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
:> +			    open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0) {
:
:Stylistically speaking, why do people keep insisting on not checking
:error conditions properly?  open(2) is documented to return "-1" on
:error, not non-negative as many want to test it against.
:
:Yes, this has caused me problems on an old 286 Xenix system.

    for open(), checking the result for >= 0 for success is historical.

    In many years past, some programmers made the mistake of checking the
    result of open for values greater then 0 (> 0), forgetting that 0 is 
    a perfectly valid file descriptor.  In disgust, T.R.O.T.W. explicitly
    began comparing the result against >= 0.

    Most people I know do not compare against -1 explicitly for any system
    call but instead use an insequality.  Historically, the result of a system
    call has been tested negative ( < 0 ) to determine if there was an error,
    or exactly 0 ( == 0) if not, except for those calls that had a specific
    type of return value (such as read()).  The one glaring exception to this,
    historically, has been lseek().  Many people these days don't even bother
    checking the return value for lseek() unless they need the new position
    of the file.

    With open(), it is especially silly to compare against an explicit -1
    because many programs will then use the file descriptor in an FD_SET or
    as an array index somewhere and thus the descriptor would blow up the
    program if it were negative (say, -2).  Better to fail.

						-Matt

:-- 
:-- David    (obrien@NUXI.com  -or-  obrien@FreeBSD.org)
:
:

    Matthew Dillon  Engineering, HiWay Technologies, Inc. & BEST Internet 
                    Communications & God knows what else.
    <dillon@backplane.com> (Please include original email in any response)    

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199810211606.JAA06226>