home *** CD-ROM | disk | FTP | other *** search
- #include "config.h"
- #include "vname.h"
- #include <pwd.h>
- #include <string.h>
- #include <ctype.h>
-
- #define isvchar(c) ((c) && ((isalnum((c))) || ((c) == '_')))
-
- static char *homedir P_((char *uname));
-
- char last_vname[MAXPATHLEN];
-
- /*
- * char *vname(path)
- * char *path;
- *
- * Return a pointer to a newly-built path. The following patterns are
- * parsed in FNAME.
- *
- * ~/ My home
- * ~user/ User's home
- * .../$VAR/... Shell Environment
- */
-
- char *vname(path)
- char *path;
- {
- char *lp = last_vname;
- char buf[BLEN], *bp, *getlogin(), *getenv();
-
- if (*path == '~') {
- if (*++path == '/') /* ~/ */
- (void) strcpy(buf, getlogin());
- else {
- for (bp = buf; *path != '/'; )
- *bp++ = *path++;
- *bp = EOS;
- }
- (void) strcpy(last_vname, homedir(buf));
- lp += strlen(last_vname);
- }
-
- for (; path && *path;) {
- if (*path != '$')
- *lp++ = *path++;
- else {
- /*
- * Find $SOMETHING
- */
-
- *lp = EOS;
- for (bp = buf, ++path; isvchar(*path); path++)
- *bp++ = *path;
- *bp = EOS;
-
- /*
- * Get from environment. If not found use the literal
- */
-
- bp = (bp = getenv(buf)) ? bp : buf;
- for (; bp && *bp; )
- *lp++ = *bp++;
- }
- }
- *lp = EOS;
- return (last_vname);
- }
-
- static char *homedir(user)
- char *user;
- {
- struct passwd *pw, *getpwnam();
- static char buf[BLEN];
-
- if ((pw = getpwnam(user)) != (struct passwd *)0)
- return (pw->pw_dir);
-
- *buf = '~';
- (void) strcpy(buf + 1, user);
- return (buf);
- }
-