home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / snews-20.zip / PWD.C < prev    next >
C/C++ Source or Header  |  1992-04-04  |  1KB  |  67 lines

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "os2defs.h"
  5.  
  6. struct passwd *getpwnam(char *name, char *confdir)
  7. {
  8.   static struct passwd pw;
  9.   static char buffer[256];
  10.   char *ptr, *logname, *fullname, *homedir;
  11.   FILE *passwd;
  12.   int i, found = 0;
  13.  
  14.   strcpy(buffer, confdir);
  15.   strcat(buffer, "/passwd");
  16.  
  17.   if ( (passwd = fopen(buffer, "r")) == NULL )
  18.     return NULL;
  19.  
  20.   while ( fgets(buffer, sizeof(buffer), passwd) != NULL )
  21.   {
  22.     buffer[strlen(buffer) - 1] = 0;
  23.  
  24.     if ( buffer[0] == '#' )
  25.       continue;
  26.  
  27.     if ( (ptr = strchr(buffer, ':')) != NULL )
  28.       *ptr++ = 0;
  29.     else
  30.       continue;
  31.  
  32.     if ( strcmp(name, buffer) == 0 )
  33.     {
  34.       logname = buffer;
  35.  
  36.       for ( i = 0; i < 3; i++ )
  37.         if ( (ptr = strchr(ptr, ':')) != NULL )
  38.           *ptr++ = 0;
  39.         else
  40.           continue;
  41.  
  42.       fullname = ptr;
  43.  
  44.       if ( (ptr = strchr(ptr, ':')) != NULL )
  45.         *ptr++ = 0;
  46.       else
  47.         continue;
  48.  
  49.       homedir = ptr;
  50.  
  51.       if ( ptr[0] && ptr[1] && (ptr = strchr(ptr + 2, ':')) != NULL )
  52.         *ptr++ = 0;   /* skip drive: */
  53.  
  54.       pw.pw_name  = logname;
  55.       pw.pw_gecos = fullname;
  56.       pw.pw_dir   = homedir;
  57.       found = 1;
  58.  
  59.       break;
  60.     }
  61.   }
  62.  
  63.   fclose(passwd);
  64.  
  65.   return found ? &pw : NULL;
  66. }
  67.