Skip to content

Commit

Permalink
portability for {Free,Net,Open,}BSD
Browse files Browse the repository at this point in the history
  • Loading branch information
agreppin committed Jun 15, 2024
1 parent c929b4f commit f595efb
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 21 deletions.
18 changes: 11 additions & 7 deletions lib/portability.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/mount.h>

Expand Down Expand Up @@ -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 <sys/event.h>

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions lib/portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/endian.h>

Expand All @@ -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

Expand Down Expand Up @@ -200,7 +206,7 @@ void *memmem(const void *haystack, size_t haystack_length,

#ifdef __APPLE__
#include <util.h>
#elif !defined(__FreeBSD__) && !defined(__OpenBSD__)
#elif !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
#include <pty.h>
#else
#include <termios.h>
Expand Down Expand Up @@ -240,7 +246,12 @@ int posix_fallocate(int, off_t, off_t);
#include <xlocale.h>
#endif

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#ifdef __NetBSD__
#include <sys/statvfs.h>
#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
Expand Down
4 changes: 4 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
{
Expand Down
4 changes: 2 additions & 2 deletions scripts/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion scripts/portability.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion tests/gunzip.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 12 additions & 3 deletions tests/gzip.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
9 changes: 6 additions & 3 deletions tests/printf.test
Original file line number Diff line number Diff line change
Expand Up @@ -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" "" ""
Expand All @@ -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" "" ""
Expand Down Expand Up @@ -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" "" ""
Expand Down
4 changes: 3 additions & 1 deletion tests/zcat.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"?
Expand Down

0 comments on commit f595efb

Please sign in to comment.