aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2022-03-19 00:42:11 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-03-19 00:42:11 +0000
commitbc93cfd83482cfe1b91c98ae2c294a5c63720f5c (patch)
treeed9b43e63c328a9174aec65027fdf49b6b07d7a9
parent261f130afe7dc8de339c2589d61c8db2ba956e18 (diff)
parent95e4e99bf49d774c4f0f201882d31e213b2aef68 (diff)
downloadtoybox-bc93cfd83482cfe1b91c98ae2c294a5c63720f5c.tar.gz
Merge "Upgrade toybox to 8a23ac205cb4995d4510a5af880939eda4a5343e" am: d061683cb1 am: 111db74ab1 am: 95e4e99bf4
Original change: https://android-review.googlesource.com/c/platform/external/toybox/+/2029124 Change-Id: I0f7cf9c31798dd6d4109d2a31bcde7aa9e328b45
-rw-r--r--METADATA4
-rw-r--r--android/device/generated/globals.h2
-rw-r--r--android/device/generated/help.h4
-rw-r--r--android/linux/generated/globals.h2
-rw-r--r--android/linux/generated/help.h4
-rw-r--r--android/mac/generated/globals.h2
-rw-r--r--android/mac/generated/help.h4
-rw-r--r--lib/lib.h6
-rw-r--r--tests/sh.test7
-rw-r--r--toys/pending/sh.c90
-rw-r--r--toys/posix/file.c1
-rw-r--r--toys/posix/tty.c9
12 files changed, 67 insertions, 68 deletions
diff --git a/METADATA b/METADATA
index c18e0b33..500cd283 100644
--- a/METADATA
+++ b/METADATA
@@ -9,11 +9,11 @@ third_party {
type: GIT
value: "https://github.com/landley/toybox"
}
- version: "7a5cb958177d4a1efd96f7b4074307731bfe9c06"
+ version: "8a23ac205cb4995d4510a5af880939eda4a5343e"
license_type: UNENCUMBERED
last_upgrade_date {
year: 2022
month: 3
- day: 4
+ day: 16
}
}
diff --git a/android/device/generated/globals.h b/android/device/generated/globals.h
index 554ebf45..844f4d02 100644
--- a/android/device/generated/globals.h
+++ b/android/device/generated/globals.h
@@ -890,7 +890,7 @@ struct sh_data {
long long SECONDS;
char *isexec, *wcpat;
unsigned options, jobcnt, LINENO;
- int hfd, pid, bangpid, varslen, cdcount, srclvl, recursion;
+ int hfd, pid, bangpid, varslen, srclvl, recursion;
// Callable function array
struct sh_function {
diff --git a/android/device/generated/help.h b/android/device/generated/help.h
index a7869567..bc30cd0a 100644
--- a/android/device/generated/help.h
+++ b/android/device/generated/help.h
@@ -402,7 +402,7 @@
#define HELP_declare "usage: declare [-pAailunxr] [NAME...]\n\nSet or print variable attributes and values.\n\n-p Print variables instead of setting\n-A Associative array\n-a Indexed array\n-i Integer\n-l Lower case\n-n Name reference (symlink)\n-r Readonly\n-u Uppercase\n-x Export"
-#define HELP_cd "usage: cd [-PL] [path]\n\nChange current directory. With no arguments, go $HOME.\n\n-P Physical path: resolve symlinks in path\n-L Local path: .. trims directories off $PWD (default)"
+#define HELP_cd "usage: cd [-PL] [-] [path]\n\nChange current directory. With no arguments, go $HOME. Sets $OLDPWD to\nprevious directory: cd - to return to $OLDPWD.\n\n-P Physical path: resolve symlinks in path\n-L Local path: .. trims directories off $PWD (default)"
#define HELP_sh "usage: sh [-c command] [script]\n\nCommand shell. Runs a shell script, or reads input interactively\nand responds to it.\n\n-c command line to execute\n-i interactive mode (default when STDIN is a tty)"
@@ -518,7 +518,7 @@
#define HELP_ulimit "usage: ulimit [-P PID] [-SHRacdefilmnpqrstuv] [LIMIT]\n\nPrint or set resource limits for process number PID. If no LIMIT specified\n(or read-only -ap selected) display current value (sizes in bytes).\nDefault is ulimit -P $PPID -Sf\" (show soft filesize of your shell).\n\n-P PID to affect (default $PPID) -a Show all limits\n-S Set/show soft limit -H Set/show hard (maximum) limit\n\n-c Core file size (blocks) -d Process data segment (KiB)\n-e Max scheduling priority -f File size (KiB)\n-i Pending signal count -l Locked memory (KiB)\n-m Resident Set Size (KiB) -n Number of open files\n-p Pipe buffer (512 bytes) -q POSIX message queues\n-r Max realtime priority -R Realtime latency (us)\n-s Stack size (KiB) -t Total CPU time (s)\n-u Maximum processes (this UID) -v Virtual memory size (KiB)"
-#define HELP_tty "usage: tty [-s]\n\nShow filename of terminal connected to stdin.\n\nPrints \"not a tty\" and exits with nonzero status if no terminal\nis connected to stdin.\n\n-s Silent, exit code only"
+#define HELP_tty "usage: tty [-s]\n\nShow filename of terminal connected to stdin. If none print \"not a tty\"\nand exit with nonzero status.\n\n-s Silent, exit code only"
#define HELP_true "usage: true\n\nReturn zero."
diff --git a/android/linux/generated/globals.h b/android/linux/generated/globals.h
index 554ebf45..844f4d02 100644
--- a/android/linux/generated/globals.h
+++ b/android/linux/generated/globals.h
@@ -890,7 +890,7 @@ struct sh_data {
long long SECONDS;
char *isexec, *wcpat;
unsigned options, jobcnt, LINENO;
- int hfd, pid, bangpid, varslen, cdcount, srclvl, recursion;
+ int hfd, pid, bangpid, varslen, srclvl, recursion;
// Callable function array
struct sh_function {
diff --git a/android/linux/generated/help.h b/android/linux/generated/help.h
index af1ef09e..ae7d47c0 100644
--- a/android/linux/generated/help.h
+++ b/android/linux/generated/help.h
@@ -404,7 +404,7 @@
#define HELP_declare "usage: declare [-pAailunxr] [NAME...]\n\nSet or print variable attributes and values.\n\n-p Print variables instead of setting\n-A Associative array\n-a Indexed array\n-i Integer\n-l Lower case\n-n Name reference (symlink)\n-r Readonly\n-u Uppercase\n-x Export"
-#define HELP_cd "usage: cd [-PL] [path]\n\nChange current directory. With no arguments, go $HOME.\n\n-P Physical path: resolve symlinks in path\n-L Local path: .. trims directories off $PWD (default)"
+#define HELP_cd "usage: cd [-PL] [-] [path]\n\nChange current directory. With no arguments, go $HOME. Sets $OLDPWD to\nprevious directory: cd - to return to $OLDPWD.\n\n-P Physical path: resolve symlinks in path\n-L Local path: .. trims directories off $PWD (default)"
#define HELP_sh "usage: sh [-c command] [script]\n\nCommand shell. Runs a shell script, or reads input interactively\nand responds to it.\n\n-c command line to execute\n-i interactive mode (default when STDIN is a tty)"
@@ -520,7 +520,7 @@
#define HELP_ulimit "usage: ulimit [-P PID] [-SHRacdefilmnpqrstuv] [LIMIT]\n\nPrint or set resource limits for process number PID. If no LIMIT specified\n(or read-only -ap selected) display current value (sizes in bytes).\nDefault is ulimit -P $PPID -Sf\" (show soft filesize of your shell).\n\n-P PID to affect (default $PPID) -a Show all limits\n-S Set/show soft limit -H Set/show hard (maximum) limit\n\n-c Core file size (blocks) -d Process data segment (KiB)\n-e Max scheduling priority -f File size (KiB)\n-i Pending signal count -l Locked memory (KiB)\n-m Resident Set Size (KiB) -n Number of open files\n-p Pipe buffer (512 bytes) -q POSIX message queues\n-r Max realtime priority -R Realtime latency (us)\n-s Stack size (KiB) -t Total CPU time (s)\n-u Maximum processes (this UID) -v Virtual memory size (KiB)"
-#define HELP_tty "usage: tty [-s]\n\nShow filename of terminal connected to stdin.\n\nPrints \"not a tty\" and exits with nonzero status if no terminal\nis connected to stdin.\n\n-s Silent, exit code only"
+#define HELP_tty "usage: tty [-s]\n\nShow filename of terminal connected to stdin. If none print \"not a tty\"\nand exit with nonzero status.\n\n-s Silent, exit code only"
#define HELP_true "usage: true\n\nReturn zero."
diff --git a/android/mac/generated/globals.h b/android/mac/generated/globals.h
index 554ebf45..844f4d02 100644
--- a/android/mac/generated/globals.h
+++ b/android/mac/generated/globals.h
@@ -890,7 +890,7 @@ struct sh_data {
long long SECONDS;
char *isexec, *wcpat;
unsigned options, jobcnt, LINENO;
- int hfd, pid, bangpid, varslen, cdcount, srclvl, recursion;
+ int hfd, pid, bangpid, varslen, srclvl, recursion;
// Callable function array
struct sh_function {
diff --git a/android/mac/generated/help.h b/android/mac/generated/help.h
index af1ef09e..ae7d47c0 100644
--- a/android/mac/generated/help.h
+++ b/android/mac/generated/help.h
@@ -404,7 +404,7 @@
#define HELP_declare "usage: declare [-pAailunxr] [NAME...]\n\nSet or print variable attributes and values.\n\n-p Print variables instead of setting\n-A Associative array\n-a Indexed array\n-i Integer\n-l Lower case\n-n Name reference (symlink)\n-r Readonly\n-u Uppercase\n-x Export"
-#define HELP_cd "usage: cd [-PL] [path]\n\nChange current directory. With no arguments, go $HOME.\n\n-P Physical path: resolve symlinks in path\n-L Local path: .. trims directories off $PWD (default)"
+#define HELP_cd "usage: cd [-PL] [-] [path]\n\nChange current directory. With no arguments, go $HOME. Sets $OLDPWD to\nprevious directory: cd - to return to $OLDPWD.\n\n-P Physical path: resolve symlinks in path\n-L Local path: .. trims directories off $PWD (default)"
#define HELP_sh "usage: sh [-c command] [script]\n\nCommand shell. Runs a shell script, or reads input interactively\nand responds to it.\n\n-c command line to execute\n-i interactive mode (default when STDIN is a tty)"
@@ -520,7 +520,7 @@
#define HELP_ulimit "usage: ulimit [-P PID] [-SHRacdefilmnpqrstuv] [LIMIT]\n\nPrint or set resource limits for process number PID. If no LIMIT specified\n(or read-only -ap selected) display current value (sizes in bytes).\nDefault is ulimit -P $PPID -Sf\" (show soft filesize of your shell).\n\n-P PID to affect (default $PPID) -a Show all limits\n-S Set/show soft limit -H Set/show hard (maximum) limit\n\n-c Core file size (blocks) -d Process data segment (KiB)\n-e Max scheduling priority -f File size (KiB)\n-i Pending signal count -l Locked memory (KiB)\n-m Resident Set Size (KiB) -n Number of open files\n-p Pipe buffer (512 bytes) -q POSIX message queues\n-r Max realtime priority -R Realtime latency (us)\n-s Stack size (KiB) -t Total CPU time (s)\n-u Maximum processes (this UID) -v Virtual memory size (KiB)"
-#define HELP_tty "usage: tty [-s]\n\nShow filename of terminal connected to stdin.\n\nPrints \"not a tty\" and exits with nonzero status if no terminal\nis connected to stdin.\n\n-s Silent, exit code only"
+#define HELP_tty "usage: tty [-s]\n\nShow filename of terminal connected to stdin. If none print \"not a tty\"\nand exit with nonzero status.\n\n-s Silent, exit code only"
#define HELP_true "usage: true\n\nReturn zero."
diff --git a/lib/lib.h b/lib/lib.h
index 6ab312e3..6e9a2640 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -16,7 +16,7 @@ struct ptr_len {
struct string_list {
struct string_list *next;
- char str[0];
+ char str[];
};
struct arg_list {
@@ -199,8 +199,8 @@ void perror_exit(char *msg, ...) printf_format __attribute__((__noreturn__));
void help_exit(char *msg, ...) printf_format __attribute__((__noreturn__));
void error_msg_raw(char *msg);
void perror_msg_raw(char *msg);
-void error_exit_raw(char *msg);
-void perror_exit_raw(char *msg);
+void error_exit_raw(char *msg) __attribute__((__noreturn__));
+void perror_exit_raw(char *msg) __attribute__((__noreturn__));
ssize_t readall(int fd, void *buf, size_t len);
ssize_t writeall(int fd, void *buf, size_t len);
off_t lskip(int fd, off_t offset);
diff --git a/tests/sh.test b/tests/sh.test
index 53cf81b7..9cfa9fd6 100644
--- a/tests/sh.test
+++ b/tests/sh.test
@@ -137,6 +137,13 @@ testing "eval0" "sh -c 'eval echo \$*' one two three" "two three\n" "" ""
# Change EVAL to call sh -c for us, using "bash" explicitly for the host.
export EVAL="$SH -c"
+
+mkdir -p one/two/three
+testing 'cd in renamed dir' \
+ 'cd one/two/three && mv ../../../{one,four} && cd .. && echo ${PWD: -9:9}' \
+ '/four/two\n' '' ''
+rm -rf one
+
testing "smoketest" "echo hello" "hello\n" "" ""
testing "line break" $'ec\\\nho hello' 'hello\n' '' ''
testing "assignment" 'x=y; echo $x' 'y\n' '' ''
diff --git a/toys/pending/sh.c b/toys/pending/sh.c
index d312552f..85be8266 100644
--- a/toys/pending/sh.c
+++ b/toys/pending/sh.c
@@ -81,9 +81,10 @@ config CD
default n
depends on SH
help
- usage: cd [-PL] [path]
+ usage: cd [-PL] [-] [path]
- Change current directory. With no arguments, go $HOME.
+ Change current directory. With no arguments, go $HOME. Sets $OLDPWD to
+ previous directory: cd - to return to $OLDPWD.
-P Physical path: resolve symlinks in path
-L Local path: .. trims directories off $PWD (default)
@@ -255,7 +256,7 @@ GLOBALS(
long long SECONDS;
char *isexec, *wcpat;
unsigned options, jobcnt, LINENO;
- int hfd, pid, bangpid, varslen, cdcount, srclvl, recursion;
+ int hfd, pid, bangpid, varslen, srclvl, recursion;
// Callable function array
struct sh_function {
@@ -329,6 +330,9 @@ static const char *redirectors[] = {"<<<", "<<-", "<<", "<&", "<>", "<", ">>",
#define OPT_C 0x200
#define OPT_x 0x400
+// only export $PWD and $OLDPWD on first cd
+#define OPT_cd 0x80000000
+
// struct sh_process->flags
#define PFLAG_NOT 1
@@ -484,7 +488,7 @@ static int recalculate(long long *dd, char **ss, int lvl)
// Always start handling unary prefixes, parenthetical blocks, and constants
if (cc=='+' || cc=='-') {
++*ss;
- if (!recalculate(dd, ss, 1) || **ss) return 0;
+ if (!recalculate(dd, ss, 1)) return 0;
if (cc=='-') *dd = -*dd;
} else if (cc=='(') {
++*ss;
@@ -3994,68 +3998,58 @@ void sh_main(void)
#include "generated/flags.h"
void cd_main(void)
{
- char *home = getvar("HOME") ? : "/", *pwd = getvar("PWD"), *from, *to = 0,
- *dd = xstrdup(*toys.optargs ? *toys.optargs : home);
- int bad = 0;
+ char *from, *to = 0, *dd = *toys.optargs ? : (getvar("HOME") ? : "/"),
+ *pwd = FLAG(P) ? 0 : getvar("PWD"), *zap = 0;
+ struct stat st1, st2;
// TODO: CDPATH? Really?
- // prepend cwd or $PWD to relative path
- if (*dd != '/') {
- from = pwd ? : (to = getcwd(0, 0));
- if (!from) setvarval("PWD", "(nowhere)");
- else {
- from = xmprintf("%s/%s", from, dd);
- free(dd);
- free(to);
- dd = from;
- }
- }
+ // For cd - use $OLDPWD as destination directory
+ if (!strcmp(dd, "-") && (!(dd = getvar("OLDPWD")) || !*dd))
+ return perror_msg("No $OLDPWD");
- if (FLAG(P)) {
- struct stat st;
- char *pp;
+ if (*dd == '/') pwd = 0;
- // Does this directory exist?
- if ((pp = xabspath(dd, 1)) && stat(pp, &st) && !S_ISDIR(st.st_mode))
- bad++, errno = ENOTDIR;
- else {
- free(dd);
- dd = pp;
- }
- } else {
+ // Did $PWD move out from under us?
+ if (pwd && !stat(".", &st1))
+ if (stat(pwd, &st2) || st1.st_dev!=st2.st_dev || st1.st_ino!=st2.st_ino)
+ pwd = 0;
+
+ // Handle logical relative path
+ if (pwd) {
+ zap = xmprintf("%s/%s", pwd, dd);
// cancel out . and .. in the string
- for (from = to = dd; *from;) {
+ for (from = to = zap; *from;) {
if (*from=='/' && from[1]=='/') from++;
else if (*from!='/' || from[1]!='.') *to++ = *from++;
else if (!from[2] || from[2]=='/') from += 2;
else if (from[2]=='.' && (!from[3] || from[3]=='/')) {
from += 3;
- while (to>dd && *--to != '/');
+ while (to>zap && *--to != '/');
} else *to++ = *from++;
}
- if (to == dd) to++;
- if (to-dd>1 && to[-1]=='/') to--;
+ if (to == zap) to++;
+ if (to-zap>1 && to[-1]=='/') to--;
*to = 0;
}
- if (bad || chdir(dd)) perror_msg("chdir '%s'", dd);
- else {
- if (pwd) {
- setvarval("OLDPWD", pwd);
- if (TT.cdcount == 1) {
- export("OLDPWD");
- TT.cdcount++;
- }
- }
- setvarval("PWD", dd);
- if (!TT.cdcount) {
- export("PWD");
- TT.cdcount++;
- }
- }
+ // If logical chdir doesn't work, fall back to physical
+ if (!zap || chdir(zap)) {
+ free(zap);
+ if (chdir(dd)) return perror_msg("%s", dd);
+ if (!(dd = getcwd(0, 0))) dd = xstrdup("(nowhere)");
+ } else dd = zap;
+
+ if ((pwd = getvar("PWD"))) setvarval("OLDPWD", pwd);
+ setvarval("PWD", dd);
free(dd);
+
+ if (!(TT.options&OPT_cd)) {
+ export("OLDPWD");
+ export("PWD");
+ TT.options |= OPT_cd;
+ }
}
void exit_main(void)
diff --git a/toys/posix/file.c b/toys/posix/file.c
index 45e614dd..9330da13 100644
--- a/toys/posix/file.c
+++ b/toys/posix/file.c
@@ -133,6 +133,7 @@ static void do_elf_file(int fd)
sh_type = elf_int(shdr+4, 4);
sh_offset = elf_int(shdr+8+(8<<bits), 4<<bits);
sh_size = elf_int(shdr+8+(12<<bits), 4);
+ if (sh_type == 8 /*SHT_NOBITS*/) sh_size = 0;
if (sh_offset>TT.len || sh_size>TT.len-sh_offset) goto bad;
if (sh_type == 2 /*SHT_SYMTAB*/) {
diff --git a/toys/posix/tty.c b/toys/posix/tty.c
index 9a833d5e..6116f135 100644
--- a/toys/posix/tty.c
+++ b/toys/posix/tty.c
@@ -12,10 +12,8 @@ config TTY
help
usage: tty [-s]
- Show filename of terminal connected to stdin.
-
- Prints "not a tty" and exits with nonzero status if no terminal
- is connected to stdin.
+ Show filename of terminal connected to stdin. If none print "not a tty"
+ and exit with nonzero status.
-s Silent, exit code only
*/
@@ -26,7 +24,6 @@ void tty_main(void)
{
char *tty = ttyname(0);
- if (!toys.optflags) puts(tty ? tty : "not a tty");
-
toys.exitval = !tty;
+ if (!toys.optflags) puts(tty ? : "not a tty");
}