home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / inc-elim / unenv.c < prev   
Encoding:
C/C++ Source or Header  |  1988-05-30  |  1.7 KB  |  95 lines

  1. /* LINTLIBRARY */
  2.  
  3. #ifndef lint
  4. static char *RCSid = "$Header: unenv.c,v 1.1 1988-02-29 00:41:23 sten Exp $";
  5. #endif
  6.  
  7. /*
  8.  * $Log:    unenv.c,v $
  9.  * 
  10.  * Revision 1.1  1988-02-29  00:41:23  sten
  11.  * Initial revision
  12.  * 
  13.  * Revision 1.1  1986-10-20  16:51:22  sten
  14.  * Initial revisi
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <pwd.h>
  20. #define Strcpy (void) strcpy
  21. #define Strcat (void) strcat
  22.  
  23. /****************************************\
  24. *                      *
  25. *  untilde: perform tilde substitution.     *
  26. *                      *
  27. \****************************************/
  28.  
  29. untilde(out, in)
  30. char *out, *in;
  31. {
  32.     if (in[0] != '~')
  33.     {
  34.     Strcpy(out, in);
  35.     return (0);
  36.     }
  37.     if (isalnum(in[1]))
  38.     {
  39.     char            user[80];
  40.     struct passwd  *pw, *getpwnam();
  41.     char           *p, *pu;
  42.  
  43.     p = in + 1;
  44.     pu = user;
  45.     while (isalnum(*p) || *p == '-')
  46.         *pu++ = *p++;
  47.     *pu = '\0';
  48.     if ((pw = getpwnam(user)) == NULL)
  49.         return (1);
  50.     Strcpy(out, pw->pw_dir);
  51.     Strcat(out, p);
  52.     } else
  53.     {
  54.     char           *p;
  55.  
  56.     if ((p = (char *) getenv("HOME")) == (char *) NULL)
  57.         return (1);
  58.     Strcpy(out, p);
  59.     Strcat(out, in + 1);
  60.     }
  61.     return (0);
  62. }
  63.  
  64. /*******************************************\
  65. *                         *
  66. *  unenv: perform environment substitution  *
  67. *                         *
  68. \*******************************************/
  69.  
  70. unenv(out, in)
  71. char *out, *in;
  72. {
  73.     while (*in != '\0')
  74.     {
  75.     if (*in != '$')
  76.         *out++ = *in++;
  77.     else
  78.     {
  79.         char            envar[80];
  80.         char           *p;
  81.  
  82.         in++;
  83.         for (p = envar; isalnum(*in);)
  84.         *p++ = *in++;
  85.         *p = '\0';
  86.         if ((p = (char *) getenv(envar)) == (char *) NULL)
  87.         return (1);
  88.         Strcpy(out, p);
  89.         out += strlen(out);
  90.     }
  91.     }
  92.     *out = '\0';
  93.     return (0);
  94. }
  95.