home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / awm2 / part10 / exp_path.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-21  |  3.4 KB  |  156 lines

  1.  
  2.  
  3.  
  4. #ifndef lint
  5. static char *rcsid_exp_path_c = "$Header: /usr/graph2/X11.3/contrib/windowmgrs/awm/RCS/exp_path.c,v 1.2 89/02/07 21:24:42 jkh Exp $";
  6. #endif  lint
  7.  
  8. #include "X11/copyright.h"
  9. /*
  10.  *
  11.  * Copyright 1987, 1988 by Ardent Computer Corporation, Sunnyvale, Ca.
  12.  *
  13.  * Copyright 1987 by Jordan Hubbard.
  14.  *
  15.  *
  16.  *                         All Rights Reserved
  17.  *
  18.  * Permission to use, copy, modify, and distribute this software and its
  19.  * documentation for any purpose and without fee is hereby granted,
  20.  * provided that the above copyright notice appear in all copies and that
  21.  * both that copyright notice and this permission notice appear in
  22.  * supporting documentation, and that the name of Ardent Computer
  23.  * Corporation or Jordan Hubbard not be used in advertising or publicity
  24.  * pertaining to distribution of the software without specific, written
  25.  * prior permission.
  26.  *
  27.  */
  28.  
  29. #if defined(PCS) || defined(titan)
  30. #include <unistd.h>
  31. #endif
  32. #include <pwd.h>
  33. #include <stdio.h>
  34. #include "awm.h"
  35.  
  36. /*
  37.  * WARNING: This code is obscure.
  38.  * Modify at your Own Risk.
  39.  */
  40. char *expand_file(s)
  41. register char *s;
  42. {
  43.      static char tmp[256], *cp, err[80];
  44.  
  45.      Entry("expand_file")
  46.      
  47.      /* zero tmp */
  48.      tmp[0] = 0;
  49.      if (*s == '/')
  50.       Leave(s)
  51.      if (*s == '~') {
  52.       if (s[1] == '/') { /* It's $HOME */
  53.            if (!(cp = (char *)getenv("HOME"))) {
  54.             sprintf(err, "expand: Can't find $HOME!\n");
  55.             yywarn(err);
  56.             Leave(0)
  57.            }
  58.            strcpy(tmp, cp);
  59.            strcat(tmp, s + 1);
  60.            Leave(tmp)
  61.       }
  62.       else { /* it's another user */
  63.            struct passwd *pwd;
  64.            char uname[32];
  65.            int i;
  66.  
  67.            for (i = 1; s[i] != '/'; i++)
  68.             uname[i - 1] = s[i];
  69.            uname[i] = '\0';
  70.            pwd = getpwnam(uname);
  71.            if (!pwd) {
  72.             sprintf(err, "expand: user '%s' not found.\n", uname);
  73.             yywarn(err);
  74.             Leave(0)
  75.            }
  76.            strcpy(tmp, pwd->pw_dir);
  77.            strcat(tmp, s + i);
  78.            Leave(tmp)
  79.       }
  80.      }
  81.      else
  82.       Leave(s)
  83. }
  84.  
  85. char *deblank(s)
  86. register char *s;
  87. {
  88.      Entry("deblank")
  89.  
  90.      if (!s)
  91.       Leave(s)
  92.      while (*s && (*s == ' ' || *s == '\t'))
  93.       s++;
  94.      Leave(s)
  95. }
  96.  
  97. char *expand_from_path(s)
  98. register char *s;
  99. {
  100.      char tmp[256], *tm;
  101.      int i, plen;
  102.  
  103.      Entry("expand_from_path")
  104.  
  105.      tmp[0] = '\0';
  106.      s = deblank(s);
  107.      if (!s || !*s)
  108.       Leave(0)
  109.      s = expand_file(s);
  110.      if (!s)
  111.       Leave(0)
  112.      if (!access(s, R_OK))
  113.       Leave(s)
  114.      /*
  115.       * If it starts with a slash, we know it either expanded and couldn't
  116.       * be found, or that it started with a slash in the first place and
  117.       * just plain couldn't be found.
  118.       */
  119.      if (*s == '/')
  120.       Leave(0)
  121.      /*
  122.       * At this stage we haven't found the file by name, so it's time to
  123.       * search the path.
  124.       */
  125.      if (!awmPath || !*awmPath)
  126.       Leave(0)
  127.      plen = strlen(awmPath);
  128.      i = 0;
  129.      while (1) {
  130.       int p, l;
  131.       
  132.       tmp[0] = '\0';
  133.       while (i < plen && awmPath[i] == ' ' || awmPath[i] == '\t')
  134.            i++;
  135.       for (p = i; p < plen && awmPath[p] != ' ' && awmPath[p] != '\t'; p++)
  136.            tmp[p - i] = awmPath[p];
  137.       if (!*tmp)
  138.            Leave(0)
  139.       tmp[p - i] = '\0';
  140.       i = p;
  141.       tm = expand_file(tmp);
  142.       if (!tm || !*tm)
  143.            continue;
  144.       l = strlen(tm);
  145.       if (l < 1)
  146.            continue;
  147.       if (tm[l - 1] != '/') { /* append / if necessary */
  148.            tm[l] = '/';
  149.            tm[++l] = '\0';
  150.       }
  151.       strcat(tm, s);
  152.       if (!access(tm, R_OK))
  153.            Leave(tm)
  154.      }
  155. }
  156.