Back to index

glibc  2.9
getpw.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,96,98,99,2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <alloca.h>
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <unistd.h>
00023 #include <pwd.h>
00024 
00025 
00026 /* Re-construct the password-file line for the given uid
00027    in the given buffer.  This knows the format that the caller
00028    will expect, but this need not be the format of the password file.  */
00029 
00030 int __getpw (__uid_t uid, char *buf);
00031 
00032 int
00033 __getpw (uid, buf)
00034      __uid_t uid;
00035      char *buf;
00036 {
00037   size_t buflen;
00038   char *tmpbuf;
00039   struct passwd resbuf, *p;
00040 
00041   if (buf == NULL)
00042     {
00043       __set_errno (EINVAL);
00044       return -1;
00045     }
00046 
00047   buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
00048   tmpbuf = alloca (buflen);
00049 
00050   if (__getpwuid_r (uid, &resbuf, tmpbuf, buflen, &p) != 0)
00051     return -1;
00052 
00053   if (p == NULL)
00054     return -1;
00055 
00056   if (sprintf (buf, "%s:%s:%lu:%lu:%s:%s:%s", p->pw_name, p->pw_passwd,
00057               (unsigned long int) p->pw_uid, (unsigned long int) p->pw_gid,
00058               p->pw_gecos, p->pw_dir, p->pw_shell) < 0)
00059     return -1;
00060 
00061   return 0;
00062 }
00063 weak_alias (__getpw, getpw)
00064 
00065 link_warning (getpw, "the `getpw' function is dangerous and should not be used.")