home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / sgi / 18334 < prev    next >
Encoding:
Internet Message Format  |  1992-12-24  |  2.0 KB

  1. Xref: sparky comp.sys.sgi:18334 comp.sys.sgi.bugs:45 comp.unix.ultrix:9077
  2. Path: sparky!uunet!wupost!spool.mu.edu!olivea!apple!TIS.COM!mjr
  3. From: mjr@TIS.COM (Marcus J. Ranum)
  4. Newsgroups: comp.sys.sgi,comp.sys.sgi.bugs,comp.unix.ultrix
  5. Subject: Re: tempnam(3S) bug, possibly on all MIPS
  6. Message-ID: <9212241830.AA23324@TIS.COM>
  7. Date: 24 Dec 92 18:30:11 GMT
  8. References: <1gu849INNp6k@menudo.uh.edu> <tqcnepk@zuni.esd.sgi.com> <1992Dec24.011451.497@pixar.com>
  9. Sender: usenet@Apple.COM
  10. Reply-To: mjr@TIS.COM
  11. Followup-To: comp.sys.sgi
  12. Organization: Trusted Informations Systems, Inc.
  13. Lines: 45
  14.  
  15. aaa@pixar.com (Tony Apodaca) writes:
  16.  
  17. >    The point is that a smart programmer
  18. >checks errno after every system call.
  19.  
  20.     The *wise* programmer checks the approriate return value of the
  21. system call, and if it returns a value indicating an error, then the errno
  22. value is used.
  23.  
  24.     Consider the following:
  25.  
  26.     int    fd;
  27.     fd = open("foo",O_RDWR|O_CREAT,0600);
  28.     if(errno != 0)
  29.         perror("foo");
  30.  
  31.  
  32.     Versus:
  33.  
  34.     int    fd;
  35.     if((fd = open("foo",O_RDWR|O_CREAT,0600)) < 0)
  36.         perror("foo");
  37.  
  38.     Now, the manual page on open(2) states explicitly:
  39.  
  40. RETURN VALUE
  41.      The value -1 is returned if an error  occurs,  and  external
  42.      variable  errno  is  set to indicate the cause of the error.
  43.  
  44.     There's a good reason for writing your code this way. You are
  45. using the return value from the function, rather than a global value
  46. which can get stepped on by something unusual you don't know about.
  47. This means that your code doesn't have to care about what is a system
  48. call versus what is a library routine. Do you care? I don't. I just
  49. want to know if it didn't work right. Then I want to know why.
  50.  
  51.     I believe that you can also run into problems if you have
  52. a signal handler that gets invoked, and resets errno in the period
  53. between your successful system call and your test for success. It's
  54. easier to just treat the system calls like a function call, and
  55. look at its return value. If you somehow think that using errno
  56. rather than having to declare a local to capture the return value is
  57. an optimization, you're mistaken.
  58.  
  59. mjr.
  60.