home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part12 / pwpack.c < prev    next >
C/C++ Source or Header  |  1993-08-14  |  3KB  |  152 lines

  1. /*
  2.  * Copyright 1990, 1991, 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 "config.h"
  13. #include <stdio.h>
  14. #include "pwd.h"
  15. #ifdef    BSD
  16. #include <strings.h>
  17. #define    strchr    index
  18. #define    strrchr    rindex
  19. #else
  20. #include <string.h>
  21. #endif
  22.  
  23. #ifndef    lint
  24. static    char    sccsid[] = "@(#)pwpack.c    3.4    11:50:31    28 Dec 1991";
  25. #endif
  26.  
  27. /*
  28.  * pw_pack - convert a (struct pwd) to a packed record
  29.  */
  30.  
  31. int
  32. pw_pack (passwd, buf)
  33. struct    passwd    *passwd;
  34. char    *buf;
  35. {
  36.     char    *cp;
  37.  
  38.     cp = buf;
  39.     strcpy (cp, passwd->pw_name);
  40.     cp += strlen (cp) + 1;
  41.  
  42.     strcpy (cp, passwd->pw_passwd);
  43. #ifdef    ATT_AGE
  44.     if (passwd->pw_age[0]) {
  45.         *cp++ = ',';
  46.         strcat (cp, passwd->pw_age);
  47.     }
  48. #endif
  49.     cp += strlen (cp) + 1;
  50.  
  51.     memcpy (cp, (void *) &passwd->pw_uid, sizeof passwd->pw_uid);
  52.     cp += sizeof passwd->pw_uid;
  53.  
  54.     memcpy (cp, (void *) &passwd->pw_gid, sizeof passwd->pw_gid);
  55.     cp += sizeof passwd->pw_gid;
  56. #ifdef    BSD_QUOTAS
  57.     memcpy (cp, (void *) &passwd->pw_quota, sizeof passwd->pw_quota);
  58.     cp += sizeof passwd->pw_quota;
  59. #endif
  60. #ifdef    ATT_COMMENT
  61.     if (passwd->pw_comment) {
  62.         strcpy (cp, passwd->pw_comment);
  63.         cp += strlen (cp) + 1;
  64.     } else
  65.         *cp++ = '\0';
  66. #endif
  67.     strcpy (cp, passwd->pw_gecos);
  68.     cp += strlen (cp) + 1;
  69.  
  70.     strcpy (cp, passwd->pw_dir);
  71.     cp += strlen (cp) + 1;
  72.  
  73.     strcpy (cp, passwd->pw_shell);
  74.         cp += strlen (cp) + 1;
  75.  
  76.     return cp - buf;
  77. }
  78.  
  79. /*
  80.  * pw_unpack - convert a packed (struct pwd) record to a (struct pwd)
  81.  */
  82.  
  83. int
  84. pw_unpack (buf, len, passwd)
  85. char    *buf;
  86. int    len;
  87. struct    passwd    *passwd;
  88. {
  89.     char    *org = buf;
  90.     char    *cp;
  91.  
  92.     memset ((void *) passwd, 0, sizeof *passwd);
  93.  
  94.     passwd->pw_name = buf;
  95.     buf += strlen (buf) + 1;
  96.     if (buf - org > len)
  97.         return -1;
  98.  
  99.     passwd->pw_passwd = buf;
  100.     buf += strlen (buf) + 1;
  101.     if (buf - org > len)
  102.         return -1;
  103.  
  104. #ifdef    ATT_AGE
  105.     if (cp = strchr (passwd->pw_passwd, ',')) {
  106.         *cp++ = '\0';
  107.         passwd->pw_age = cp;
  108.     } else
  109.         passwd->pw_age = "";
  110. #endif
  111.  
  112.     memcpy ((void *) &passwd->pw_uid, (void *) buf, sizeof passwd->pw_uid);
  113.     buf += sizeof passwd->pw_uid;
  114.     if (buf - org > len)
  115.         return -1;
  116.  
  117.     memcpy ((void *) &passwd->pw_gid, (void *) buf, sizeof passwd->pw_gid);
  118.     buf += sizeof passwd->pw_gid;
  119.     if (buf - org > len)
  120.         return -1;
  121.  
  122. #ifdef    BSD_QUOTAS
  123.     memcpy ((void *) &passwd->pw_quota, (void *) buf,
  124.         sizeof passwd->pw_quota);
  125.     buf += sizeof passwd->pw_quota;
  126.     if (buf - org > len)
  127.         return -1;
  128. #endif
  129. #ifdef    ATT_COMMENT
  130.     passwd->pw_comment = buf;
  131.     buf += strlen (buf) + 1;
  132.     if (buf - org > len)
  133.         return -1;
  134. #endif
  135.     passwd->pw_gecos = buf;
  136.     buf += strlen (buf) + 1;
  137.     if (buf - org > len)
  138.         return -1;
  139.  
  140.     passwd->pw_dir = buf;
  141.     buf += strlen (buf) + 1;
  142.     if (buf - org > len)
  143.         return -1;
  144.  
  145.     passwd->pw_shell = buf;
  146.     buf += strlen (buf) + 1;
  147.     if (buf - org > len)
  148.         return -1;
  149.  
  150.     return 0;
  151. }
  152.