This file and its contents are supplied under the terms of the
Common Development and Distribution License ("CDDL"), version 1.0.
You may only use this file in accordance with the terms of version
1.0 of the CDDL.
A full copy of the text of the CDDL should have accompanied this
source. A copy of the CDDL is also available via the Internet at
http://www.illumos.org/license/CDDL.
Copyright (c) 2013, Joyent, Inc. All rights reserved.
#include <stdio.h>
ssize_t getline(char **restrict ptr, \ size_t *restrict cap, FILE *restrict stream);
ssize_t getdelim(char **restrict ptr, \ size_t *restrict cap, int delimiter, FILE *restrict stream);
If successful, getdelim() and getline() return the number of bytes written into the buffer, excluding the terminating null byte. If an error occurs, or if end-of-file is reached prior to reading any bytes, the value -1 is returned and errno is set to indicate the error.
The getline() and getdelim() functions may fail due to the following errors: EINVAL
Either ptr or cap are NULL, or the delimiter is not a valid character.
More than SSIZE_MAX characters were read from the stream without encountering the delimiter.
The getline() and getdelim() functions may also fail and set errno for any of the errors specified for the library routines realloc(3C) or fgetc(3C).
Example 1 Read a line from stdin.
The following example uses getline to read a line from stdin.
#include <stdio.h> ... char *ptr = NULL; size_t cap = 0; if (getline(&ptr, &cap, stdin) == -1) { perror("getline"); exit(1); } fprintf(stdout, "input line: %s", ptr); free(ptr); .
ATTRIBUTE TYPE ATTRIBUTE VALUE |
Interface Stability Committed |
MT-Level MT-Safe |
fgetc(3C), fgets(3C), free(3C), malloc(3C), realloc(3C), attributes(5)