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 3.3 22:02:10 11/16/90";
- #endif
-
- static FILE *shadow;
- #define FIELDS 9
- #define OFIELDS 5
-
- void
- setspent ()
- {
- if (shadow)
- rewind (shadow);
- else
- shadow = fopen (SHADOW, "r");
- }
-
- void
- endspent ()
- {
- if (shadow)
- (void) fclose (shadow);
-
- shadow = (FILE *) 0;
- }
-
- struct spwd *
- sgetspent (string)
- char *string;
- {
- static char buf[BUFSIZ];
- static struct spwd spwd;
- char *fields[FIELDS];
- char *cp;
- char *cpp;
- int atoi ();
- long atol ();
- int i;
-
- strncpy (buf, string, BUFSIZ-1);
- buf[BUFSIZ-1] = '\0';
-
- if (cp = strrchr (buf, '\n'))
- *cp = '\0';
-
- for (cp = buf, i = 0;*cp && i < FIELDS;i++) {
- fields[i] = cp;
- while (*cp && *cp != ':')
- cp++;
-
- if (*cp)
- *cp++ = '\0';
- }
- if (*cp || (i != FIELDS && i != OFIELDS))
- return 0;
-
- spwd.sp_namp = fields[0];
- spwd.sp_pwdp = fields[1];
-
- if ((spwd.sp_lstchg = strtol (fields[2], &cpp, 10)) == 0 && *cpp)
- if (fields[2][0] == '\0')
- spwd.sp_lstchg = -1;
- else
- return 0;
-
- if ((spwd.sp_min = strtol (fields[3], &cpp, 10)) == 0 && *cpp)
- if (fields[3][0] == '\0')
- spwd.sp_min = -1;
- else
- return 0;
-
- if ((spwd.sp_max = strtol (fields[4], &cpp, 10)) == 0 && *cpp)
- if (fields[4][0] == '\0')
- spwd.sp_max = -1;
- else
- return 0;
-
- if (i == OFIELDS) {
- spwd.sp_warn = spwd.sp_inact = spwd.sp_expire =
- spwd.sp_flag = -1;
-
- return &spwd;
- }
- if ((spwd.sp_warn = strtol (fields[5], &cpp, 10)) == 0 && *cpp)
- if (fields[5][0] == '\0')
- spwd.sp_warn = -1;
- else
- return 0;
-
- if ((spwd.sp_inact = strtol (fields[6], &cpp, 10)) == 0 && *cpp)
- if (fields[6][0] == '\0')
- spwd.sp_inact = -1;
- else
- return 0;
-
- if ((spwd.sp_expire = strtol (fields[7], &cpp, 10)) == 0 && *cpp)
- if (fields[7][0] == '\0')
- spwd.sp_expire = -1;
- else
- return 0;
-
- if ((spwd.sp_flag = strtol (fields[8], &cpp, 10)) == 0 && *cpp)
- if (fields[8][0] == '\0')
- spwd.sp_flag = -1;
- else
- return 0;
-
- return (&spwd);
- }
-
- struct spwd
- *fgetspent (fp)
- FILE *fp;
- {
- char buf[BUFSIZ];
-
- if (! fp)
- return (0);
-
- if (fgets (buf, BUFSIZ, fp) == (char *) 0)
- return (0);
-
- return sgetspent (buf);
- }
-
- 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;
- {
- int errors = 0;
-
- if (! fp || ! spwd)
- return -1;
-
- if (fprintf (fp, "%s:%s:", spwd->sp_namp, spwd->sp_pwdp) < 0)
- errors++;
-
- if (spwd->sp_lstchg != -1) {
- if (fprintf (fp, "%ld:", spwd->sp_lstchg) < 0)
- errors++;
- } else if (putc (':', fp) == EOF)
- errors++;
-
- if (spwd->sp_min != -1) {
- if (fprintf (fp, "%ld:", spwd->sp_min) < 0)
- errors++;
- } else if (putc (':', fp) == EOF)
- errors++;
-
- if (spwd->sp_max != -1) {
- if (fprintf (fp, "%ld", spwd->sp_max) < 0)
- errors++;
- }
-
- /*
- * See if the structure has any of the SVR4 fields in
- * it. If none of those fields have any data there is
- * no reason to write them out since they will be filled
- * in the same way when they are read back in. Otherwise
- * there is at least one SVR4 field that must be output.
- */
-
- if (spwd->sp_warn == -1 && spwd->sp_inact == -1 &&
- spwd->sp_expire == -1 && spwd->sp_flag == -1) {
- if (putc ('\n', fp) == EOF || errors)
- return -1;
- else
- return 0;
- } else if (putc (':', fp) == EOF)
- errors++;
-
- if (spwd->sp_warn != -1) {
- if (fprintf (fp, "%ld:", spwd->sp_warn) < 0)
- errors++;
- } else if (putc (':', fp) == EOF)
- errors++;
-
- if (spwd->sp_inact != -1) {
- if (fprintf (fp, "%ld:", spwd->sp_inact) < 0)
- errors++;
- } else if (putc (':', fp) == EOF)
- errors++;
-
- if (spwd->sp_expire != -1) {
- if (fprintf (fp, "%ld:", spwd->sp_expire) < 0)
- errors++;
- } else if (putc (':', fp) == EOF)
- errors++;
-
- if (spwd->sp_flag != -1) {
- if (fprintf (fp, "%ld", spwd->sp_flag) < 0)
- errors++;
- }
- if (putc ('\n', fp) == EOF)
- errors++;
-
- if (errors)
- return -1;
- else
- return 0;
- }
-