home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / sun / misc / 3309 < prev    next >
Encoding:
Internet Message Format  |  1992-07-23  |  1.7 KB

  1. Xref: sparky comp.sys.sun.misc:3309 comp.unix.ultrix:5860 comp.unix.bsd:2732
  2. Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!ICSI.Berkeley.EDU!stolcke
  3. From: stolcke@ICSI.Berkeley.EDU (Andreas Stolcke)
  4. Newsgroups: comp.sys.sun.misc,comp.unix.ultrix,comp.unix.bsd
  5. Subject: Beware of fileno()
  6. Date: 24 Jul 1992 00:36:30 GMT
  7. Organization: International Computer Science Institute, Berkeley, CA, U.S.A.
  8. Lines: 29
  9. Distribution: world
  10. Message-ID: <14njaeINNvk@agate.berkeley.edu>
  11. NNTP-Posting-Host: icsib30.icsi.berkeley.edu
  12. Keywords: sunos, ultrix, stdio, fileno, lossage
  13.  
  14. Hi,
  15.  
  16. I was just wondering if I am the first one to hit upon this (I suspect not).
  17. I was debugging a program that uses lots of file descriptors, which 
  18. started to elicit Bad file number errors for no obvious reasons.
  19.  
  20. It turned out, the fileno() macro from stdio.h was to blame.  If you do a
  21.  
  22.     FILE *stream = fopen(....);
  23.     int d = fileno(stream);
  24.  
  25. and you have used up all file descriptors up to 127, you're screwed.
  26. This is because fileno() returns a char field in the FILE structure.
  27. On systems like Sun where char is signed this results in a negative
  28. integer file descriptor.  To prevent lossage, one has to use
  29.  
  30.     (unsigned char)fileno(stream)
  31.  
  32. or explicitly mask the most significant bits off.
  33. The right solution, of course, would be to define the file number field
  34. as unsigned char to begin with.
  35.  
  36. SunOS, Ultrix and MIPS RISC/os have the same problem, so I suspect it comes
  37. from BSD.  Why has this never been fixed?
  38.  
  39. -- 
  40. Andreas Stolcke                    stolcke@icsi.berkeley.edu
  41. International Computer Science Institute    stolcke@ucbicsi.bitnet
  42. 1947 Center St., Suite 600, Berkeley, CA 94704    (510) 642-4274 ext. 126
  43.