diff --git a/lib/portability.c b/lib/portability.c index abcecc803..e1a598148 100644 --- a/lib/portability.c +++ b/lib/portability.c @@ -54,7 +54,7 @@ void xgetrandom(void *buf, unsigned buflen) // Get list of mounted filesystems, including stat and statvfs info. // Returns a reversed list, which is good for finding overmounts and such. -#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) #include @@ -169,7 +169,7 @@ struct mtab_list *xgetmountlist(char *path) #endif -#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) #include @@ -313,7 +313,7 @@ ssize_t xattr_fset(int fd, const char* name, return fsetxattr(fd, name, value, size, 0, flags); } -#elif !defined(__FreeBSD__) && !defined(__OpenBSD__) +#elif !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) ssize_t xattr_get(const char *path, const char *name, void *value, size_t size) { @@ -515,7 +515,7 @@ int dev_minor(int dev) return ((dev&0xfff00000)>>12)|(dev&0xff); #elif defined(__APPLE__) return dev&0xffffff; -#elif defined(__FreeBSD__) || defined(__OpenBSD__) +#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) return minor(dev); #else #error @@ -528,7 +528,7 @@ int dev_major(int dev) return (dev&0xfff00)>>8; #elif defined(__APPLE__) return (dev>>24)&0xff; -#elif defined(__FreeBSD__) || defined(__OpenBSD__) +#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) return major(dev); #else #error @@ -541,7 +541,7 @@ int dev_makedev(int major, int minor) return (minor&0xff)|((major&0xfff)<<8)|((minor&0xfff00)<<12); #elif defined(__APPLE__) return (minor&0xffffff)|((major&0xff)<<24); -#elif defined(__FreeBSD__) || defined(__OpenBSD__) +#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) return makedev(major, minor); #else #error @@ -550,7 +550,7 @@ int dev_makedev(int major, int minor) char *fs_type_name(struct statfs *statfs) { -#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) // macOS has an `f_type` field, but assigns values dynamically as filesystems // are registered. They do give you the name directly though, so use that. return statfs->f_fstypename; @@ -644,6 +644,10 @@ static int check_copy_file_range(void) #endif } +#ifdef __OpenBSD__ +#define syscall(...) -1 +#endif + // Return bytes copied from in to out. If bytes <0 copy all of in to out. // If consumed isn't null, amount read saved there (return is written or error) long long sendfile_len(int in, int out, long long bytes, long long *consumed) diff --git a/lib/portability.h b/lib/portability.h index cb4725b81..ab84ceabd 100644 --- a/lib/portability.h +++ b/lib/portability.h @@ -145,7 +145,7 @@ void *memmem(const void *haystack, size_t haystack_length, #define bswap_32(x) OSSwapInt32(x) #define bswap_64(x) OSSwapInt64(x) -#elif defined(__FreeBSD__) || defined(__OpenBSD__) +#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) #include @@ -155,9 +155,15 @@ void *memmem(const void *haystack, size_t haystack_length, #define IS_BIG_ENDIAN 0 #endif +#ifdef __OpenBSD__ +#define bswap_16(x) swap16(x) +#define bswap_32(x) swap32(x) +#define bswap_64(x) swap64(x) +#else #define bswap_16(x) bswap16(x) #define bswap_32(x) bswap32(x) #define bswap_64(x) bswap64(x) +#endif #else @@ -200,7 +206,7 @@ void *memmem(const void *haystack, size_t haystack_length, #ifdef __APPLE__ #include -#elif !defined(__FreeBSD__) && !defined(__OpenBSD__) +#elif !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) #include #else #include @@ -240,7 +246,12 @@ int posix_fallocate(int, off_t, off_t); #include #endif -#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) +#ifdef __NetBSD__ +#include +#define statfs statvfs +#endif + +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) static inline long statfs_bsize(struct statfs *sf) { return sf->f_iosize; } static inline long statfs_frsize(struct statfs *sf) { return sf->f_bsize; } #else diff --git a/main.c b/main.c index 2cbaf1726..a6e32b6f2 100644 --- a/main.c +++ b/main.c @@ -151,6 +151,10 @@ void check_help(char **arg) } } +#ifdef __NetBSD__ +#define uselocale(...) do {} while (0) +#endif + // Setup toybox global state for this command. void toy_singleinit(struct toy_list *which, char *argv[]) { diff --git a/scripts/make.sh b/scripts/make.sh index ff677d5d4..e7174253b 100755 --- a/scripts/make.sh +++ b/scripts/make.sh @@ -236,8 +236,8 @@ if grep -qx 'CONFIG_TOYBOX_ZHELP=y' "$KCONFIG_CONFIG" then do_loudly $HOSTCC -I . scripts/install.c -o "$UNSTRIPPED"/instlist || exit 1 { echo "#define ZHELP_LEN $("$UNSTRIPPED"/instlist --help | wc -c)" && - "$UNSTRIPPED"/instlist --help | gzip -9 | od -Anone -vtx1 | \ - sed 's/ /,0x/g;1s/^,/static char zhelp_data[] = {\n /;$s/.*/&};/' + "$UNSTRIPPED"/instlist --help | gzip -9 | $OD -An -vtx1 | \ + $SED 's/ */,0x/g;1s/^,/static char zhelp_data[] = {\n /;$s/.*/&};/' } > "$GENDIR"/zhelp.h || exit 1 else rm -f "$GENDIR"/zhelp.h diff --git a/scripts/portability.sh b/scripts/portability.sh index e8ad197a7..99ac04f69 100644 --- a/scripts/portability.sh +++ b/scripts/portability.sh @@ -8,6 +8,10 @@ then exit 1 fi +case ${OD-} in +'') OD=$(command -v god || command -v ggod || echo od);; # use od from GNU coretutils +esac + if [ -z "$SED" ] then [ ! -z "$(command -v gsed 2>/dev/null)" ] && SED=gsed || SED=sed @@ -19,7 +23,12 @@ then CFLAGS+=" -Wno-deprecated-declarations" : ${LDOPTIMIZE:=-Wl,-dead_strip} ${STRIP:=strip} else - : ${LDOPTIMIZE:=-Wl,--gc-sections -Wl,--as-needed} ${STRIP:=strip -s -R .note* -R .comment} + case "$(uname)" in + 'NetBSD'|'OpenBSD') # don't strip .note.*bsd* + : ${LDOPTIMIZE:=-Wl,--gc-sections -Wl,--as-needed} ${STRIP:=strip -s -R .comment};; + *) + : ${LDOPTIMIZE:=-Wl,--gc-sections -Wl,--as-needed} ${STRIP:=strip -s -R .note* -R .comment};; + esac fi # Disable pointless warnings only clang produces @@ -28,6 +37,11 @@ fi # And ones only gcc produces CFLAGS+=" -Wno-restrict -Wno-format-overflow" +case "$(uname)" in +'NetBSD'|'OpenBSD') + unset ASAN;; +esac + # Address Sanitizer if [ -n "$ASAN" ]; then # Turn ASan on and disable most optimization to get more readable backtraces. diff --git a/tests/gunzip.test b/tests/gunzip.test index bf9b9834a..b779576b0 100755 --- a/tests/gunzip.test +++ b/tests/gunzip.test @@ -51,7 +51,16 @@ echo "hello world" | gzip > f1.gz chmod 0411 f1.gz touch -a -t 197801020304 f1.gz touch -m -t 198704030201 f1.gz -testing "permissions/times preservation" \ +case $(uname) in +'FreeBSD'|'NetBSD'|'OpenBSD') + testing "permissions/times preservation" \ + "gunzip -k f1.gz && stat -f '%p %a %m' f1 && stat -f '%p %m' f1.gz" \ + "100411 252558240 544413660\n100411 544413660\n" "" "" + ;; +*) + testing "permissions/times preservation" \ "gunzip -k f1.gz && stat -c '%a %X %Y' f1 && stat -c '%a %Y' f1.gz" \ "411 252558240 544413660\n411 544413660\n" "" "" + ;; +esac rm -f f1 f1.gz diff --git a/tests/gzip.test b/tests/gzip.test index b458307e1..8a1bf9c37 100644 --- a/tests/gzip.test +++ b/tests/gzip.test @@ -72,9 +72,18 @@ echo "hello world" > f1 chmod 0411 f1 touch -a -t 197801020304 f1 touch -m -t 198704030201 f1 -testing "permissions/times preservation" \ - "gzip -k f1 && TZ=UTC stat -c '%a %Y' f1 && stat -c '%a %X %Y' f1.gz" \ - "411 544413660\n411 252558240 544413660\n" "" "" +case $(uname) in +'FreeBSD'|'NetBSD'|'OpenBSD') + testing "permissions/times preservation" \ + "gunzip -k f1.gz && stat -f '%p %a %m' f1 && stat -f '%p %m' f1.gz" \ + "100411 252558240 544413660\n100411 544413660\n" "" "" + ;; +*) + testing "permissions/times preservation" \ + "gunzip -k f1.gz && stat -c '%a %X %Y' f1 && stat -c '%a %Y' f1.gz" \ + "411 252558240 544413660\n411 544413660\n" "" "" + ;; +esac rm -f f1 f1.gz testing "reject non-gzip" "gzip -dc $FILES/blkid/msdos.bz2 2>/dev/null || diff --git a/tests/printf.test b/tests/printf.test index 57f2a2839..6580e5e0f 100755 --- a/tests/printf.test +++ b/tests/printf.test @@ -7,6 +7,9 @@ #testing "name" "command" "result" "infile" "stdin" +[ -z "$OD" ] && \ + OD=$(command -v god || command -v ggod || echo od) # use od from GNU coretutils + # Note: must use "testcmd" not "testing" else it's testing the shell builtin. testcmd "text" "TEXT" "TEXT" "" "" @@ -19,11 +22,11 @@ testcmd "escapes" "'one\ntwo\n\v\t\r\f\e\b\athree'" \ testcmd "%b escapes" "%b 'one\ntwo\n\v\t\r\f\e\b\athree'" \ "one\ntwo\n\v\t\r\f\x1b\b\athree" "" "" -testcmd "null" "'x\0y' | od -An -tx1" ' 78 00 79\n' "" "" +testcmd "null" "'x\0y' | $OD -An -tx1" ' 78 00 79\n' "" "" testcmd "trailing slash" "'abc\'" 'abc\' "" "" testcmd "octal" "' \1\002\429\045x'" ' \001\002"9%x' "" "" testcmd "not octal" "'\9'" '\9' "" "" -testcmd "hex" "'A\x1b\x2B\x3Q\xa' | od -An -tx1" ' 41 1b 2b 03 51 0a\n' "" "" +testcmd "hex" "'A\x1b\x2B\x3Q\xa' | $OD -An -tx1" ' 41 1b 2b 03 51 0a\n' "" "" testcmd "%x" "'%x\n' 0x2a" "2a\n" "" "" testcmd "%d 42" "%d 42" "42" "" "" @@ -67,7 +70,7 @@ testcmd "corner case" "'\\8'" '\8' '' '' # so treating the \0066 in %b like the \0066 not in %b is wrong because posix. testcmd "posix inconsistency" "'\\0066-%b' '\\0066'" "\x066-6" "" "" -testcmd '\x' "'A\x1b\x2B\x3Q\xa' | od -An -tx1" " 41 1b 2b 03 51 0a\n" \ +testcmd '\x' "'A\x1b\x2B\x3Q\xa' | $OD -An -tx1" " 41 1b 2b 03 51 0a\n" \ "" "" testcmd '\c' "'one\ctwo'" "one" "" "" diff --git a/tests/zcat.test b/tests/zcat.test index 878a11d44..9723f7939 100755 --- a/tests/zcat.test +++ b/tests/zcat.test @@ -4,6 +4,8 @@ #testing "name" "command" "result" "infile" "stdin" +HEAD=$(command -v ghead || echo head) # use head from GNU coreutils + echo -n "hi " | gzip > 1.gz echo "there" | gzip > 2.gz @@ -12,7 +14,7 @@ testcmd "files" "1.gz 2.gz && test -f 1.gz && test -f 2.gz" "hi there\n" "" "" # zcat -c is allowed but changes nothing testcmd "-c" "-c 1.gz 2.gz && test -f 1.gz && test -f 2.gz" "hi there\n" "" "" testing "concatenated" "{ cat 1.gz 2.gz; } | zcat" "hi there\n" "" "" -testing "error" "head -c 10 2.gz | { zcat 2>/dev/null || echo fail; }" "fail\n"\ +testing "error" "$HEAD -c 10 2.gz | { zcat 2>/dev/null || echo fail; }" "fail\n"\ "" "" # TODO: how to test "zcat -f"?