2 * Copyright (c) 2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
42 rk_getpwnam_r(const char *, struct passwd
*, char *, size_t, struct passwd
**);
45 #if !defined(POSIX_GETPWNAM_R) || defined(TEST_GETXXYYY)
48 * At least limit the race between threads
51 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
52 rk_getpwnam_r(const char *name
, struct passwd
*pwd
, char *buffer
,
53 size_t bufsize
, struct passwd
**result
)
62 return (errno
= ENOENT
);
64 memset(pwd
, 0, sizeof(*pwd
));
68 slen = strlen(p->el) + 1; \
69 if (slen > bufsize) return (errno = ENOMEM); \
70 memcpy(buffer, p->el, slen); \
79 pwd
->pw_uid
= p
->pw_uid
;
80 pwd
->pw_gid
= p
->pw_gid
;
90 #endif /* POSIX_GETPWNAM_R */
99 print_result(struct passwd
*p
)
103 printf("%s\n", p
->pw_name
);
104 printf("%d\n", (int)p
->pw_uid
);
105 printf("%s\n", p
->pw_shell
);
106 printf("%s\n", p
->pw_dir
);
110 main(int argc
, char **argv
)
112 struct passwd pwd
, *result
;
117 user
= getenv("USER");
121 ret
= rk_getpwnam_r(user
, &pwd
, buf
, sizeof(buf
), &result
);
123 errx(1, "rk_getpwnam_r");
124 print_result(result
);
126 ret
= rk_getpwnam_r(user
, &pwd
, buf
, 1, &result
);
128 errx(1, "rk_getpwnam_r too small buf");
130 ret
= rk_getpwnam_r("no-user-here-promise", &pwd
, buf
, sizeof(buf
), &result
);
132 errx(1, "rk_getpwnam_r no user");