1 #include "pwf.h"
2 #include <pthread.h>
3
4 #define FIX(x) (gr->gr_##x = gr->gr_##x-line+buf)
5
getgr_r(const char * name,gid_t gid,struct group * gr,char * buf,size_t size,struct group ** res)6 static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf, size_t size, struct group **res)
7 {
8 char *line = 0;
9 size_t len = 0;
10 char **mem = 0;
11 size_t nmem = 0;
12 int rv = 0;
13 size_t i;
14 int cs;
15
16 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
17
18 rv = __getgr_a(name, gid, gr, &line, &len, &mem, &nmem, res);
19 if (*res && size < len + (nmem+1)*sizeof(char *) + 32) {
20 *res = 0;
21 rv = ERANGE;
22 }
23 if (*res) {
24 buf += (16-(uintptr_t)buf)%16;
25 gr->gr_mem = (void *)buf;
26 buf += (nmem+1)*sizeof(char *);
27 memcpy(buf, line, len);
28 FIX(name);
29 FIX(passwd);
30 for (i=0; mem[i]; i++)
31 gr->gr_mem[i] = mem[i]-line+buf;
32 gr->gr_mem[i] = 0;
33 }
34 free(mem);
35 free(line);
36 pthread_setcancelstate(cs, 0);
37 if (rv) errno = rv;
38 return rv;
39 }
40
getgrnam_r(const char * name,struct group * gr,char * buf,size_t size,struct group ** res)41 int getgrnam_r(const char *name, struct group *gr, char *buf, size_t size, struct group **res)
42 {
43 return getgr_r(name, 0, gr, buf, size, res);
44 }
45
getgrgid_r(gid_t gid,struct group * gr,char * buf,size_t size,struct group ** res)46 int getgrgid_r(gid_t gid, struct group *gr, char *buf, size_t size, struct group **res)
47 {
48 return getgr_r(0, gid, gr, buf, size, res);
49 }
50