diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-09-05 20:15:17 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-09-05 20:15:17 +0200 |
| commit | 6db4784d27be020d892f92db0cba70452b1c128e (patch) | |
| tree | f71502294df15bbbf211453c07b6763fddad63ad /shell | |
| parent | 2cc709139ebd543a1bb126d3ce77683961a89d75 (diff) | |
| download | busybox-w32-6db4784d27be020d892f92db0cba70452b1c128e.tar.gz busybox-w32-6db4784d27be020d892f92db0cba70452b1c128e.tar.bz2 busybox-w32-6db4784d27be020d892f92db0cba70452b1c128e.zip | |
hush: export PWD
function old new delta
set_pwd_var - 36 +36
hush_main 941 951 +10
evalvar 1365 1363 -2
ash_main 1364 1362 -2
builtin_cd 75 72 -3
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/3 up/down: 46/-7) Total: 39 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
| -rw-r--r-- | shell/ash.c | 4 | ||||
| -rw-r--r-- | shell/hush.c | 61 |
2 files changed, 60 insertions, 5 deletions
diff --git a/shell/ash.c b/shell/ash.c index 35d5cde58..eca4ab98c 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
| @@ -12985,7 +12985,7 @@ init(void) | |||
| 12985 | /* from var.c: */ | 12985 | /* from var.c: */ |
| 12986 | { | 12986 | { |
| 12987 | char **envp; | 12987 | char **envp; |
| 12988 | char ppid[sizeof(int)*3 + 1]; | 12988 | char ppid[sizeof(int)*3 + 2]; |
| 12989 | const char *p; | 12989 | const char *p; |
| 12990 | struct stat st1, st2; | 12990 | struct stat st1, st2; |
| 12991 | 12991 | ||
| @@ -12996,7 +12996,7 @@ init(void) | |||
| 12996 | } | 12996 | } |
| 12997 | } | 12997 | } |
| 12998 | 12998 | ||
| 12999 | snprintf(ppid, sizeof(ppid), "%u", (unsigned) getppid()); | 12999 | sprintf(ppid, "%u", (unsigned) getppid()); |
| 13000 | setvar("PPID", ppid, 0); | 13000 | setvar("PPID", ppid, 0); |
| 13001 | 13001 | ||
| 13002 | p = lookupvar("PWD"); | 13002 | p = lookupvar("PWD"); |
diff --git a/shell/hush.c b/shell/hush.c index 7ac29ace2..5794b1ddf 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
| @@ -58,7 +58,7 @@ | |||
| 58 | * TODOs: | 58 | * TODOs: |
| 59 | * grep for "TODO" and fix (some of them are easy) | 59 | * grep for "TODO" and fix (some of them are easy) |
| 60 | * builtins: ulimit | 60 | * builtins: ulimit |
| 61 | * special variables (PWD etc) | 61 | * special variables (done: PWD) |
| 62 | * follow IFS rules more precisely, including update semantics | 62 | * follow IFS rules more precisely, including update semantics |
| 63 | * export builtin should be special, its arguments are assignments | 63 | * export builtin should be special, its arguments are assignments |
| 64 | * and therefore expansion of them should be "one-word" expansion: | 64 | * and therefore expansion of them should be "one-word" expansion: |
| @@ -1432,6 +1432,13 @@ static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_ | |||
| 1432 | return 0; | 1432 | return 0; |
| 1433 | } | 1433 | } |
| 1434 | 1434 | ||
| 1435 | /* Used at startup and after each cd */ | ||
| 1436 | static void set_pwd_var(int exp) | ||
| 1437 | { | ||
| 1438 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), | ||
| 1439 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); | ||
| 1440 | } | ||
| 1441 | |||
| 1435 | static int unset_local_var_len(const char *name, int name_len) | 1442 | static int unset_local_var_len(const char *name, int name_len) |
| 1436 | { | 1443 | { |
| 1437 | struct variable *cur; | 1444 | struct variable *cur; |
| @@ -1604,6 +1611,9 @@ static const char* setup_prompt_string(int promptmode) | |||
| 1604 | /* Set up the prompt */ | 1611 | /* Set up the prompt */ |
| 1605 | if (promptmode == 0) { /* PS1 */ | 1612 | if (promptmode == 0) { /* PS1 */ |
| 1606 | free((char*)G.PS1); | 1613 | free((char*)G.PS1); |
| 1614 | /* bash uses $PWD value, even if it is set by user. | ||
| 1615 | * It uses current dir only if PWD is unset. | ||
| 1616 | * We always use current dir. */ | ||
| 1607 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); | 1617 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
| 1608 | prompt_str = G.PS1; | 1618 | prompt_str = G.PS1; |
| 1609 | } else | 1619 | } else |
| @@ -6432,8 +6442,49 @@ int hush_main(int argc, char **argv) | |||
| 6432 | } | 6442 | } |
| 6433 | e++; | 6443 | e++; |
| 6434 | } | 6444 | } |
| 6445 | /* reinstate HUSH_VERSION */ | ||
| 6435 | debug_printf_env("putenv '%s'\n", hush_version_str); | 6446 | debug_printf_env("putenv '%s'\n", hush_version_str); |
| 6436 | putenv((char *)hush_version_str); /* reinstate HUSH_VERSION */ | 6447 | putenv((char *)hush_version_str); |
| 6448 | |||
| 6449 | /* Export PWD */ | ||
| 6450 | set_pwd_var(/*exp:*/ 1); | ||
| 6451 | /* bash also exports SHLVL and _, | ||
| 6452 | * and sets (but doesn't export) the following variables: | ||
| 6453 | * BASH=/bin/bash | ||
| 6454 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") | ||
| 6455 | * BASH_VERSION='3.2.0(1)-release' | ||
| 6456 | * HOSTTYPE=i386 | ||
| 6457 | * MACHTYPE=i386-pc-linux-gnu | ||
| 6458 | * OSTYPE=linux-gnu | ||
| 6459 | * HOSTNAME=<xxxxxxxxxx> | ||
| 6460 | * PPID=<NNNNN> | ||
| 6461 | * EUID=<NNNNN> | ||
| 6462 | * UID=<NNNNN> | ||
| 6463 | * GROUPS=() | ||
| 6464 | * LINES=<NNN> | ||
| 6465 | * COLUMNS=<NNN> | ||
| 6466 | * BASH_ARGC=() | ||
| 6467 | * BASH_ARGV=() | ||
| 6468 | * BASH_LINENO=() | ||
| 6469 | * BASH_SOURCE=() | ||
| 6470 | * DIRSTACK=() | ||
| 6471 | * PIPESTATUS=([0]="0") | ||
| 6472 | * HISTFILE=/<xxx>/.bash_history | ||
| 6473 | * HISTFILESIZE=500 | ||
| 6474 | * HISTSIZE=500 | ||
| 6475 | * MAILCHECK=60 | ||
| 6476 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. | ||
| 6477 | * SHELL=/bin/bash | ||
| 6478 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor | ||
| 6479 | * TERM=dumb | ||
| 6480 | * OPTERR=1 | ||
| 6481 | * OPTIND=1 | ||
| 6482 | * IFS=$' \t\n' | ||
| 6483 | * PS1='\s-\v\$ ' | ||
| 6484 | * PS2='> ' | ||
| 6485 | * PS4='+ ' | ||
| 6486 | */ | ||
| 6487 | |||
| 6437 | #if ENABLE_FEATURE_EDITING | 6488 | #if ENABLE_FEATURE_EDITING |
| 6438 | G.line_input_state = new_line_input_t(FOR_SHELL); | 6489 | G.line_input_state = new_line_input_t(FOR_SHELL); |
| 6439 | #endif | 6490 | #endif |
| @@ -6816,7 +6867,11 @@ static int FAST_FUNC builtin_cd(char **argv) | |||
| 6816 | bb_perror_msg("cd: %s", newdir); | 6867 | bb_perror_msg("cd: %s", newdir); |
| 6817 | return EXIT_FAILURE; | 6868 | return EXIT_FAILURE; |
| 6818 | } | 6869 | } |
| 6819 | get_cwd(1); | 6870 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 6871 | * Note: do not enforce exporting. If PWD was unset or unexported, | ||
| 6872 | * set it again, but do not export. bash does the same. | ||
| 6873 | */ | ||
| 6874 | set_pwd_var(/*exp:*/ 0); | ||
| 6820 | return EXIT_SUCCESS; | 6875 | return EXIT_SUCCESS; |
| 6821 | } | 6876 | } |
| 6822 | 6877 | ||
