home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume1 / bourne / part1 / homedir.c < prev    next >
C/C++ Source or Header  |  1986-11-30  |  3KB  |  155 lines

  1. /*
  2.  * homedir.c
  3.  *
  4.  * find a person's login directory, for use by the shell
  5.  * also find the current user's login name.
  6.  *
  7.  * Arnold Robbins
  8.  */
  9.  
  10. #include "defs.h"
  11.  
  12. /* validtilde --- indicate whether or not a ~ is valid */
  13.  
  14. int validtilde (start, argp)
  15. register char *start, *argp;
  16. {
  17.     return (
  18.     start == argp - 1 ||            /* ~ at beginning of argument */
  19.     argp[-2] == '=' ||            /* ~ after an assignment */
  20.     (*start == '-' && argp - 3 == start)    /* in middle of an option */
  21.                         /* CSH does not do that one */
  22.     );
  23. }
  24.  
  25. /* homedir --- return the person's login directory */
  26.  
  27. char *homedir (person)
  28. register char *person;
  29. {
  30.     register int count, i, j, fd;
  31.     static char dir[150];
  32.     char buf[300], name[100], rest[100];
  33.  
  34.     if (person[0] == '\0')    /* just a plain ~ */
  35.         return (homenod.namval);
  36.     else if (person[0] == '/')    /* e.g. ~/bin */
  37.     {
  38.         /* sprintf (dir, "%s%s", homenod.namval, person); */
  39.         movstr (movstr (homenod.namval, dir), person);
  40.         return (dir);
  41.     }
  42.  
  43.     if ((fd = open ("/etc/passwd", 0)) < 0)
  44.         return (nullstr);
  45.     
  46.     /*
  47.      * this stuff is to handle the ~person/bin sort of thing
  48.      * for catpath()
  49.      */
  50.     movstr (person, name);
  51.     *rest = '\0';
  52.     for (i = 0; person[i]; i++)
  53.         if (person[i] == '/')
  54.         {
  55.             movstr (& person[i], rest);
  56.             name[i] = '\0';
  57.             break;
  58.         }
  59.  
  60.     while ((count = read (fd, buf, sizeof(buf))) > 0)
  61.     {
  62.         for (i = 0; i < count; i++)
  63.             if (buf[i] == '\n')
  64.             {
  65.                 i++;
  66.                 lseek (fd, (long) (- (count - i)), 1);
  67.                 break;
  68.             }
  69.         buf[i] = '\0';
  70.         for (j = 0; name[j] && buf[j] == name[j]; j++)
  71.             ;
  72.         if (buf[j] == ':' && name[j] == '\0')
  73.             break;    /* found it */
  74.     }
  75.     if (count == 0)
  76.     {
  77.         close (fd);
  78.         return (nullstr);
  79.     }
  80.  
  81.     j--;
  82.     for (i = 1; i <= 5; i++)
  83.     {
  84.         for (; buf[j] != ':'; j++)
  85.             ;
  86.         j++;
  87.     }
  88.     for (i = 0; buf[j] != ':'; i++, j++)
  89.         dir[i] = buf[j];
  90.     if (rest[0])
  91.         for (j = 0; rest[j]; j++)
  92.             dir[i++] = rest[j];
  93.     dir[i] = '\0';
  94.     close (fd);
  95.     return (dir);
  96. }
  97.  
  98. /* username --- return the user's login name */
  99.  
  100. /*
  101.  * this routine returns the first user name in /etc/passwd that matches the
  102.  * real uid.  This could be a problem on some systems, but we don't want to
  103.  * call getlogin(), since it uses stdio, and the shell does not.
  104.  */
  105.  
  106. char *username ()
  107. {
  108.     register int count, i, j, fd;
  109.     static char logname[50];
  110.     static int foundname = FALSE;
  111.     char buf[300];
  112.  
  113.     if (foundname)
  114.         return (logname);
  115.  
  116.     if ((fd = open ("/etc/passwd", 0)) < 0)
  117.         return (nullstr);
  118.     
  119.     itos (getuid());
  120.     while ((count = read (fd, buf, sizeof(buf))) > 0)
  121.     {
  122.         for (i = 0; i < count; i++)
  123.             if (buf[i] == '\n')
  124.             {
  125.                 i++;
  126.                 lseek (fd, (long) (- (count - i)), 1);
  127.                 break;
  128.             }
  129.         buf[i] = '\0';
  130.         for (j = 0, i = 1; i <= 2; i++)
  131.         {
  132.             for (; buf[j] != ':'; j++)
  133.                 ;    /* skip name && passwd */
  134.             j++;
  135.         }
  136.             
  137.         for (i = 0; numbuf[i] && buf[j] == numbuf[i]; i++, j++)
  138.             ;
  139.         if (buf[j] == ':' && numbuf[i] == '\0')
  140.             break;    /* found it */
  141.     }
  142.     if (count == 0)
  143.     {
  144.         close (fd);
  145.         return (nullstr);
  146.     }
  147.  
  148.     for (i = 0; buf[i] != ':'; i++)
  149.         logname[i] = buf[i];
  150.     logname[i] = '\0';
  151.     foundname = TRUE;
  152.     close (fd);
  153.     return (logname);
  154. }
  155.