OSDN Git Service

mkostemp: fix implementation
[uclinux-h8/uClibc.git] / libc / pwd_grp / pwd_grp_internal.c
1 /*
2  * Copyright (C) 2003     Manuel Novoa III <mjn3@uclibc.org>
3  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
4  *
5  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6  */
7
8 /*  Nov 6, 2003  Initial version.
9  *
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.
14  *
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.
19  *
20  */
21
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 <assert.h>
30 #include <ctype.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <paths.h>
34 #ifdef __UCLIBC_HAS_SHADOW__
35 #include <shadow.h>
36 #endif
37
38 /**********************************************************************/
39 /* Sizes for statically allocated buffers. */
40
41 /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
42  * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
43 #define PWD_BUFFER_SIZE 256
44 #define GRP_BUFFER_SIZE 256
45
46 /**********************************************************************/
47 /* Prototypes for internal functions. */
48
49 #ifndef GETXXKEY_R_FUNC
50 #error GETXXKEY_R_FUNC is not defined!
51 #endif
52 /**********************************************************************/
53 #ifdef GETXXKEY_R_FUNC
54
55 libc_hidden_proto(GETXXKEY_R_FUNC)
56 int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
57                                         GETXXKEY_R_ENTTYPE *__restrict resultbuf,
58                                         char *__restrict buffer, size_t buflen,
59                                         GETXXKEY_R_ENTTYPE **__restrict result)
60 {
61         FILE *stream;
62         int rv;
63
64         *result = NULL;
65
66         if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
67                 rv = errno;
68         } else {
69                 __STDIO_SET_USER_LOCKING(stream);
70                 do {
71                         if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
72                                                                    buffer, buflen, stream))
73                                 ) {
74                                 if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
75                                         *result = resultbuf;
76                                         break;
77                                 }
78                         } else {
79                                 if (rv == ENOENT) {     /* end-of-file encountered. */
80                                         rv = 0;
81                                 }
82                                 break;
83                         }
84                 } while (1);
85                 fclose(stream);
86         }
87
88         return rv;
89 }
90 libc_hidden_def(GETXXKEY_R_FUNC)
91
92 #endif
93 /**********************************************************************/
94 #undef GETXXKEY_R_FUNC_HIDDEN
95 #undef GETXXKEY_R_FUNC
96 #undef GETXXKEY_R_PARSER
97 #undef GETXXKEY_R_ENTTYPE
98 #undef GETXXKEY_R_TEST
99 #undef DO_GETXXKEY_R_KEYTYPE
100 #undef DO_GETXXKEY_R_PATHNAME