suppress glibc "use sysmacros.h for major" warning
[oweals/busybox.git] / include / libbb.h
index 63d0419576afe73d694bbcb52db86f23ace94e00..b1fec01577fb0608fffcbca00c772abe445e0364 100644 (file)
@@ -20,6 +20,7 @@
 #include <netdb.h>
 #include <setjmp.h>
 #include <signal.h>
+#include <paths.h>
 #if defined __UCLIBC__ /* TODO: and glibc? */
 /* use inlined versions of these: */
 # define sigfillset(s)    __sigfillset(s)
 #include <stdarg.h>
 #include <stddef.h>
 #include <string.h>
-#include <sys/poll.h>
+/* There are two incompatible basename's, let's not use them! */
+/* See the dirname/basename man page for details */
+#include <libgen.h> /* dirname,basename */
+#undef basename
+#define basename dont_use_basename
+#include <poll.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#ifndef major
+#if !defined(major) || defined(__GLIBC__)
 # include <sys/sysmacros.h>
 #endif
 #include <sys/wait.h>
 #include <termios.h>
 #include <time.h>
 #include <sys/param.h>
+#include <pwd.h>
+#include <grp.h>
+#if ENABLE_FEATURE_SHADOWPASSWDS
+# if !ENABLE_USE_BB_SHADOW
+/* If using busybox's shadow implementation, do not include the shadow.h
+ * header as the toolchain may not provide it at all.
+ */
+#  include <shadow.h>
+# endif
+#endif
+#if defined(ANDROID) || defined(__ANDROID__)
+# define endpwent() ((void)0)
+# define endgrent() ((void)0)
+#endif
 #ifdef HAVE_MNTENT_H
 # include <mntent.h>
 #endif
 #if ENABLE_SELINUX
 # include <selinux/selinux.h>
 # include <selinux/context.h>
-# include <selinux/flask.h>
-# include <selinux/av_permissions.h>
 #endif
 #if ENABLE_FEATURE_UTMP
-# include <utmp.h>
+# if defined __UCLIBC__ && ( \
+    (UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 32) \
+     && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 34) \
+     && defined __UCLIBC_HAS_UTMPX__ \
+    ) || ( \
+        UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 34) \
+       ) \
+  )
+#  include <utmpx.h>
+# elif defined __UCLIBC__
+#  include <utmp.h>
+#  define utmpx utmp
+#  define setutxent setutent
+#  define endutxent endutent
+#  define getutxent getutent
+#  define getutxid getutid
+#  define getutxline getutline
+#  define pututxline pututline
+#  define utmpxname utmpname
+#  define updwtmpx updwtmp
+#  define _PATH_UTMPX _PATH_UTMP
+# else
+#  include <utmp.h>
+#  include <utmpx.h>
+#  if defined _PATH_UTMP && !defined _PATH_UTMPX
+#   define _PATH_UTMPX _PATH_UTMP
+#  endif
+# endif
 #endif
 #if ENABLE_LOCALE_SUPPORT
 # include <locale.h>
 #ifdef DMALLOC
 # include <dmalloc.h>
 #endif
-#include <pwd.h>
-#include <grp.h>
-#if ENABLE_FEATURE_SHADOWPASSWDS
-# if !ENABLE_USE_BB_SHADOW
-/* If using busybox's shadow implementation, do not include the shadow.h
- * header as the toolchain may not provide it at all.
- */
-#  include <shadow.h>
-# endif
-#endif
 /* Just in case libc doesn't define some of these... */
 #ifndef _PATH_PASSWD
 #define _PATH_PASSWD  "/etc/passwd"
 # include <netinet/in.h>
 #else
 # include <arpa/inet.h>
-# if !defined(__socklen_t_defined) && !defined(_SOCKLEN_T_DECLARED)
-/* We #define socklen_t *after* includes, otherwise we get
- * typedef redefinition errors from system headers
- * (in case "is it defined already" detection above failed)
- */
-#  define socklen_t bb_socklen_t
-   typedef unsigned socklen_t;
-# endif
+//This breaks on bionic:
+//# if !defined(__socklen_t_defined) && !defined(_SOCKLEN_T_DECLARED)
+///* We #define socklen_t *after* includes, otherwise we get
+// * typedef redefinition errors from system headers
+// * (in case "is it defined already" detection above failed)
+// */
+//#  define socklen_t bb_socklen_t
+//   typedef unsigned socklen_t;
+//# endif
+//if this is still needed, add a fix along the lines of
+//  ifdef SPECIFIC_BROKEN_LIBC_CHECK / typedef socklen_t / endif
+//in platform.h instead!
 #endif
 #ifndef HAVE_CLEARENV
 # define clearenv() do { if (environ) environ[0] = NULL; } while (0)
@@ -133,12 +172,6 @@ int vdprintf(int d, const char *format, va_list ap);
 #endif
 /* klogctl is in libc's klog.h, but we cheat and not #include that */
 int klogctl(int type, char *b, int len);
-/* This is declared here rather than #including <libgen.h> in order to avoid
- * confusing the two versions of basename.  See the dirname/basename man page
- * for details. */
-#if !defined __FreeBSD__
-char *dirname(char *path);
-#endif
 #ifndef PATH_MAX
 # define PATH_MAX 256
 #endif
@@ -150,24 +183,33 @@ char *dirname(char *path);
 /* Busybox does not use threads, we can speed up stdio. */
 #ifdef HAVE_UNLOCKED_STDIO
 # undef  getc
-# define getc(stream) getc_unlocked(stream)
+# define getc(stream)   getc_unlocked(stream)
 # undef  getchar
-# define getchar() getchar_unlocked()
+# define getchar()      getchar_unlocked()
 # undef  putc
-# define putc(c, stream) putc_unlocked(c, stream)
+# define putc(c,stream) putc_unlocked(c,stream)
 # undef  putchar
-# define putchar(c) putchar_unlocked(c)
+# define putchar(c)     putchar_unlocked(c)
 # undef  fgetc
-# define fgetc(stream) getc_unlocked(stream)
+# define fgetc(stream)  getc_unlocked(stream)
 # undef  fputc
-# define fputc(c, stream) putc_unlocked(c, stream)
+# define fputc(c,stream) putc_unlocked(c,stream)
 #endif
 /* Above functions are required by POSIX.1-2008, below ones are extensions */
 #ifdef HAVE_UNLOCKED_LINE_OPS
 # undef  fgets
-# define fgets(s, n, stream) fgets_unlocked(s, n, stream)
+# define fgets(s,n,stream) fgets_unlocked(s,n,stream)
 # undef  fputs
-# define fputs(s, stream) fputs_unlocked(s, stream)
+# define fputs(s,stream) fputs_unlocked(s,stream)
+/* musl <= 1.1.15 does not support fflush_unlocked(NULL) */
+//# undef  fflush
+//# define fflush(stream) fflush_unlocked(stream)
+# undef  feof
+# define feof(stream)   feof_unlocked(stream)
+# undef  ferror
+# define ferror(stream) ferror_unlocked(stream)
+# undef  fileno
+# define fileno(stream) fileno_unlocked(stream)
 #endif
 
 
@@ -212,7 +254,7 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
 # if ULONG_MAX > 0xffffffff
 /* "long" is long enough on this system */
 typedef unsigned long uoff_t;
-#  define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
+#  define XATOOFF(a) xatoul_range((a), 0, LONG_MAX)
 /* usage: sz = BB_STRTOOFF(s, NULL, 10); if (errno || sz < 0) die(); */
 #  define BB_STRTOOFF bb_strtoul
 #  define STRTOOFF strtoul
@@ -221,7 +263,7 @@ typedef unsigned long uoff_t;
 # else
 /* "long" is too short, need "long long" */
 typedef unsigned long long uoff_t;
-#  define XATOOFF(a) xatoull_range(a, 0, LLONG_MAX)
+#  define XATOOFF(a) xatoull_range((a), 0, LLONG_MAX)
 #  define BB_STRTOOFF bb_strtoull
 #  define STRTOOFF strtoull
 #  define OFF_FMT "ll"
@@ -238,7 +280,7 @@ typedef unsigned long uoff_t;
 #  define OFF_FMT "l"
 # else
 typedef unsigned long uoff_t;
-#  define XATOOFF(a) xatoul_range(a, 0, LONG_MAX)
+#  define XATOOFF(a) xatoul_range((a), 0, LONG_MAX)
 #  define BB_STRTOOFF bb_strtoul
 #  define STRTOOFF strtol
 #  define OFF_FMT "l"
@@ -246,6 +288,12 @@ typedef unsigned long uoff_t;
 #endif
 /* scary. better ideas? (but do *test* them first!) */
 #define OFF_T_MAX  ((off_t)~((off_t)1 << (sizeof(off_t)*8-1)))
+/* Users report bionic to use 32-bit off_t even if LARGEFILE support is requested.
+ * We misdetected that. Don't let it build:
+ */
+struct BUG_off_t_size_is_misdetected {
+       char BUG_off_t_size_is_misdetected[sizeof(off_t) == sizeof(uoff_t) ? 1 : -1];
+};
 
 /* Some useful definitions */
 #undef FALSE
@@ -255,13 +303,6 @@ typedef unsigned long uoff_t;
 #undef SKIP
 #define SKIP   ((int) 2)
 
-/* for mtab.c */
-#define MTAB_GETMOUNTPT '1'
-#define MTAB_GETDEVICE  '2'
-
-#define BUF_SIZE        8192
-#define EXPAND_ALLOC    1024
-
 /* Macros for min/max.  */
 #ifndef MIN
 #define MIN(a,b) (((a)<(b))?(a):(b))
@@ -316,8 +357,8 @@ extern char *strrstr(const char *haystack, const char *needle) FAST_FUNC;
 
 //TODO: supply a pointer to char[11] buffer (avoid statics)?
 extern const char *bb_mode_string(mode_t mode) FAST_FUNC;
-extern int is_directory(const char *name, int followLinks, struct stat *statBuf) FAST_FUNC;
-enum { /* DO NOT CHANGE THESE VALUES!  cp.c, mv.c, install.c depend on them. */
+extern int is_directory(const char *name, int followLinks) FAST_FUNC;
+enum { /* cp.c, mv.c, install.c depend on these values. CAREFUL when changing them! */
        FILEUTILS_PRESERVE_STATUS = 1 << 0, /* -p */
        FILEUTILS_DEREFERENCE     = 1 << 1, /* !-d */
        FILEUTILS_RECUR           = 1 << 2, /* -R */
@@ -327,12 +368,25 @@ enum {    /* DO NOT CHANGE THESE VALUES!  cp.c, mv.c, install.c depend on them. */
        FILEUTILS_MAKE_SOFTLINK   = 1 << 6, /* -s */
        FILEUTILS_DEREF_SOFTLINK  = 1 << 7, /* -L */
        FILEUTILS_DEREFERENCE_L0  = 1 << 8, /* -H */
+       /* -a = -pdR (mapped in cp.c) */
+       /* -r = -dR  (mapped in cp.c) */
+       /* -P = -d   (mapped in cp.c) */
+       FILEUTILS_VERBOSE         = (1 << 12) * ENABLE_FEATURE_VERBOSE, /* -v */
+       FILEUTILS_UPDATE          = 1 << 13, /* -u */
+#if ENABLE_SELINUX
+       FILEUTILS_PRESERVE_SECURITY_CONTEXT = 1 << 14, /* -c */
+#endif
+       FILEUTILS_RMDEST          = 1 << (15 - !ENABLE_SELINUX), /* --remove-destination */
+       /*
+        * Hole. cp may have some bits set here,
+        * they should not affect remove_file()/copy_file()
+        */
 #if ENABLE_SELINUX
-       FILEUTILS_PRESERVE_SECURITY_CONTEXT = 1 << 9, /* -c */
-       FILEUTILS_SET_SECURITY_CONTEXT = 1 << 10,
+       FILEUTILS_SET_SECURITY_CONTEXT = 1 << 30,
 #endif
+       FILEUTILS_IGNORE_CHMOD_ERR = 1 << 31,
 };
-#define FILEUTILS_CP_OPTSTR "pdRfilsLH" IF_SELINUX("c")
+#define FILEUTILS_CP_OPTSTR "pdRfilsLHarPvu" IF_SELINUX("c")
 extern int remove_file(const char *path, int flags) FAST_FUNC;
 /* NB: without FILEUTILS_RECUR in flags, it will basically "cat"
  * the source, not copy (unless "source" is a directory).
@@ -383,9 +437,12 @@ char *bb_get_last_path_component_nostrip(const char *path) FAST_FUNC;
 const char *bb_basename(const char *name) FAST_FUNC;
 /* NB: can violate const-ness (similarly to strchr) */
 char *last_char_is(const char *s, int c) FAST_FUNC;
+const char* endofname(const char *name) FAST_FUNC;
+char *is_prefixed_with(const char *string, const char *key) FAST_FUNC;
+char *is_suffixed_with(const char *string, const char *key) FAST_FUNC;
 
-void ndelay_on(int fd) FAST_FUNC;
-void ndelay_off(int fd) FAST_FUNC;
+int ndelay_on(int fd) FAST_FUNC;
+int ndelay_off(int fd) FAST_FUNC;
 void close_on_exec_on(int fd) FAST_FUNC;
 void xdup2(int, int) FAST_FUNC;
 void xmove_fd(int, int) FAST_FUNC;
@@ -457,7 +514,10 @@ void record_signo(int signo); /* not FAST_FUNC! */
 
 void xsetgid(gid_t gid) FAST_FUNC;
 void xsetuid(uid_t uid) FAST_FUNC;
+void xsetegid(gid_t egid) FAST_FUNC;
+void xseteuid(uid_t euid) FAST_FUNC;
 void xchdir(const char *path) FAST_FUNC;
+void xfchdir(int fd) FAST_FUNC;
 void xchroot(const char *path) FAST_FUNC;
 void xsetenv(const char *key, const char *value) FAST_FUNC;
 void bb_unsetenv(const char *key) FAST_FUNC;
@@ -465,11 +525,12 @@ void bb_unsetenv_and_free(char *key) FAST_FUNC;
 void xunlink(const char *pathname) FAST_FUNC;
 void xstat(const char *pathname, struct stat *buf) FAST_FUNC;
 void xfstat(int fd, struct stat *buf, const char *errmsg) FAST_FUNC;
+int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC;
+int open_or_warn(const char *pathname, int flags) FAST_FUNC;
+int xopen3(const char *pathname, int flags, int mode) FAST_FUNC;
 int xopen(const char *pathname, int flags) FAST_FUNC;
 int xopen_nonblocking(const char *pathname) FAST_FUNC;
-int xopen3(const char *pathname, int flags, int mode) FAST_FUNC;
-int open_or_warn(const char *pathname, int flags) FAST_FUNC;
-int open3_or_warn(const char *pathname, int flags, int mode) FAST_FUNC;
+int xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g) FAST_FUNC;
 int open_or_warn_stdin(const char *pathname) FAST_FUNC;
 int xopen_stdin(const char *pathname) FAST_FUNC;
 void xrename(const char *oldpath, const char *newpath) FAST_FUNC;
@@ -479,9 +540,9 @@ int xmkstemp(char *template) FAST_FUNC;
 off_t fdlength(int fd) FAST_FUNC;
 
 uoff_t FAST_FUNC get_volume_size_in_bytes(int fd,
-                const char *override,
-                unsigned override_units,
-                int extend);
+               const char *override,
+               unsigned override_units,
+               int extend);
 
 void xpipe(int filedes[2]) FAST_FUNC;
 /* In this form code with pipes is much more readable */
@@ -519,7 +580,8 @@ struct BUG_too_small {
 
 void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC;
 time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC;
-
+char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
+char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
 
 int xsocket(int domain, int type, int protocol) FAST_FUNC;
 void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC;
@@ -527,6 +589,11 @@ void xlisten(int s, int backlog) FAST_FUNC;
 void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) FAST_FUNC;
 ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
                                socklen_t tolen) FAST_FUNC;
+
+int setsockopt_int(int fd, int level, int optname, int optval) FAST_FUNC;
+int setsockopt_1(int fd, int level, int optname) FAST_FUNC;
+int setsockopt_SOL_SOCKET_int(int fd, int optname, int optval) FAST_FUNC;
+int setsockopt_SOL_SOCKET_1(int fd, int optname) FAST_FUNC;
 /* SO_REUSEADDR allows a server to rebind to an address that is already
  * "in use" by old connections to e.g. previous server instance which is
  * killed or crashed. Without it bind will fail until all such connections
@@ -534,6 +601,7 @@ ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
  * regardless of SO_REUSEADDR (unlike some other flavors of Unix).
  * Turn it on before you call bind(). */
 void setsockopt_reuseaddr(int fd) FAST_FUNC; /* On Linux this never fails. */
+int setsockopt_keepalive(int fd) FAST_FUNC;
 int setsockopt_broadcast(int fd) FAST_FUNC;
 int setsockopt_bindtodevice(int fd, const char *iface) FAST_FUNC;
 /* NB: returns port in host byte order */
@@ -566,12 +634,7 @@ enum {
  * and if kernel doesn't support it, fall back to IPv4.
  * This is useful if you plan to bind to resulting local lsa.
  */
-#if ENABLE_FEATURE_IPV6
 int xsocket_type(len_and_sockaddr **lsap, int af, int sock_type) FAST_FUNC;
-#else
-int xsocket_type(len_and_sockaddr **lsap, int sock_type) FAST_FUNC;
-#define xsocket_type(lsap, af, sock_type) xsocket_type((lsap), (sock_type))
-#endif
 int xsocket_stream(len_and_sockaddr **lsap) FAST_FUNC;
 /* Create server socket bound to bindaddr:port. bindaddr can be NULL,
  * numeric IP ("N.N.N.N") or numeric IPv6 address,
@@ -640,18 +703,23 @@ ssize_t recv_from_to(int fd, void *buf, size_t len, int flags,
                struct sockaddr *to,
                socklen_t sa_size) FAST_FUNC;
 
+uint16_t inet_cksum(uint16_t *addr, int len) FAST_FUNC;
 
 char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC;
 char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC;
+void *xmemdup(const void *s, int n) FAST_FUNC RETURNS_MALLOC;
 void overlapping_strcpy(char *dst, const char *src) FAST_FUNC;
 char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC;
 char *strncpy_IFNAMSIZ(char *dst, const char *src) FAST_FUNC;
+unsigned count_strstr(const char *str, const char *sub) FAST_FUNC;
+char *xmalloc_substitute_string(const char *src, int count, const char *sub, const char *repl) FAST_FUNC;
 /* Guaranteed to NOT be a macro (smallest code). Saves nearly 2k on uclibc.
  * But potentially slow, don't use in one-billion-times loops */
 int bb_putchar(int ch) FAST_FUNC;
 /* Note: does not use stdio, writes to fd 2 directly */
 int bb_putchar_stderr(char ch) FAST_FUNC;
 char *xasprintf(const char *format, ...) __attribute__ ((format(printf, 1, 2))) FAST_FUNC RETURNS_MALLOC;
+char *auto_string(char *str) FAST_FUNC;
 // gcc-4.1.1 still isn't good enough at optimizing it
 // (+200 bytes compared to macro)
 //static ALWAYS_INLINE
@@ -677,6 +745,13 @@ const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str);
  * else it is printed as-is (except for ch = 0x9b) */
 enum { PRINTABLE_META = 0x100 };
 void fputc_printable(int ch, FILE *file) FAST_FUNC;
+/* Return a string that is the printable representation of character ch.
+ * Buffer must hold at least four characters. */
+enum {
+       VISIBLE_ENDLINE   = 1 << 0,
+       VISIBLE_SHOW_TABS = 1 << 1,
+};
+void visible(unsigned ch, char *buf, int flags) FAST_FUNC;
 
 /* dmalloc will redefine these to it's own implementation. It is safe
  * to have the prototypes here unconditionally.  */
@@ -684,17 +759,20 @@ void *malloc_or_warn(size_t size) FAST_FUNC RETURNS_MALLOC;
 void *xmalloc(size_t size) FAST_FUNC RETURNS_MALLOC;
 void *xzalloc(size_t size) FAST_FUNC RETURNS_MALLOC;
 void *xrealloc(void *old, size_t size) FAST_FUNC;
-/* After xrealloc_vector(v, 4, idx) it's ok to use
+/* After v = xrealloc_vector(v, SHIFT, idx) it's ok to use
  * at least v[idx] and v[idx+1], for all idx values.
- * shift specifies how many new elements are added (1: 2, 2: 4... 8: 256...)
- * when all elements are used up. New elements are zeroed out. */
+ * SHIFT specifies how many new elements are added (1:2, 2:4, ..., 8:256...)
+ * when all elements are used up. New elements are zeroed out.
+ * xrealloc_vector(v, SHIFT, idx) *MUST* be called with consecutive IDXs -
+ * skipping an index is a bad bug - it may miss a realloc!
+ */
 #define xrealloc_vector(vector, shift, idx) \
        xrealloc_vector_helper((vector), (sizeof((vector)[0]) << 8) + (shift), (idx))
 void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) FAST_FUNC;
 
 
 extern ssize_t safe_read(int fd, void *buf, size_t count) FAST_FUNC;
-extern ssize_t nonblock_immune_read(int fd, void *buf, size_t count, int loop_on_EINTR) FAST_FUNC;
+extern ssize_t nonblock_immune_read(int fd, void *buf, size_t count) FAST_FUNC;
 // NB: will return short read on error, not -1,
 // if some data was read before error occurred
 extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC;
@@ -712,18 +790,37 @@ extern void *xmalloc_read(int fd, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
 extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
 /* Never returns NULL */
 extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
-/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
-#if ENABLE_FEATURE_SEAMLESS_LZMA \
- || ENABLE_FEATURE_SEAMLESS_BZ2 \
- || ENABLE_FEATURE_SEAMLESS_GZ \
- /* || ENABLE_FEATURE_SEAMLESS_Z */
-extern void setup_unzip_on_fd(int fd /*, int fail_if_not_detected*/) FAST_FUNC;
+
+#if defined(ARG_MAX) && (ARG_MAX >= 60*1024 || !defined(_SC_ARG_MAX))
+/* Use _constant_ maximum if: defined && (big enough || no variable one exists) */
+# define bb_arg_max() ((unsigned)ARG_MAX)
+#elif defined(_SC_ARG_MAX)
+/* Else use variable one (a bit more expensive) */
+unsigned bb_arg_max(void) FAST_FUNC;
 #else
-# define setup_unzip_on_fd(...) ((void)0)
+/* If all else fails */
+# define bb_arg_max() ((unsigned)(32 * 1024))
 #endif
+unsigned bb_clk_tck(void) FAST_FUNC;
+
+#define SEAMLESS_COMPRESSION (0 \
+ || ENABLE_FEATURE_SEAMLESS_XZ \
+ || ENABLE_FEATURE_SEAMLESS_LZMA \
+ || ENABLE_FEATURE_SEAMLESS_BZ2 \
+ || ENABLE_FEATURE_SEAMLESS_GZ \
+ || ENABLE_FEATURE_SEAMLESS_Z)
+
+#if SEAMLESS_COMPRESSION
+/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
+extern int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC;
 /* Autodetects .gz etc */
-extern int open_zipped(const char *fname) FAST_FUNC;
+extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC;
 extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
+#else
+# define setup_unzip_on_fd(...) (0)
+# define open_zipped(fname, fail_if_not_compressed)  open((fname), O_RDONLY);
+# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p))
+#endif
 
 extern ssize_t safe_write(int fd, const void *buf, size_t count) FAST_FUNC;
 // NB: will return short write on error, not -1,
@@ -788,7 +885,6 @@ void qsort_string_vector(char **sv, unsigned count) FAST_FUNC;
 int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout_ms) FAST_FUNC;
 
 char *safe_gethostname(void) FAST_FUNC;
-char *safe_getdomainname(void) FAST_FUNC;
 
 /* Convert each alpha char in str to lower-case */
 char* str_tolower(char *str) FAST_FUNC;
@@ -799,8 +895,8 @@ char *itoa(int n) FAST_FUNC;
 char *utoa_to_buf(unsigned n, char *buf, unsigned buflen) FAST_FUNC;
 char *itoa_to_buf(int n, char *buf, unsigned buflen) FAST_FUNC;
 /* Intelligent formatters of bignums */
-void smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale) FAST_FUNC;
-void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
+char *smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale) FAST_FUNC;
+char *smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
 /* If block_size == 0, display size without fractional part,
  * else display (size * block_size) with one decimal digit.
  * If display_unit == 0, show value no bigger than 1024 with suffix (K,M,G...),
@@ -811,9 +907,9 @@ void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_F
 const char *make_human_readable_str(unsigned long long size,
                unsigned long block_size, unsigned long display_unit) FAST_FUNC;
 /* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */
-char *bin2hex(char *buf, const char *cp, int count) FAST_FUNC;
+char *bin2hex(char *dst, const char *src, int count) FAST_FUNC;
 /* Reverse */
-char* hex2bin(char *dst, const char *str, int count) FAST_FUNC;
+char* hex2bin(char *dst, const char *src, int count) FAST_FUNC;
 
 /* Generate a UUID */
 void generate_uuid(uint8_t *buf) FAST_FUNC;
@@ -823,6 +919,11 @@ struct suffix_mult {
        char suffix[4];
        unsigned mult;
 };
+extern const struct suffix_mult bkm_suffixes[];
+#define km_suffixes (bkm_suffixes + 1)
+extern const struct suffix_mult cwbkMG_suffixes[];
+#define kMG_suffixes (cwbkMG_suffixes + 3)
+
 #include "xatonum.h"
 /* Specialized: */
 
@@ -846,14 +947,13 @@ long xuname2uid(const char *name) FAST_FUNC;
 long xgroup2gid(const char *name) FAST_FUNC;
 /* wrapper: allows string to contain numeric uid or gid */
 unsigned long get_ug_id(const char *s, long FAST_FUNC (*xname2id)(const char *)) FAST_FUNC;
-/* from chpst. Does not die, returns 0 on failure */
 struct bb_uidgid_t {
        uid_t uid;
        gid_t gid;
 };
-/* always sets uid and gid */
-int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok) FAST_FUNC;
-/* always sets uid and gid, allows numeric; exits on failure */
+/* always sets uid and gid; returns 0 on failure */
+int get_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC;
+/* always sets uid and gid; exits on failure */
 void xget_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC;
 /* chown-like handling of "user[:[group]" */
 void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group) FAST_FUNC;
@@ -882,15 +982,17 @@ void die_if_bad_username(const char* name) FAST_FUNC;
 #if ENABLE_FEATURE_UTMP
 void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);
 void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);
+void FAST_FUNC update_utmp_DEAD_PROCESS(pid_t pid);
 #else
 # define write_new_utmp(pid, new_type, tty_name, username, hostname) ((void)0)
 # define update_utmp(pid, new_type, tty_name, username, hostname) ((void)0)
+# define update_utmp_DEAD_PROCESS(pid) ((void)0)
 #endif
 
 
-int execable_file(const char *name) FAST_FUNC;
-char *find_execable(const char *filename, char **PATHp) FAST_FUNC;
-int exists_execable(const char *filename) FAST_FUNC;
+int file_is_executable(const char *name) FAST_FUNC;
+char *find_executable(const char *filename, char **PATHp) FAST_FUNC;
+int executable_exists(const char *filename) FAST_FUNC;
 
 /* BB_EXECxx always execs (it's not doing NOFORK/NOEXEC stuff),
  * but it may exec busybox and call applet instead of searching PATH.
@@ -907,9 +1009,10 @@ int BB_EXECVP(const char *file, char *const argv[]) FAST_FUNC;
 #define BB_EXECVP(prog,cmd)     execvp(prog,cmd)
 #define BB_EXECLP(prog,cmd,...) execlp(prog,cmd,__VA_ARGS__)
 #endif
-int BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
+void BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
+void exec_prog_or_SHELL(char **argv) NORETURN FAST_FUNC;
 
-/* xvfork() can't be a _function_, return after vfork mangles stack
+/* xvfork() can't be a _function_, return after vfork in child mangles stack
  * in the parent. It must be a macro. */
 #define xvfork() \
 ({ \
@@ -921,6 +1024,7 @@ int BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
 #if BB_MMU
 pid_t xfork(void) FAST_FUNC;
 #endif
+void xvfork_parent_waits_and_exits(void) FAST_FUNC;
 
 /* NOMMU friendy fork+exec: */
 pid_t spawn(char **argv) FAST_FUNC;
@@ -937,6 +1041,7 @@ pid_t wait_any_nohang(int *wstat) FAST_FUNC;
  *      if (rc > 0) bb_error_msg("exit code: %d", rc & 0xff);
  */
 int wait4pid(pid_t pid) FAST_FUNC;
+int wait_for_exitstatus(pid_t pid) FAST_FUNC;
 /* Same as wait4pid(spawn(argv)), but with NOFORK/NOEXEC if configured: */
 int spawn_and_wait(char **argv) FAST_FUNC;
 /* Does NOT check that applet is NOFORK, just blindly runs it */
@@ -970,6 +1075,7 @@ enum {
        DAEMON_DEVNULL_STDIO = 2,
        DAEMON_CLOSE_EXTRA_FDS = 4,
        DAEMON_ONLY_SANITIZE = 8, /* internal use */
+       DAEMON_DOUBLE_FORK = 16, /* double fork to avoid controlling tty */
 };
 #if BB_MMU
   enum { re_execed = 0 };
@@ -978,6 +1084,9 @@ enum {
 # define bb_daemonize(flags)                bb_daemonize_or_rexec(flags, bogus)
 #else
   extern bool re_execed;
+  /* Note: re_exec() and fork_or_rexec() do argv[0][0] |= 0x80 on NOMMU!
+   * _Parent_ needs to undo it if it doesn't want to have argv[0] mangled.
+   */
   void re_exec(char **argv) NORETURN FAST_FUNC;
   pid_t fork_or_rexec(char **argv) FAST_FUNC;
   int  BUG_fork_is_unavailable_on_nommu(void) FAST_FUNC;
@@ -1046,10 +1155,10 @@ enum {
        LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
 };
 extern const char *msg_eol;
+extern smallint syslog_level;
 extern smallint logmode;
-extern int die_sleep;
 extern uint8_t xfunc_error_retval;
-extern jmp_buf die_jmp;
+extern void (*die_func)(void);
 extern void xfunc_die(void) NORETURN FAST_FUNC;
 extern void bb_show_usage(void) NORETURN FAST_FUNC;
 extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
@@ -1062,8 +1171,8 @@ extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1,
 extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC;
 extern void bb_perror_nomsg_and_die(void) NORETURN FAST_FUNC;
 extern void bb_perror_nomsg(void) FAST_FUNC;
-extern void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
 extern void bb_verror_msg(const char *s, va_list p, const char *strerr) FAST_FUNC;
+extern void bb_logenv_override(void) FAST_FUNC;
 
 /* We need to export XXX_main from libbusybox
  * only if we build "individual" binaries
@@ -1080,8 +1189,16 @@ int bb_cat(char** argv);
 /* If shell needs them, they exist even if not enabled as applets */
 int echo_main(int argc, char** argv) IF_ECHO(MAIN_EXTERNALLY_VISIBLE);
 int printf_main(int argc, char **argv) IF_PRINTF(MAIN_EXTERNALLY_VISIBLE);
-int test_main(int argc, char **argv) IF_TEST(MAIN_EXTERNALLY_VISIBLE);
-int kill_main(int argc, char **argv) IF_KILL(MAIN_EXTERNALLY_VISIBLE);
+int test_main(int argc, char **argv)
+#if ENABLE_TEST || ENABLE_TEST1 || ENABLE_TEST2
+               MAIN_EXTERNALLY_VISIBLE
+#endif
+;
+int kill_main(int argc, char **argv)
+#if ENABLE_KILL || ENABLE_KILLALL || ENABLE_KILLALL5
+               MAIN_EXTERNALLY_VISIBLE
+#endif
+;
 /* Similar, but used by chgrp, not shell */
 int chown_main(int argc, char **argv) IF_CHOWN(MAIN_EXTERNALLY_VISIBLE);
 /* Used by ftpd */
@@ -1096,9 +1213,6 @@ void bb_displayroutes(int noresolve, int netstatfmt) FAST_FUNC;
 
 
 /* Networking */
-int create_icmp_socket(void) FAST_FUNC;
-int create_icmp6_socket(void) FAST_FUNC;
-/* interface.c */
 /* This structure defines protocol families and their handlers. */
 struct aftype {
        const char *name;
@@ -1127,6 +1241,7 @@ struct hwtype {
 };
 extern smallint interface_opt_a;
 int display_interfaces(char *ifname) FAST_FUNC;
+int in_ether(const char *bufp, struct sockaddr *sap) FAST_FUNC;
 #if ENABLE_FEATURE_HWIB
 int in_ib(const char *bufp, struct sockaddr *sap) FAST_FUNC;
 #else
@@ -1139,8 +1254,6 @@ const struct hwtype *get_hwntype(int type) FAST_FUNC;
 
 #ifndef BUILD_INDIVIDUAL
 extern int find_applet_by_name(const char *name) FAST_FUNC;
-/* Returns only if applet is not found. */
-extern void run_applet_and_exit(const char *name, char **argv) FAST_FUNC;
 extern void run_applet_no_and_exit(int a, char **argv) NORETURN FAST_FUNC;
 #endif
 
@@ -1164,7 +1277,7 @@ extern int del_loop(const char *device) FAST_FUNC;
 /* If *devname is not NULL, use that name, otherwise try to find free one,
  * malloc and return it in *devname.
  * return value: 1: read-only loopdev was setup, 0: rw, < 0: error */
-extern int set_loop(char **devname, const char *file, unsigned long long offset) FAST_FUNC;
+extern int set_loop(char **devname, const char *file, unsigned long long offset, int ro) FAST_FUNC;
 
 /* Like bb_ask below, but asks on stdin with no timeout.  */
 char *bb_ask_stdin(const char * prompt) FAST_FUNC;
@@ -1172,7 +1285,8 @@ char *bb_ask_stdin(const char * prompt) FAST_FUNC;
 char *bb_ask(const int fd, int timeout, const char * prompt) FAST_FUNC;
 int bb_ask_confirmation(void) FAST_FUNC;
 
-int bb_parse_mode(const char* s, mode_t* theMode) FAST_FUNC;
+/* Returns -1 if input is invalid. current_mode is a base for e.g. "u+rw" */
+int bb_parse_mode(const char* s, unsigned cur_mode) FAST_FUNC;
 
 /*
  * Config file parser
@@ -1185,13 +1299,14 @@ enum {
        PARSE_MIN_DIE   = 0x00100000, // die if < min tokens found
        // keep a copy of current line
        PARSE_KEEP_COPY = 0x00200000 * ENABLE_FEATURE_CROND_D,
-//     PARSE_ESCAPE    = 0x00400000, // process escape sequences in tokens
+       PARSE_EOL_COMMENTS = 0x00400000, // comments are recognized even if they aren't the first char
        // NORMAL is:
        // * remove leading and trailing delimiters and collapse
        //   multiple delimiters into one
        // * warn and continue if less than mintokens delimiters found
        // * grab everything into last token
-       PARSE_NORMAL    = PARSE_COLLAPSE | PARSE_TRIM | PARSE_GREEDY,
+       // * comments are recognized even if they aren't the first char
+       PARSE_NORMAL    = PARSE_COLLAPSE | PARSE_TRIM | PARSE_GREEDY | PARSE_EOL_COMMENTS,
 };
 typedef struct parser_t {
        FILE *fp;
@@ -1227,16 +1342,18 @@ char *bb_simplify_path(const char *path) FAST_FUNC;
 /* Returns ptr to NUL */
 char *bb_simplify_abs_path_inplace(char *path) FAST_FUNC;
 
+#ifndef LOGIN_FAIL_DELAY
 #define LOGIN_FAIL_DELAY 3
+#endif
 extern void bb_do_delay(int seconds) FAST_FUNC;
 extern void change_identity(const struct passwd *pw) FAST_FUNC;
-extern void run_shell(const char *shell, int loginshell, const char *command, const char **additional_args) NORETURN FAST_FUNC;
+extern void run_shell(const char *shell, int loginshell, const char **args) NORETURN FAST_FUNC;
 
 /* Returns $SHELL, getpwuid(getuid())->pw_shell, or DEFAULT_SHELL.
  * Note that getpwuid result might need xstrdup'ing
  * if there is a possibility of intervening getpwxxx() calls.
  */
-const char *get_shell_name(void);
+const char *get_shell_name(void) FAST_FUNC;
 
 #if ENABLE_SELINUX
 extern void renew_current_security_context(void) FAST_FUNC;
@@ -1251,11 +1368,6 @@ extern void selinux_preserve_fcontext(int fdesc) FAST_FUNC;
 extern void selinux_or_die(void) FAST_FUNC;
 
 
-/* systemd support */
-#define SD_LISTEN_FDS_START 3
-int sd_listen_fds(void);
-
-
 /* setup_environment:
  * if chdir pw->pw_dir: ok: else if to_tmp == 1: goto /tmp else: goto / or die
  * if clear_env = 1: cd(pw->pw_dir), clear environment, then set
@@ -1274,8 +1386,12 @@ int sd_listen_fds(void);
 #define SETUP_ENV_CHANGEENV (1 << 0)
 #define SETUP_ENV_CLEARENV  (1 << 1)
 #define SETUP_ENV_TO_TMP    (1 << 2)
-extern void setup_environment(const char *shell, int flags, const struct passwd *pw) FAST_FUNC;
-extern int correct_password(const struct passwd *pw) FAST_FUNC;
+#define SETUP_ENV_NO_CHDIR  (1 << 4)
+void setup_environment(const char *shell, int flags, const struct passwd *pw) FAST_FUNC;
+void nuke_str(char *str) FAST_FUNC;
+int check_password(const struct passwd *pw, const char *plaintext) FAST_FUNC;
+int ask_and_check_password_extended(const struct passwd *pw, int timeout, const char *prompt) FAST_FUNC;
+int ask_and_check_password(const struct passwd *pw) FAST_FUNC;
 /* Returns a malloced string */
 #if !ENABLE_USE_BB_CRYPT
 #define pw_encrypt(clear, salt, cleanup) pw_encrypt(clear, salt)
@@ -1318,6 +1434,7 @@ extern void print_login_prompt(void) FAST_FUNC;
 char *xmalloc_ttyname(int fd) FAST_FUNC RETURNS_MALLOC;
 /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */
 int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC;
+int get_terminal_width(int fd) FAST_FUNC;
 
 int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC;
 
@@ -1352,35 +1469,47 @@ unsigned long long bb_makedev(unsigned major, unsigned minor) FAST_FUNC;
  * yet doesn't represent any valid Unicode character.
  * Also, -1 is reserved for error indication and we don't use it. */
 enum {
-       KEYCODE_UP       =  -2,
-       KEYCODE_DOWN     =  -3,
-       KEYCODE_RIGHT    =  -4,
-       KEYCODE_LEFT     =  -5,
-       KEYCODE_HOME     =  -6,
-       KEYCODE_END      =  -7,
-       KEYCODE_INSERT   =  -8,
-       KEYCODE_DELETE   =  -9,
-       KEYCODE_PAGEUP   = -10,
-       KEYCODE_PAGEDOWN = -11,
-
-       KEYCODE_CTRL_UP    = KEYCODE_UP    & ~0x40,
-       KEYCODE_CTRL_DOWN  = KEYCODE_DOWN  & ~0x40,
-       KEYCODE_CTRL_RIGHT = KEYCODE_RIGHT & ~0x40,
-       KEYCODE_CTRL_LEFT  = KEYCODE_LEFT  & ~0x40,
+       KEYCODE_UP        =  -2,
+       KEYCODE_DOWN      =  -3,
+       KEYCODE_RIGHT     =  -4,
+       KEYCODE_LEFT      =  -5,
+       KEYCODE_HOME      =  -6,
+       KEYCODE_END       =  -7,
+       KEYCODE_INSERT    =  -8,
+       KEYCODE_DELETE    =  -9,
+       KEYCODE_PAGEUP    = -10,
+       KEYCODE_PAGEDOWN  = -11,
+       KEYCODE_BACKSPACE = -12, /* Used only if Alt/Ctrl/Shifted */
+       KEYCODE_D         = -13, /* Used only if Alted */
 #if 0
-       KEYCODE_FUN1     = -12,
-       KEYCODE_FUN2     = -13,
-       KEYCODE_FUN3     = -14,
-       KEYCODE_FUN4     = -15,
-       KEYCODE_FUN5     = -16,
-       KEYCODE_FUN6     = -17,
-       KEYCODE_FUN7     = -18,
-       KEYCODE_FUN8     = -19,
-       KEYCODE_FUN9     = -20,
-       KEYCODE_FUN10    = -21,
-       KEYCODE_FUN11    = -22,
-       KEYCODE_FUN12    = -23,
-#endif
+       KEYCODE_FUN1      = ,
+       KEYCODE_FUN2      = ,
+       KEYCODE_FUN3      = ,
+       KEYCODE_FUN4      = ,
+       KEYCODE_FUN5      = ,
+       KEYCODE_FUN6      = ,
+       KEYCODE_FUN7      = ,
+       KEYCODE_FUN8      = ,
+       KEYCODE_FUN9      = ,
+       KEYCODE_FUN10     = ,
+       KEYCODE_FUN11     = ,
+       KEYCODE_FUN12     = ,
+#endif
+       /* ^^^^^ Be sure that last defined value is small enough.
+        * Current read_key() code allows going up to -32 (0xfff..fffe0).
+        * This gives three upper bits in LSB to play with:
+        * KEYCODE_foo values are 0xfff..fffXX, lowest XX bits are: scavvvvv,
+        * s=0 if SHIFT, c=0 if CTRL, a=0 if ALT,
+        * vvvvv bits are the same for same key regardless of "shift bits".
+        */
+       //KEYCODE_SHIFT_...   = KEYCODE_...   & ~0x80,
+       KEYCODE_CTRL_RIGHT    = KEYCODE_RIGHT & ~0x40,
+       KEYCODE_CTRL_LEFT     = KEYCODE_LEFT  & ~0x40,
+       KEYCODE_ALT_RIGHT     = KEYCODE_RIGHT & ~0x20,
+       KEYCODE_ALT_LEFT      = KEYCODE_LEFT  & ~0x20,
+       KEYCODE_ALT_BACKSPACE = KEYCODE_BACKSPACE & ~0x20,
+       KEYCODE_ALT_D         = KEYCODE_D     & ~0x20,
+
        KEYCODE_CURSOR_POS = -0x100, /* 0xfff..fff00 */
        /* How long is the longest ESC sequence we know?
         * We want it big enough to be able to contain
@@ -1409,7 +1538,7 @@ void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC;
 /* It's NOT just ENABLEd or disabled. It's a number: */
 # if defined CONFIG_FEATURE_EDITING_HISTORY && CONFIG_FEATURE_EDITING_HISTORY > 0
 #  define MAX_HISTORY (CONFIG_FEATURE_EDITING_HISTORY + 0)
-unsigned size_from_HISTFILESIZE(const char *hp);
+unsigned size_from_HISTFILESIZE(const char *hp) FAST_FUNC;
 # else
 #  define MAX_HISTORY 0
 # endif
@@ -1421,6 +1550,12 @@ typedef struct line_input_t {
        int cur_history;
        int max_history; /* must never be <= 0 */
 #  if ENABLE_FEATURE_EDITING_SAVEHISTORY
+       /* meaning of this field depends on FEATURE_EDITING_SAVE_ON_EXIT:
+        * if !FEATURE_EDITING_SAVE_ON_EXIT: "how many lines are
+        * in on-disk history"
+        * if FEATURE_EDITING_SAVE_ON_EXIT: "how many in-memory lines are
+        * also in on-disk history (and thus need to be skipped on save)"
+        */
        unsigned cnt_history_in_file;
        const char *hist_file;
 #  endif
@@ -1428,13 +1563,12 @@ typedef struct line_input_t {
 # endif
 } line_input_t;
 enum {
-       DO_HISTORY = 1 * (MAX_HISTORY > 0),
-       SAVE_HISTORY = 2 * (MAX_HISTORY > 0) * ENABLE_FEATURE_EDITING_SAVEHISTORY,
-       TAB_COMPLETION = 4 * ENABLE_FEATURE_TAB_COMPLETION,
-       USERNAME_COMPLETION = 8 * ENABLE_FEATURE_USERNAME_COMPLETION,
-       VI_MODE = 0x10 * ENABLE_FEATURE_EDITING_VI,
-       WITH_PATH_LOOKUP = 0x20,
-       FOR_SHELL = DO_HISTORY | SAVE_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
+       DO_HISTORY       = 1 * (MAX_HISTORY > 0),
+       TAB_COMPLETION   = 2 * ENABLE_FEATURE_TAB_COMPLETION,
+       USERNAME_COMPLETION = 4 * ENABLE_FEATURE_USERNAME_COMPLETION,
+       VI_MODE          = 8 * ENABLE_FEATURE_EDITING_VI,
+       WITH_PATH_LOOKUP = 0x10,
+       FOR_SHELL        = DO_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION,
 };
 line_input_t *new_line_input_t(int flags) FAST_FUNC;
 /* So far static: void free_line_input_t(line_input_t *n) FAST_FUNC; */
@@ -1446,6 +1580,10 @@ line_input_t *new_line_input_t(int flags) FAST_FUNC;
  * >0 length of input string, including terminating '\n'
  */
 int read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize, int timeout) FAST_FUNC;
+void show_history(const line_input_t *st) FAST_FUNC;
+# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
+void save_history(line_input_t *st);
+# endif
 #else
 #define MAX_HISTORY 0
 int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
@@ -1483,7 +1621,7 @@ struct smaprec {
        procps_read_smaps(pid, total)
 #endif
 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);
 
 typedef struct procps_status_t {
        DIR *dir;
@@ -1570,15 +1708,25 @@ int starts_with_cpu(const char *str) FAST_FUNC;
 unsigned get_cpu_count(void) FAST_FUNC;
 
 
-extern const char bb_uuenc_tbl_base64[];
-extern const char bb_uuenc_tbl_std[];
+/* Use strict=1 if you process input from untrusted source:
+ * it will return NULL on invalid %xx (bad hex chars)
+ * and str + 1 if decoded char is / or NUL.
+ * In non-strict mode, it always succeeds (returns str),
+ * and also it additionally decoded '+' to space.
+ */
+char *percent_decode_in_place(char *str, int strict) FAST_FUNC;
+
+
+extern const char bb_uuenc_tbl_base64[] ALIGN1;
+extern const char bb_uuenc_tbl_std[] ALIGN1;
 void bb_uuencode(char *store, const void *s, int length, const char *tbl) FAST_FUNC;
 enum {
        BASE64_FLAG_UU_STOP = 0x100,
        /* Sign-extends to a value which never matches fgetc result: */
        BASE64_FLAG_NO_STOP_CHAR = 0x80,
 };
-void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags);
+const char *decode_base64(char **pp_dst, const char *src) FAST_FUNC;
+void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC;
 
 typedef struct md5_ctx_t {
        uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
@@ -1593,8 +1741,13 @@ typedef struct sha512_ctx_t {
        uint64_t hash[8];
        uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */
 } sha512_ctx_t;
+typedef struct sha3_ctx_t {
+       uint64_t state[25];
+       unsigned bytes_queued;
+       unsigned input_block_bytes;
+} sha3_ctx_t;
 void md5_begin(md5_ctx_t *ctx) FAST_FUNC;
-void md5_hash(md5_ctx_t *ctx, const void *data, size_t length) FAST_FUNC;
+void md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
 void md5_end(md5_ctx_t *ctx, void *resbuf) FAST_FUNC;
 void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC;
 #define sha1_hash md5_hash
@@ -1605,6 +1758,9 @@ void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
 void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
 void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
 void sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC;
+void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC;
+void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
+void sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC;
 
 extern uint32_t *global_crc32_table;
 uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;
@@ -1638,6 +1794,9 @@ void bb_progress_update(bb_progress_t *p,
                        uoff_t transferred,
                        uoff_t totalsize) FAST_FUNC;
 
+unsigned ubi_devnum_from_devname(const char *str) FAST_FUNC;
+int ubi_get_volid_by_name(unsigned ubi_devnum, const char *vol_name) FAST_FUNC;
+
 
 extern const char *applet_name;
 
@@ -1651,24 +1810,24 @@ extern const char *applet_name;
  * Therefore now we use #defines.
  */
 /* "BusyBox vN.N.N (timestamp or extra_version)" */
-extern const char bb_banner[];
-extern const char bb_msg_memory_exhausted[];
-extern const char bb_msg_invalid_date[];
+extern const char bb_banner[] ALIGN1;
+extern const char bb_msg_memory_exhausted[] ALIGN1;
+extern const char bb_msg_invalid_date[] ALIGN1;
 #define bb_msg_read_error "read error"
 #define bb_msg_write_error "write error"
-extern const char bb_msg_unknown[];
-extern const char bb_msg_can_not_create_raw_socket[];
-extern const char bb_msg_perm_denied_are_you_root[];
-extern const char bb_msg_you_must_be_root[];
-extern const char bb_msg_requires_arg[];
-extern const char bb_msg_invalid_arg[];
-extern const char bb_msg_standard_input[];
-extern const char bb_msg_standard_output[];
+extern const char bb_msg_unknown[] ALIGN1;
+extern const char bb_msg_can_not_create_raw_socket[] ALIGN1;
+extern const char bb_msg_perm_denied_are_you_root[] ALIGN1;
+extern const char bb_msg_you_must_be_root[] ALIGN1;
+extern const char bb_msg_requires_arg[] ALIGN1;
+extern const char bb_msg_invalid_arg_to[] ALIGN1;
+extern const char bb_msg_standard_input[] ALIGN1;
+extern const char bb_msg_standard_output[] ALIGN1;
 
 /* NB: (bb_hexdigits_upcase[i] | 0x20) -> lowercase hex digit */
-extern const char bb_hexdigits_upcase[];
+extern const char bb_hexdigits_upcase[] ALIGN1;
 
-extern const char bb_path_wtmp_file[];
+extern const char bb_path_wtmp_file[] ALIGN1;
 
 /* Busybox mount uses either /proc/mounts or /etc/mtab to
  * get the list of currently mounted filesystems */
@@ -1682,20 +1841,16 @@ extern const char bb_path_wtmp_file[];
 #define bb_path_motd_file "/etc/motd"
 
 #define bb_dev_null "/dev/null"
-extern const char bb_busybox_exec_path[];
+extern const char bb_busybox_exec_path[] ALIGN1;
 /* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin,
  * but I want to save a few bytes here */
-extern const char bb_PATH_root_path[]; /* "PATH=/sbin:/usr/sbin:/bin:/usr/bin" */
+extern const char bb_PATH_root_path[] ALIGN1; /* "PATH=/sbin:/usr/sbin:/bin:/usr/bin" */
 #define bb_default_root_path (bb_PATH_root_path + sizeof("PATH"))
 #define bb_default_path      (bb_PATH_root_path + sizeof("PATH=/sbin:/usr/sbin"))
 
 extern const int const_int_0;
-extern const int const_int_1;
-
+//extern const int const_int_1;
 
-/* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
-enum { COMMON_BUFSIZE = (BUFSIZ >= 256*sizeof(void*) ? BUFSIZ+1 : 256*sizeof(void*)) };
-extern char bb_common_bufsiz1[COMMON_BUFSIZE];
 /* This struct is deliberately not defined. */
 /* See docs/keep_data_small.txt */
 struct globals;
@@ -1709,13 +1864,18 @@ extern struct globals *const ptr_to_globals;
        (*(struct globals**)&ptr_to_globals) = (void*)(x); \
        barrier(); \
 } while (0)
+#define FREE_PTR_TO_GLOBALS() do { \
+       if (ENABLE_FEATURE_CLEAN_UP) { \
+               free(ptr_to_globals); \
+       } \
+} while (0)
 
 /* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
  * use bb_default_login_shell and following defines.
  * If you change LIBBB_DEFAULT_LOGIN_SHELL,
  * don't forget to change increment constant. */
 #define LIBBB_DEFAULT_LOGIN_SHELL  "-/bin/sh"
-extern const char bb_default_login_shell[];
+extern const char bb_default_login_shell[] ALIGN1;
 /* "/bin/sh" */
 #define DEFAULT_SHELL              (bb_default_login_shell+1)
 /* "sh" */
@@ -1750,7 +1910,7 @@ extern const char bb_default_login_shell[];
 # define VC_4 "/dev/vc/4"
 # define VC_5 "/dev/vc/5"
 # define VC_FORMAT "/dev/vc/%d"
-# define LOOP_FORMAT "/dev/loop/%d"
+# define LOOP_FORMAT "/dev/loop/%u"
 # define LOOP_NAMESIZE (sizeof("/dev/loop/") + sizeof(int)*3 + 1)
 # define LOOP_NAME "/dev/loop/"
 # define FB_0 "/dev/fb/0"
@@ -1763,7 +1923,7 @@ extern const char bb_default_login_shell[];
 # define VC_4 "/dev/tty4"
 # define VC_5 "/dev/tty5"
 # define VC_FORMAT "/dev/tty%d"
-# define LOOP_FORMAT "/dev/loop%d"
+# define LOOP_FORMAT "/dev/loop%u"
 # define LOOP_NAMESIZE (sizeof("/dev/loop") + sizeof(int)*3 + 1)
 # define LOOP_NAME "/dev/loop"
 # define FB_0 "/dev/fb0"
@@ -1771,6 +1931,7 @@ extern const char bb_default_login_shell[];
 
 
 #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
 
 
 /* We redefine ctype macros. Unicode-correct handling of char types
@@ -1856,6 +2017,140 @@ static ALWAYS_INLINE unsigned char bb_ascii_tolower(unsigned char a)
 #define isprint_asciionly(a) ((unsigned)((a) - 0x20) <= 0x7e - 0x20)
 
 
+/* Simple unit-testing framework */
+
+typedef void (*bbunit_testfunc)(void);
+
+struct bbunit_listelem {
+       const char* name;
+       bbunit_testfunc testfunc;
+};
+
+void bbunit_registertest(struct bbunit_listelem* test);
+void bbunit_settestfailed(void);
+
+#define BBUNIT_DEFINE_TEST(NAME) \
+       static void bbunit_##NAME##_test(void); \
+       static struct bbunit_listelem bbunit_##NAME##_elem = { \
+               .name = #NAME, \
+               .testfunc = bbunit_##NAME##_test, \
+       }; \
+       static void INIT_FUNC bbunit_##NAME##_register(void) \
+       { \
+               bbunit_registertest(&bbunit_##NAME##_elem); \
+       } \
+       static void bbunit_##NAME##_test(void)
+
+/*
+ * Both 'goto bbunit_end' and 'break' are here only to get rid
+ * of compiler warnings.
+ */
+#define BBUNIT_ENDTEST \
+       do { \
+               goto bbunit_end; \
+       bbunit_end: \
+               break; \
+       } while (0)
+
+#define BBUNIT_PRINTASSERTFAIL \
+       do { \
+               bb_error_msg( \
+                       "[ERROR] Assertion failed in file %s, line %d", \
+                       __FILE__, __LINE__); \
+       } while (0)
+
+#define BBUNIT_ASSERTION_FAILED \
+       do { \
+               bbunit_settestfailed(); \
+               goto bbunit_end; \
+       } while (0)
+
+/*
+ * Assertions.
+ * For now we only offer assertions which cause tests to fail
+ * immediately. In the future 'expects' might be added too -
+ * similar to those offered by the gtest framework.
+ */
+#define BBUNIT_ASSERT_EQ(EXPECTED, ACTUAL) \
+       do { \
+               if ((EXPECTED) != (ACTUAL)) { \
+                       BBUNIT_PRINTASSERTFAIL; \
+                       bb_error_msg("[ERROR] '%s' isn't equal to '%s'", \
+                                               #EXPECTED, #ACTUAL); \
+                       BBUNIT_ASSERTION_FAILED; \
+               } \
+       } while (0)
+
+#define BBUNIT_ASSERT_NOTEQ(EXPECTED, ACTUAL) \
+       do { \
+               if ((EXPECTED) == (ACTUAL)) { \
+                       BBUNIT_PRINTASSERTFAIL; \
+                       bb_error_msg("[ERROR] '%s' is equal to '%s'", \
+                                               #EXPECTED, #ACTUAL); \
+                       BBUNIT_ASSERTION_FAILED; \
+               } \
+       } while (0)
+
+#define BBUNIT_ASSERT_NOTNULL(PTR) \
+       do { \
+               if ((PTR) == NULL) { \
+                       BBUNIT_PRINTASSERTFAIL; \
+                       bb_error_msg("[ERROR] '%s' is NULL!", #PTR); \
+                       BBUNIT_ASSERTION_FAILED; \
+               } \
+       } while (0)
+
+#define BBUNIT_ASSERT_NULL(PTR) \
+       do { \
+               if ((PTR) != NULL) { \
+                       BBUNIT_PRINTASSERTFAIL; \
+                       bb_error_msg("[ERROR] '%s' is not NULL!", #PTR); \
+                       BBUNIT_ASSERTION_FAILED; \
+               } \
+       } while (0)
+
+#define BBUNIT_ASSERT_FALSE(STATEMENT) \
+       do { \
+               if ((STATEMENT)) { \
+                       BBUNIT_PRINTASSERTFAIL; \
+                       bb_error_msg("[ERROR] Statement '%s' evaluated to true!", \
+                                                               #STATEMENT); \
+                       BBUNIT_ASSERTION_FAILED; \
+               } \
+       } while (0)
+
+#define BBUNIT_ASSERT_TRUE(STATEMENT) \
+       do { \
+               if (!(STATEMENT)) { \
+                       BBUNIT_PRINTASSERTFAIL; \
+                       bb_error_msg("[ERROR] Statement '%s' evaluated to false!", \
+                                       #STATEMENT); \
+                       BBUNIT_ASSERTION_FAILED; \
+               } \
+       } while (0)
+
+#define BBUNIT_ASSERT_STREQ(STR1, STR2) \
+       do { \
+               if (strcmp(STR1, STR2) != 0) { \
+                       BBUNIT_PRINTASSERTFAIL; \
+                       bb_error_msg("[ERROR] Strings '%s' and '%s' " \
+                                       "are not the same", STR1, STR2); \
+                       BBUNIT_ASSERTION_FAILED; \
+               } \
+       } while (0)
+
+#define BBUNIT_ASSERT_STRNOTEQ(STR1, STR2) \
+       do { \
+               if (strcmp(STR1, STR2) == 0) { \
+                       BBUNIT_PRINTASSERTFAIL; \
+                       bb_error_msg("[ERROR] Strings '%s' and '%s' " \
+                                       "are the same, but were " \
+                                       "expected to differ", STR1, STR2); \
+                       BBUNIT_ASSERTION_FAILED; \
+               } \
+       } while (0)
+
+
 POP_SAVED_FUNCTION_VISIBILITY
 
 #endif