Email List: Xaustin-group-lX
[All Lists]

Re: Proposed submissions for the revision from The Open Group

To: yyyyyyyyyyyyyy@xxxxxxxxxxxxx
Subject: Re: Proposed submissions for the revision from The Open Group
From: Garrett Wollman <yyyyyyy@xxxxxxxxxxxxx>
Date: Wed, 11 Jan 2006 13:19:49 -0500
References: <1060109184758.ZM17257@skye.rdg.opengroup.org><17346.48571.685761.731503@khavrinen.csail.mit.edu><43c3f58c.DCRQQCxQd+2zWxCK%gunnarr@acm.org><43C53F98.nail90I217O41@burner>
<<On Wed, 11 Jan 2006 18:25:44 +0100, Joerg Schilling 
<yyyyyyyyy@xxxxxxxxxxxxxxxxxxx> said:

> Maybe, I should mention again (I did this already a long time ago)
> the interface of getline()/fgetline() that was in use on a UNIX clone
> since at least 19812?

>     int getline(char * buffer, int length)

>     Returns either the number of bytes in the buffer or EOF (-1)

The problem that this interface has, which neither glibc getline() nor
4.4BSD fgetln() share, is that it does not provide a means to directly
read a line of arbitrary length.  It is marginally better than fgets()
by dint of returning a more useful value.

If I may summarize the properties of the major interfaces:

gets()
        + Reads an entire line, up to '\n' or EOF.
        - Reads only from stdin.
        - No bounds-checking.
        - Application must perform all memory management.
        - No way to tell whether the line ended in '\n' or EOF.
        ? No wide-character version.

fgets()
        + Bounds-checked.
        + Reads from arbitrary FILE.
        - Application must perform all memory management.
        - Application must loop to read long lines.
        ? Has wide-character version, fgetws().

fgetln()
        + Reads an entire line, up to '\n' or EOF.
        + No bounds-checking required.
        + Reads from arbitrary FILE.
        + Buffer managed by implementation.
        - Not thread-safe unless FILE is locked with flockfile().
        - Does not return C strings.
        ? Has wide-character version, fgetwln().

getline()/getdelim()
    o When application supplies buffer:
        + Bounds-checked, sort of.
        - Application must perform all memory management.
        - Application must use malloc() to allocate buffer; auto
          buffers not permitted.
    o When implementation supplies buffer:
        + No bounds-checking required.
        - Application must free returned buffer.
    o In both cases:
        + Reads from arbitrary FILE.
        + Allows application to specify an alternative ending delimiter.
        + Reads an entire line, up to specified delimiter or EOF.
        ? No wide-character version.

getdelim() is clearly the most general, and also the most
complicated.  gets() should never be used, and should be removed from
the standard as soon as procedurally possible.  fgetln() was designed
to make a certain class of applications (specifically, filters) as
efficient as possible without imposing arbitrary limits; it's also
useful for configuration files and other tasks requring a simple
parser.  (The fparseln() interface, found in NetBSD and Darwin, takes
this one step further but I doubt that there would be consensus to
standardize it.)

-GAWollman

<Prev in Thread] Current Thread [Next in Thread>