From 7d86eaba8c5a78eca35a7dfddf636f455c8c33c9 Mon Sep 17 00:00:00 2001 From: tg Date: Sat, 26 Mar 2011 19:43:49 +0000 Subject: [PATCH] simplify code to use existing $PWD --- main.c | 35 +++++++++++++---------------------- misc.c | 4 ++-- sh.h | 4 ++-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index c557d2f..e8fb7dd 100644 --- a/main.c +++ b/main.c @@ -33,7 +33,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/main.c,v 1.185 2011/03/26 19:35:35 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/main.c,v 1.186 2011/03/26 19:43:47 tg Exp $"); extern char **environ; @@ -156,12 +156,12 @@ main(int argc, const char *argv[]) Source *s = NULL; struct block *l; unsigned char restricted, errexit, utf_flag; + char *cp; const char *ccp, **wp; struct tbl *vp; struct stat s_stdin; #if !defined(_PATH_DEFPATH) && defined(_CS_PATH) ssize_t k; - char *cp; #endif /* do things like getpgrp() et al. */ @@ -306,26 +306,17 @@ main(int argc, const char *argv[]) substitute(initsubs, 0); /* Figure out the current working directory and set $PWD */ - { - struct tbl *pwd_v = global("PWD"); - char *pwd = str_val(pwd_v); - char *pwdx = pwd; - - /* Try to use existing $PWD if it is valid */ - if (pwd[0] != '/' || - !test_eval(NULL, TO_FILEQ, pwd, ".", true)) - pwdx = NULL; - set_current_wd(pwdx); - if (current_wd[0]) - simplify_path(current_wd); - /* - * Only set pwd if we know where we are or if it had a - * bogus value - */ - if (current_wd[0] || pwd != null) - /* setstr can't fail here */ - setstr(pwd_v, current_wd, KSH_RETURN_ERROR); - } + vp = global("PWD"); + cp = str_val(vp); + /* Try to use existing $PWD if it is valid */ + set_current_wd((cp[0] == '/' && test_eval(NULL, TO_FILEQ, cp, ".", + true)) ? cp : NULL); + if (current_wd[0]) + simplify_path(current_wd); + /* Only set pwd if we know where we are or if it had a bogus value */ + if (current_wd[0] || *cp) + /* setstr can't fail here */ + setstr(vp, current_wd, KSH_RETURN_ERROR); for (wp = initcoms; *wp != NULL; wp++) { shcomexec(wp); diff --git a/misc.c b/misc.c index 18eb277..54c7484 100644 --- a/misc.c +++ b/misc.c @@ -29,7 +29,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.160 2011/03/26 19:29:41 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.161 2011/03/26 19:43:48 tg Exp $"); /* type bits for unsigned char */ unsigned char chtypes[UCHAR_MAX + 1]; @@ -1586,7 +1586,7 @@ simplify_path(char *pathl) } void -set_current_wd(char *nwd) +set_current_wd(const char *nwd) { char *allocd = NULL; diff --git a/sh.h b/sh.h index 8229c19..688245f 100644 --- a/sh.h +++ b/sh.h @@ -154,7 +154,7 @@ #endif #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.452 2011/03/24 19:05:49 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.453 2011/03/26 19:43:49 tg Exp $"); #endif #define MKSH_VERSION "R39 2011/03/23" @@ -1705,7 +1705,7 @@ int reset_nonblock(int); char *ksh_get_wd(void); char *do_realpath(const char *); void simplify_path(char *); -void set_current_wd(char *); +void set_current_wd(const char *); int c_cd(const char **); #ifdef MKSH_SMALL char *strdup_(const char *, Area *);