Does getc(3) use the read(2) syscall?

Oliver Fromme olli at lurza.secnetix.de
Wed Feb 3 14:28:28 UTC 2010


Stefan Midjich <swehack at gmail.com> wrote:
 > I'm having trouble looking this function up in the source tree, the trail
 > seems to end at __sys_read which has a bunch of prototypes but i can't find
 > the actual function code.
 > 
 > So my question is primarily, does getc use the read system call eventually?
 > 
 > But i would also love it if someone could show me where __sys_read is
 > defined.

getc() works like this:

1.  The getc() macro can be found in /usr/include/stdio.h,
    the getc() function (for threaded programs) can be found
    in src/lib/libc/stdio/getc.c.

2.  getc() (both the macro and the function) use the __sgetc()
    macro defined in stdio.h.

3.  The __sgetc() macro either returns a character directly
    from the buffer, or it calls the __srget() function to
    refill the buffer.  All of the stdio functions can be
    found in src/lib/libc/stdio/*.

4.  The __srget() function calls the __srefill() function,
    then returns a character from the newly refilled buffer.

5.  The __srefill() function uses the _sread() function to
    perform the actual read operation.

6.  The _sread() function uses the _read() method from the
    FILE struct.

7.  The actual value of the _read() method depends on how
    the file was opened.  If it's a standard file opened
    with fopen() or similar, then the _read() method is
    initialized to the __sread() function.

8.  Finally, the __sread() function calls _read().

9.  _read() is simply an alias for the read() syscall; the
    definition is in src/lib/libc/include/namespace.h.

So to answer you question:
Yes, getc() uses the read() syscall.

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

"Python tricks" is a tough one, cuz the language is so clean. E.g.,
C makes an art of confusing pointers with arrays and strings, which
leads to lotsa neat pointer tricks; APL mistakes everything for an
array, leading to neat one-liners; and Perl confuses everything
period, making each line a joyous adventure <wink>.
        -- Tim Peters


More information about the freebsd-hackers mailing list