GETDELIM(3)                POSIX Programmer's Manual               GETDELIM(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       getdelim, getline -- read a delimited record from _s_t_r_e_a_m

SSYYNNOOPPSSIISS
       #include <stdio.h>

       ssize_t getdelim(char **restrict _l_i_n_e_p_t_r, size_t *restrict _n,
           int _d_e_l_i_m_i_t_e_r, FILE *restrict _s_t_r_e_a_m);
       ssize_t getline(char **restrict _l_i_n_e_p_t_r, size_t *restrict _n,
           FILE *restrict _s_t_r_e_a_m);

DDEESSCCRRIIPPTTIIOONN
       The _g_e_t_d_e_l_i_m() function shall read from _s_t_r_e_a_m until it encounters a
       character matching the _d_e_l_i_m_i_t_e_r character. The _d_e_l_i_m_i_t_e_r argument is
       an iinntt, the value of which the application shall ensure is a character
       representable as an uunnssiiggnneedd cchhaarr of equal value that terminates the
       read process. If the _d_e_l_i_m_i_t_e_r argument has any other value, the
       behavior is undefined.

       The application shall ensure that _*_l_i_n_e_p_t_r is a valid argument that
       could be passed to the _f_r_e_e() function. If _*_n is non-zero, the
       application shall ensure that _*_l_i_n_e_p_t_r either points to an object of
       size at least _*_n bytes, or is a null pointer.

       If _*_l_i_n_e_p_t_r is a null pointer or if the object pointed to by _*_l_i_n_e_p_t_r
       is of insufficient size, an object shall be allocated as if by _m_a_l_l_o_c()
       or the object shall be reallocated as if by _r_e_a_l_l_o_c(), respectively,
       such that the object is large enough to hold the characters to be
       written to it, including the terminating NUL, and _*_n shall be set to
       the new size. If the object was allocated, or if the reallocation
       operation moved the object, _*_l_i_n_e_p_t_r shall be updated to point to the
       new object or new location.  The characters read, including any
       delimiter, shall be stored in the object, and a terminating NUL added
       when the delimiter or end-of-file is encountered.

       The _g_e_t_l_i_n_e() function shall be equivalent to the _g_e_t_d_e_l_i_m() function
       with the _d_e_l_i_m_i_t_e_r character equal to the <newline> character.

       The _g_e_t_d_e_l_i_m() and _g_e_t_l_i_n_e() functions may mark the last data access
       timestamp of the file associated with _s_t_r_e_a_m for update. The last data
       access timestamp shall be marked for update by the first successful
       execution of _f_g_e_t_c(), _f_g_e_t_s(), _f_r_e_a_d(), _f_s_c_a_n_f(), _g_e_t_c(), _g_e_t_c_h_a_r(),
       _g_e_t_d_e_l_i_m(), _g_e_t_l_i_n_e(), _g_e_t_s(), or _s_c_a_n_f() using _s_t_r_e_a_m that returns
       data not supplied by a prior call to _u_n_g_e_t_c().

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _g_e_t_l_i_n_e() and _g_e_t_d_e_l_i_m() functions
       shall return the number of bytes written into the buffer, including the
       delimiter character if one was encountered before EOF, but excluding
       the terminating NUL character. If the end-of-file indicator for the
       stream is set, or if no characters were read and the stream is at end-
       of-file, the end-of-file indicator for the stream shall be set and the
       function shall return -1.  If an error occurs, the error indicator for
       the stream shall be set, and the function shall return -1 and set _e_r_r_n_o
       to indicate the error.

EERRRROORRSS
       For the conditions under which the _g_e_t_d_e_l_i_m() and _g_e_t_l_i_n_e() functions
       shall fail and may fail, refer to _f_g_e_t_c().

       In addition, these functions shall fail if:

       EEIINNVVAALL _l_i_n_e_p_t_r or _n is a null pointer.

       EENNOOMMEEMM Insufficient memory is available.

       These functions may fail if:

       EEOOVVEERRFFLLOOWW
              The number of bytes to be written into the buffer, including the
              delimiter character (if encountered), would exceed {SSIZE_MAX}.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
           #include <stdio.h>
           #include <stdlib.h>

           int main(void)
           {
               FILE *fp;
               char *line = NULL;
               size_t len = 0;
               ssize_t read;
               fp = fopen("/etc/motd", "r");
               if (fp == NULL)
                   exit(1);
               while ((read = getline(&line, &len, fp)) != -1) {
                   printf("Retrieved line of length %zu :\n", read);
                   printf("%s", line);
               }
               if (ferror(fp)) {
                   /* handle error */
               }
               free(line);
               fclose(fp);
               return 0;
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       Setting _*_l_i_n_e_p_t_r to a null pointer and _*_n to zero are allowed and a
       recommended way to start parsing a file.

       The _f_e_r_r_o_r() or _f_e_o_f() functions should be used to distinguish between
       an error condition and an end-of-file condition.

       Although a NUL terminator is always supplied after the line, note that
       _s_t_r_l_e_n(*_l_i_n_e_p_t_r) will be smaller than the return value if the line
       contains embedded NUL characters.

RRAATTIIOONNAALLEE
       These functions are widely used to solve the problem that the _f_g_e_t_s()
       function has with long lines. The functions automatically enlarge the
       target buffers if needed. These are especially useful since they reduce
       code needed for applications.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _S_e_c_t_i_o_n _2_._5, _S_t_a_n_d_a_r_d _I_/_O _S_t_r_e_a_m_s, _f_g_e_t_c(), _f_g_e_t_s(), _f_r_e_e(), _m_a_l_l_o_c(),
       _r_e_a_l_l_o_c()

       The Base Definitions volume of POSIX.1-2017, <<ssttddiioo..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                          GETDELIM(3)
