home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3349 / gsdbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  1.8 KB  |  93 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[] = "@(#)gsdbm.c    3.3    11:25:29    12/19/90";
  14. #endif
  15.  
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include "shadow.h"
  19. #include "config.h"
  20.  
  21. #ifdef    NDBM
  22. #include <ndbm.h>
  23. DBM    *sgr_dbm;
  24.  
  25. #define    GRP_FRAG    256
  26.  
  27. /*
  28.  * sgr_dbm_update
  29.  *
  30.  * Updates the DBM password files, if they exist.
  31.  */
  32.  
  33. int
  34. sgr_dbm_update (sgr)
  35. struct    sgrp    *sgr;
  36. {
  37.     datum    key;
  38.     datum    content;
  39.     char    data[BUFSIZ*8];
  40.     char    sgrpkey[60];
  41.     char    *cp;
  42.     int    len;
  43.     int    i;
  44.     int    cnt;
  45.     static    int    once;
  46.  
  47.     if (! once) {
  48.         if (! sgr_dbm)
  49.             setsgent ();
  50.  
  51.         once++;
  52.     }
  53.     if (! sgr_dbm)
  54.         return 0;
  55.  
  56.     len = sgr_pack (sgr, data);
  57.  
  58.     if (len <= GRP_FRAG) {
  59.         content.dsize = len;
  60.         content.dptr = data;
  61.  
  62.         key.dsize = strlen (sgr->sg_name);
  63.         key.dptr = sgr->sg_name;
  64.         if (dbm_store (sgr_dbm, key, content, DBM_REPLACE))
  65.             return 0;
  66.     } else {
  67.         content.dsize = sizeof cnt;
  68.         content.dptr = (char *) &cnt;
  69.         cnt = (len + (GRP_FRAG-1)) / GRP_FRAG;
  70.  
  71.         key.dsize = strlen (sgr->sg_name);
  72.         key.dptr = sgr->sg_name;
  73.         if (dbm_store (sgr_dbm, key, content, DBM_REPLACE))
  74.             return 0;
  75.  
  76.         for (cp = data, i = 0;i < cnt;i++) {
  77.             content.dsize = len > GRP_FRAG ? GRP_FRAG:len;
  78.             len -= content.dsize;
  79.             content.dptr = cp;
  80.             cp += content.dsize;
  81.  
  82.             key.dsize = sizeof i + strlen (sgr->sg_name);
  83.             key.dptr = sgrpkey;
  84.             memcpy (sgrpkey, &i, sizeof i);
  85.             strcpy (sgrpkey + sizeof i, sgr->sg_name);
  86.             if (dbm_store (sgr_dbm, key, content, DBM_REPLACE))
  87.                 return 0;
  88.         }
  89.     }
  90.     return 1;
  91. }
  92. #endif
  93.