getpw_r.c
Go to the documentation of this file.
1 
2 #include <../pthread.h>
3 #include <pwd.h>
4 #include <sys/types.h>
5 #include <string.h>
6 
7 static pthread_mutex_t getpw_mutex = PTHREAD_MUTEX_INITIALIZER;
8 
9 static int
10 convert (struct passwd *ret, struct passwd *result,
11  char *buf, int buflen)
12 {
13  int len;
14 
15  if (!buf) return -1;
16 
17  *result = *ret;
18 /* user name */
19  result->pw_name = (char *) buf;
20  len = strlen (ret->pw_name) + 1;
21  if (len > buflen) return -1;
22  buflen -= len;
23  buf += len;
24  strcpy (result->pw_name, ret->pw_name);
25 
26 /* user password */
27  result->pw_passwd = (char *) buf;
28  len = strlen (ret->pw_passwd) + 1;
29  if (len > buflen) return -1;
30  buflen -= len;
31  buf += len;
32  strcpy (result->pw_passwd, ret->pw_passwd);
33 
34 /* real name */
35  result->pw_gecos = (char *) buf;
36  len = strlen (ret->pw_gecos) + 1;
37  if (len > buflen) return -1;
38  buflen -= len;
39  buf += len;
40  strcpy (result->pw_gecos, ret->pw_gecos);
41 
42 /* home directory */
43  result->pw_dir = (char *) buf;
44  len = strlen (ret->pw_dir) + 1;
45  if (len > buflen) return -1;
46  buflen -= len;
47  buf += len;
48  strcpy (result->pw_dir, ret->pw_dir);
49 
50 /* shell program */
51  result->pw_shell = (char *) buf;
52  len = strlen (ret->pw_shell) + 1;
53  if (len > buflen) return -1;
54  buflen -= len;
55  buf += len;
56  strcpy (result->pw_shell, ret->pw_shell);
57 
58  return 0;
59 }
60 
61 int
62 getpwnam_r (const char *name,
63  struct passwd *result, char *buffer, size_t buflen)
64 {
65  struct passwd *ret;
66 
67  pthread_mutex_lock (&getpw_mutex);
68 
69  ret = getpwnam (name);
70  if (!ret ||
71  convert (ret, result, buffer, buflen) != 0)
72  {
73  result = NULL;
74  }
75 
76  pthread_mutex_unlock (&getpw_mutex);
77  return 0;
78 }
79 
80 int
81 getpwuid_r (uid_t uid,
82  struct passwd *result, char *buffer, size_t buflen)
83 {
84  struct passwd *ret;
85 
86  pthread_mutex_lock (&getpw_mutex);
87 
88  ret = getpwuid(uid);
89  if (!ret ||
90  convert (ret, result, buffer, buflen) != 0)
91  {
92  result = NULL;
93  }
94 
95  pthread_mutex_unlock (&getpw_mutex);
96  return 0;
97 }
int getpwuid_r(uid_t uid, struct passwd *result, char *buffer, size_t buflen)
Definition: getpw_r.c:81
static int convert(struct passwd *ret, struct passwd *result, char *buf, int buflen)
Definition: getpw_r.c:10
static pthread_mutex_t getpw_mutex
Definition: getpw_r.c:7
#define NULL
Definition: transargv.c:8
static char buf[CHAR_SIZE]
Definition: helpsub.c:23
int getpwnam_r(const char *name, struct passwd *result, char *buffer, size_t buflen)
Definition: getpw_r.c:62


euslisp
Author(s): Toshihiro Matsui
autogenerated on Fri Feb 21 2020 03:20:54