simplify code to use existing $PWD

This commit is contained in:
tg 2011-03-26 19:43:49 +00:00
parent e3ed074981
commit 7d86eaba8c
3 changed files with 17 additions and 26 deletions

35
main.c
View File

@ -33,7 +33,7 @@
#include <locale.h>
#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);

4
misc.c
View File

@ -29,7 +29,7 @@
#include <grp.h>
#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;

4
sh.h
View File

@ -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 *);