home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3348 / pwdbm.c < prev    next >
C/C++ Source or Header  |  1991-05-16  |  2KB  |  97 lines

  1. /*
  2.  * Copyright 1990, 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. #ifndef    lint
  13. static    char    sccsid[] = "@(#)pwdbm.c    3.3    12:31:13    12/12/90";
  14. #endif
  15.  
  16. #ifdef    BSD
  17. #include <strings.h>
  18. #define    strchr    index
  19. #define    strrchr    rindex
  20. #else
  21. #include <string.h>
  22. #endif
  23. #include <stdio.h>
  24. #include "pwd.h"
  25. #include "config.h"
  26.  
  27. #ifdef    DBM
  28. #include <dbm.h>
  29. #endif
  30. #ifdef    NDBM
  31. #include <ndbm.h>
  32. DBM    *pw_dbm;
  33. #endif
  34.  
  35. /*
  36.  * pw_dbm_update
  37.  *
  38.  * Updates the DBM password files, if they exist.
  39.  */
  40.  
  41. int
  42. pw_dbm_update (pw)
  43. struct    passwd    *pw;
  44. {
  45.     datum    key;
  46.     datum    content;
  47.     char    data[BUFSIZ];
  48.     int    len;
  49.     static    int    once;
  50.  
  51.     if (! once) {
  52. #ifdef    NDBM
  53.         if (! pw_dbm)
  54.             setpwent ();
  55. #else
  56.         setpwent ();
  57. #endif
  58.         once++;
  59.     }
  60. #ifdef    DBM
  61.     strcpy (data, PWDFILE);
  62.     strcat (data, ".pag");
  63.     if (access (data, 0))
  64.         return 0;
  65. #endif
  66. #ifdef    NDBM
  67.     if (! pw_dbm)
  68.         return 0;
  69. #endif
  70.     len = pw_pack (pw, data);
  71.     content.dsize = len;
  72.     content.dptr = data;
  73.  
  74.     key.dsize = strlen (pw->pw_name);
  75.     key.dptr = pw->pw_name;
  76. #ifdef    DBM
  77.     if (store (key, content))
  78.         return 0;
  79. #endif
  80. #ifdef    NDBM
  81.     if (dbm_store (pw_dbm, key, content, DBM_REPLACE))
  82.         return 0;
  83. #endif
  84.  
  85.     key.dsize = sizeof pw->pw_uid;
  86.     key.dptr = (char *) &pw->pw_uid;
  87. #ifdef    DBM
  88.     if (store (key, content))
  89.         return 0;
  90. #endif
  91. #ifdef    NDBM
  92.     if (dbm_store (pw_dbm, key, content, DBM_REPLACE))
  93.         return 0;
  94. #endif
  95.     return 1;
  96. }
  97.