View Single Post
  #9   (View Single Post)  
Old 14th April 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Talking

Quote:
Originally Posted by IdOp View Post
Well, since fseek() and read() are tested for their respective error condition (-1 in each case) and those cases aren't entered, probably errno won't be set, right?

FWIW: I tried creating a version that checks errno after each call via a macro, only to have it segfault on run. Then I yanked the version in your post (again) to a temp file, compiled & run as in the last post and it segfaulted exactly the same way (same machine).

Code:
Terry@dixie$ gcc -ggdb3 -Wall /tmp/t.c -o /tmp/t && gdb /tmp/t
GNU gdb 6.1.1 [FreeBSD]
...
This GDB was configured as "i386-marcel-freebsd"...
(gdb) run
Starting program: /tmp/t 

Program received signal SIGSEGV, Segmentation fault.
0x08048587 in main () at /tmp/t.c:13
13          ifd = fileno( ifp );
(gdb)

== beyond that ==

I've never tried to mix standard I/O functions with I/O system calls (why does anyone need to do that, normally?), but I remember a comment in the book Programming Perl: a warning about mixing things like read() and sysread(), should only be done if you are into wizardry, pain, or both. (read() and sysread() in Perl are basically equivalents to a Unix/C's fread() and read() respectively). I would reckon is you manipulate the file descriptor without updating the structure on the other side of a FILE *, like f.*() functions should do; things could probably get out of sync between the integer file descriptor and the FILE *stream; and get pissed off accordingly if certain ops were done, hypothetically anyway.

I'd really suggest trying it with fread() and such instead, as BSDFan suggests.

== other ==

The documentation on read() system call returns the # of bytes read, 0 if the read was EOF, -1 if a cork popped and sets errno. So if it's not reading the specified amount, I would rather assume it hit EOF and returned what was read up to that point (i.e. a number of bytes that is > 0 but < 2352)

edit: yep

Quote:
Originally Posted by The Open Group Base Specifications Issue 6
IEEE Std 1003.1, 2004 Edition; System Interfaces; ssize_t read(int fildes, void *buf, size_t nbyte);
Upon successful completion, where nbyte is greater than 0, read() shall mark for update the st_atime field of the file, and shall return the number of bytes read. This number shall never be greater than nbyte. The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal, or if the file is a pipe or FIFO or special file and has fewer than nbyte bytes immediately available for reading. For example, a read() from a file associated with a terminal may return one typed line of data.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.

Last edited by TerryP; 14th April 2009 at 07:40 AM.
Reply With Quote