home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1989, 1990, John F. Haugh II
- * All rights reserved.
- *
- * Use, duplication, and disclosure prohibited without
- * the express written permission of the author.
- */
-
- #include "shadow.h"
- #include <stdio.h>
- #ifndef BSD
- #include <string.h>
- #include <memory.h>
- #else
- #include <strings.h>
- #define strchr index
- #define strrchr rindex
- #endif
-
- #ifndef lint
- static char _sccsid[] = "@(#)shadow.c 2.5 19:24:12 7/29/90";
- #endif
-
- static FILE *shadow;
- #define FIELDS 5
-
- void setspent ()
- {
- if (shadow)
- rewind (shadow);
- else
- shadow = fopen (SHADOW, "r");
- }
-
- void endspent ()
- {
- if (shadow)
- (void) fclose (shadow);
-
- shadow = (FILE *) 0;
- }
-
- struct spwd *fgetspent (fp)
- FILE *fp;
- {
- static struct spwd spwd;
- static char buf[BUFSIZ];
- char *fields[FIELDS];
- char *cp;
- int i;
- int atoi ();
- long atol ();
-
- if (! fp)
- return (0);
-
- if (fgets (buf, BUFSIZ, fp) == (char *) 0)
- return (0);
-
- if (cp = strrchr (buf, '\n'))
- *cp = '\0';
-
- for (cp = buf, i = 0;i < FIELDS;i++) {
- fields[i] = cp;
- while (*cp && *cp != ':')
- cp++;
-
- if (*cp)
- *cp++ = '\0';
- }
- if (*cp || i != FIELDS)
- return 0;
-
- spwd.sp_namp = fields[0];
- spwd.sp_pwdp = fields[1];
-
- if ((spwd.sp_lstchg = strtol (fields[2], (char **) 0, 10)) == -1)
- return 0;
-
- if ((spwd.sp_min = strtol (fields[3], (char **) 0, 10)) == -1)
- return 0;
-
- if ((spwd.sp_max = strtol (fields[4], (char **) 0, 10)) == -1)
- return 0;
-
- return (&spwd);
- }
-
- struct spwd *getspent ()
- {
- if (! shadow)
- setspent ();
-
- return (fgetspent (shadow));
- }
-
- struct spwd *getspnam (name)
- char *name;
- {
- struct spwd *spwd;
-
- setspent ();
-
- while ((spwd = getspent ()) != (struct spwd *) 0) {
- if (strcmp (name, spwd->sp_namp) == 0)
- return (spwd);
- }
- return (0);
- }
-
- int putspent (spwd, fp)
- struct spwd *spwd;
- FILE *fp;
- {
- if (! fp)
- return -1;
-
- fprintf (fp, "%s:%s:%ld:%ld:%ld\n",
- spwd->sp_namp, spwd->sp_pwdp,
- spwd->sp_lstchg, spwd->sp_min, spwd->sp_max);
-
- if (ferror (fp))
- return -1;
- else
- return 0;
- }
-