home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / pwd / shadow.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-19  |  5.0 KB  |  271 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, 1992, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <ansidecl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "shadow.h"
  17.  
  18. #if 0
  19. static char sccsid[] = "@(#)shadow.c    3.10    20:38:10    3/7/92";
  20. #endif
  21.  
  22. static FILE *shadow;
  23. static char *spwbuf = NULL;
  24. static struct spwd spwd;
  25.  
  26. #define FIELDS 9
  27. #define OFIELDS 5
  28.  
  29. #define MAXBUF BUFSIZ
  30.  
  31. static void
  32. DEFUN_VOID(initmem)
  33. {
  34.   if (spwbuf)
  35.     return;
  36.  
  37.   spwbuf = (char *) malloc (MAXBUF + 1);
  38. }
  39.  
  40. void
  41. DEFUN_VOID(setspent)
  42. {
  43. #undef TRUE
  44. #undef FALSE
  45. #define TRUE    1
  46. #define FALSE    0
  47. static char no_etc_shadow = FALSE;
  48.  
  49.   if (shadow)
  50.     rewind (shadow);
  51.   else
  52.     if (!no_etc_shadow)
  53.        no_etc_shadow = (shadow = fopen (SHADOW, "r")) ? FALSE : TRUE;
  54. }
  55.  
  56. void
  57. DEFUN_VOID(endspent)
  58. {
  59.   if (shadow)
  60.     {
  61.       fclose (shadow);
  62.       shadow = NULL;
  63.     }
  64. }
  65.  
  66. struct spwd *
  67. DEFUN(sgetspent, (string), CONST char *string)
  68. {
  69.   char *fields[FIELDS];
  70.   char *cp;
  71.   char *cpp;
  72.   int i;
  73.  
  74.   initmem();
  75.  
  76.   strncpy (spwbuf, string, MAXBUF);
  77.   spwbuf[MAXBUF] = '\0';
  78.  
  79.   if ((cp = strrchr (spwbuf, '\n')))
  80.     *cp = '\0';
  81.  
  82.   for (cp = spwbuf, i = 0; *cp && i < FIELDS; i++)
  83.     {
  84.       fields[i] = cp;
  85.       while (*cp && *cp != ':')
  86.     cp++;
  87.  
  88.       if (*cp)
  89.     *cp++ = '\0';
  90.     }
  91.   if (i == FIELDS - 1)
  92.     fields[i++] = cp;
  93.  
  94.   if (*cp || (i != FIELDS && i != OFIELDS))
  95.     return 0;
  96.  
  97.   spwd.sp_namp = fields[0];
  98.   spwd.sp_pwdp = fields[1];
  99.  
  100.   if ((spwd.sp_lstchg = strtol (fields[2], &cpp, 10)) == 0 && *cpp)
  101.     return 0;
  102.   else if (fields[2][0] == '\0')
  103.     spwd.sp_lstchg = -1;
  104.  
  105.   if ((spwd.sp_min = strtol (fields[3], &cpp, 10)) == 0 && *cpp)
  106.     return 0;
  107.   else if (fields[3][0] == '\0')
  108.     spwd.sp_min = -1;
  109.  
  110.   if ((spwd.sp_max = strtol (fields[4], &cpp, 10)) == 0 && *cpp)
  111.     return 0;
  112.   else if (fields[4][0] == '\0')
  113.     spwd.sp_max = -1;
  114.  
  115.   if (i == OFIELDS)
  116.     {
  117.       spwd.sp_warn = spwd.sp_inact = spwd.sp_expire =
  118.     spwd.sp_flag = -1;
  119.  
  120.       return &spwd;
  121.     }
  122.   if ((spwd.sp_warn = strtol (fields[5], &cpp, 10)) == 0 && *cpp)
  123.     return NULL;
  124.   else if (fields[5][0] == '\0')
  125.     spwd.sp_warn = -1;
  126.  
  127.   if ((spwd.sp_inact = strtol (fields[6], &cpp, 10)) == 0 && *cpp)
  128.     return NULL;
  129.   else if (fields[6][0] == '\0')
  130.     spwd.sp_inact = -1;
  131.  
  132.   if ((spwd.sp_expire = strtol (fields[7], &cpp, 10)) == 0 && *cpp)
  133.     return NULL;
  134.   else if (fields[7][0] == '\0')
  135.     spwd.sp_expire = -1;
  136.  
  137.   if ((spwd.sp_flag = strtol (fields[8], &cpp, 10)) == 0 && *cpp)
  138.     return NULL;
  139.   else if (fields[8][0] == '\0')
  140.     spwd.sp_flag = -1;
  141.  
  142.   return &spwd;
  143. }
  144.  
  145. struct spwd *
  146. DEFUN(fgetspent, (fp), FILE *fp)
  147. {
  148.   char buf[BUFSIZ];
  149.  
  150.   if (!fp)
  151.     return NULL;
  152.  
  153.   if (fgets (buf, BUFSIZ, fp) == NULL)
  154.     return NULL;
  155.  
  156.   return sgetspent (buf);
  157. }
  158.  
  159. struct spwd *
  160. DEFUN_VOID(getspent)
  161. {
  162.   if (!shadow)
  163.     setspent ();
  164.  
  165.   return fgetspent (shadow);
  166. }
  167.  
  168. struct spwd *
  169. DEFUN(getspnam, (name), CONST char *name)
  170. {
  171.   struct spwd *sp;
  172.  
  173.   setspent ();
  174.   while ((sp = getspent ()) != NULL)
  175.     {
  176.       if (strcmp (name, sp->sp_namp) == 0)
  177.     return sp;
  178.     }
  179.   return NULL;
  180. }
  181.  
  182. int
  183. DEFUN(putspent, (sp, fp), CONST struct spwd *sp AND FILE *fp)
  184. {
  185.   int errors = 0;
  186.  
  187.   if (!fp || !sp)
  188.     return -1;
  189.  
  190.   if (fprintf (fp, "%s:%s:", sp->sp_namp, sp->sp_pwdp) < 0)
  191.     errors++;
  192.  
  193.   if (sp->sp_lstchg != -1)
  194.     {
  195.       if (fprintf (fp, "%ld:", sp->sp_lstchg) < 0)
  196.     errors++;
  197.     }
  198.   else if (putc (':', fp) == EOF)
  199.     errors++;
  200.  
  201.   if (sp->sp_min != -1)
  202.     {
  203.       if (fprintf (fp, "%ld:", sp->sp_min) < 0)
  204.     errors++;
  205.     }
  206.   else if (putc (':', fp) == EOF)
  207.     errors++;
  208.  
  209.   if (sp->sp_max != -1)
  210.     {
  211.       if (fprintf (fp, "%ld", sp->sp_max) < 0)
  212.     errors++;
  213.     }
  214.  
  215.   /*
  216.    * See if the structure has any of the SVR4 fields in
  217.    * it.  If none of those fields have any data there is
  218.    * no reason to write them out since they will be filled
  219.    * in the same way when they are read back in.  Otherwise
  220.    * there is at least one SVR4 field that must be output.
  221.    */
  222.  
  223.   if (sp->sp_warn == -1 && sp->sp_inact == -1
  224.       && sp->sp_expire == -1 && sp->sp_flag == -1)
  225.     {
  226.       if (putc ('\n', fp) == EOF || errors)
  227.     return -1;
  228.       else
  229.     return 0;
  230.     }
  231.   else if (putc (':', fp) == EOF)
  232.     errors++;
  233.  
  234.   if (sp->sp_warn != -1)
  235.     {
  236.       if (fprintf (fp, "%ld:", sp->sp_warn) < 0)
  237.     errors++;
  238.     }
  239.   else if (putc (':', fp) == EOF)
  240.     errors++;
  241.  
  242.   if (sp->sp_inact != -1)
  243.     {
  244.       if (fprintf (fp, "%ld:", sp->sp_inact) < 0)
  245.     errors++;
  246.     }
  247.   else if (putc (':', fp) == EOF)
  248.     errors++;
  249.  
  250.   if (sp->sp_expire != -1)
  251.     {
  252.       if (fprintf (fp, "%ld:", sp->sp_expire) < 0)
  253.     errors++;
  254.     }
  255.   else if (putc (':', fp) == EOF)
  256.     errors++;
  257.  
  258.   if (sp->sp_flag != -1)
  259.     {
  260.       if (fprintf (fp, "%ld", sp->sp_flag) < 0)
  261.     errors++;
  262.     }
  263.   if (putc ('\n', fp) == EOF)
  264.     errors++;
  265.  
  266.   if (errors)
  267.     return -1;
  268.   else
  269.     return 0;
  270. }
  271.