libdl: end must be bigger than start
[uclibc-ng.git] / libc / pwd_grp / pwd_grp_internal.c
blobf82c298d57f3b270775b6e4117b64b2f6a960cfe
1 /*
2 * Copyright (C) 2003 Manuel Novoa III <mjn3@uclibc.org>
3 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
5 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6 */
8 /* Nov 6, 2003 Initial version.
10 * NOTE: This implementation is quite strict about requiring all
11 * field seperators. It also does not allow leading whitespace
12 * except when processing the numeric fields. glibc is more
13 * lenient. See the various glibc difference comments below.
15 * TODO:
16 * Move to dynamic allocation of (currently statically allocated)
17 * buffers; especially for the group-related functions since
18 * large group member lists will cause error returns.
22 #include <features.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <stddef.h>
28 #include <errno.h>
29 #include <malloc.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <paths.h>
35 #ifdef __UCLIBC_HAS_SHADOW__
36 #include <shadow.h>
37 #endif
39 /**********************************************************************/
40 /* Prototypes for internal functions. */
42 #if !defined(GETXXKEY_R_FUNC) && !defined(GETXXKEY_FUNC)
43 #error GETXXKEY_R_FUNC/GETXXKEY_FUNC are not defined!
44 #endif
45 /**********************************************************************/
46 #ifdef GETXXKEY_R_FUNC
48 libc_hidden_proto(GETXXKEY_R_FUNC)
49 int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
50 GETXXKEY_R_ENTTYPE *__restrict resultbuf,
51 char *__restrict buffer, size_t buflen,
52 GETXXKEY_R_ENTTYPE **__restrict result)
54 FILE *stream;
55 int rv;
57 *result = NULL;
59 if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
60 rv = errno;
61 } else {
62 __STDIO_SET_USER_LOCKING(stream);
63 do {
64 if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
65 buffer, buflen, stream))
66 ) {
67 if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
68 *result = resultbuf;
69 break;
71 } else {
72 if (rv == ENOENT) { /* end-of-file encountered. */
73 rv = 0;
75 break;
77 } while (1);
78 fclose(stream);
81 return rv;
83 libc_hidden_def(GETXXKEY_R_FUNC)
85 #endif /* GETXXKEY_R_FUNC */
87 /**********************************************************************/
88 #ifdef GETXXKEY_FUNC
90 #define REENTRANT_NAME APPEND_R(GETXXKEY_FUNC)
91 #define APPEND_R(name) APPEND_R1(name)
92 #define APPEND_R1(name) name##_r
94 GETXXKEY_ENTTYPE *GETXXKEY_FUNC(GETXXKEY_ADD_PARAMS)
96 static char *buffer = NULL;
97 static GETXXKEY_ENTTYPE resultbuf;
98 GETXXKEY_ENTTYPE *result;
100 if (buffer == NULL)
101 buffer = (char *)__uc_malloc(GETXXKEY_BUFLEN);
103 # ifdef GETXXKEY_ADD_VARIABLES
104 REENTRANT_NAME(GETXXKEY_ADD_VARIABLES, &resultbuf, buffer, GETXXKEY_BUFLEN, &result);
105 # else
106 REENTRANT_NAME(&resultbuf, buffer, GETXXKEY_BUFLEN, &result);
107 # endif
108 return result;
110 #ifdef GETXXKEY_FUNC_HIDDEN
111 libc_hidden_def(GETXXKEY_FUNC)
112 #endif
114 #undef REENTRANT_NAME
115 #undef APPEND_R
116 #undef APPEND_R1
117 #endif /* GETXXKEY_FUNC */
119 /**********************************************************************/
120 #undef GETXXKEY_FUNC
121 #undef GETXXKEY_ENTTYPE
122 #undef GETXXKEY_BUFLEN
123 #undef GETXXKEY_FUNC_HIDDEN
124 #undef GETXXKEY_ADD_PARAMS
125 #undef GETXXKEY_ADD_VARIABLES
126 #undef GETXXKEY_R_FUNC
127 #undef GETXXKEY_R_PARSER
128 #undef GETXXKEY_R_ENTTYPE
129 #undef GETXXKEY_R_TEST
130 #undef DO_GETXXKEY_R_KEYTYPE
131 #undef DO_GETXXKEY_R_PATHNAME