home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.sys.sgi:18334 comp.sys.sgi.bugs:45 comp.unix.ultrix:9077
- Path: sparky!uunet!wupost!spool.mu.edu!olivea!apple!TIS.COM!mjr
- From: mjr@TIS.COM (Marcus J. Ranum)
- Newsgroups: comp.sys.sgi,comp.sys.sgi.bugs,comp.unix.ultrix
- Subject: Re: tempnam(3S) bug, possibly on all MIPS
- Message-ID: <9212241830.AA23324@TIS.COM>
- Date: 24 Dec 92 18:30:11 GMT
- References: <1gu849INNp6k@menudo.uh.edu> <tqcnepk@zuni.esd.sgi.com> <1992Dec24.011451.497@pixar.com>
- Sender: usenet@Apple.COM
- Reply-To: mjr@TIS.COM
- Followup-To: comp.sys.sgi
- Organization: Trusted Informations Systems, Inc.
- Lines: 45
-
- aaa@pixar.com (Tony Apodaca) writes:
-
- > The point is that a smart programmer
- >checks errno after every system call.
-
- The *wise* programmer checks the approriate return value of the
- system call, and if it returns a value indicating an error, then the errno
- value is used.
-
- Consider the following:
-
- int fd;
- fd = open("foo",O_RDWR|O_CREAT,0600);
- if(errno != 0)
- perror("foo");
-
-
- Versus:
-
- int fd;
- if((fd = open("foo",O_RDWR|O_CREAT,0600)) < 0)
- perror("foo");
-
- Now, the manual page on open(2) states explicitly:
-
- RETURN VALUE
- The value -1 is returned if an error occurs, and external
- variable errno is set to indicate the cause of the error.
-
- There's a good reason for writing your code this way. You are
- using the return value from the function, rather than a global value
- which can get stepped on by something unusual you don't know about.
- This means that your code doesn't have to care about what is a system
- call versus what is a library routine. Do you care? I don't. I just
- want to know if it didn't work right. Then I want to know why.
-
- I believe that you can also run into problems if you have
- a signal handler that gets invoked, and resets errno in the period
- between your successful system call and your test for success. It's
- easier to just treat the system calls like a function call, and
- look at its return value. If you somehow think that using errno
- rather than having to declare a local to capture the return value is
- an optimization, you're mistaken.
-
- mjr.
-