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