• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "pwf.h"
2 #include <pthread.h>
3 
4 #define FIX(x) (pw->pw_##x = pw->pw_##x-line+buf)
5 
getpw_r(const char * name,uid_t uid,struct passwd * pw,char * buf,size_t size,struct passwd ** res)6 static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res)
7 {
8 	char *line = 0;
9 	size_t len = 0;
10 	int rv = 0;
11 	int cs;
12 
13 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
14 
15 	rv = __getpw_a(name, uid, pw, &line, &len, res);
16 	if (*res && size < len) {
17 		*res = 0;
18 		rv = ERANGE;
19 	}
20 	if (*res) {
21 		memcpy(buf, line, len);
22 		FIX(name);
23 		FIX(passwd);
24 		FIX(gecos);
25 		FIX(dir);
26 		FIX(shell);
27 	}
28  	free(line);
29 	pthread_setcancelstate(cs, 0);
30 	if (rv) errno = rv;
31 	return rv;
32 }
33 
getpwnam_r(const char * name,struct passwd * pw,char * buf,size_t size,struct passwd ** res)34 int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t size, struct passwd **res)
35 {
36 	return getpw_r(name, 0, pw, buf, size, res);
37 }
38 
getpwuid_r(uid_t uid,struct passwd * pw,char * buf,size_t size,struct passwd ** res)39 int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res)
40 {
41 	return getpw_r(0, uid, pw, buf, size, res);
42 }
43