GETDELIM(3P)

πŸ¦‹ getdelim(3P) β€” the POSIX Programmer's Manual one, bestie πŸ¦‹

okay so real talk first: this page lives in the POSIX Programmer's Manual. That means the Linux implementation of this interface might act different (go check the Linux man page for the actual deets, fr), or honestly it might not even be implemented on Linux at all. πŸ™ˆ


πŸ“– PROLOG πŸ“–

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” so consult the corresponding Linux manual page for details of Linux behavior β€” or the interface may not be implemented on Linux. iykyk


✨ NAME ✨

getdelim, getline β€” read a delimited record from stream πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <stdio.h>

ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
    int delimiter, FILE *restrict stream);
ssize_t getline(char **restrict lineptr, size_t *restrict n,
    FILE *restrict stream);

πŸ’– DESCRIPTION πŸ’–

So getdelim() reads from stream until it hits a character matching the delimiter character. The delimiter argument is an int, and the value of it β€” the application shall ensure β€” is a character representable as an unsigned char of equal value that terminates the read process. If delimiter has any other value? Behavior is undefined. No cap, don't do that. πŸ’€

The application shall ensure that *lineptr is a valid argument that could be passed to the free() function. And if *n is non-zero, the application shall ensure that *lineptr either points to an object of size at least *n bytes, or is a null pointer. Them's the rules. 🫢

Now the fun part, the way that it just handles things: if *lineptr is a null pointer, or if the object pointed to by *lineptr is of insufficient size, then an object shall be allocated as if by malloc() β€” or the object shall be reallocated as if by realloc(), 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, *lineptr 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. iconic behavior honestly. ✨

And getline()? It shall be equivalent to the getdelim() function with the delimiter character equal to the <newline> character. That's the whole vibe. 🌸

Last thing: getdelim() and getline() may mark the last data access timestamp of the file associated with stream for update. That timestamp shall be marked for update by the first successful execution of fgetc(), fgets(), fread(), fscanf(), getc(), getchar(), getdelim(), getline(), gets(), or scanf() using stream that returns data not supplied by a prior call to ungetc(). πŸ‘‘


πŸ”₯ RETURN VALUE πŸ”₯

Upon successful completion, getline() and getdelim() 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. Periodt. πŸ’…

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 errno to indicate the error. 😭


πŸ‘‘ ERRORS πŸ‘‘

For the conditions under which getdelim() and getline() shall fail and may fail, go peek at fgetc(). πŸ‘€

In addition, these functions shall fail if:

  • EINVAL β€” lineptr or n is a null pointer. πŸ™ˆ
  • ENOMEM β€” Insufficient memory is available.

These functions may fail if:

  • EOVERFLOW β€” The number of bytes to be written into the buffer, including the delimiter character (if encountered), would exceed {SSIZE_MAX}. big yikes.

The following sections are informative. πŸ“


🌈 EXAMPLES 🌈

#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;
}

🧚 APPLICATION USAGE 🧚

Setting *lineptr to a null pointer and *n to zero? Allowed, and lowkey the recommended way to start parsing a file. Do it bestie. πŸ“

Use ferror() or feof() to tell the difference between an error condition and an end-of-file condition β€” you gotta know which one you're in.

And hear me out: although a NUL terminator is always supplied after the line, note that strlen(*lineptr) will be smaller than the return value if the line contains embedded NUL characters. yes this is a real gotcha, keep it in mind. πŸ’œ


πŸ’… RATIONALE πŸ’…

These functions are widely used to solve the problem that fgets() has with long lines. The functions automatically enlarge the target buffers if needed. These are especially useful since they reduce code needed for applications. less code, more slay. ⭐


πŸ¦‹ FUTURE DIRECTIONS πŸ¦‹

None. that's it. nothing. we're done here. ✨


πŸ”— SEE ALSO πŸ”—

  • Section 2.5, Standard I/O Streams
  • fgetc()
  • fgets()
  • free()
  • malloc()
  • realloc()

The Base Definitions volume of POSIX.1‐2017, <stdio.h> πŸ“š


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 .


and that's the whole page bestie 🫢 this one's from IEEE/The Open Group, dated 2017, and it's GETDELIM(3P) β€” so like, go forth and read your delimited records, mwah πŸ’–πŸŒΈβœ¨

πŸ“¦ where this page comes from