]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getline.3
ffix
[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 .br
34 .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim ", FILE *" stream );
35 .SH DESCRIPTION
36 .BR getline ()
37 reads an entire line from \fIstream\fP,
38 storing the address of the buffer containing the text into
39 .IR "*lineptr" .
40 The buffer is null-terminated and includes the newline character, if
41 one was found.
42
43 .\" FIXME what happens if *lineptr is NULL but *n isn't zero ?
44 .\" Answer: *n is ignored and a new buffer is allocated
45 If
46 .IR "*lineptr"
47 is NULL, then
48 .BR getline ()
49 will allocate a buffer for storing the line, which should be freed
50 by the user program.
51 Alternatively, before calling
52 .BR getline (),
53 .IR "*lineptr"
54 can contain a pointer to a
55 .BR malloc ()\-allocated
56 buffer
57 .IR "*n"
58 bytes in size. If the buffer is not large enough to hold the line,
59 .BR getline ()
60 resizes it with
61 .BR realloc (),
62 updating
63 .IR "*lineptr"
64 and
65 .IR "*n"
66 as necessary. In either case, on a successful call,
67 .IR "*lineptr"
68 and
69 .IR "*n"
70 will be updated to reflect the buffer address and allocated size respectively.
71
72 .BR getdelim ()
73 works like
74 .BR getline (),
75 except a line delimiter other than newline can be specified as the
76 .IR delimiter
77 argument. As with
78 .BR getline (),
79 a delimiter character is not added if one was not present
80 in the input before end of file was reached.
81 .SH "RETURN VALUE"
82 On success,
83 .BR getline ()
84 and
85 .BR getdelim ()
86 return the number of characters read, including the delimiter character,
87 but not including the terminating null byte. This value can be used
88 to handle embedded null bytes in the line read.
89
90 Both functions return \-1 on failure to read a line (including end of file
91 condition).
92 .SH ERRORS
93 .TP
94 .B EINVAL
95 Bad parameters
96 .RI ( n
97 or
98 .I lineptr
99 is NULL, or
100 .I stream
101 is not valid).
102 .SH "EXAMPLE"
103 .nf
104 #define _GNU_SOURCE
105 #include <stdio.h>
106 #include <stdlib.h>
107
108 int
109 main(void)
110 {
111 FILE * fp;
112 char * line = NULL;
113 size_t len = 0;
114 ssize_t read;
115 fp = fopen("/etc/motd", "r");
116 if (fp == NULL)
117 exit(EXIT_FAILURE);
118 while ((read = getline(&line, &len, fp)) != \-1) {
119 printf("Retrieved line of length %zu :\en", read);
120 printf("%s", line);
121 }
122 if (line)
123 free(line);
124 return EXIT_SUCCESS;
125 }
126 .fi
127 .SH "CONFORMING TO"
128 Both
129 .BR getline ()
130 and
131 .BR getdelim ()
132 are GNU extensions.
133 They are available since libc 4.6.27.
134 .SH "SEE ALSO"
135 .BR read (2),
136 .BR fgets (3),
137 .BR fopen (3),
138 .BR fread (3),
139 .BR gets (3),
140 .BR scanf (3),
141 .BR feature_test_macros (7)