home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / lib / getpwnam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  2.0 KB  |  126 lines

  1. /*
  2.  *  GETPWNAM.C
  3.  *
  4.  *  (C) Copyright 1989-1990 by Matthew Dillon,    All Rights Reserved.
  5.  *
  6.  *  (UUCP source support)
  7.  */
  8.  
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <pwd.h>
  13. #include "config.h"
  14.  
  15. Prototype struct passwd *getpwnam (const char *);
  16.  
  17. static char *Breakout (char **);
  18.  
  19. char
  20.     *PasswdFile = NULL;
  21.  
  22. struct passwd *
  23. getpwnam (const char *name)
  24. {
  25.     FILE
  26.         *fi;
  27.     char
  28.         *buf = malloc (256);
  29.     static char
  30.         User [32],
  31.         Passwd [32],
  32.         RealName [64],
  33.         Dir [128],
  34.         Shell [256];
  35.     static struct passwd
  36.         Pas = { User, Passwd, 0, 0, 0, "", "", Dir, Shell, NULL, NULL, RealName};
  37.  
  38.     if (!buf)
  39.         return NULL;
  40.  
  41.     if (PasswdFile)
  42.         fi = fopen (PasswdFile, "r");
  43.     else
  44.         fi = fopen (MakeConfigPath (UULIB, "Passwd"), "r");
  45.  
  46.     if (fi == NULL) {
  47.         free (buf);
  48.         return NULL;
  49.     }
  50.  
  51.     while (fgets (buf, 256, fi)) {
  52.         int
  53.             i;
  54.         char
  55.             *str,
  56.             *ptr = buf,
  57.             *arg;
  58.  
  59.         arg = Breakout (&ptr);
  60.         if (strcmp (name, arg) != 0)
  61.             continue;
  62.         strcpy (Pas.pw_name, arg);
  63.         strcpy (Pas.pw_passwd, Breakout (&ptr));
  64.         Pas.pw_uid = atoi (Breakout (&ptr));
  65.         Pas.pw_gid = atoi (Breakout (&ptr));
  66.         strcpy (Pas.pw_real_name, Breakout (&ptr)); /* finger info */
  67.         strcpy (Pas.pw_dir,      Breakout (&ptr));
  68.         strcpy (Pas.pw_shell,      Breakout (&ptr));
  69.  
  70.         i = strlen (Pas.pw_dir) - 1;
  71.         if (i >= 0 && Pas.pw_dir [i] != ':' && Pas.pw_dir [i] != '/') {
  72.             Pas.pw_dir [++i] = '/';
  73.             Pas.pw_dir [++i] = 0;
  74.         }
  75.  
  76.         Pas.pw_shell_arg0 = Pas.pw_shell;
  77.         for (str = Pas.pw_shell; *str && *str != ' ' && *str != '\t'; ++str)
  78.             ;
  79.         if (*str) {
  80.             *str = 0;
  81.             ++str;
  82.             while (*str == ' ' || *str == '\t')
  83.                 ++str;
  84.             Pas.pw_shell_argn = str;
  85.         }
  86.         else {
  87.             Pas.pw_shell_argn = str;
  88.         }
  89.  
  90.  
  91.         fclose (fi);
  92.         free (buf);
  93.         return &Pas;
  94.     }
  95.  
  96.     fclose (fi);
  97.     free (buf);
  98.     return NULL;
  99. }
  100.  
  101. static char *
  102. Breakout (char **pptr)
  103. {
  104.     char
  105.         *base,
  106.         *ptr;
  107.  
  108.     base = *pptr;
  109.     if (base == NULL)
  110.         return "";
  111.  
  112.     for (ptr = base; *ptr && *ptr != '\n' && *ptr != ','; ++ptr)
  113.         ;
  114.  
  115.     if (*ptr == ',') {
  116.         *pptr = ptr + 1;
  117.         *ptr = 0;
  118.     }
  119.     else {
  120.         *pptr = NULL;
  121.         *ptr = 0;
  122.     }
  123.  
  124.     return base;
  125. }
  126.