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