Busybox 1.21.0 squashed commit for jellybean

Change-Id: I423c7fc1254050c6495126b1b18dd33af07fed6b
Signed-off-by: Tanguy Pruvot <tanguy.pruvot@gmail.com>
diff --git a/libbb/Config.src b/libbb/Config.src
index ee1b66a..19021fe 100644
--- a/libbb/Config.src
+++ b/libbb/Config.src
@@ -28,6 +28,16 @@
 	  2                   3.0                5088
 	  3 (smallest)        5.1                4912
 
+config SHA3_SMALL
+	int "SHA3: Trade bytes for speed (0:fast, 1:slow)"
+	default 1
+	range 0 1
+	help
+	  Trade binary size versus speed for the sha3sum algorithm.
+	  SHA3_SMALL=0 compared to SHA3_SMALL=1 (approximate):
+	  64-bit x86: +270 bytes of code, 45% faster
+	  32-bit x86: +450 bytes of code, 75% faster
+
 config FEATURE_FAST_TOP
 	bool "Faster /proc scanning code (+100 bytes)"
 	default y
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index c5d6d7d..fc1847a 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -140,10 +140,9 @@
 }
 
 #if NUM_APPLETS > 8
-/* NB: any char pointer will work as well, not necessarily applet_names */
-static int applet_name_compare(const void *name, const void *v)
+static int applet_name_compare(const void *name, const void *idx)
 {
-	int i = (const char *)v - applet_names;
+	int i = (int)(ptrdiff_t)idx - 1;
 	return strcmp(name, APPLET_NAME(i));
 }
 #endif
@@ -152,10 +151,12 @@
 #if NUM_APPLETS > 8
 	/* Do a binary search to find the applet entry given the name. */
 	const char *p;
-	p = bsearch(name, applet_names, ARRAY_SIZE(applet_main), 1, applet_name_compare);
-	if (!p)
-		return -1;
-	return p - applet_names;
+	p = bsearch(name, (void*)(ptrdiff_t)1, ARRAY_SIZE(applet_main), 1, applet_name_compare);
+	/*
+	 * if (!p) return -1;
+	 * ^^^^^^^^^^^^^^^^^^ the code below will do this if p == NULL :)
+	 */
+	return (int)(ptrdiff_t)p - 1;
 #else
 	/* A version which does not pull in bsearch */
 	int i = 0;
@@ -627,9 +628,9 @@
 		full_write2_str(bb_banner); /* reuse const string */
 		full_write2_str(" multi-call binary.\n"); /* reuse */
 		full_write2_str(
-			"Copyright (C) 1998-2012 Erik Andersen, Rob Landley, Denys Vlasenko\n"
-			"and others. Licensed under GPLv2. Merged for bionic by tpruvot@github\n"
-			"See source distribution for full notice.\n"
+			"BusyBox is copyrighted by many authors between 1998-2012.\n"
+			"Licensed under GPLv2. See source distribution for detailed\n"
+			"copyright notices. Merged for bionic by tpruvot@github\n"
 			"\n"
 			"Usage: busybox [function [arguments]...]\n"
 			"   or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n"
@@ -747,8 +748,11 @@
 		/* Special case. POSIX says "test --help"
 		 * should be no different from e.g. "test --foo".  */
 //TODO: just compare applet_no with APPLET_NO_test
-		if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
+		if (!ENABLE_TEST || strcmp(applet_name, "test") != 0) {
+			/* If you want "foo --help" to return 0: */
+			/*xfunc_error_retval = 0;*/
 			bb_show_usage();
+		}
 	}
 	if (ENABLE_FEATURE_SUID)
 		check_suid(applet_no);
diff --git a/libbb/correct_password.c b/libbb/correct_password.c
index 6301589..7cabd33 100644
--- a/libbb/correct_password.c
+++ b/libbb/correct_password.c
@@ -41,12 +41,6 @@
 	char *unencrypted, *encrypted;
 	const char *correct;
 	int r;
-#if ENABLE_FEATURE_SHADOWPASSWDS
-	/* Using _r function to avoid pulling in static buffers */
-	struct spwd spw;
-	char buffer[256];
-#endif
-
 	/* fake salt. crypt() can choke otherwise. */
 	correct = "aa";
 	if (!pw) {
@@ -55,7 +49,10 @@
 	}
 	correct = pw->pw_passwd;
 #if ENABLE_FEATURE_SHADOWPASSWDS
+	/* Using _r function to avoid pulling in static buffers */
 	if ((correct[0] == 'x' || correct[0] == '*') && !correct[1]) {
+		struct spwd spw;
+		char buffer[256];
 		/* getspnam_r may return 0 yet set result to NULL.
 		 * At least glibc 2.4 does this. Be extra paranoid here. */
 		struct spwd *result = NULL;
diff --git a/libbb/get_shell_name.c b/libbb/get_shell_name.c
index c930afd..5aebe9c 100644
--- a/libbb/get_shell_name.c
+++ b/libbb/get_shell_name.c
@@ -8,7 +8,7 @@
 
 #include "libbb.h"
 
-const char *get_shell_name(void)
+const char* FAST_FUNC get_shell_name(void)
 {
 	struct passwd *pw;
 	char *shell;
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c
index a313c2a..b4d955e 100644
--- a/libbb/hash_md5_sha.c
+++ b/libbb/hash_md5_sha.c
@@ -31,6 +31,11 @@
 	return (x >> n) | (x << (64 - n));
 }
 
+/* rotl64 only used for sha3 currently */
+static ALWAYS_INLINE uint64_t rotl64(uint64_t x, unsigned n)
+{
+	return (x << n) | (x >> (64 - n));
+}
 
 /* Feed data through a temporary buffer.
  * The internal buffer remembers previous data until it has 64
@@ -51,7 +56,7 @@
 		len -= remaining;
 		buffer = (const char *)buffer + remaining;
 		bufpos += remaining;
-		/* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */
+		/* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
 		bufpos -= 64;
 		if (bufpos != 0)
 			break;
@@ -185,10 +190,9 @@
 	int i;
 	uint32_t temp;
 
-# if BB_BIG_ENDIAN
-	for (i = 0; i < 16; i++)
-		words[i] = SWAP_LE32(words[i]);
-# endif
+	if (BB_BIG_ENDIAN)
+		for (i = 0; i < 16; i++)
+			words[i] = SWAP_LE32(words[i]);
 
 # if MD5_SMALL == 3
 	pc = C_array;
@@ -462,12 +466,13 @@
 	common64_end(ctx, /*swap_needed:*/ BB_BIG_ENDIAN);
 
 	/* The MD5 result is in little endian byte order */
-#if BB_BIG_ENDIAN
-	ctx->hash[0] = SWAP_LE32(ctx->hash[0]);
-	ctx->hash[1] = SWAP_LE32(ctx->hash[1]);
-	ctx->hash[2] = SWAP_LE32(ctx->hash[2]);
-	ctx->hash[3] = SWAP_LE32(ctx->hash[3]);
-#endif
+	if (BB_BIG_ENDIAN) {
+		ctx->hash[0] = SWAP_LE32(ctx->hash[0]);
+		ctx->hash[1] = SWAP_LE32(ctx->hash[1]);
+		ctx->hash[2] = SWAP_LE32(ctx->hash[2]);
+		ctx->hash[3] = SWAP_LE32(ctx->hash[3]);
+	}
+
 	memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * 4);
 }
 
@@ -834,7 +839,7 @@
 		len -= remaining;
 		buffer = (const char *)buffer + remaining;
 		bufpos += remaining;
-		/* clever way to do "if (bufpos != 128) break; ... ; bufpos = 0;" */
+		/* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
 		bufpos -= 128;
 		if (bufpos != 0)
 			break;
@@ -896,3 +901,268 @@
 	}
 	memcpy(resbuf, ctx->hash, sizeof(ctx->hash));
 }
+
+
+/*
+ * The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
+ * Michael Peeters and Gilles Van Assche. For more information, feedback or
+ * questions, please refer to our website: http://keccak.noekeon.org/
+ *
+ * Implementation by Ronny Van Keer,
+ * hereby denoted as "the implementer".
+ *
+ * To the extent possible under law, the implementer has waived all copyright
+ * and related or neighboring rights to the source code in this file.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ *
+ * Busybox modifications (C) Lauri Kasanen, under the GPLv2.
+ */
+
+#if CONFIG_SHA3_SMALL < 0
+# define SHA3_SMALL 0
+#elif CONFIG_SHA3_SMALL > 1
+# define SHA3_SMALL 1
+#else
+# define SHA3_SMALL CONFIG_SHA3_SMALL
+#endif
+
+enum {
+	SHA3_IBLK_BYTES = 72, /* 576 bits / 8 */
+};
+
+/*
+ * In the crypto literature this function is usually called Keccak-f().
+ */
+static void sha3_process_block72(uint64_t *state)
+{
+	enum { NROUNDS = 24 };
+
+	/* Elements should be 64-bit, but top half is always zero or 0x80000000.
+	 * We encode 63rd bits in a separate word below.
+	 * Same is true for 31th bits, which lets us use 16-bit table instead of 64-bit.
+	 * The speed penalty is lost in the noise.
+	 */
+	static const uint16_t IOTA_CONST[NROUNDS] = {
+		0x0001,
+		0x8082,
+		0x808a,
+		0x8000,
+		0x808b,
+		0x0001,
+		0x8081,
+		0x8009,
+		0x008a,
+		0x0088,
+		0x8009,
+		0x000a,
+		0x808b,
+		0x008b,
+		0x8089,
+		0x8003,
+		0x8002,
+		0x0080,
+		0x800a,
+		0x000a,
+		0x8081,
+		0x8080,
+		0x0001,
+		0x8008,
+	};
+	/* bit for CONST[0] is in msb: 0011 0011 0000 0111 1101 1101 */
+	const uint32_t IOTA_CONST_bit63 = (uint32_t)(0x3307dd00);
+	/* bit for CONST[0] is in msb: 0001 0110 0011 1000 0001 1011 */
+	const uint32_t IOTA_CONST_bit31 = (uint32_t)(0x16381b00);
+
+	static const uint8_t ROT_CONST[24] = {
+		1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
+		27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44,
+	};
+	static const uint8_t PI_LANE[24] = {
+		10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
+		15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
+	};
+	/*static const uint8_t MOD5[10] = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, };*/
+
+	unsigned x, y;
+	unsigned round;
+
+	if (BB_BIG_ENDIAN) {
+		for (x = 0; x < 25; x++) {
+			state[x] = SWAP_LE64(state[x]);
+		}
+	}
+
+	for (round = 0; round < NROUNDS; ++round) {
+		/* Theta */
+		{
+			uint64_t BC[10];
+			for (x = 0; x < 5; ++x) {
+				BC[x + 5] = BC[x] = state[x]
+					^ state[x + 5] ^ state[x + 10]
+					^ state[x + 15]	^ state[x + 20];
+			}
+			/* Using 2x5 vector above eliminates the need to use
+			 * BC[MOD5[x+N]] trick below to fetch BC[(x+N) % 5],
+			 * and the code is a bit _smaller_.
+			 */
+			for (x = 0; x < 5; ++x) {
+				uint64_t temp = BC[x + 4] ^ rotl64(BC[x + 1], 1);
+				state[x] ^= temp;
+				state[x + 5] ^= temp;
+				state[x + 10] ^= temp;
+				state[x + 15] ^= temp;
+				state[x + 20] ^= temp;
+			}
+		}
+
+		/* Rho Pi */
+		if (SHA3_SMALL) {
+			uint64_t t1 = state[1];
+			for (x = 0; x < 24; ++x) {
+				uint64_t t0 = state[PI_LANE[x]];
+				state[PI_LANE[x]] = rotl64(t1, ROT_CONST[x]);
+				t1 = t0;
+			}
+		} else {
+			/* Especially large benefit for 32-bit arch (75% faster):
+			 * 64-bit rotations by non-constant usually are SLOW on those.
+			 * We resort to unrolling here.
+			 * This optimizes out PI_LANE[] and ROT_CONST[],
+			 * but generates 300-500 more bytes of code.
+			 */
+			uint64_t t0;
+			uint64_t t1 = state[1];
+#define RhoPi_twice(x) \
+	t0 = state[PI_LANE[x  ]]; \
+	state[PI_LANE[x  ]] = rotl64(t1, ROT_CONST[x  ]); \
+	t1 = state[PI_LANE[x+1]]; \
+	state[PI_LANE[x+1]] = rotl64(t0, ROT_CONST[x+1]);
+			RhoPi_twice(0); RhoPi_twice(2);
+			RhoPi_twice(4); RhoPi_twice(6);
+			RhoPi_twice(8); RhoPi_twice(10);
+			RhoPi_twice(12); RhoPi_twice(14);
+			RhoPi_twice(16); RhoPi_twice(18);
+			RhoPi_twice(20); RhoPi_twice(22);
+#undef RhoPi_twice
+		}
+
+		/* Chi */
+		for (y = 0; y <= 20; y += 5) {
+			uint64_t BC0, BC1, BC2, BC3, BC4;
+			BC0 = state[y + 0];
+			BC1 = state[y + 1];
+			BC2 = state[y + 2];
+			state[y + 0] = BC0 ^ ((~BC1) & BC2);
+			BC3 = state[y + 3];
+			state[y + 1] = BC1 ^ ((~BC2) & BC3);
+			BC4 = state[y + 4];
+			state[y + 2] = BC2 ^ ((~BC3) & BC4);
+			state[y + 3] = BC3 ^ ((~BC4) & BC0);
+			state[y + 4] = BC4 ^ ((~BC0) & BC1);
+		}
+
+		/* Iota */
+		state[0] ^= IOTA_CONST[round]
+			| (uint32_t)((IOTA_CONST_bit31 << round) & 0x80000000)
+			| (uint64_t)((IOTA_CONST_bit63 << round) & 0x80000000) << 32;
+	}
+
+	if (BB_BIG_ENDIAN) {
+		for (x = 0; x < 25; x++) {
+			state[x] = SWAP_LE64(state[x]);
+		}
+	}
+}
+
+void FAST_FUNC sha3_begin(sha3_ctx_t *ctx)
+{
+	memset(ctx, 0, sizeof(*ctx));
+}
+
+void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len)
+{
+#if SHA3_SMALL
+	const uint8_t *data = buffer;
+	unsigned bufpos = ctx->bytes_queued;
+
+	while (1) {
+		unsigned remaining = SHA3_IBLK_BYTES - bufpos;
+		if (remaining > len)
+			remaining = len;
+		len -= remaining;
+		/* XOR data into buffer */
+		while (remaining != 0) {
+			uint8_t *buf = (uint8_t*)ctx->state;
+			buf[bufpos] ^= *data++;
+			bufpos++;
+			remaining--;
+		}
+		/* Clever way to do "if (bufpos != N) break; ... ; bufpos = 0;" */
+		bufpos -= SHA3_IBLK_BYTES;
+		if (bufpos != 0)
+			break;
+		/* Buffer is filled up, process it */
+		sha3_process_block72(ctx->state);
+		/*bufpos = 0; - already is */
+	}
+	ctx->bytes_queued = bufpos + SHA3_IBLK_BYTES;
+#else
+	/* +50 bytes code size, but a bit faster because of long-sized XORs */
+	const uint8_t *data = buffer;
+	unsigned bufpos = ctx->bytes_queued;
+
+	/* If already data in queue, continue queuing first */
+	while (len != 0 && bufpos != 0) {
+		uint8_t *buf = (uint8_t*)ctx->state;
+		buf[bufpos] ^= *data++;
+		len--;
+		bufpos++;
+		if (bufpos == SHA3_IBLK_BYTES) {
+			bufpos = 0;
+			goto do_block;
+		}
+	}
+
+	/* Absorb complete blocks */
+	while (len >= SHA3_IBLK_BYTES) {
+		/* XOR data onto beginning of state[].
+		 * We try to be efficient - operate one word at a time, not byte.
+		 * Careful wrt unaligned access: can't just use "*(long*)data"!
+		 */
+		unsigned count = SHA3_IBLK_BYTES / sizeof(long);
+		long *buf = (long*)ctx->state;
+		do {
+			long v;
+			move_from_unaligned_long(v, (long*)data);
+			*buf++ ^= v;
+			data += sizeof(long);
+		} while (--count);
+		len -= SHA3_IBLK_BYTES;
+ do_block:
+		sha3_process_block72(ctx->state);
+	}
+
+	/* Queue remaining data bytes */
+	while (len != 0) {
+		uint8_t *buf = (uint8_t*)ctx->state;
+		buf[bufpos] ^= *data++;
+		bufpos++;
+		len--;
+	}
+
+	ctx->bytes_queued = bufpos;
+#endif
+}
+
+void FAST_FUNC sha3_end(sha3_ctx_t *ctx, void *resbuf)
+{
+	/* Padding */
+	uint8_t *buf = (uint8_t*)ctx->state;
+	buf[ctx->bytes_queued]   ^= 1;
+	buf[SHA3_IBLK_BYTES - 1] ^= 0x80;
+
+	sha3_process_block72(ctx->state);
+
+	/* Output */
+	memcpy(resbuf, ctx->state, 64);
+}
diff --git a/libbb/inet_common.c b/libbb/inet_common.c
index 7208db9..0f4fca1 100644
--- a/libbb/inet_common.c
+++ b/libbb/inet_common.c
@@ -97,7 +97,7 @@
 	if (s_in->sin_family != AF_INET) {
 #ifdef DEBUG
 		bb_error_msg("rresolve: unsupported address family %d!",
-				  s_in->sin_family);
+				s_in->sin_family);
 #endif
 		errno = EAFNOSUPPORT;
 		return NULL;
@@ -195,7 +195,7 @@
 	if (sin6->sin6_family != AF_INET6) {
 #ifdef DEBUG
 		bb_error_msg("rresolve: unsupported address family %d!",
-				  sin6->sin6_family);
+				sin6->sin6_family);
 #endif
 		errno = EAFNOSUPPORT;
 		return NULL;
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 69a1e7d..0da625f 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -2528,9 +2528,9 @@
 					/* Delete word forward */
 					int nc, sc = cursor;
 					ctrl_right();
-					nc = cursor;
-					input_backward(cursor - sc);
-					while (--nc >= cursor)
+					nc = cursor - sc;
+					input_backward(nc);
+					while (--nc >= 0)
 						input_delete(1);
 					break;
 				}
@@ -2730,7 +2730,8 @@
 {
 	fputs(prompt, stdout);
 	fflush_all();
-	fgets(command, maxsize, stdin);
+	if (!fgets(command, maxsize, stdin))
+		return -1;
 	return strlen(command);
 }
 
diff --git a/libbb/loop.c b/libbb/loop.c
index b3a5208..823fba0 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -150,9 +150,9 @@
 			}
 
 		/* If this block device already set up right, re-use it.
-		   (Yes this is racy, but associating two loop devices with the same
-		   file isn't pretty either.  In general, mounting the same file twice
-		   without using losetup manually is problematic.)
+		 * (Yes this is racy, but associating two loop devices with the same
+		 * file isn't pretty either.  In general, mounting the same file twice
+		 * without using losetup manually is problematic.)
 		 */
 		} else
 		if (strcmp(file, (char *)loopinfo.lo_file_name) != 0
diff --git a/libbb/missing_syscalls.c b/libbb/missing_syscalls.c
new file mode 100644
index 0000000..dd430e3
--- /dev/null
+++ b/libbb/missing_syscalls.c
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2012, Denys Vlasenko
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
+ */
+
+//kbuild:lib-y += missing_syscalls.o
+
+/*#include <linux/timex.h> - for struct timex, but may collide with <time.h> */
+#include <sys/syscall.h>
+#include "libbb.h"
+
+#if defined(ANDROID) || defined(__ANDROID__)
+pid_t getsid(pid_t pid)
+{
+	return syscall(__NR_getsid, pid);
+}
+
+int stime(const time_t *t)
+{
+	struct timeval tv;
+	tv.tv_sec = *t;
+	tv.tv_usec = 0;
+	return settimeofday(&tv, NULL);
+}
+
+int sethostname(const char *name, size_t len)
+{
+	return syscall(__NR_sethostname, name, len);
+}
+
+struct timex;
+int adjtimex(struct timex *buf)
+{
+	return syscall(__NR_adjtimex, buf);
+}
+
+int pivot_root(const char *new_root, const char *put_old)
+{
+	return syscall(__NR_pivot_root, new_root, put_old);
+}
+#endif
diff --git a/libbb/procps.c b/libbb/procps.c
index 40587db..5b68d34 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -180,7 +180,7 @@
 
 #if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
 int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
-		      void (*cb)(struct smaprec *, void *), void *data)
+		void (*cb)(struct smaprec *, void *), void *data)
 {
 	FILE *file;
 	struct smaprec currec;
@@ -425,7 +425,7 @@
 			if (n < 11)
 				continue; /* bogus data, get next /proc/XXX */
 # if ENABLE_FEATURE_TOP_SMP_PROCESS
-			if (n < 11+15)
+			if (n == 11)
 				sp->last_seen_on_cpu = 0;
 # endif
 
@@ -583,6 +583,8 @@
 		buf[sz] = '\0';
 		while (--sz >= 0 && buf[sz] == '\0')
 			continue;
+		/* Prevent basename("process foo/bar") = "bar" */
+		strchrnul(buf, ' ')[0] = '\0';
 		base = bb_basename(buf); /* before we replace argv0's NUL with space */
 		while (sz >= 0) {
 			if ((unsigned char)(buf[sz]) < ' ')
diff --git a/libbb/read_key.c b/libbb/read_key.c
index 8d72d2a..ace23de 100644
--- a/libbb/read_key.c
+++ b/libbb/read_key.c
@@ -15,7 +15,10 @@
 	const char *seq;
 	int n;
 
-	/* Known escape sequences for cursor and function keys */
+	/* Known escape sequences for cursor and function keys.
+	 * See "Xterm Control Sequences"
+	 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
+	 */
 	static const char esccmds[] ALIGN1 = {
 		'O','A'        |0x80,KEYCODE_UP      ,
 		'O','B'        |0x80,KEYCODE_DOWN    ,
@@ -44,6 +47,8 @@
 		/* ESC [ 1 ; 4 x, where x = A/B/C/D: Alt-Shift-<arrow> */
 		/* ESC [ 1 ; 5 x, where x = A/B/C/D: Ctrl-<arrow> - implemented below */
 		/* ESC [ 1 ; 6 x, where x = A/B/C/D: Ctrl-Shift-<arrow> */
+		/* ESC [ 1 ; 7 x, where x = A/B/C/D: Ctrl-Alt-<arrow> */
+		/* ESC [ 1 ; 8 x, where x = A/B/C/D: Ctrl-Alt-Shift-<arrow> */
 		'[','H'        |0x80,KEYCODE_HOME    , /* xterm */
 		'[','F'        |0x80,KEYCODE_END     , /* xterm */
 		/* [ESC] ESC [ [2] H - [Alt-][Shift-]Home (End similarly?) */
@@ -64,10 +69,10 @@
 		'[','7','~'    |0x80,KEYCODE_HOME    , /* vt100? linux vt? or what? */
 		'[','8','~'    |0x80,KEYCODE_END     , /* vt100? linux vt? or what? */
 #if 0
-		'[','1','1','~'|0x80,KEYCODE_FUN1    ,
-		'[','1','2','~'|0x80,KEYCODE_FUN2    ,
-		'[','1','3','~'|0x80,KEYCODE_FUN3    ,
-		'[','1','4','~'|0x80,KEYCODE_FUN4    ,
+		'[','1','1','~'|0x80,KEYCODE_FUN1    , /* old xterm, deprecated by ESC O P */
+		'[','1','2','~'|0x80,KEYCODE_FUN2    , /* old xterm... */
+		'[','1','3','~'|0x80,KEYCODE_FUN3    , /* old xterm... */
+		'[','1','4','~'|0x80,KEYCODE_FUN4    , /* old xterm... */
 		'[','1','5','~'|0x80,KEYCODE_FUN5    ,
 		/* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
 		'[','1','7','~'|0x80,KEYCODE_FUN6    ,
diff --git a/libbb/remove_file.c b/libbb/remove_file.c
index c6531a0..5b75f7f 100644
--- a/libbb/remove_file.c
+++ b/libbb/remove_file.c
@@ -33,7 +33,7 @@
 		int status = 0;
 
 		if (!(flags & FILEUTILS_RECUR)) {
-			bb_error_msg("%s: is a directory", path);
+			bb_error_msg("'%s' is a directory", path);
 			return -1;
 		}
 
diff --git a/libbb/safe_gethostname.c b/libbb/safe_gethostname.c
index bdb9896..cac99ae 100644
--- a/libbb/safe_gethostname.c
+++ b/libbb/safe_gethostname.c
@@ -50,25 +50,3 @@
 	uname(&uts);
 	return xstrndup(!uts.nodename[0] ? "?" : uts.nodename, sizeof(uts.nodename));
 }
-
-/*
- * On success return the current malloced and NUL terminated domainname.
- * On error return malloced and NUL terminated string "?".
- * This is an illegal first character for a domainname.
- * The returned malloced string must be freed by the caller.
- */
-char* FAST_FUNC safe_getdomainname(void)
-{
-#if defined(__linux__)
-/* The field domainname of struct utsname is Linux specific. */
-	struct utsname uts;
-	uname(&uts);
-	return xstrndup(!uts.domainname[0] ? "?" : uts.domainname, sizeof(uts.domainname));
-#else
-	/* We really don't care about people with domain names wider than most screens */
-	char buf[256];
-	int r = getdomainname(buf, sizeof(buf));
-	buf[sizeof(buf)-1] = '\0';
-	return xstrdup(r < 0 ? "?" : buf);
-#endif
-}
diff --git a/libbb/selinux_common.c b/libbb/selinux_common.c
index 62910e2..c258555 100644
--- a/libbb/selinux_common.c
+++ b/libbb/selinux_common.c
@@ -10,7 +10,7 @@
 #include <selinux/context.h>
 
 context_t FAST_FUNC set_security_context_component(security_context_t cur_context,
-					 char *user, char *role, char *type, char *range)
+			char *user, char *role, char *type, char *range)
 {
 	context_t con = context_new(cur_context);
 	if (!con)
diff --git a/libbb/signals.c b/libbb/signals.c
index cdc37b1..5651247 100644
--- a/libbb/signals.c
+++ b/libbb/signals.c
@@ -39,7 +39,7 @@
 
 	while (sigs) {
 		if (sigs & bit) {
-			sigs &= ~bit;
+			sigs -= bit;
 			signal(sig_no, f);
 		}
 		sig_no++;
@@ -60,7 +60,7 @@
 
 	while (sigs) {
 		if (sigs & bit) {
-			sigs &= ~bit;
+			sigs -= bit;
 			sigaction_set(sig_no, &sa);
 		}
 		sig_no++;
@@ -97,7 +97,7 @@
 	signal(sig, SIG_DFL);
 	sig_unblock(sig);
 	raise(sig);
-	_exit(EXIT_FAILURE); /* Should not reach it */
+	_exit(sig | 128); /* Should not reach it */
 }
 
 void FAST_FUNC signal_SA_RESTART_empty_mask(int sig, void (*handler)(int))
diff --git a/libbb/xatonum_template.c b/libbb/xatonum_template.c
index 029f662..e047198 100644
--- a/libbb/xatonum_template.c
+++ b/libbb/xatonum_template.c
@@ -59,7 +59,7 @@
 	}
 
 	/* Note: trailing space is an error.
-	   It would be easy enough to allow though if desired. */
+	 * It would be easy enough to allow though if desired. */
 	if (*e)
 		goto inval;
  chk_range: