home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2287 / shadow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.1 KB  |  140 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.6.1.1    08:13:12    11/9/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 *
  44. sgetspent (string)
  45. char    *string;
  46. {
  47.     static    char    buf[BUFSIZ];
  48.     static    struct    spwd    spwd;
  49.     char    *fields[FIELDS];
  50.     char    *cp;
  51.     char    *cpp;
  52.     int    atoi ();
  53.     long    atol ();
  54.     int    i;
  55.  
  56.     strncpy (buf, string, BUFSIZ-1);
  57.     buf[BUFSIZ-1] = '\0';
  58.  
  59.     if (cp = strrchr (buf, '\n'))
  60.         *cp = '\0';
  61.  
  62.     for (cp = buf, i = 0;*cp && i < FIELDS;i++) {
  63.         fields[i] = cp;
  64.         while (*cp && *cp != ':')
  65.             cp++;
  66.  
  67.         if (*cp)
  68.             *cp++ = '\0';
  69.     }
  70.     if (*cp || i != FIELDS)
  71.         return 0;
  72.  
  73.     spwd.sp_namp = fields[0];
  74.     spwd.sp_pwdp = fields[1];
  75.  
  76.     if ((spwd.sp_lstchg = strtol (fields[2], &cpp, 10)) == 0 && *cpp)
  77.         return 0;
  78.  
  79.     if ((spwd.sp_min = strtol (fields[3], &cpp, 10)) == 0 && *cpp)
  80.         return 0;
  81.  
  82.     if ((spwd.sp_max = strtol (fields[4], &cpp, 10)) == 0 && *cpp)
  83.         return 0;
  84.  
  85.     return (&spwd);
  86. }
  87.  
  88. struct    spwd    *fgetspent (fp)
  89. FILE    *fp;
  90. {
  91.     char    buf[BUFSIZ];
  92.  
  93.     if (! fp)
  94.         return (0);
  95.  
  96.     if (fgets (buf, BUFSIZ, fp) == (char *) 0)
  97.         return (0);
  98.  
  99.     return sgetspent (buf);
  100. }
  101.  
  102. struct    spwd    *getspent ()
  103. {
  104.     if (! shadow)
  105.         setspent ();
  106.  
  107.     return (fgetspent (shadow));
  108. }
  109.  
  110. struct    spwd    *getspnam (name)
  111. char    *name;
  112. {
  113.     struct    spwd    *spwd;
  114.  
  115.     setspent ();
  116.  
  117.     while ((spwd = getspent ()) != (struct spwd *) 0) {
  118.         if (strcmp (name, spwd->sp_namp) == 0)
  119.             return (spwd);
  120.     }
  121.     return (0);
  122. }
  123.  
  124. int    putspent (spwd, fp)
  125. struct    spwd    *spwd;
  126. FILE    *fp;
  127. {
  128.     if (! fp)
  129.         return -1;
  130.  
  131.     fprintf (fp, "%s:%s:%ld:%ld:%ld\n",
  132.             spwd->sp_namp, spwd->sp_pwdp,
  133.             spwd->sp_lstchg, spwd->sp_min, spwd->sp_max);
  134.  
  135.     if (ferror (fp))
  136.         return -1;
  137.     else
  138.         return 0;
  139. }
  140.