home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2288 / entry.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  2KB  |  116 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 <stdio.h>
  10. #include <pwd.h>
  11. #ifndef    BSD
  12. #include <string.h>
  13. #else
  14. #include <strings.h>
  15. #define    strchr    index
  16. #define    strrchr    rindex
  17. #endif
  18. #include "config.h"
  19. #ifdef    SHADOWPWD
  20. #include "shadow.h"
  21. #endif
  22.  
  23. #ifndef    lint
  24. static    char    _sccsid[] = "@(#)entry.c    2.3    07:47:34    8/14/90";
  25. #endif
  26.  
  27. struct    passwd    *fgetpwent ();
  28. char    *malloc ();
  29.  
  30. char    *strdup (s)
  31. char    *s;
  32. {
  33.     char    *cp;
  34.  
  35.     if (s == (char *) 0)
  36.         return ((char *) 0);
  37.  
  38.     if (! (cp = malloc ((unsigned) strlen (s) + 1)))
  39.         return ((char *) 0);
  40.  
  41.     return (strcpy (cp, s));
  42. }
  43.  
  44. void    entry (name, pwent)
  45. char    *name;
  46. struct    passwd    *pwent;
  47. {
  48.     FILE    *pwd;
  49.     struct    passwd    *passwd;
  50. #ifdef    SHADOWPWD
  51.     struct    spwd    *spwd;
  52.     char    *l64a ();
  53. #endif
  54.     char    *cp;
  55.  
  56.     if ((pwd = fopen (PWDFILE, "r")) == (FILE *) 0) {
  57.         pwent->pw_passwd = (char *) 0;
  58.         return;
  59.     }
  60.     while (passwd = fgetpwent (pwd)) {
  61.         if (strcmp (name, passwd->pw_name) == 0)
  62.             break;
  63.     }
  64.     fclose (pwd);
  65.  
  66.     if (passwd == (struct passwd *) 0) {
  67.         pwent->pw_name = (char *) 0;
  68.         pwent->pw_passwd = (char *) 0;
  69.     } else  {
  70.         pwent->pw_name = strdup (passwd->pw_name);
  71.         pwent->pw_uid = passwd->pw_uid;
  72.         pwent->pw_gid = passwd->pw_gid;
  73.         pwent->pw_comment = (char *) 0;
  74.         pwent->pw_gecos = strdup (passwd->pw_gecos);
  75.         pwent->pw_dir = strdup (passwd->pw_dir);
  76.         pwent->pw_shell = strdup (passwd->pw_shell);
  77. #ifdef    SHADOWPWD
  78.         setspent ();
  79.         if (spwd = getspnam (name)) {
  80.             pwent->pw_passwd = strdup (spwd->sp_pwdp);
  81.  
  82.             pwent->pw_age = malloc (5);
  83.  
  84.             if (spwd->sp_max > (63*7))
  85.                 spwd->sp_max = (63*7);
  86.             if (spwd->sp_min > (63*7))
  87.                 spwd->sp_min = (63*7);
  88.  
  89.             pwent->pw_age[0] = i64c (spwd->sp_max / 7);
  90.             pwent->pw_age[1] = i64c (spwd->sp_min / 7);
  91.  
  92.             cp = l64a (spwd->sp_lstchg / 7);
  93.             pwent->pw_age[2] = cp[0];
  94.             pwent->pw_age[3] = cp[1];
  95.  
  96.             pwent->pw_age[4] = '\0';
  97.  
  98.             endspent ();
  99.             return;
  100.         }
  101.         endspent ();
  102.         passwd->pw_age = pwent->pw_age = (char *) 0;
  103. #endif
  104.         if (passwd->pw_passwd)
  105.             pwent->pw_passwd = strdup (passwd->pw_passwd);
  106.         else
  107.             pwent->pw_passwd = (char *) 0;
  108.  
  109.         if (passwd->pw_age) {
  110.             pwent->pw_age = malloc (5);    /* longest legal time */
  111.             (void) strncpy (pwent->pw_age, passwd->pw_age, 5);
  112.         } else
  113.             pwent->pw_age = (char *) 0;
  114.     }
  115. }
  116.