home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 4
/
DATAFILE_PDCD4.iso
/
unix
/
unixlib36d
/
src
/
c
/
getpw
< prev
next >
Wrap
Text File
|
1994-03-08
|
4KB
|
271 lines
#ifdef __STDC__
static char sccs_id[] = "@(#) getpw.c 2.1 " __DATE__ " HJR";
#else
static char sccs_id[] = "@(#) getpw.c 2.1 11/08/90 HJR";
#endif
/* getpw.c (c) Copyright 1990 H.Rogers */
/* #define P_TEST */
/* test main() */
#include <stdio.h>
#include <string.h>
#ifdef __STDC__
#include <stdlib.h>
#else
extern int exit ();
extern int atoi ();
#endif
#include "pwd.h"
#ifdef __STDC__
static void p_pdecode (char *, struct passwd *);
static char *p_pstrcp (char **);
#else
static void p_pdecode ();
static char *p_pstrcp ();
#endif
#define PBUFSIZ 256 /* max. length of a line in /etc/passwd */
static char *p_buf = 0;
static FILE *p_pwfile = 0;
static struct passwd p_rval[1];
static char *pwfile = "/etc/passwd";
/* getpwent() */
#ifdef __STDC__
struct passwd *
getpwent (void)
#else
struct passwd *
getpwent ()
#endif
{
if (!p_pwfile)
if (setpwfile (pwfile) < 0)
return (0);
return (fgetpwent (p_pwfile));
}
/* fgetpwent() */
#ifdef __STDC__
struct passwd *
fgetpwent (register FILE * pfile)
#else
struct passwd *
fgetpwent (pfile)
register FILE *pfile;
#endif
{
register char *bp;
if (!pfile)
return (0);
if (!p_buf)
if (!(p_buf = malloc (PBUFSIZ)))
return (0);
if (!fgets (p_buf, 1024, pfile))
return (0);
bp = p_buf;
while (*bp)
bp++;
if (*--bp != '\n')
return (0);
*bp = 0;
p_pdecode (p_buf, p_rval);
return (p_rval);
}
/* getpwuid() */
#ifdef __STDC__
struct passwd *
getpwuid (register int uid)
#else
struct passwd *
getpwuid (uid)
register int uid;
#endif
{
if (setpwent () < 0)
return (0);
while (fgetpwent (p_pwfile))
if (p_rval->pw_uid == uid)
return (p_rval);
return (0);
}
/* getpwnam() */
#ifdef __STDC__
struct passwd *
getpwnam (register char *name)
#else
struct passwd *
getpwnam (name)
register char *name;
#endif
{
if (!(name ? *name : 0))
return (0);
if (setpwent () < 0)
return (0);
while (fgetpwent (p_pwfile))
if (!strcmp (p_rval->pw_name, name) && p_rval->pw_name)
return (p_rval);
return (0);
}
/* setpwent() */
#ifdef __STDC__
int
setpwent (void)
#else
int
setpwent ()
#endif
{
if (!p_pwfile)
if (setpwfile (pwfile) < 0)
return (-1);
return (fseek (p_pwfile, 0L, 0) ? -1 : 0);
}
/* endpwent() */
#ifdef __STDC__
int
endpwent (void)
#else
int
endpwent ()
#endif
{
if (!p_pwfile)
return (-1); /* 0 ? */
if (fclose (p_pwfile))
return (-1);
p_pwfile = 0;
return (0);
}
/* setpwfile() */
#ifdef __STDC__
int
setpwfile (char *name)
#else
int
setpwfile (name)
char *name;
#endif
{
if (!(name ? *name : 0))
return (-1); /* 0 ? */
if (p_pwfile)
{
fclose (p_pwfile);
p_pwfile = 0;
}
return ((p_pwfile = fopen (name, "r")) ? 0 : -1);
}
/* p_pdecode() */
#ifdef __STDC__
static void
p_pdecode (register char *line, register struct passwd *passwd)
#else
static void
p_pdecode (line, passwd)
register char *line;
register struct passwd *passwd;
#endif
{
char *lp;
if ((!line) || (!passwd))
return;
lp = line;
passwd->pw_name = p_pstrcp (&lp);
passwd->pw_passwd = p_pstrcp (&lp);
passwd->pw_uid = atoi (p_pstrcp (&lp));
passwd->pw_gid = atoi (p_pstrcp (&lp));
passwd->pw_quota = 0;
passwd->pw_comment = 0;
passwd->pw_gecos = p_pstrcp (&lp);
passwd->pw_dir = p_pstrcp (&lp);
passwd->pw_shell = p_pstrcp (&lp);
}
/* p_pstrcp() */
#ifdef __STDC__
static char *
p_pstrcp (register char **lp)
#else
static char *
p_pstrcp (lp)
register char **lp;
#endif
{
register char *l = *lp, *r = l;
while ((*l != ':') && *l)
l++;
if (*l)
*l++ = 0;
*lp = l;
return (r);
}
#ifdef P_TEST
/* main() */
#ifdef __STDC__
int
main (int argc, char **argv)
#else
int
main (argc, argv)
int argc;
char **argv;
#endif
{
struct passwd *p;
if (argc != 2)
{
fprintf (stderr, "usage: getpw user\n");
exit (1);
}
if (!(p = getpwnam (argv[1])))
{
printf ("getpw: %s not found\n", argv[1]);
exit (1);
}
printf ("name:\t%s\n", p->pw_name);
printf ("passwd:\t%s\n", p->pw_passwd);
printf ("uid:\t%d\n", p->pw_uid);
printf ("gid:\t%d\n", p->pw_gid);
printf ("gecos:\t%s\n", p->pw_gecos);
printf ("dir:\t%s\n", p->pw_dir);
printf ("shell:\t%s\n", p->pw_shell);
}
#endif /* P_TEST */