home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1704 / shadow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.9 KB  |  127 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. #include "shadow.h"
  10. #include <stdio.h>
  11. #ifndef    BSD
  12. #include <string.h>
  13. #include <memory.h>
  14. #else
  15. #include <strings.h>
  16. #define    strchr    index
  17. #define    strrchr    rindex
  18. #endif
  19.  
  20. #ifndef    lint
  21. static    char    _sccsid[] = "@(#)shadow.c    2.5    19:24:12    7/29/90";
  22. #endif
  23.  
  24. static    FILE    *shadow;
  25. #define    FIELDS    5
  26.  
  27. void    setspent ()
  28. {
  29.     if (shadow)
  30.         rewind (shadow);
  31.     else
  32.         shadow = fopen (SHADOW, "r");
  33. }
  34.  
  35. void    endspent ()
  36. {
  37.     if (shadow)
  38.         (void) fclose (shadow);
  39.  
  40.     shadow = (FILE *) 0;
  41. }
  42.  
  43. struct    spwd    *fgetspent (fp)
  44. FILE    *fp;
  45. {
  46.     static    struct    spwd    spwd;
  47.     static    char    buf[BUFSIZ];
  48.     char    *fields[FIELDS];
  49.     char    *cp;
  50.     int    i;
  51.     int    atoi ();
  52.     long    atol ();
  53.  
  54.     if (! fp)
  55.         return (0);
  56.  
  57.     if (fgets (buf, BUFSIZ, fp) == (char *) 0)
  58.         return (0);
  59.  
  60.     if (cp = strrchr (buf, '\n'))
  61.         *cp = '\0';
  62.  
  63.     for (cp = buf, i = 0;i < FIELDS;i++) {
  64.         fields[i] = cp;
  65.         while (*cp && *cp != ':')
  66.             cp++;
  67.  
  68.         if (*cp)
  69.             *cp++ = '\0';
  70.     }
  71.     if (*cp || i != FIELDS)
  72.         return 0;
  73.  
  74.     spwd.sp_namp = fields[0];
  75.     spwd.sp_pwdp = fields[1];
  76.  
  77.     if ((spwd.sp_lstchg = strtol (fields[2], (char **) 0, 10)) == -1)
  78.         return 0;
  79.  
  80.     if ((spwd.sp_min = strtol (fields[3], (char **) 0, 10)) == -1)
  81.         return 0;
  82.  
  83.     if ((spwd.sp_max = strtol (fields[4], (char **) 0, 10)) == -1)
  84.         return 0;
  85.  
  86.     return (&spwd);
  87. }
  88.  
  89. struct    spwd    *getspent ()
  90. {
  91.     if (! shadow)
  92.         setspent ();
  93.  
  94.     return (fgetspent (shadow));
  95. }
  96.  
  97. struct    spwd    *getspnam (name)
  98. char    *name;
  99. {
  100.     struct    spwd    *spwd;
  101.  
  102.     setspent ();
  103.  
  104.     while ((spwd = getspent ()) != (struct spwd *) 0) {
  105.         if (strcmp (name, spwd->sp_namp) == 0)
  106.             return (spwd);
  107.     }
  108.     return (0);
  109. }
  110.  
  111. int    putspent (spwd, fp)
  112. struct    spwd    *spwd;
  113. FILE    *fp;
  114. {
  115.     if (! fp)
  116.         return -1;
  117.  
  118.     fprintf (fp, "%s:%s:%ld:%ld:%ld\n",
  119.             spwd->sp_namp, spwd->sp_pwdp,
  120.             spwd->sp_lstchg, spwd->sp_min, spwd->sp_max);
  121.  
  122.     if (ferror (fp))
  123.         return -1;
  124.     else
  125.         return 0;
  126. }
  127.