getprotoent_r(3) Library Functions Manual getprotoent_r(3) getprotoent_r, getprotobyname_r, getprotobynumber_r - ( ) LIBRARY Standard C library (libc, -lc) #include int getprotoent_r(struct protoent *restrict result_buf, char buf[restrict .buflen], size_t buflen, struct protoent **restrict result); int getprotobyname_r(const char *restrict name, struct protoent *restrict result_buf, char buf[restrict .buflen], size_t buflen, struct protoent **restrict result); int getprotobynumber_r(int proto, struct protoent *restrict result_buf, char buf[restrict .buflen], size_t buflen, struct protoent **restrict result); glibc (. feature_test_macros(7)): getprotoent_r(), getprotobyname_r(), getprotobynumber_r(): glibc 2.19: _DEFAULT_SOURCE glibc 2.19 : _BSD_SOURCE || _SVID_SOURCE getprotoent_r(), getprotobyname_r() getprotobynumber_r() getprotoent(3), getprotobyname(3) getprotobynumber(3), . protoent, . . protoent , result_buf. buf protoent ( ). buflen. buf , ERANGE, ( 1024 ). , *result result_buf; *result NULL. 0. , . , (getprotobyname_r(), getprotobynumber_r()) (getprotoent_r()) result NULL. ENOENT (getprotoent_r()) . ERANGE buf . ( buflen). attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getprotoent_r(), | | MT-Safe locale | |getprotobyname_r(), | | | |getprotobynumber_r() | | | +----------------------------+----------------------------------------------------------+--------------------------+ Functions with similar names exist on some other systems, though typically with different calling signatures. GNU. , , getprotobyname_r() , . (), buflen; getprotobyname_r() ERANGE, . : $ ./a.out tcp 1 ERANGE! getprotobyname_r(): 0 () (buflen=78) p_name=tcp; p_proto=6; aliases=TCP $ ./a.out xxx 1 ERANGE! getprotobyname_r(): 0 () (buflen=100) / #define _GNU_SOURCE #include #include #include #include #include #include #define MAX_BUF 10000 int main(int argc, char *argv[]) { int buflen, erange_cnt, s; struct protoent result_buf; struct protoent *result; char buf[MAX_BUF]; if (argc < 2) { printf("Usage: %s proto-name [buflen]\n", argv[0]); exit(EXIT_FAILURE); } buflen = 1024; if (argc > 2) buflen = atoi(argv[2]); if (buflen > MAX_BUF) { printf("Exceeded buffer limit (%d)\n", MAX_BUF); exit(EXIT_FAILURE); } erange_cnt = 0; do { s = getprotobyname_r(argv[1], &result_buf, buf, buflen, &result); if (s == ERANGE) { if (erange_cnt == 0) printf("ERANGE! Retrying with larger buffer\n"); erange_cnt++; /* Increment a byte at a time so we can see exactly what size buffer was required. */ buflen++; if (buflen > MAX_BUF) { printf("Exceeded buffer limit (%d)\n", MAX_BUF); exit(EXIT_FAILURE); } } } while (s == ERANGE); printf("getprotobyname_r() returned: %s (buflen=%d)\n", (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" : strerror(s), buflen); if (s != 0 || result == NULL) { printf("Call failed/record not found\n"); exit(EXIT_FAILURE); } printf("p_name=%s; p_proto=%d; aliases=", result_buf.p_name, result_buf.p_proto); for (char **p = result_buf.p_aliases; *p != NULL; p++) printf("%s ", *p); printf("\n"); exit(EXIT_SUCCESS); } . getprotoent(3), protocols(5) Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . getprotoent_r(3)