]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (c) 2001 John Levon <moz@compsoc.man.ac.uk> |
2 | .\" Based in part on GNU libc documentation | |
3 | .\" | |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
5 | .\" Permission is granted to make and distribute verbatim copies of this |
6 | .\" manual provided the copyright notice and this permission notice are | |
7 | .\" preserved on all copies. | |
8 | .\" | |
9 | .\" Permission is granted to copy and distribute modified versions of this | |
10 | .\" manual under the conditions for verbatim copying, provided that the | |
11 | .\" entire resulting derived work is distributed under the terms of a | |
12 | .\" permission notice identical to this one. | |
c13182ef | 13 | .\" |
fea681da MK |
14 | .\" Since the Linux kernel and libraries are constantly changing, this |
15 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
16 | .\" responsibility for errors or omissions, or for damages resulting from | |
17 | .\" the use of the information contained herein. The author(s) may not | |
18 | .\" have taken the same level of care in the production of this manual, | |
19 | .\" which is licensed free of charge, as they might when working | |
20 | .\" professionally. | |
c13182ef | 21 | .\" |
fea681da MK |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
c08df37a | 25 | .\" |
7a4a8221 | 26 | .TH GETLINE 3 2010-06-12 "GNU" "Linux Programmer's Manual" |
fea681da MK |
27 | .SH NAME |
28 | getline, getdelim \- delimited string input | |
29 | .SH SYNOPSIS | |
30 | .nf | |
fea681da MK |
31 | .B #include <stdio.h> |
32 | .sp | |
33 | .BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream ); | |
5895e7eb | 34 | |
c8250206 MK |
35 | .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \ |
36 | ", FILE *" stream ); | |
37 | .fi | |
6deb7a03 MK |
38 | .sp |
39 | .in -4n | |
40 | Feature Test Macro Requirements for glibc (see | |
41 | .BR feature_test_macros (7)): | |
42 | .in | |
cfb08d1e | 43 | .sp |
6f1b61a1 | 44 | .ad l |
6deb7a03 MK |
45 | .BR getline (), |
46 | .BR getdelim (): | |
cfb08d1e MK |
47 | .PD 0 |
48 | .RS 4 | |
49 | .TP 4 | |
50 | Since glibc 2.10: | |
6f1b61a1 | 51 | _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700 |
cfb08d1e MK |
52 | .TP |
53 | Before glibc 2.10: | |
54 | _GNU_SOURCE | |
55 | .RE | |
56 | .PD | |
6f1b61a1 | 57 | .ad |
fea681da | 58 | .SH DESCRIPTION |
63aa9df0 | 59 | .BR getline () |
c13182ef | 60 | reads an entire line from \fIstream\fP, |
2df47dc8 | 61 | storing the address of the buffer containing the text into |
a5e0a0e4 | 62 | .IR "*lineptr" . |
2df47dc8 MK |
63 | The buffer is null-terminated and includes the newline character, if |
64 | one was found. | |
fea681da | 65 | |
fea681da | 66 | If |
0daa9e92 | 67 | .I "*lineptr" |
2df47dc8 | 68 | is NULL, then |
63aa9df0 | 69 | .BR getline () |
865be3cc MK |
70 | will allocate a buffer for storing the line, |
71 | which should be freed by the user program. | |
9f864ad2 | 72 | (In this case, the value in |
865be3cc MK |
73 | .I *n |
74 | is ignored.) | |
75 | ||
fea681da | 76 | Alternatively, before calling |
e1d6264d | 77 | .BR getline (), |
0daa9e92 | 78 | .I "*lineptr" |
fea681da | 79 | can contain a pointer to a |
fb186734 | 80 | .BR malloc (3)\-allocated |
fea681da | 81 | buffer |
0daa9e92 | 82 | .I "*n" |
c13182ef MK |
83 | bytes in size. |
84 | If the buffer is not large enough to hold the line, | |
63aa9df0 | 85 | .BR getline () |
2df47dc8 | 86 | resizes it with |
fb186734 | 87 | .BR realloc (3), |
fea681da | 88 | updating |
0daa9e92 | 89 | .I "*lineptr" |
fea681da | 90 | and |
0daa9e92 | 91 | .I "*n" |
c13182ef | 92 | as necessary. |
865be3cc | 93 | |
c13182ef | 94 | In either case, on a successful call, |
0daa9e92 | 95 | .I "*lineptr" |
fea681da | 96 | and |
0daa9e92 | 97 | .I "*n" |
2df47dc8 | 98 | will be updated to reflect the buffer address and allocated size respectively. |
fea681da | 99 | |
63aa9df0 | 100 | .BR getdelim () |
fea681da | 101 | works like |
e1d6264d | 102 | .BR getline (), |
6f1b61a1 | 103 | except that a line delimiter other than newline can be specified as the |
0daa9e92 | 104 | .I delimiter |
c13182ef MK |
105 | argument. |
106 | As with | |
e1d6264d | 107 | .BR getline (), |
fea681da MK |
108 | a delimiter character is not added if one was not present |
109 | in the input before end of file was reached. | |
47297adb | 110 | .SH RETURN VALUE |
fea681da | 111 | On success, |
e1d6264d | 112 | .BR getline () |
fea681da | 113 | and |
f87925c6 | 114 | .BR getdelim () |
fea681da | 115 | return the number of characters read, including the delimiter character, |
c13182ef MK |
116 | but not including the terminating null byte. |
117 | This value can be used | |
28d88c17 | 118 | to handle embedded null bytes in the line read. |
fea681da | 119 | |
1c1a8c02 | 120 | Both functions return \-1 on failure to read a line (including end-of-file |
fea681da | 121 | condition). |
fea681da MK |
122 | .SH ERRORS |
123 | .TP | |
124 | .B EINVAL | |
c4bb193f | 125 | Bad arguments |
fea681da MK |
126 | .RI ( n |
127 | or | |
128 | .I lineptr | |
129 | is NULL, or | |
130 | .I stream | |
131 | is not valid). | |
6deb7a03 MK |
132 | .SH VERSIONS |
133 | These functions are available since libc 4.6.27. | |
47297adb | 134 | .SH CONFORMING TO |
2b2581ee MK |
135 | Both |
136 | .BR getline () | |
137 | and | |
138 | .BR getdelim () | |
6deb7a03 MK |
139 | were originally GNU extensions. |
140 | They were standardized in POSIX.1-2008. | |
47297adb | 141 | .SH EXAMPLE |
fea681da MK |
142 | .nf |
143 | #define _GNU_SOURCE | |
144 | #include <stdio.h> | |
145 | #include <stdlib.h> | |
146 | ||
c13182ef | 147 | int |
cf0a9ace | 148 | main(void) |
fea681da | 149 | { |
9b50d251 MK |
150 | FILE *fp; |
151 | char *line = NULL; | |
cf0a9ace MK |
152 | size_t len = 0; |
153 | ssize_t read; | |
e77234bb | 154 | |
cf0a9ace MK |
155 | fp = fopen("/etc/motd", "r"); |
156 | if (fp == NULL) | |
157 | exit(EXIT_FAILURE); | |
e77234bb | 158 | |
cf0a9ace | 159 | while ((read = getline(&line, &len, fp)) != \-1) { |
31a6818e | 160 | printf("Retrieved line of length %zu :\en", read); |
cf0a9ace MK |
161 | printf("%s", line); |
162 | } | |
e77234bb | 163 | |
7a4a8221 | 164 | free(line); |
e77234bb | 165 | exit(EXIT_SUCCESS); |
fea681da MK |
166 | } |
167 | .fi | |
47297adb | 168 | .SH SEE ALSO |
fea681da MK |
169 | .BR read (2), |
170 | .BR fgets (3), | |
171 | .BR fopen (3), | |
172 | .BR fread (3), | |
173 | .BR gets (3), | |
0a4f8b7b | 174 | .BR scanf (3) |