cgroups.7: wfix
[man-pages.git] / man3 / getprotoent_r.3
blob57b9c63afbdc63af23cb727f5332c0bd58abf609
1 .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
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.
13 .\"
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.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH GETPROTOENT_R 3  2019-03-06 "GNU" "Linux Programmer's Manual"
27 .SH NAME
28 getprotoent_r, getprotobyname_r, getprotobynumber_r \- get
29 protocol entry (reentrant)
30 .SH SYNOPSIS
31 .nf
32 .B #include <netdb.h>
33 .PP
34 .BI "int getprotoent_r(struct protoent *" result_buf ", char *" buf ,
35 .BI "                size_t " buflen ", struct protoent **" result );
36 .PP
37 .BI "int getprotobyname_r(const char *" name ,
38 .BI "                struct protoent *" result_buf ", char *" buf ,
39 .BI "                size_t " buflen ", struct protoent **" result );
40 .PP
41 .BI "int getprotobynumber_r(int " proto ,
42 .BI "                struct protoent *" result_buf ", char *" buf ,
43 .BI "                size_t " buflen ", struct protoent **" result );
44 .PP
45 .fi
46 .in -4n
47 Feature Test Macro Requirements for glibc (see
48 .BR feature_test_macros (7)):
49 .ad l
50 .in
51 .PP
52 .BR getprotoent_r (),
53 .BR getprotobyname_r (),
54 .BR getprotobynumber_r ():
55     Since glibc 2.19:
56         _DEFAULT_SOURCE
57     Glibc 2.19 and earlier:
58         _BSD_SOURCE || _SVID_SOURCE
59 .ad b
60 .SH DESCRIPTION
61 The
62 .BR getprotoent_r (),
63 .BR getprotobyname_r (),
64 and
65 .BR getprotobynumber_r ()
66 functions are the reentrant equivalents of, respectively,
67 .BR getprotoent (3),
68 .BR getprotobyname (3),
69 and
70 .BR getprotobynumber (3).
71 They differ in the way that the
72 .I protoent
73 structure is returned,
74 and in the function calling signature and return value.
75 This manual page describes just the differences from
76 the nonreentrant functions.
77 .PP
78 Instead of returning a pointer to a statically allocated
79 .I protoent
80 structure as the function result,
81 these functions copy the structure into the location pointed to by
82 .IR result_buf .
83 .PP
84 The
85 .I buf
86 array is used to store the string fields pointed to by the returned
87 .I protoent
88 structure.
89 (The nonreentrant functions allocate these strings in static storage.)
90 The size of this array is specified in
91 .IR buflen .
93 .I buf
94 is too small, the call fails with the error
95 .BR ERANGE ,
96 and the caller must try again with a larger buffer.
97 (A buffer of length 1024 bytes should be sufficient for most applications.)
98 .\" I can find no information on the required/recommended buffer size;
99 .\" the nonreentrant functions use a 1024 byte buffer.
100 .\" The 1024 byte value is also what the Solaris man page suggests. -- mtk
102 If the function call successfully obtains a protocol record, then
103 .I *result
104 is set pointing to
105 .IR result_buf ;
106 otherwise,
107 .I *result
108 is set to NULL.
109 .SH RETURN VALUE
110 On success, these functions return 0.
111 On error, they return one of the positive error numbers listed in ERRORS.
113 On error, record not found
114 .RB ( getprotobyname_r (),
115 .BR getprotobynumber_r ()),
116 or end of input
117 .RB ( getprotoent_r ())
118 .I result
119 is set to NULL.
120 .SH ERRORS
122 .B ENOENT
123 .RB ( getprotoent_r ())
124 No more records in database.
126 .B ERANGE
127 .I buf
128 is too small.
129 Try again with a larger buffer
130 (and increased
131 .IR buflen ).
132 .SH ATTRIBUTES
133 For an explanation of the terms used in this section, see
134 .BR attributes (7).
136 allbox;
137 lbw20 lb lb
138 l l l.
139 Interface       Attribute       Value
141 .BR getprotoent_r (),
143 .BR getprotobyname_r (),
145 .BR getprotobynumber_r ()
146 T}      Thread safety   MT-Safe locale
148 .sp 1
149 .SH CONFORMING TO
150 These functions are GNU extensions.
151 Functions with similar names exist on some other systems,
152 though typically with different calling signatures.
153 .SH EXAMPLE
154 The program below uses
155 .BR getprotobyname_r ()
156 to retrieve the protocol record for the protocol named
157 in its first command-line argument.
158 If a second (integer) command-line argument is supplied,
159 it is used as the initial value for
160 .IR buflen ;
162 .BR getprotobyname_r ()
163 fails with the error
164 .BR ERANGE ,
165 the program retries with larger buffer sizes.
166 The following shell session shows a couple of sample runs:
168 .in +4n
170 .RB "$" " ./a.out tcp 1"
171 ERANGE! Retrying with larger buffer
172 getprotobyname_r() returned: 0 (success)  (buflen=78)
173 p_name=tcp; p_proto=6; aliases=TCP
174 .RB "$" " ./a.out xxx 1"
175 ERANGE! Retrying with larger buffer
176 getprotobyname_r() returned: 0 (success)  (buflen=100)
177 Call failed/record not found
180 .SS Program source
183 #define _GNU_SOURCE
184 #include <ctype.h>
185 #include <netdb.h>
186 #include <stdlib.h>
187 #include <stdio.h>
188 #include <errno.h>
189 #include <string.h>
191 #define MAX_BUF 10000
194 main(int argc, char *argv[])
196     int buflen, erange_cnt, s;
197     struct protoent result_buf;
198     struct protoent *result;
199     char buf[MAX_BUF];
200     char **p;
202     if (argc < 2) {
203         printf("Usage: %s proto\-name [buflen]\en", argv[0]);
204         exit(EXIT_FAILURE);
205     }
207     buflen = 1024;
208     if (argc > 2)
209         buflen = atoi(argv[2]);
211     if (buflen > MAX_BUF) {
212         printf("Exceeded buffer limit (%d)\en", MAX_BUF);
213         exit(EXIT_FAILURE);
214     }
216     erange_cnt = 0;
217     do {
218         s = getprotobyname_r(argv[1], &result_buf,
219                      buf, buflen, &result);
220         if (s == ERANGE) {
221             if (erange_cnt == 0)
222                 printf("ERANGE! Retrying with larger buffer\en");
223             erange_cnt++;
225             /* Increment a byte at a time so we can see exactly
226                what size buffer was required */
228             buflen++;
230             if (buflen > MAX_BUF) {
231                 printf("Exceeded buffer limit (%d)\en", MAX_BUF);
232                 exit(EXIT_FAILURE);
233             }
234         }
235     } while (s == ERANGE);
237     printf("getprotobyname_r() returned: %s  (buflen=%d)\en",
238             (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
239             strerror(s), buflen);
241     if (s != 0 || result == NULL) {
242         printf("Call failed/record not found\en");
243         exit(EXIT_FAILURE);
244     }
246     printf("p_name=%s; p_proto=%d; aliases=",
247                 result_buf.p_name, result_buf.p_proto);
248     for (p = result_buf.p_aliases; *p != NULL; p++)
249         printf("%s ", *p);
250     printf("\en");
252     exit(EXIT_SUCCESS);
255 .SH SEE ALSO
256 .BR getprotoent (3),
257 .BR protocols (5)