home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Os2 / varios / PCNFS / PWD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-16  |  3.4 KB  |  191 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <pwd.h>
  6.  
  7. struct passwd *getpwnam(char *name)
  8. {
  9.   static struct passwd pw;
  10.   static char buffer[256];
  11.   char *ptr;
  12.   FILE *passwd;
  13.   int found = 0;
  14.  
  15.   if ((ptr = getenv("ETC")) == NULL)
  16.     return NULL;
  17.  
  18.   strcpy(buffer, ptr);
  19.   strcat(buffer, "/passwd");
  20.  
  21.   if ( (passwd = fopen(buffer, "r")) == NULL )
  22.     return NULL;
  23.  
  24.   while ( fgets(buffer, sizeof(buffer), passwd) != NULL )
  25.   {
  26.     if (buffer[strlen(buffer) - 1] == '\n')
  27.       buffer[strlen(buffer) - 1] = 0;
  28.  
  29.     if ( buffer[0] == '#' )
  30.       continue;
  31.  
  32.     pw.pw_name = buffer;
  33.  
  34.     if ( (ptr = strchr(buffer, ':')) != NULL )
  35.       *ptr++ = 0;
  36.     else
  37.       continue;
  38.  
  39.     if ( strcmp(name, pw.pw_name) == 0 )
  40.     {
  41.       pw.pw_passwd = ptr;
  42.  
  43.       if ( (ptr = strchr(ptr, ':')) != NULL )
  44.     *ptr++ = 0;
  45.       else
  46.     continue;
  47.  
  48.       pw.pw_uid = atoi(ptr);
  49.  
  50.       if ( (ptr = strchr(ptr, ':')) != NULL )
  51.     *ptr++ = 0;
  52.       else
  53.     continue;
  54.  
  55.       pw.pw_gid = atoi(ptr);
  56.  
  57.       if ( (ptr = strchr(ptr, ':')) != NULL )
  58.     *ptr++ = 0;
  59.       else
  60.     continue;
  61.  
  62.       pw.pw_gecos = ptr;
  63.  
  64.       if ( (ptr = strchr(ptr, ':')) != NULL )
  65.         *ptr++ = 0;
  66.       else
  67.         continue;
  68.  
  69.       pw.pw_dir = ptr;
  70.  
  71.       if ( ptr[0] && ptr[1] && (ptr = strchr(ptr + 2, ':')) != NULL )
  72.         *ptr++ = 0;   /* skip drive: */
  73.  
  74.       found = 1;
  75.  
  76.       break;
  77.     }
  78.   }
  79.  
  80.   fclose(passwd);
  81.  
  82.   if ( !found )
  83.     return NULL;
  84.  
  85.   return &pw;
  86. }
  87.  
  88. int setpwnam(char *name, char *crypted)
  89. {
  90.   static struct passwd pw;
  91.   static char old[256], new[256], bak[256], line[256], buffer[256];
  92.   char *ptr;
  93.   FILE *passwd, *newpasswd;
  94.   int found = 0;
  95.  
  96.   if ((ptr = getenv("ETC")) == NULL)
  97.     return NULL;
  98.  
  99.   strcpy(old, ptr);
  100.   strcat(old, "/passwd");
  101.   strcpy(new, old);
  102.   strcat(new, ".new");
  103.   strcpy(bak, old);
  104.   strcat(bak, ".bak");
  105.  
  106.   if ( (passwd = fopen(old, "r")) == NULL )
  107.     return -1;
  108.  
  109.   if ( (newpasswd = fopen(new, "w")) == NULL )
  110.     return -1;
  111.  
  112.   while ( fgets(line, sizeof(line), passwd) != NULL )
  113.   {
  114.     strcpy(buffer, line);
  115.  
  116.     if (buffer[strlen(buffer) - 1] == '\n')
  117.       buffer[strlen(buffer) - 1] = 0;
  118.  
  119.     if ( buffer[0] == '#' )
  120.       goto done;
  121.  
  122.     pw.pw_name = buffer;
  123.  
  124.     if ( (ptr = strchr(buffer, ':')) != NULL )
  125.       *ptr++ = 0;
  126.     else
  127.       goto done;
  128.  
  129.     if (strcmp(name, pw.pw_name) == 0)
  130.     {
  131.       pw.pw_passwd = ptr;
  132.  
  133.       if ( (ptr = strchr(ptr, ':')) != NULL )
  134.     *ptr++ = 0;
  135.       else
  136.     goto done;
  137.  
  138.       pw.pw_uid = atoi(ptr);
  139.  
  140.       if ( (ptr = strchr(ptr, ':')) != NULL )
  141.     *ptr++ = 0;
  142.       else
  143.     goto done;
  144.  
  145.       pw.pw_gid = atoi(ptr);
  146.  
  147.       if ( (ptr = strchr(ptr, ':')) != NULL )
  148.     *ptr++ = 0;
  149.       else
  150.     goto done;
  151.  
  152.       pw.pw_gecos = ptr;
  153.  
  154.       if ( (ptr = strchr(ptr, ':')) != NULL )
  155.         *ptr++ = 0;
  156.       else
  157.     goto done;
  158.  
  159.       pw.pw_dir = ptr;
  160.  
  161.       if ( ptr[0] && ptr[1] && (ptr = strchr(ptr + 2, ':')) != NULL )
  162.         *ptr++ = 0;   /* skip drive: */
  163.  
  164.       found = 1;
  165.  
  166.       sprintf(line, "%s:%s:%d:%d:%s:%s:\n", pw.pw_name, crypted,
  167.           pw.pw_uid, pw.pw_gid, pw.pw_gecos, pw.pw_dir);
  168.     }
  169.  
  170.   done:
  171.     fputs(line, newpasswd);
  172.   }
  173.  
  174.   fclose(passwd);
  175.   fclose(newpasswd);
  176.  
  177.   if ( !found )
  178.   {
  179.     unlink(new);
  180.     return -1;
  181.   }
  182.  
  183.   unlink(bak);
  184.   if (rename(old, bak))
  185.     return -1;
  186.   if (rename(new, old))
  187.     return -1;
  188.  
  189.   return 0;
  190. }
  191.