home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1704 / pwpack.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  2KB  |  112 lines

  1. /*
  2.  * Copyright 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.  * Duplication is permitted for non-commercial [ profit making ]
  9.  * purposes provided this and other copyright notices remain
  10.  * intact.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <pwd.h>
  15. #ifdef    BSD
  16. #include <strings.h>
  17. #else
  18. #include <string.h>
  19. #endif
  20.  
  21. #ifndef    lint
  22. static    char    sccsid[] = "@(#)pwpack.c    2.3    23:06:29    8/5/90";
  23. #endif
  24.  
  25. int    pw_pack (passwd, buf)
  26. struct    passwd    *passwd;
  27. char    *buf;
  28. {
  29.     char    *cp;
  30.  
  31.     cp = buf;
  32.     strcpy (cp, passwd->pw_name);
  33.     cp += strlen (cp) + 1;
  34.  
  35.     strcpy (cp, passwd->pw_passwd);
  36.     if (passwd->pw_age && passwd->pw_age[0]) {
  37.         cp += strlen (cp);
  38.         *cp++ = ',';
  39.         strcpy (cp, passwd->pw_age);
  40.     }
  41.     cp += strlen (cp) + 1;
  42.  
  43.     memcpy (cp, (void *) &passwd->pw_uid, sizeof passwd->pw_uid);
  44.     cp += sizeof passwd->pw_uid;
  45.  
  46.     memcpy (cp, (void *) &passwd->pw_gid, sizeof passwd->pw_gid);
  47.     cp += sizeof passwd->pw_gid;
  48.  
  49.     strcpy (cp, passwd->pw_gecos);
  50.     cp += strlen (cp) + 1;
  51.  
  52.     strcpy (cp, passwd->pw_dir);
  53.     cp += strlen (cp) + 1;
  54.  
  55.     strcpy (cp, passwd->pw_shell);
  56.         cp += strlen (cp) + 1;
  57.  
  58.     return cp - buf;
  59. }
  60.  
  61. int    pw_unpack (buf, len, passwd)
  62. char    *buf;
  63. int    len;
  64. struct    passwd    *passwd;
  65. {
  66.     char    *org = buf;
  67.     char    *cp;
  68.  
  69.     passwd->pw_name = buf;
  70.     buf += strlen (buf) + 1;
  71.     if (buf - org > len)
  72.         return -1;
  73.  
  74.     passwd->pw_passwd = buf;
  75.     buf += strlen (buf) + 1;
  76.     if (buf - org > len)
  77.         return -1;
  78.  
  79.     if (cp = strchr (passwd->pw_passwd, ',')) {
  80.         *cp++ = '\0';
  81.         passwd->pw_age = cp;
  82.     } else
  83.         passwd->pw_age = "";
  84.  
  85.     memcpy ((void *) &passwd->pw_uid, (void *) buf, sizeof passwd->pw_uid);
  86.     buf += sizeof passwd->pw_uid;
  87.     if (buf - org > len)
  88.         return -1;
  89.  
  90.     memcpy ((void *) &passwd->pw_gid, (void *) buf, sizeof passwd->pw_gid);
  91.     buf += sizeof passwd->pw_gid;
  92.     if (buf - org > len)
  93.         return -1;
  94.  
  95.     passwd->pw_gecos = buf;
  96.     buf += strlen (buf) + 1;
  97.     if (buf - org > len)
  98.         return -1;
  99.  
  100.     passwd->pw_dir = buf;
  101.     buf += strlen (buf) + 1;
  102.     if (buf - org > len)
  103.         return -1;
  104.  
  105.     passwd->pw_shell = buf;
  106.     buf += strlen (buf) + 1;
  107.     if (buf - org > len)
  108.         return -1;
  109.  
  110.     return 0;
  111. }
  112.