home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2083 / shadow.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  4.3 KB  |  246 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    3.3    22:02:10    11/16/90";
  22. #endif
  23.  
  24. static    FILE    *shadow;
  25. #define    FIELDS    9
  26. #define    OFIELDS    5
  27.  
  28. void
  29. setspent ()
  30. {
  31.     if (shadow)
  32.         rewind (shadow);
  33.     else
  34.         shadow = fopen (SHADOW, "r");
  35. }
  36.  
  37. void
  38. endspent ()
  39. {
  40.     if (shadow)
  41.         (void) fclose (shadow);
  42.  
  43.     shadow = (FILE *) 0;
  44. }
  45.  
  46. struct spwd *
  47. sgetspent (string)
  48. char    *string;
  49. {
  50.     static    char    buf[BUFSIZ];
  51.     static    struct    spwd    spwd;
  52.     char    *fields[FIELDS];
  53.     char    *cp;
  54.     char    *cpp;
  55.     int    atoi ();
  56.     long    atol ();
  57.     int    i;
  58.  
  59.     strncpy (buf, string, BUFSIZ-1);
  60.     buf[BUFSIZ-1] = '\0';
  61.  
  62.     if (cp = strrchr (buf, '\n'))
  63.         *cp = '\0';
  64.  
  65.     for (cp = buf, i = 0;*cp && i < FIELDS;i++) {
  66.         fields[i] = cp;
  67.         while (*cp && *cp != ':')
  68.             cp++;
  69.  
  70.         if (*cp)
  71.             *cp++ = '\0';
  72.     }
  73.     if (*cp || (i != FIELDS && i != OFIELDS))
  74.         return 0;
  75.  
  76.     spwd.sp_namp = fields[0];
  77.     spwd.sp_pwdp = fields[1];
  78.  
  79.     if ((spwd.sp_lstchg = strtol (fields[2], &cpp, 10)) == 0 && *cpp)
  80.         if (fields[2][0] == '\0')
  81.             spwd.sp_lstchg = -1;
  82.         else
  83.             return 0;
  84.  
  85.     if ((spwd.sp_min = strtol (fields[3], &cpp, 10)) == 0 && *cpp)
  86.         if (fields[3][0] == '\0')
  87.             spwd.sp_min = -1;
  88.         else
  89.             return 0;
  90.  
  91.     if ((spwd.sp_max = strtol (fields[4], &cpp, 10)) == 0 && *cpp)
  92.         if (fields[4][0] == '\0')
  93.             spwd.sp_max = -1;
  94.         else
  95.             return 0;
  96.  
  97.     if (i == OFIELDS) {
  98.         spwd.sp_warn = spwd.sp_inact = spwd.sp_expire =
  99.             spwd.sp_flag = -1;
  100.  
  101.         return &spwd;
  102.     }
  103.     if ((spwd.sp_warn = strtol (fields[5], &cpp, 10)) == 0 && *cpp)
  104.         if (fields[5][0] == '\0')
  105.             spwd.sp_warn = -1;
  106.         else
  107.             return 0;
  108.  
  109.     if ((spwd.sp_inact = strtol (fields[6], &cpp, 10)) == 0 && *cpp)
  110.         if (fields[6][0] == '\0')
  111.             spwd.sp_inact = -1;
  112.         else
  113.             return 0;
  114.  
  115.     if ((spwd.sp_expire = strtol (fields[7], &cpp, 10)) == 0 && *cpp)
  116.         if (fields[7][0] == '\0')
  117.             spwd.sp_expire = -1;
  118.         else
  119.             return 0;
  120.  
  121.     if ((spwd.sp_flag = strtol (fields[8], &cpp, 10)) == 0 && *cpp)
  122.         if (fields[8][0] == '\0')
  123.             spwd.sp_flag = -1;
  124.         else
  125.             return 0;
  126.  
  127.     return (&spwd);
  128. }
  129.  
  130. struct spwd
  131. *fgetspent (fp)
  132. FILE    *fp;
  133. {
  134.     char    buf[BUFSIZ];
  135.  
  136.     if (! fp)
  137.         return (0);
  138.  
  139.     if (fgets (buf, BUFSIZ, fp) == (char *) 0)
  140.         return (0);
  141.  
  142.     return sgetspent (buf);
  143. }
  144.  
  145. struct spwd
  146. *getspent ()
  147. {
  148.     if (! shadow)
  149.         setspent ();
  150.  
  151.     return (fgetspent (shadow));
  152. }
  153.  
  154. struct spwd
  155. *getspnam (name)
  156. char    *name;
  157. {
  158.     struct    spwd    *spwd;
  159.  
  160.     setspent ();
  161.  
  162.     while ((spwd = getspent ()) != (struct spwd *) 0) {
  163.         if (strcmp (name, spwd->sp_namp) == 0)
  164.             return (spwd);
  165.     }
  166.     return (0);
  167. }
  168.  
  169. int
  170. putspent (spwd, fp)
  171. struct    spwd    *spwd;
  172. FILE    *fp;
  173. {
  174.     int    errors = 0;
  175.  
  176.     if (! fp || ! spwd)
  177.         return -1;
  178.  
  179.     if (fprintf (fp, "%s:%s:", spwd->sp_namp, spwd->sp_pwdp) < 0)
  180.         errors++;
  181.  
  182.     if (spwd->sp_lstchg != -1) {
  183.         if (fprintf (fp, "%ld:", spwd->sp_lstchg) < 0)
  184.             errors++;
  185.     } else if (putc (':', fp) == EOF)
  186.         errors++;
  187.  
  188.     if (spwd->sp_min != -1) {
  189.         if (fprintf (fp, "%ld:", spwd->sp_min) < 0)
  190.             errors++;
  191.     } else if (putc (':', fp) == EOF)
  192.         errors++;
  193.  
  194.     if (spwd->sp_max != -1) {
  195.         if (fprintf (fp, "%ld", spwd->sp_max) < 0)
  196.             errors++;
  197.     }
  198.  
  199.     /*
  200.      * See if the structure has any of the SVR4 fields in
  201.      * it.  If none of those fields have any data there is
  202.      * no reason to write them out since they will be filled
  203.      * in the same way when they are read back in.  Otherwise
  204.      * there is at least one SVR4 field that must be output.
  205.      */
  206.  
  207.     if (spwd->sp_warn == -1 && spwd->sp_inact == -1 &&
  208.             spwd->sp_expire == -1 && spwd->sp_flag == -1) {
  209.         if (putc ('\n', fp) == EOF || errors)
  210.             return -1;
  211.         else
  212.             return 0;
  213.     } else if (putc (':', fp) == EOF)
  214.         errors++;
  215.  
  216.     if (spwd->sp_warn != -1) {
  217.         if (fprintf (fp, "%ld:", spwd->sp_warn) < 0)
  218.             errors++;
  219.     } else if (putc (':', fp) == EOF)
  220.         errors++;
  221.  
  222.     if (spwd->sp_inact != -1) {
  223.         if (fprintf (fp, "%ld:", spwd->sp_inact) < 0)
  224.             errors++;
  225.     } else if (putc (':', fp) == EOF)
  226.         errors++;
  227.  
  228.     if (spwd->sp_expire != -1) {
  229.         if (fprintf (fp, "%ld:", spwd->sp_expire) < 0)
  230.             errors++;
  231.     } else if (putc (':', fp) == EOF)
  232.         errors++;
  233.  
  234.     if (spwd->sp_flag != -1) {
  235.         if (fprintf (fp, "%ld", spwd->sp_flag) < 0)
  236.             errors++;
  237.     }
  238.     if (putc ('\n', fp) == EOF)
  239.         errors++;
  240.  
  241.     if (errors)
  242.         return -1;
  243.     else
  244.         return 0;
  245. }
  246.