getpw_r.c
Go to the documentation of this file.
00001 
00002 #include <../pthread.h>
00003 #include <pwd.h>
00004 #include <sys/types.h>
00005 #include <string.h>
00006 
00007 static pthread_mutex_t getpw_mutex = PTHREAD_MUTEX_INITIALIZER;
00008 
00009 static int
00010 convert (struct passwd *ret, struct passwd *result,
00011        char *buf, int buflen)
00012 {
00013   int len;
00014 
00015   if (!buf) return -1;
00016 
00017   *result = *ret;
00018 /*  user name */
00019   result->pw_name = (char *) buf;
00020   len = strlen (ret->pw_name) + 1;
00021   if (len > buflen) return -1;
00022   buflen -= len;
00023   buf += len;
00024   strcpy (result->pw_name, ret->pw_name);
00025 
00026 /*  user password */
00027   result->pw_passwd = (char *) buf;
00028   len = strlen (ret->pw_passwd) + 1;
00029   if (len > buflen) return -1;
00030   buflen -= len;
00031   buf += len;
00032   strcpy (result->pw_passwd, ret->pw_passwd);
00033 
00034 /*  real name */
00035   result->pw_gecos = (char *) buf;
00036   len = strlen (ret->pw_gecos) + 1;
00037   if (len > buflen) return -1;
00038   buflen -= len;
00039   buf += len;
00040   strcpy (result->pw_gecos, ret->pw_gecos);
00041 
00042 /*  home directory */
00043   result->pw_dir = (char *) buf;
00044   len = strlen (ret->pw_dir) + 1;
00045   if (len > buflen) return -1;
00046   buflen -= len;
00047   buf += len;
00048   strcpy (result->pw_dir, ret->pw_dir);
00049 
00050 /* shell program */
00051   result->pw_shell = (char *) buf;
00052   len = strlen (ret->pw_shell) + 1;
00053   if (len > buflen) return -1;
00054   buflen -= len;
00055   buf += len;
00056   strcpy (result->pw_shell, ret->pw_shell);
00057 
00058   return 0;
00059 }
00060 
00061 int
00062 getpwnam_r (const char *name, 
00063        struct passwd *result, char *buffer, size_t buflen)
00064 {
00065   struct passwd *ret;
00066 
00067   pthread_mutex_lock (&getpw_mutex);
00068 
00069   ret = getpwnam (name);
00070   if (!ret ||
00071        convert (ret, result, buffer, buflen) != 0)
00072   {
00073     result = NULL;
00074   }
00075 
00076   pthread_mutex_unlock (&getpw_mutex);
00077   return 0;
00078 }
00079 
00080 int
00081 getpwuid_r (uid_t uid, 
00082        struct passwd *result, char *buffer, size_t buflen)
00083 {
00084   struct passwd *ret;
00085 
00086   pthread_mutex_lock (&getpw_mutex);
00087 
00088   ret = getpwuid(uid);
00089   if (!ret ||
00090        convert (ret, result, buffer, buflen) != 0)
00091   {
00092     result = NULL;
00093   }
00094 
00095   pthread_mutex_unlock (&getpw_mutex);
00096   return 0;
00097 }


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Sep 3 2015 10:36:19