Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2afdcc77cc | ||
|
|
c74f1d2cb4 | ||
|
|
62b7083c13 | ||
|
|
39bf15ba3e | ||
|
|
37277a23fe | ||
|
|
e3821088af | ||
|
|
4a80eb142a | ||
|
|
1574aff942 | ||
|
|
f5b9a22678 | ||
|
|
1123922320 | ||
|
|
ce5ba9277b | ||
|
|
20f02c3c87 | ||
|
|
277636670c | ||
|
|
4e71661e3a | ||
|
|
59675c625d |
No files matched your search
-11
@@ -50,14 +50,3 @@ cscope.files
|
||||
cscope.in.out
|
||||
cscope.out
|
||||
cscope.po.out
|
||||
|
||||
#
|
||||
# ctags output
|
||||
#
|
||||
tags
|
||||
TAGS
|
||||
|
||||
#
|
||||
# user-supplied scripts
|
||||
#
|
||||
/embed
|
||||
@@ -1,6 +1,6 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see docs/Kconfig-language.txt.
|
||||
# see scripts/kbuild/config-language.txt.
|
||||
#
|
||||
|
||||
mainmenu "Configuration"
|
||||
@@ -12,7 +12,7 @@ config HAVE_DOT_CONFIG
|
||||
menu "Settings"
|
||||
|
||||
config DESKTOP
|
||||
bool "Enable compatibility for full-blown desktop systems (8kb)"
|
||||
bool "Enable compatibility for full-blown desktop systems"
|
||||
default y
|
||||
help
|
||||
Enable applet options and features which are not essential.
|
||||
@@ -156,13 +156,12 @@ config FEATURE_PIDFILE
|
||||
config PID_FILE_PATH
|
||||
string "Directory for pidfiles"
|
||||
default "/var/run"
|
||||
depends on FEATURE_PIDFILE || FEATURE_CROND_SPECIAL_TIMES
|
||||
depends on FEATURE_PIDFILE
|
||||
help
|
||||
This is the default path where pidfiles are created. Applets which
|
||||
allow you to set the pidfile path on the command line will override
|
||||
this value. The option has no effect on applets that require you to
|
||||
specify a pidfile path. When crond has the 'Support special times'
|
||||
option enabled, the 'crond.reboot' file is also stored here.
|
||||
specify a pidfile path.
|
||||
|
||||
config BUSYBOX
|
||||
bool "Include busybox applet"
|
||||
@@ -179,11 +178,6 @@ config BUSYBOX
|
||||
|
||||
Running "busybox APPLET [ARGS...]" will still work, of course.
|
||||
|
||||
config FEATURE_SHOW_SCRIPT
|
||||
bool "Support --show SCRIPT"
|
||||
default y
|
||||
depends on BUSYBOX
|
||||
|
||||
config FEATURE_INSTALLER
|
||||
bool "Support --install [-s] to install applet links at runtime"
|
||||
default y
|
||||
@@ -312,6 +306,7 @@ config BUSYBOX_EXEC_PATH
|
||||
config SELINUX
|
||||
bool "Support NSA Security Enhanced Linux"
|
||||
default n
|
||||
select PLATFORM_LINUX
|
||||
help
|
||||
Enable support for SELinux in applets ls, ps, and id. Also provide
|
||||
the option of compiling in SELinux applets.
|
||||
@@ -339,15 +334,6 @@ config FEATURE_CLEAN_UP
|
||||
Don't enable this unless you have a really good reason to clean
|
||||
things up manually.
|
||||
|
||||
config FEATURE_SYSLOG_INFO
|
||||
bool "Support LOG_INFO level syslog messages"
|
||||
default y
|
||||
depends on FEATURE_SYSLOG
|
||||
help
|
||||
Applets which send their output to syslog use either LOG_INFO or
|
||||
LOG_ERR log levels, but by disabling this option all messages will
|
||||
be logged at the LOG_ERR level, saving just under 200 bytes.
|
||||
|
||||
# These are auto-selected by other options
|
||||
|
||||
config FEATURE_SYSLOG
|
||||
@@ -357,6 +343,17 @@ config FEATURE_SYSLOG
|
||||
#This option is auto-selected when you select any applet which may
|
||||
#send its output to syslog. You do not need to select it manually.
|
||||
|
||||
config PLATFORM_LINUX
|
||||
bool #No description makes it a hidden option
|
||||
default n
|
||||
#help
|
||||
#For the most part, busybox requires only POSIX compatibility
|
||||
#from the target system, but some applets and features use
|
||||
#Linux-specific interfaces.
|
||||
#
|
||||
#This is automatically selected if any applet or feature requires
|
||||
#Linux-specific interfaces. You do not need to select it manually.
|
||||
|
||||
comment 'Build Options'
|
||||
|
||||
config STATIC
|
||||
@@ -521,22 +518,6 @@ config USE_PORTABLE_CODE
|
||||
compiler other than gcc.
|
||||
If you do use gcc, this option may needlessly increase code size.
|
||||
|
||||
config STACK_OPTIMIZATION_386
|
||||
bool "Use -mpreferred-stack-boundary=2 on i386 arch"
|
||||
default y
|
||||
help
|
||||
This option makes for smaller code, but some libc versions
|
||||
do not work with it (they use SSE instructions without
|
||||
ensuring stack alignment).
|
||||
|
||||
config STATIC_LIBGCC
|
||||
bool "Use -static-libgcc"
|
||||
default y
|
||||
help
|
||||
This option instructs gcc to link in a static version of its
|
||||
support library, libgcc. This means that the binary will require
|
||||
one fewer dynamic library at run time.
|
||||
|
||||
comment 'Installation Options ("make install" behavior)'
|
||||
|
||||
choice
|
||||
@@ -658,19 +639,6 @@ config WERROR
|
||||
|
||||
Most people should answer N.
|
||||
|
||||
config WARN_SIMPLE_MSG
|
||||
bool "Warn about single parameter bb_xx_msg calls"
|
||||
default n
|
||||
help
|
||||
This will cause warnings to be shown for any instances of
|
||||
bb_error_msg(), bb_error_msg_and_die(), bb_perror_msg(),
|
||||
bb_perror_msg_and_die(), bb_herror_msg() or bb_herror_msg_and_die()
|
||||
being called with a single parameter. In these cases the equivalent
|
||||
bb_simple_xx_msg function should be used instead.
|
||||
Note that use of STRERROR_FMT may give false positives.
|
||||
|
||||
If you aren't developing busybox, say N here.
|
||||
|
||||
choice
|
||||
prompt "Additional debugging library"
|
||||
default NO_DEBUG_LIB
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
VERSION = 1
|
||||
PATCHLEVEL = 35
|
||||
SUBLEVEL = 0
|
||||
PATCHLEVEL = 28
|
||||
SUBLEVEL = 1
|
||||
EXTRAVERSION =
|
||||
NAME = Unnamed
|
||||
|
||||
@@ -361,14 +361,14 @@ scripts/basic/%: scripts_basic ;
|
||||
|
||||
# This target generates Kbuild's and Config.in's from *.c files
|
||||
PHONY += gen_build_files
|
||||
gen_build_files: $(wildcard $(srctree)/*/*.c) $(wildcard $(srctree)/*/*/*.c) $(wildcard $(srctree)/embed/*)
|
||||
gen_build_files: $(wildcard $(srctree)/*/*.c) $(wildcard $(srctree)/*/*/*.c)
|
||||
$(Q)$(srctree)/scripts/gen_build_files.sh $(srctree) $(objtree)
|
||||
|
||||
# bbox: we have helpers in applets/
|
||||
# we depend on scripts_basic, since scripts/basic/fixdep
|
||||
# must be built before any other host prog
|
||||
PHONY += applets_dir
|
||||
applets_dir: scripts_basic gen_build_files include/config/MARKER
|
||||
applets_dir: scripts_basic gen_build_files
|
||||
$(Q)$(MAKE) $(build)=applets
|
||||
|
||||
applets/%: applets_dir ;
|
||||
@@ -612,7 +612,6 @@ quiet_cmd_busybox__ ?= LINK $@
|
||||
"$(core-y)" \
|
||||
"$(libs-y)" \
|
||||
"$(LDLIBS)" \
|
||||
"$(CONFIG_EXTRA_LDLIBS)" \
|
||||
&& $(srctree)/scripts/generate_BUFSIZ.sh --post include/common_bufsiz.h
|
||||
|
||||
# Generate System.map
|
||||
@@ -851,14 +850,11 @@ quiet_cmd_gen_common_bufsiz = GEN include/common_bufsiz.h
|
||||
cmd_gen_common_bufsiz = $(srctree)/scripts/generate_BUFSIZ.sh include/common_bufsiz.h
|
||||
quiet_cmd_split_autoconf = SPLIT include/autoconf.h -> include/config/*
|
||||
cmd_split_autoconf = scripts/basic/split-include include/autoconf.h include/config
|
||||
quiet_cmd_gen_embedded_scripts = GEN include/embedded_scripts.h
|
||||
cmd_gen_embedded_scripts = $(srctree)/scripts/embedded_scripts include/embedded_scripts.h $(srctree)/embed $(srctree)/applets_sh
|
||||
#bbox# piggybacked generation of few .h files
|
||||
include/config/MARKER: scripts/basic/split-include include/autoconf.h $(wildcard $(srctree)/embed/*) $(wildcard $(srctree)/applets_sh/*) $(srctree)/scripts/embedded_scripts
|
||||
include/config/MARKER: scripts/basic/split-include include/autoconf.h
|
||||
$(call cmd,split_autoconf)
|
||||
$(call cmd,gen_bbconfigopts)
|
||||
$(call cmd,gen_common_bufsiz)
|
||||
$(call cmd,gen_embedded_scripts)
|
||||
@touch $@
|
||||
|
||||
# Generate some files
|
||||
@@ -978,7 +974,6 @@ MRPROPER_FILES += .config .config.old include/asm .version .old_version \
|
||||
include/autoconf.h \
|
||||
include/bbconfigopts.h \
|
||||
include/bbconfigopts_bz2.h \
|
||||
include/embedded_scripts.h \
|
||||
include/usage_compressed.h \
|
||||
include/applet_tables.h \
|
||||
include/applets.h \
|
||||
|
||||
@@ -11,9 +11,6 @@ busybox.cfg.nosuid: $(srctree)/applets/busybox.mksuid $(objtree)/include/autocon
|
||||
$(Q)-SUID="DROP" $(SHELL) $^ > $@
|
||||
|
||||
.PHONY: install
|
||||
ifeq ($(CONFIG_INSTALL_APPLET_DONT),y)
|
||||
INSTALL_OPTS:= --none
|
||||
endif
|
||||
ifeq ($(CONFIG_INSTALL_APPLET_SYMLINKS),y)
|
||||
INSTALL_OPTS:= --symlinks
|
||||
endif
|
||||
@@ -49,9 +46,6 @@ ifeq ($(strip $(CONFIG_FEATURE_SUID)),y)
|
||||
@echo
|
||||
endif
|
||||
|
||||
install-noclobber: INSTALL_OPTS+=--noclobber
|
||||
install-noclobber: install
|
||||
|
||||
uninstall: busybox.links
|
||||
rm -f $(CONFIG_PREFIX)/bin/busybox
|
||||
for i in `cat busybox.links` ; do rm -f $(CONFIG_PREFIX)$$i; done
|
||||
|
||||
+13
-41
@@ -15,7 +15,7 @@ CPPFLAGS += \
|
||||
-include include/autoconf.h \
|
||||
-D_GNU_SOURCE -DNDEBUG \
|
||||
$(if $(CONFIG_LFS),-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64) \
|
||||
-DBB_VER=$(squote)$(quote)$(BB_VER)$(quote)$(squote)
|
||||
-D"BB_VER=KBUILD_STR($(BB_VER))"
|
||||
|
||||
CFLAGS += $(call cc-option,-Wall,)
|
||||
CFLAGS += $(call cc-option,-Wshadow,)
|
||||
@@ -47,29 +47,12 @@ endif
|
||||
# gcc 3.x emits bogus "old style proto" warning on find.c:alloc_action()
|
||||
CFLAGS += $(call cc-ifversion, -ge, 0400, -Wold-style-definition)
|
||||
|
||||
ifneq ($(CC),clang)
|
||||
# "clang-9: warning: optimization flag '-finline-limit=0' is not supported
|
||||
CFLAGS += $(call cc-option,-finline-limit=0,)
|
||||
endif
|
||||
|
||||
CFLAGS += $(call cc-option,-fno-builtin-strlen -fomit-frame-pointer -ffunction-sections -fdata-sections,)
|
||||
CFLAGS += $(call cc-option,-fno-builtin-strlen -finline-limit=0 -fomit-frame-pointer -ffunction-sections -fdata-sections,)
|
||||
# -fno-guess-branch-probability: prohibit pseudo-random guessing
|
||||
# of branch probabilities (hopefully makes bloatcheck more stable):
|
||||
CFLAGS += $(call cc-option,-fno-guess-branch-probability,)
|
||||
CFLAGS += $(call cc-option,-funsigned-char,)
|
||||
|
||||
ifeq ($(CONFIG_STATIC_LIBGCC),y)
|
||||
# Disable it, for example, if you get
|
||||
# "clang-9: warning: argument unused during compilation: '-static-libgcc'"
|
||||
CFLAGS += $(call cc-option,-static-libgcc,)
|
||||
endif
|
||||
|
||||
CFLAGS += $(call cc-option,-falign-functions=1,)
|
||||
ifneq ($(CC),clang)
|
||||
# "clang-9: warning: optimization flag '-falign-jumps=1' is not supported" (and same for other two)
|
||||
CFLAGS += $(call cc-option,-falign-jumps=1 -falign-labels=1 -falign-loops=1,)
|
||||
endif
|
||||
|
||||
CFLAGS += $(call cc-option,-funsigned-char -static-libgcc,)
|
||||
CFLAGS += $(call cc-option,-falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1,)
|
||||
# Defeat .eh_frame bloat (gcc 4.6.3 x86-32 defconfig: 20% smaller busybox binary):
|
||||
CFLAGS += $(call cc-option,-fno-unwind-tables,)
|
||||
CFLAGS += $(call cc-option,-fno-asynchronous-unwind-tables,)
|
||||
@@ -77,11 +60,6 @@ CFLAGS += $(call cc-option,-fno-asynchronous-unwind-tables,)
|
||||
# (try disabling this and comparing assembly, it's instructive)
|
||||
CFLAGS += $(call cc-option,-fno-builtin-printf,)
|
||||
|
||||
# clang-9 does not like "str" + N and "if (CONFIG_ITEM && cond)" constructs
|
||||
ifeq ($(CC),clang)
|
||||
CFLAGS += $(call cc-option,-Wno-string-plus-int -Wno-constant-logical-operand)
|
||||
endif
|
||||
|
||||
# FIXME: These warnings are at least partially to be concerned about and should
|
||||
# be fixed..
|
||||
#CFLAGS += $(call cc-option,-Wconversion,)
|
||||
@@ -145,20 +123,15 @@ CFLAGS += --sysroot=$(CONFIG_SYSROOT)
|
||||
export SYSROOT=$(CONFIG_SYSROOT)
|
||||
endif
|
||||
|
||||
# libm may be needed for dc, awk, ntpd
|
||||
LDLIBS += m
|
||||
# Android has no separate crypt library
|
||||
# gcc-4.2.1 fails if we try to feed C source on stdin:
|
||||
# echo 'int main(void){return 0;}' | $(CC) $(CFLAGS) -lcrypt -o /dev/null -xc -
|
||||
# fall back to using a temp file:
|
||||
CRYPT_AVAILABLE := $(shell echo 'int main(void){return 0;}' >bb_libtest.c; $(CC) $(CFLAGS) $(CFLAGS_busybox) -lcrypt -o /dev/null bb_libtest.c >/dev/null 2>&1 && echo "y"; rm bb_libtest.c)
|
||||
RT_AVAILABLE := $(shell echo 'int main(void){return 0;}' >bb_libtest.c; $(CC) $(CFLAGS) $(CFLAGS_busybox) -lrt -o /dev/null bb_libtest.c >/dev/null 2>&1 && echo "y"; rm bb_libtest.c)
|
||||
CRYPT_AVAILABLE := $(shell echo 'int main(void){return 0;}' >crypttest.c; $(CC) $(CFLAGS) -lcrypt -o /dev/null crypttest.c >/dev/null 2>&1 && echo "y"; rm crypttest.c)
|
||||
ifeq ($(CRYPT_AVAILABLE),y)
|
||||
LDLIBS += crypt
|
||||
endif
|
||||
# librt may be needed for clock_gettime()
|
||||
ifeq ($(RT_AVAILABLE),y)
|
||||
LDLIBS += rt
|
||||
LDLIBS += m crypt
|
||||
else
|
||||
LDLIBS += m
|
||||
endif
|
||||
|
||||
# libpam may use libpthread, libdl and/or libaudit.
|
||||
@@ -183,12 +156,6 @@ CPPFLAGS += $(SELINUX_CFLAGS)
|
||||
LDLIBS += $(if $(SELINUX_LIBS),$(SELINUX_LIBS:-l%=%),$(SELINUX_PC_MODULES:lib%=%))
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_FEATURE_NSLOOKUP_BIG),y)
|
||||
ifneq (,$(findstring linux,$(shell $(CC) $(CFLAGS) -dumpmachine)))
|
||||
LDLIBS += resolv
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_EFENCE),y)
|
||||
LDLIBS += efence
|
||||
endif
|
||||
@@ -212,6 +179,11 @@ LDFLAGS += $(strip $(subst ",,$(CONFIG_EXTRA_LDFLAGS)))
|
||||
#"))
|
||||
endif
|
||||
|
||||
ifneq ($(CONFIG_EXTRA_LDLIBS),)
|
||||
LDLIBS += $(strip $(subst ",,$(CONFIG_EXTRA_LDLIBS)))
|
||||
#"))
|
||||
endif
|
||||
|
||||
# Busybox is a stack-fatty so make sure we increase default size
|
||||
# TODO: use "make stksizes" to find & fix big stack users
|
||||
# (we stole scripts/checkstack.pl from the kernel... thanks guys!)
|
||||
|
||||
+3
-3
@@ -72,7 +72,7 @@ brctl - noexec
|
||||
bunzip2 - runner
|
||||
bzcat - runner
|
||||
bzip2 - runner
|
||||
cal - noexec. can be runner: cal -n9999
|
||||
cal - runner: cal -n9999
|
||||
cat - runner: cat HUGEFILE
|
||||
chat - longterm (when used as intended - talking to modem over stdin/out)
|
||||
chattr - noexec. runner
|
||||
@@ -89,7 +89,7 @@ clear - NOFORK
|
||||
cmp - runner
|
||||
comm - runner
|
||||
conspy - interactive, longterm
|
||||
cp - noexec. sometimes runner
|
||||
cp - noexec. runner
|
||||
cpio - runner
|
||||
crond - daemon
|
||||
crontab - longterm (runs $EDITOR), leaks: open+xasprintf
|
||||
@@ -255,7 +255,7 @@ mount - suid
|
||||
mountpoint - noexec. leaks: option -n "print dev name": find_block_device -> readdir+xstrdup
|
||||
mpstat - longterm: "mpstat 1" runs indefinitely
|
||||
mt - hardware
|
||||
mv - noexec. sometimes runner
|
||||
mv - noexec candidate, runner
|
||||
nameif - noexec. openlog(), leaks: config_open2+ioctl_or_perror_and_die
|
||||
nbd-client - noexec
|
||||
nc - runner
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
exec >NOFORK_NOEXEC.lst1
|
||||
|
||||
false && grep -Fv 'NOFORK' NOFORK_NOEXEC.lst \
|
||||
| grep -v 'noexec.' | grep -v 'noexec$' \
|
||||
| grep -v ' suid' \
|
||||
| grep -v ' daemon' \
|
||||
| grep -v ' longterm' \
|
||||
| grep rare
|
||||
|
||||
echo === nofork candidate
|
||||
grep -F 'nofork candidate' NOFORK_NOEXEC.lst \
|
||||
|
||||
echo === noexec candidate
|
||||
grep -F 'noexec candidate' NOFORK_NOEXEC.lst \
|
||||
|
||||
echo === ^C
|
||||
grep -F '^C' NOFORK_NOEXEC.lst \
|
||||
| grep -F ' - ' \
|
||||
|
||||
echo === talks
|
||||
grep -F 'talks' NOFORK_NOEXEC.lst \
|
||||
| grep -F ' - ' \
|
||||
|
||||
echo ===
|
||||
grep -Fv 'NOFORK' NOFORK_NOEXEC.lst \
|
||||
| grep '^[^ ][^ ]* - ' \
|
||||
| grep -v 'noexec.' | grep -v ' - noexec$' \
|
||||
| grep -v ' suid' \
|
||||
| grep -v ' daemon' \
|
||||
| grep -v 'longterm' \
|
||||
| grep -v 'interactive' \
|
||||
| grep -v 'hardware' \
|
||||
+4
-28
@@ -10,7 +10,6 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
@@ -20,10 +19,6 @@
|
||||
#undef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
|
||||
|
||||
#ifndef PATH_MAX
|
||||
#define PATH_MAX 1024
|
||||
#endif
|
||||
|
||||
#include "../include/autoconf.h"
|
||||
#include "../include/applet_metadata.h"
|
||||
|
||||
@@ -56,7 +51,7 @@ static int cmp_name(const void *a, const void *b)
|
||||
static int str_isalnum_(const char *s)
|
||||
{
|
||||
while (*s) {
|
||||
if (!isalnum((unsigned char)*s) && *s != '_')
|
||||
if (!isalnum(*s) && *s != '_')
|
||||
return 0;
|
||||
s++;
|
||||
}
|
||||
@@ -66,7 +61,6 @@ static int str_isalnum_(const char *s)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j;
|
||||
char tmp1[PATH_MAX], tmp2[PATH_MAX];
|
||||
|
||||
// In find_applet_by_name(), before linear search, narrow it down
|
||||
// by looking at N "equidistant" names. With ~350 applets:
|
||||
@@ -88,18 +82,9 @@ int main(int argc, char **argv)
|
||||
|
||||
qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
|
||||
|
||||
for (i = j = 0; i < NUM_APPLETS-1; ++i) {
|
||||
if (cmp_name(applets+i, applets+i+1) == 0) {
|
||||
fprintf(stderr, "%s: duplicate applet name '%s'\n", argv[0],
|
||||
applets[i].name);
|
||||
j = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (j != 0 || !argv[1])
|
||||
if (!argv[1])
|
||||
return 1;
|
||||
snprintf(tmp1, PATH_MAX, "%s.%u.new", argv[1], (int) getpid());
|
||||
i = open(tmp1, O_WRONLY | O_TRUNC | O_CREAT, 0666);
|
||||
i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
|
||||
if (i < 0)
|
||||
return 1;
|
||||
dup2(i, 1);
|
||||
@@ -224,21 +209,12 @@ int main(int argc, char **argv)
|
||||
// fclose(fp);
|
||||
// }
|
||||
// if (strcmp(line_old, line_new) != 0) {
|
||||
snprintf(tmp2, PATH_MAX, "%s.%u.new", argv[2], (int) getpid());
|
||||
fp = fopen(tmp2, "w");
|
||||
fp = fopen(argv[2], "w");
|
||||
if (!fp)
|
||||
return 1;
|
||||
fputs(line_new, fp);
|
||||
if (fclose(fp))
|
||||
return 1;
|
||||
// }
|
||||
}
|
||||
|
||||
if (fclose(stdout))
|
||||
return 1;
|
||||
if (rename(tmp1, argv[1]))
|
||||
return 1;
|
||||
if (rename(tmp2, argv[2]))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#!/bin/sh
|
||||
# Make busybox scripted applet list file.
|
||||
|
||||
# input $1: full path to Config.h
|
||||
# input $2: full path to applets.h
|
||||
# output (stdout): list of pathnames that should be linked to busybox
|
||||
|
||||
export LC_ALL=POSIX
|
||||
export LC_CTYPE=POSIX
|
||||
|
||||
CONFIG_H=${1:-include/autoconf.h}
|
||||
APPLETS_H=${2:-include/applets.h}
|
||||
$HOSTCC -E -DMAKE_SCRIPTS -include $CONFIG_H $APPLETS_H |
|
||||
awk '/^[ \t]*SCRIPT/{
|
||||
print $2
|
||||
}'
|
||||
@@ -19,6 +19,6 @@ int main(int argc, char **argv)
|
||||
|
||||
void bb_show_usage(void)
|
||||
{
|
||||
fputs_stdout(APPLET_full_usage "\n");
|
||||
fputs(APPLET_full_usage "\n", stdout);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
+32
-34
@@ -5,12 +5,9 @@ export LC_CTYPE=POSIX
|
||||
|
||||
prefix=$1
|
||||
if [ -z "$prefix" ]; then
|
||||
echo "usage: applets/install.sh DESTINATION TYPE [OPTS ...]"
|
||||
echo " TYPE is one of: --symlinks --hardlinks --binaries --scriptwrapper --none"
|
||||
echo " OPTS is one or more of: --cleanup --noclobber"
|
||||
echo "usage: applets/install.sh DESTINATION [--symlinks/--hardlinks/--binaries/--scriptwrapper]"
|
||||
exit 1
|
||||
fi
|
||||
shift # Keep only remaining options
|
||||
|
||||
# Source the configuration
|
||||
. ./.config
|
||||
@@ -24,23 +21,20 @@ scriptwrapper="n"
|
||||
binaries="n"
|
||||
cleanup="0"
|
||||
noclobber="0"
|
||||
while [ ${#} -gt 0 ]; do
|
||||
case "$1" in
|
||||
--hardlinks) linkopts="-f";;
|
||||
--symlinks) linkopts="-fs";;
|
||||
--binaries) binaries="y";;
|
||||
--scriptwrapper) scriptwrapper="y"; swrapall="y";;
|
||||
--sw-sh-hard) scriptwrapper="y"; linkopts="-f";;
|
||||
--sw-sh-sym) scriptwrapper="y"; linkopts="-fs";;
|
||||
--cleanup) cleanup="1";;
|
||||
--noclobber) noclobber="1";;
|
||||
--none) h="";;
|
||||
*) echo "Unknown install option: $1"; exit 1;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
case "$2" in
|
||||
--hardlinks) linkopts="-f";;
|
||||
--symlinks) linkopts="-fs";;
|
||||
--binaries) binaries="y";;
|
||||
--scriptwrapper) scriptwrapper="y";swrapall="y";;
|
||||
--sw-sh-hard) scriptwrapper="y";linkopts="-f";;
|
||||
--sw-sh-sym) scriptwrapper="y";linkopts="-fs";;
|
||||
--cleanup) cleanup="1";;
|
||||
--noclobber) noclobber="1";;
|
||||
"") h="";;
|
||||
*) echo "Unknown install option: $2"; exit 1;;
|
||||
esac
|
||||
|
||||
if [ -n "$DO_INSTALL_LIBS" ] && [ x"$DO_INSTALL_LIBS" != x"n" ]; then
|
||||
if [ -n "$DO_INSTALL_LIBS" ] && [ "$DO_INSTALL_LIBS" != "n" ]; then
|
||||
# get the target dir for the libs
|
||||
# assume it starts with lib
|
||||
libdir=$($CC -print-file-name=libc.so | \
|
||||
@@ -60,7 +54,7 @@ if [ -n "$DO_INSTALL_LIBS" ] && [ x"$DO_INSTALL_LIBS" != x"n" ]; then
|
||||
done
|
||||
fi
|
||||
|
||||
if [ x"$cleanup" = x"1" ] && [ -e "$prefix/bin/busybox" ]; then
|
||||
if [ "$cleanup" = "1" ] && [ -e "$prefix/bin/busybox" ]; then
|
||||
inode=`ls -i "$prefix/bin/busybox" | awk '{print $1}'`
|
||||
sub_shell_it=`
|
||||
cd "$prefix"
|
||||
@@ -83,13 +77,9 @@ install -m 755 busybox "$prefix/bin/busybox" || exit 1
|
||||
for i in $h; do
|
||||
appdir=`dirname "$i"`
|
||||
app=`basename "$i"`
|
||||
if [ x"$noclobber" = x"1" ] && ([ -e "$prefix/$i" ] || [ -h "$prefix/$i" ]); then
|
||||
echo " $prefix/$i already exists"
|
||||
continue
|
||||
fi
|
||||
mkdir -p "$prefix/$appdir" || exit 1
|
||||
if [ x"$scriptwrapper" = x"y" ]; then
|
||||
if [ x"$swrapall" != x"y" ] && [ x"$i" = x"/bin/sh" ]; then
|
||||
if [ "$scriptwrapper" = "y" ]; then
|
||||
if [ "$swrapall" != "y" ] && [ "$i" = "/bin/sh" ]; then
|
||||
ln $linkopts busybox "$prefix/$i" || exit 1
|
||||
else
|
||||
rm -f "$prefix/$i"
|
||||
@@ -97,17 +87,21 @@ for i in $h; do
|
||||
chmod +x "$prefix/$i"
|
||||
fi
|
||||
echo " $prefix/$i"
|
||||
elif [ x"$binaries" = x"y" ]; then
|
||||
elif [ "$binaries" = "y" ]; then
|
||||
# Copy the binary over rather
|
||||
if [ -e "$sharedlib_dir/$app" ]; then
|
||||
echo " Copying $sharedlib_dir/$app to $prefix/$i"
|
||||
cp -pPR "$sharedlib_dir/$app" "$prefix/$i" || exit 1
|
||||
if [ -e $sharedlib_dir/$app ]; then
|
||||
if [ "$noclobber" = "0" ] || [ ! -e "$prefix/$i" ]; then
|
||||
echo " Copying $sharedlib_dir/$app to $prefix/$i"
|
||||
cp -pPR $sharedlib_dir/$app $prefix/$i || exit 1
|
||||
else
|
||||
echo " $prefix/$i already exists"
|
||||
fi
|
||||
else
|
||||
echo "Error: Could not find $sharedlib_dir/$app"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
if [ x"$linkopts" = x"-f" ]; then
|
||||
if [ "$2" = "--hardlinks" ]; then
|
||||
bb_path="$prefix/bin/busybox"
|
||||
else
|
||||
case "$appdir" in
|
||||
@@ -129,8 +123,12 @@ for i in $h; do
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
echo " $prefix/$i -> $bb_path"
|
||||
ln $linkopts "$bb_path" "$prefix/$i" || exit 1
|
||||
if [ "$noclobber" = "0" ] || [ ! -e "$prefix/$i" ]; then
|
||||
echo " $prefix/$i -> $bb_path"
|
||||
ln $linkopts "$bb_path" "$prefix/$i" || exit 1
|
||||
else
|
||||
echo " $prefix/$i already exists"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ int main(void)
|
||||
} else {
|
||||
printf(", ");
|
||||
}
|
||||
printf("%s", usage_array[i].aname);
|
||||
printf(usage_array[i].aname);
|
||||
col += len2;
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
This directory contains examples of applets implemented as shell scripts.
|
||||
|
||||
So far these scripts are not hooked to the build system and are not
|
||||
installed by "make install". If you want to use them,
|
||||
you need to install them by hand.
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
# TODO: use getopt to avoid parsing options as filenames,
|
||||
# and to support -- and --help
|
||||
[ $# -ne 0 ] && DASH_I=-i
|
||||
sed $DASH_I -e 's/\r$//' "$@"
|
||||
@@ -1,39 +0,0 @@
|
||||
#!/bin/sh
|
||||
MIMFILE="Mimfile"
|
||||
if [ $# -ge 2 ] && [ "$1" = "-f" ]
|
||||
then
|
||||
MIMFILE="$2"
|
||||
shift 2
|
||||
fi
|
||||
exec <"$MIMFILE" || exit 1
|
||||
{
|
||||
INCASE=false
|
||||
while read -r REPLY
|
||||
do
|
||||
case $REPLY in
|
||||
*:)
|
||||
if ! $INCASE
|
||||
then
|
||||
printf '[ $# -eq 0 ] && set -- "%s"
|
||||
TARGET="$1"
|
||||
shift
|
||||
case "$TARGET" in
|
||||
' "${REPLY%:}"
|
||||
else
|
||||
printf ';;\n'
|
||||
fi
|
||||
printf '%s)\n' "${REPLY%:}"
|
||||
INCASE=true
|
||||
;;
|
||||
"") ;;
|
||||
*) printf '%s\n' "${REPLY##[ ]}";;
|
||||
esac
|
||||
done
|
||||
$INCASE && printf ';;\n'
|
||||
printf '*)
|
||||
echo "Unknown command $TARGET"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
'
|
||||
} | sh -s "$@"
|
||||
+2
-1
@@ -1,3 +1,4 @@
|
||||
cat /etc/nologin.txt 2>/dev/null || echo This account is not available
|
||||
#!/bin/sh
|
||||
cat /etc/nologin.txt 2>/dev/null || echo "This account is not available"
|
||||
sleep 5
|
||||
exit 1
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
# TODO: use getopt to avoid parsing options as filenames,
|
||||
# and to support -- and --help
|
||||
for i in "$@"
|
||||
do
|
||||
sed -e '1!G;h;$!d' "$i"
|
||||
done
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
# TODO: use getopt to avoid parsing options as filenames,
|
||||
# and to support -- and --help
|
||||
[ $# -ne 0 ] && DASH_I=-i
|
||||
sed $DASH_I -e 's/$/\r/' "$@"
|
||||
+1
-15
@@ -2,20 +2,6 @@
|
||||
# Build system
|
||||
# ==========================================================================
|
||||
|
||||
# Allow i486 insns (basically, bswap insn)
|
||||
# Do not try to tune for 486+ (might add padding)
|
||||
CFLAGS += $(call cc-option,-march=i486 -mtune=i386,)
|
||||
|
||||
ifeq ($(CONFIG_STACK_OPTIMIZATION_386),y)
|
||||
# -mpreferred-stack-boundary=2 is essential in preventing gcc 4.2.x
|
||||
# from aligning stack to 16 bytes. (Which is gcc's way of supporting SSE).
|
||||
CFLAGS += $(call cc-option,-mpreferred-stack-boundary=2,)
|
||||
endif
|
||||
|
||||
# "Control how GCC aligns variables.
|
||||
# Supported values for type are compat uses increased alignment value
|
||||
# compatible uses GCC 4.8 and earlier, abi uses alignment value as specified by the psABI,
|
||||
# and cacheline uses increased alignment value to match the cache line size.
|
||||
# compat is the default."
|
||||
# "abi" seems to be somewhat successful in preventing oversealous data alignment.
|
||||
CFLAGS += $(call cc-option,-malign-data=abi,)
|
||||
CFLAGS += $(call cc-option,-march=i386 -mpreferred-stack-boundary=2,)
|
||||
@@ -1,11 +0,0 @@
|
||||
# ==========================================================================
|
||||
# Build system
|
||||
# ==========================================================================
|
||||
|
||||
# "Control how GCC aligns variables.
|
||||
# Supported values for type are compat uses increased alignment value
|
||||
# compatible uses GCC 4.8 and earlier, abi uses alignment value as specified by the psABI,
|
||||
# and cacheline uses increased alignment value to match the cache line size.
|
||||
# compat is the default."
|
||||
# "abi" seems to be somewhat successful in preventing oversealous data alignment.
|
||||
CFLAGS += $(call cc-option,-malign-data=abi,)
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see docs/Kconfig-language.txt.
|
||||
# see scripts/kbuild/config-language.txt.
|
||||
#
|
||||
|
||||
menu "Archival Utilities"
|
||||
|
||||
+30
-34
@@ -48,9 +48,19 @@
|
||||
|
||||
//kbuild:lib-$(CONFIG_AR) += ar.o
|
||||
|
||||
//usage:#define ar_trivial_usage
|
||||
//usage: "[-o] [-v] [-p] [-t] [-x] ARCHIVE FILES"
|
||||
//usage:#define ar_full_usage "\n\n"
|
||||
//usage: "Extract or list FILES from an ar archive\n"
|
||||
//usage: "\n -o Preserve original dates"
|
||||
//usage: "\n -p Extract to stdout"
|
||||
//usage: "\n -t List"
|
||||
//usage: "\n -x Extract"
|
||||
//usage: "\n -v Verbose"
|
||||
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
#include "ar_.h"
|
||||
#include "ar.h"
|
||||
|
||||
#if ENABLE_FEATURE_AR_CREATE
|
||||
/* filter out entries with same names as specified on the command line */
|
||||
@@ -196,10 +206,9 @@ static int write_ar_archive(archive_handle_t *handle)
|
||||
|
||||
static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
|
||||
{
|
||||
char mode[12];
|
||||
const char *mode = bb_mode_string(file_header->mode);
|
||||
char *mtime;
|
||||
|
||||
bb_mode_string(mode, file_header->mode);
|
||||
mtime = ctime(&file_header->mtime);
|
||||
mtime[16] = ' ';
|
||||
memmove(&mtime[17], &mtime[20], 4);
|
||||
@@ -211,36 +220,23 @@ static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
|
||||
);
|
||||
}
|
||||
|
||||
//usage:#define ar_trivial_usage
|
||||
//usage: "x|p|t"IF_FEATURE_AR_CREATE("|r")" [-ov] ARCHIVE [FILE]..."
|
||||
//usage:#define ar_full_usage "\n\n"
|
||||
//usage: "Extract or list FILEs from an ar archive"IF_FEATURE_AR_CREATE(", or create it")"\n"
|
||||
//usage: "\n x Extract"
|
||||
//usage: "\n p Extract to stdout"
|
||||
//usage: "\n t List"
|
||||
//usage: IF_FEATURE_AR_CREATE(
|
||||
//usage: "\n r Create"
|
||||
//usage: )
|
||||
//usage: "\n -o Restore mtime"
|
||||
//usage: "\n -v Verbose"
|
||||
#define AR_OPT_VERBOSE (1 << 0)
|
||||
#define AR_OPT_PRESERVE_DATE (1 << 1)
|
||||
/* "ar r" implies create, but warns about it. c suppresses warning.
|
||||
* bbox accepts but ignores it: */
|
||||
#define AR_OPT_CREATE (1 << 2)
|
||||
|
||||
#define AR_CMD_PRINT (1 << 3)
|
||||
#define FIRST_CMD AR_CMD_PRINT
|
||||
#define AR_CMD_LIST (1 << 4)
|
||||
#define AR_CMD_EXTRACT (1 << 5)
|
||||
#define AR_CMD_INSERT (1 << 6)
|
||||
|
||||
int ar_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int ar_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
archive_handle_t *archive_handle;
|
||||
unsigned opt, t;
|
||||
enum {
|
||||
OPT_VERBOSE = (1 << 0),
|
||||
OPT_PRESERVE_DATE = (1 << 1),
|
||||
/* "ar r" implies create, but warns about it. c suppresses warning.
|
||||
* bbox accepts but ignores it: */
|
||||
OPT_CREATE = (1 << 2),
|
||||
CMD_PRINT = (1 << 3),
|
||||
FIRST_CMD = CMD_PRINT,
|
||||
CMD_LIST = (1 << 4),
|
||||
CMD_EXTRACT = (1 << 5),
|
||||
CMD_INSERT = ((1 << 6) * ENABLE_FEATURE_AR_CREATE),
|
||||
};
|
||||
|
||||
archive_handle = init_handle();
|
||||
|
||||
@@ -260,26 +256,26 @@ int ar_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (t & (t-1)) /* more than one of p,t,x[,r] are specified */
|
||||
bb_show_usage();
|
||||
|
||||
if (opt & CMD_PRINT) {
|
||||
if (opt & AR_CMD_PRINT) {
|
||||
archive_handle->action_data = data_extract_to_stdout;
|
||||
}
|
||||
if (opt & CMD_LIST) {
|
||||
if (opt & AR_CMD_LIST) {
|
||||
archive_handle->action_header = header_list;
|
||||
}
|
||||
if (opt & CMD_EXTRACT) {
|
||||
if (opt & AR_CMD_EXTRACT) {
|
||||
archive_handle->action_data = data_extract_all;
|
||||
}
|
||||
if (opt & OPT_PRESERVE_DATE) {
|
||||
if (opt & AR_OPT_PRESERVE_DATE) {
|
||||
archive_handle->ah_flags |= ARCHIVE_RESTORE_DATE;
|
||||
}
|
||||
if (opt & OPT_VERBOSE) {
|
||||
if (opt & AR_OPT_VERBOSE) {
|
||||
archive_handle->action_header = header_verbose_list_ar;
|
||||
}
|
||||
#if ENABLE_FEATURE_AR_CREATE
|
||||
archive_handle->ar__name = *argv;
|
||||
#endif
|
||||
archive_handle->src_fd = xopen(*argv++,
|
||||
(opt & CMD_INSERT)
|
||||
(opt & AR_CMD_INSERT)
|
||||
? O_RDWR | O_CREAT
|
||||
: O_RDONLY
|
||||
);
|
||||
@@ -291,7 +287,7 @@ int ar_main(int argc UNUSED_PARAM, char **argv)
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_AR_CREATE
|
||||
if (opt & CMD_INSERT)
|
||||
if (opt & AR_CMD_INSERT)
|
||||
return write_ar_archive(archive_handle);
|
||||
#endif
|
||||
|
||||
|
||||
+49
-42
@@ -21,6 +21,19 @@
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
/* Note: must be kept in sync with archival/lzop.c */
|
||||
enum {
|
||||
OPT_STDOUT = 1 << 0,
|
||||
OPT_FORCE = 1 << 1,
|
||||
/* only some decompressors: */
|
||||
OPT_KEEP = 1 << 2,
|
||||
OPT_VERBOSE = 1 << 3,
|
||||
OPT_QUIET = 1 << 4,
|
||||
OPT_DECOMPRESS = 1 << 5,
|
||||
OPT_TEST = 1 << 6,
|
||||
SEAMLESS_MAGIC = (1 << 31) * ENABLE_ZCAT * SEAMLESS_COMPRESSION,
|
||||
};
|
||||
|
||||
static
|
||||
int open_to_or_warn(int to_fd, const char *filename, int flags, int mode)
|
||||
{
|
||||
@@ -59,7 +72,7 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
|
||||
/* Open src */
|
||||
if (filename) {
|
||||
if (!(option_mask32 & BBUNPK_SEAMLESS_MAGIC)) {
|
||||
if (!(option_mask32 & SEAMLESS_MAGIC)) {
|
||||
if (stat(filename, &stat_buf) != 0) {
|
||||
err_name:
|
||||
bb_simple_perror_msg(filename);
|
||||
@@ -78,15 +91,15 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
xmove_fd(fd, STDIN_FILENO);
|
||||
}
|
||||
} else
|
||||
if (option_mask32 & BBUNPK_SEAMLESS_MAGIC) {
|
||||
if (option_mask32 & SEAMLESS_MAGIC) {
|
||||
/* "clever zcat" on stdin */
|
||||
if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Special cases: test, stdout */
|
||||
if (option_mask32 & (BBUNPK_OPT_STDOUT|BBUNPK_OPT_TEST)) {
|
||||
if (option_mask32 & BBUNPK_OPT_TEST)
|
||||
if (option_mask32 & (OPT_STDOUT|OPT_TEST)) {
|
||||
if (option_mask32 & OPT_TEST)
|
||||
if (open_to_or_warn(STDOUT_FILENO, bb_dev_null, O_WRONLY, 0))
|
||||
xfunc_die();
|
||||
filename = NULL;
|
||||
@@ -101,7 +114,7 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
}
|
||||
|
||||
/* -f: overwrite existing output files */
|
||||
if (option_mask32 & BBUNPK_OPT_FORCE) {
|
||||
if (option_mask32 & OPT_FORCE) {
|
||||
unlink(new_name);
|
||||
}
|
||||
|
||||
@@ -113,12 +126,12 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
}
|
||||
|
||||
/* Check that the input is sane */
|
||||
if (!(option_mask32 & BBUNPK_OPT_FORCE) && isatty(STDIN_FILENO)) {
|
||||
bb_simple_error_msg_and_die("compressed data not read from terminal, "
|
||||
if (!(option_mask32 & OPT_FORCE) && isatty(STDIN_FILENO)) {
|
||||
bb_error_msg_and_die("compressed data not read from terminal, "
|
||||
"use -f to force it");
|
||||
}
|
||||
|
||||
if (!(option_mask32 & BBUNPK_SEAMLESS_MAGIC)) {
|
||||
if (!(option_mask32 & SEAMLESS_MAGIC)) {
|
||||
init_transformer_state(&xstate);
|
||||
/*xstate.signature_skipped = 0; - already is */
|
||||
/*xstate.src_fd = STDIN_FILENO; - already is */
|
||||
@@ -132,7 +145,7 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
xfunc_die();
|
||||
}
|
||||
|
||||
if (!(option_mask32 & BBUNPK_OPT_STDOUT))
|
||||
if (!(option_mask32 & OPT_STDOUT))
|
||||
xclose(STDOUT_FILENO); /* with error check! */
|
||||
|
||||
if (filename) {
|
||||
@@ -163,7 +176,7 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
}
|
||||
/* Extreme bloat for gunzip compat */
|
||||
/* Some users do want this info... */
|
||||
if (ENABLE_DESKTOP && (option_mask32 & BBUNPK_OPT_VERBOSE)) {
|
||||
if (ENABLE_DESKTOP && (option_mask32 & OPT_VERBOSE)) {
|
||||
unsigned percent = status
|
||||
? ((uoff_t)stat_buf.st_size * 100u / (unsigned long long)status)
|
||||
: 0;
|
||||
@@ -175,7 +188,7 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
}
|
||||
/* Delete _source_ file */
|
||||
del = filename;
|
||||
if (option_mask32 & BBUNPK_OPT_KEEP) /* ... unless -k */
|
||||
if (option_mask32 & OPT_KEEP) /* ... unless -k */
|
||||
del = NULL;
|
||||
}
|
||||
if (del)
|
||||
@@ -186,7 +199,7 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
}
|
||||
} while (*argv && *++argv);
|
||||
|
||||
if (option_mask32 & BBUNPK_OPT_STDOUT)
|
||||
if (option_mask32 & OPT_STDOUT)
|
||||
xclose(STDOUT_FILENO); /* with error check! */
|
||||
|
||||
return exitcode;
|
||||
@@ -219,7 +232,7 @@ char* FAST_FUNC make_new_name_generic(char *filename, const char *expected_ext)
|
||||
//usage:#define uncompress_trivial_usage
|
||||
//usage: "[-cf] [FILE]..."
|
||||
//usage:#define uncompress_full_usage "\n\n"
|
||||
//usage: "Decompress FILEs (or stdin)\n"
|
||||
//usage: "Decompress .Z file[s]\n"
|
||||
//usage: "\n -c Write to stdout"
|
||||
//usage: "\n -f Overwrite"
|
||||
|
||||
@@ -284,7 +297,7 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
|
||||
//usage: "\n -c Write to stdout"
|
||||
//usage: "\n -f Force"
|
||||
//usage: "\n -k Keep input files"
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage: "\n -t Test file integrity"
|
||||
//usage:
|
||||
//usage:#define gunzip_example_usage
|
||||
//usage: "$ ls -la /tmp/BusyBox*\n"
|
||||
@@ -299,7 +312,7 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
|
||||
//usage: "Decompress to stdout"
|
||||
|
||||
//config:config GUNZIP
|
||||
//config: bool "gunzip (11 kb)"
|
||||
//config: bool "gunzip (12 kb)"
|
||||
//config: default y
|
||||
//config: select FEATURE_GZIP_DECOMPRESS
|
||||
//config: help
|
||||
@@ -308,7 +321,7 @@ int uncompress_main(int argc UNUSED_PARAM, char **argv)
|
||||
//config: an archive, without decompressing it.
|
||||
//config:
|
||||
//config:config ZCAT
|
||||
//config: bool "zcat (24 kb)"
|
||||
//config: bool "zcat (25 kb)"
|
||||
//config: default y
|
||||
//config: select FEATURE_GZIP_DECOMPRESS
|
||||
//config: help
|
||||
@@ -376,9 +389,9 @@ int gunzip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int gunzip_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
#if ENABLE_FEATURE_GUNZIP_LONG_OPTIONS
|
||||
getopt32long(argv, BBUNPK_OPTSTR "dtn", gunzip_longopts);
|
||||
getopt32long(argv, "cfkvqdtn", gunzip_longopts);
|
||||
#else
|
||||
getopt32(argv, BBUNPK_OPTSTR "dtn");
|
||||
getopt32(argv, "cfkvqdtn");
|
||||
#endif
|
||||
argv += optind;
|
||||
|
||||
@@ -386,8 +399,8 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
|
||||
* Normally, "zcat" is just "gunzip -c".
|
||||
* But if seamless magic is enabled, then we are much more clever.
|
||||
*/
|
||||
if (ENABLE_ZCAT && applet_name[1] == 'c')
|
||||
option_mask32 |= BBUNPK_OPT_STDOUT | BBUNPK_SEAMLESS_MAGIC;
|
||||
if (ENABLE_ZCAT && (!ENABLE_GUNZIP || applet_name[1] == 'c'))
|
||||
option_mask32 |= OPT_STDOUT | SEAMLESS_MAGIC;
|
||||
|
||||
return bbunpack(argv, unpack_gz_stream, make_new_name_gunzip, /*unused:*/ NULL);
|
||||
}
|
||||
@@ -407,15 +420,13 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
|
||||
//usage: "\n -c Write to stdout"
|
||||
//usage: "\n -f Force"
|
||||
//usage: "\n -k Keep input files"
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage:
|
||||
//usage:#define bzcat_trivial_usage
|
||||
//usage: "[FILE]..."
|
||||
//usage:#define bzcat_full_usage "\n\n"
|
||||
//usage: "Decompress to stdout"
|
||||
|
||||
//config:config BUNZIP2
|
||||
//config: bool "bunzip2 (8.7 kb)"
|
||||
//config: bool "bunzip2 (8.8 kb)"
|
||||
//config: default y
|
||||
//config: select FEATURE_BZIP2_DECOMPRESS
|
||||
//config: help
|
||||
@@ -429,7 +440,7 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
|
||||
//config: should probably say N here.
|
||||
//config:
|
||||
//config:config BZCAT
|
||||
//config: bool "bzcat (8.7 kb)"
|
||||
//config: bool "bzcat (8.8 kb)"
|
||||
//config: default y
|
||||
//config: select FEATURE_BZIP2_DECOMPRESS
|
||||
//config: help
|
||||
@@ -442,10 +453,10 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
|
||||
int bunzip2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int bunzip2_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
getopt32(argv, BBUNPK_OPTSTR "dt");
|
||||
getopt32(argv, "cfkvqdt");
|
||||
argv += optind;
|
||||
if (ENABLE_BZCAT && (!ENABLE_BUNZIP2 || applet_name[2] == 'c')) /* bzcat */
|
||||
option_mask32 |= BBUNPK_OPT_STDOUT;
|
||||
option_mask32 |= OPT_STDOUT;
|
||||
|
||||
return bbunpack(argv, unpack_bz2_stream, make_new_name_generic, "bz2");
|
||||
}
|
||||
@@ -463,21 +474,19 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
|
||||
//usage:#define unlzma_trivial_usage
|
||||
//usage: "[-cfk] [FILE]..."
|
||||
//usage:#define unlzma_full_usage "\n\n"
|
||||
//usage: "Decompress FILEs (or stdin)\n"
|
||||
//usage: "Decompress FILE (or stdin)\n"
|
||||
//usage: "\n -c Write to stdout"
|
||||
//usage: "\n -f Force"
|
||||
//usage: "\n -k Keep input files"
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage:
|
||||
//usage:#define lzma_trivial_usage
|
||||
//usage: "-d [-cfk] [FILE]..."
|
||||
//usage:#define lzma_full_usage "\n\n"
|
||||
//usage: "Decompress FILEs (or stdin)\n"
|
||||
//usage: "Decompress FILE (or stdin)\n"
|
||||
//usage: "\n -d Decompress"
|
||||
//usage: "\n -c Write to stdout"
|
||||
//usage: "\n -f Force"
|
||||
//usage: "\n -k Keep input files"
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage:
|
||||
//usage:#define lzcat_trivial_usage
|
||||
//usage: "[FILE]..."
|
||||
@@ -485,7 +494,7 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
|
||||
//usage: "Decompress to stdout"
|
||||
|
||||
//config:config UNLZMA
|
||||
//config: bool "unlzma (7.5 kb)"
|
||||
//config: bool "unlzma (8.6 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
|
||||
@@ -494,7 +503,7 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
|
||||
//config: compressors.
|
||||
//config:
|
||||
//config:config LZCAT
|
||||
//config: bool "lzcat (7.5 kb)"
|
||||
//config: bool "lzcat (8.5 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Alias to "unlzma -c".
|
||||
@@ -517,15 +526,15 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
|
||||
int unlzma_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int unlzma_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
IF_LZMA(int opts =) getopt32(argv, BBUNPK_OPTSTR "dt");
|
||||
IF_LZMA(int opts =) getopt32(argv, "cfkvqdt");
|
||||
# if ENABLE_LZMA
|
||||
/* lzma without -d or -t? */
|
||||
if (applet_name[2] == 'm' && !(opts & (BBUNPK_OPT_DECOMPRESS|BBUNPK_OPT_TEST)))
|
||||
if (applet_name[2] == 'm' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
|
||||
bb_show_usage();
|
||||
# endif
|
||||
/* lzcat? */
|
||||
if (ENABLE_LZCAT && applet_name[2] == 'c')
|
||||
option_mask32 |= BBUNPK_OPT_STDOUT;
|
||||
option_mask32 |= OPT_STDOUT;
|
||||
|
||||
argv += optind;
|
||||
return bbunpack(argv, unpack_lzma_stream, make_new_name_generic, "lzma");
|
||||
@@ -536,21 +545,19 @@ int unlzma_main(int argc UNUSED_PARAM, char **argv)
|
||||
//usage:#define unxz_trivial_usage
|
||||
//usage: "[-cfk] [FILE]..."
|
||||
//usage:#define unxz_full_usage "\n\n"
|
||||
//usage: "Decompress FILEs (or stdin)\n"
|
||||
//usage: "Decompress FILE (or stdin)\n"
|
||||
//usage: "\n -c Write to stdout"
|
||||
//usage: "\n -f Force"
|
||||
//usage: "\n -k Keep input files"
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage:
|
||||
//usage:#define xz_trivial_usage
|
||||
//usage: "-d [-cfk] [FILE]..."
|
||||
//usage:#define xz_full_usage "\n\n"
|
||||
//usage: "Decompress FILEs (or stdin)\n"
|
||||
//usage: "Decompress FILE (or stdin)\n"
|
||||
//usage: "\n -d Decompress"
|
||||
//usage: "\n -c Write to stdout"
|
||||
//usage: "\n -f Force"
|
||||
//usage: "\n -k Keep input files"
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage:
|
||||
//usage:#define xzcat_trivial_usage
|
||||
//usage: "[FILE]..."
|
||||
@@ -587,15 +594,15 @@ int unlzma_main(int argc UNUSED_PARAM, char **argv)
|
||||
int unxz_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int unxz_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
IF_XZ(int opts =) getopt32(argv, BBUNPK_OPTSTR "dt");
|
||||
IF_XZ(int opts =) getopt32(argv, "cfkvqdt");
|
||||
# if ENABLE_XZ
|
||||
/* xz without -d or -t? */
|
||||
if (applet_name[2] == '\0' && !(opts & (BBUNPK_OPT_DECOMPRESS|BBUNPK_OPT_TEST)))
|
||||
if (applet_name[2] == '\0' && !(opts & (OPT_DECOMPRESS|OPT_TEST)))
|
||||
bb_show_usage();
|
||||
# endif
|
||||
/* xzcat? */
|
||||
if (ENABLE_XZCAT && applet_name[2] == 'c')
|
||||
option_mask32 |= BBUNPK_OPT_STDOUT;
|
||||
option_mask32 |= OPT_STDOUT;
|
||||
|
||||
argv += optind;
|
||||
return bbunpack(argv, unpack_xz_stream, make_new_name_generic, "xz");
|
||||
|
||||
+23
-49
@@ -7,7 +7,7 @@
|
||||
* about bzip2 library code.
|
||||
*/
|
||||
//config:config BZIP2
|
||||
//config: bool "bzip2 (16 kb)"
|
||||
//config: bool "bzip2 (18 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: bzip2 is a compression utility using the Burrows-Wheeler block
|
||||
@@ -19,23 +19,6 @@
|
||||
//config: Unless you have a specific application which requires bzip2, you
|
||||
//config: should probably say N here.
|
||||
//config:
|
||||
//config:config BZIP2_SMALL
|
||||
//config: int "Trade bytes for speed (0:fast, 9:small)"
|
||||
//config: default 8 # all "fast or small" options default to small
|
||||
//config: range 0 9
|
||||
//config: depends on BZIP2
|
||||
//config: help
|
||||
//config: Trade code size versus speed.
|
||||
//config: Approximate values with gcc-6.3.0 "bzip -9" compressing
|
||||
//config: linux-4.15.tar were:
|
||||
//config: value time (sec) code size (386)
|
||||
//config: 9 (smallest) 70.11 7687
|
||||
//config: 8 67.93 8091
|
||||
//config: 7 67.88 8405
|
||||
//config: 6 67.78 8624
|
||||
//config: 5 67.05 9427
|
||||
//config: 4-0 (fastest) 64.14 12083
|
||||
//config:
|
||||
//config:config FEATURE_BZIP2_DECOMPRESS
|
||||
//config: bool "Enable decompression"
|
||||
//config: default y
|
||||
@@ -50,28 +33,22 @@
|
||||
//kbuild:lib-$(CONFIG_BZIP2) += bzip2.o
|
||||
|
||||
//usage:#define bzip2_trivial_usage
|
||||
//usage: "[-cfk" IF_FEATURE_BZIP2_DECOMPRESS("dt") "123456789] [FILE]..."
|
||||
//usage: "[OPTIONS] [FILE]..."
|
||||
//usage:#define bzip2_full_usage "\n\n"
|
||||
//usage: "Compress FILEs (or stdin) with bzip2 algorithm\n"
|
||||
//usage: "\n -1..9 Compression level"
|
||||
//usage: IF_FEATURE_BZIP2_DECOMPRESS(
|
||||
//usage: "\n -d Decompress"
|
||||
//usage: "\n -t Test file integrity"
|
||||
//usage: )
|
||||
//usage: "\n -c Write to stdout"
|
||||
//usage: "\n -f Force"
|
||||
//usage: "\n -k Keep input files"
|
||||
//usage: IF_FEATURE_BZIP2_DECOMPRESS(
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage: )
|
||||
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
#if CONFIG_BZIP2_SMALL >= 4
|
||||
#define BZIP2_SPEED (9 - CONFIG_BZIP2_SMALL)
|
||||
#else
|
||||
#define BZIP2_SPEED 5
|
||||
#endif
|
||||
#define CONFIG_BZIP2_FAST 1
|
||||
|
||||
/* Speed test:
|
||||
* Compiled with gcc 4.2.1, run on Athlon 64 1800 MHz (512K L2 cache).
|
||||
@@ -79,7 +56,7 @@
|
||||
* (time to compress gcc-4.2.1.tar is 126.4% compared to bbox).
|
||||
* At SPEED 5 difference is 32.7%.
|
||||
*
|
||||
* Test run of all BZIP2_SPEED values on a 11Mb text file:
|
||||
* Test run of all CONFIG_BZIP2_FAST values on a 11Mb text file:
|
||||
* Size Time (3 runs)
|
||||
* 0: 10828 4.145 4.146 4.148
|
||||
* 1: 11097 3.845 3.860 3.861
|
||||
@@ -106,13 +83,15 @@
|
||||
/* No point in being shy and having very small buffer here.
|
||||
* bzip2 internal buffers are much bigger anyway, hundreds of kbytes.
|
||||
* If iobuf is several pages long, malloc() may use mmap,
|
||||
* making iobuf page aligned and thus (maybe) have one memcpy less
|
||||
* making iobuf is page aligned and thus (maybe) have one memcpy less
|
||||
* if kernel is clever enough.
|
||||
*/
|
||||
enum {
|
||||
IOBUF_SIZE = 8 * 1024
|
||||
};
|
||||
|
||||
static uint8_t level;
|
||||
|
||||
/* NB: compressStream() has to return -1 on errors, not die.
|
||||
* bbunpack() will correctly clean up in this case
|
||||
* (delete incomplete .bz2 file)
|
||||
@@ -147,7 +126,7 @@ IF_DESKTOP(long long) int bz_write(bz_stream *strm, void* rbuf, ssize_t rlen, vo
|
||||
if (n2 != n) {
|
||||
if (n2 >= 0)
|
||||
errno = 0; /* prevent bogus error message */
|
||||
bb_simple_perror_msg(n2 >= 0 ? "short write" : bb_msg_write_error);
|
||||
bb_perror_msg(n2 >= 0 ? "short write" : bb_msg_write_error);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -164,7 +143,6 @@ static
|
||||
IF_DESKTOP(long long) int FAST_FUNC compressStream(transformer_state_t *xstate UNUSED_PARAM)
|
||||
{
|
||||
IF_DESKTOP(long long) int total;
|
||||
unsigned opt, level;
|
||||
ssize_t count;
|
||||
bz_stream bzs; /* it's small */
|
||||
#define strm (&bzs)
|
||||
@@ -173,23 +151,12 @@ IF_DESKTOP(long long) int FAST_FUNC compressStream(transformer_state_t *xstate U
|
||||
#define wbuf (iobuf + IOBUF_SIZE)
|
||||
|
||||
iobuf = xmalloc(2 * IOBUF_SIZE);
|
||||
|
||||
opt = option_mask32 >> (BBUNPK_OPTSTRLEN IF_FEATURE_BZIP2_DECOMPRESS(+ 2) + 2);
|
||||
/* skipped BBUNPK_OPTSTR, "dt" and "zs" bits */
|
||||
opt |= 0x100; /* if nothing else, assume -9 */
|
||||
level = 0;
|
||||
for (;;) {
|
||||
level++;
|
||||
if (opt & 1) break;
|
||||
opt >>= 1;
|
||||
}
|
||||
|
||||
BZ2_bzCompressInit(strm, level);
|
||||
|
||||
while (1) {
|
||||
count = full_read(STDIN_FILENO, rbuf, IOBUF_SIZE);
|
||||
if (count < 0) {
|
||||
bb_simple_perror_msg(bb_msg_read_error);
|
||||
bb_perror_msg(bb_msg_read_error);
|
||||
total = -1;
|
||||
break;
|
||||
}
|
||||
@@ -229,19 +196,26 @@ int bzip2_main(int argc UNUSED_PARAM, char **argv)
|
||||
*/
|
||||
|
||||
opt = getopt32(argv, "^"
|
||||
/* Must match BBUNPK_foo constants! */
|
||||
BBUNPK_OPTSTR IF_FEATURE_BZIP2_DECOMPRESS("dt") "zs123456789"
|
||||
/* Must match bbunzip's constants OPT_STDOUT, OPT_FORCE! */
|
||||
"cfkv" IF_FEATURE_BZIP2_DECOMPRESS("dt") "123456789qzs"
|
||||
"\0" "s2" /* -s means -2 (compatibility) */
|
||||
);
|
||||
#if ENABLE_FEATURE_BZIP2_DECOMPRESS /* bunzip2_main may not be visible... */
|
||||
if (opt & (BBUNPK_OPT_DECOMPRESS|BBUNPK_OPT_TEST)) /* -d and/or -t */
|
||||
if (opt & 0x30) // -d and/or -t
|
||||
return bunzip2_main(argc, argv);
|
||||
opt >>= 6;
|
||||
#else
|
||||
/* clear "decompress" and "test" bits (or bbunpack() can get confused) */
|
||||
/* in !BZIP2_DECOMPRESS config, these bits are -zs and are unused */
|
||||
option_mask32 = opt & ~(BBUNPK_OPT_DECOMPRESS|BBUNPK_OPT_TEST);
|
||||
opt >>= 4;
|
||||
#endif
|
||||
opt = (uint8_t)opt; /* isolate bits for -1..-8 */
|
||||
opt |= 0x100; /* if nothing else, assume -9 */
|
||||
level = 1;
|
||||
while (!(opt & 1)) {
|
||||
level++;
|
||||
opt >>= 1;
|
||||
}
|
||||
|
||||
argv += optind;
|
||||
option_mask32 &= 0xf; /* ignore all except -cfkv */
|
||||
return bbunpack(argv, compressStream, append_ext, "bz2");
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Denys Vlasenko <vda.linux@googlemail.com>
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-$(CONFIG_FEATURE_TAR_CREATE) += chksum_and_xwrite_tar_header.o
|
||||
//kbuild:lib-$(CONFIG_SMEMCAP) += chksum_and_xwrite_tar_header.o
|
||||
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
void FAST_FUNC chksum_and_xwrite_tar_header(int fd, struct tar_header_t *hp)
|
||||
{
|
||||
/* POSIX says that checksum is done on unsigned bytes
|
||||
* (Sun and HP-UX gets it wrong... more details in
|
||||
* GNU tar source) */
|
||||
const unsigned char *cp;
|
||||
unsigned int chksum, size;
|
||||
|
||||
strcpy(hp->magic, "ustar ");
|
||||
|
||||
/* Calculate and store the checksum (the sum of all of the bytes of
|
||||
* the header). The checksum field must be filled with blanks for the
|
||||
* calculation. The checksum field is formatted differently from the
|
||||
* other fields: it has 6 digits, a NUL, then a space -- rather than
|
||||
* digits, followed by a NUL like the other fields... */
|
||||
memset(hp->chksum, ' ', sizeof(hp->chksum));
|
||||
cp = (const unsigned char *) hp;
|
||||
chksum = 0;
|
||||
size = sizeof(*hp);
|
||||
do { chksum += *cp++; } while (--size);
|
||||
sprintf(hp->chksum, "%06o", chksum);
|
||||
|
||||
xwrite(fd, hp, sizeof(*hp));
|
||||
}
|
||||
+5
-69
@@ -11,7 +11,7 @@
|
||||
* Only supports new ASCII and CRC formats
|
||||
*/
|
||||
//config:config CPIO
|
||||
//config: bool "cpio (15 kb)"
|
||||
//config: bool "cpio (14 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: cpio is an archival utility program used to create, modify, and
|
||||
@@ -38,20 +38,6 @@
|
||||
//config: depends on FEATURE_CPIO_O
|
||||
//config: help
|
||||
//config: Passthrough mode. Rarely used.
|
||||
//config:
|
||||
//config:config FEATURE_CPIO_IGNORE_DEVNO
|
||||
//config: bool "Support --ignore-devno like GNU cpio"
|
||||
//config: default y
|
||||
//config: depends on FEATURE_CPIO_O && LONG_OPTS
|
||||
//config: help
|
||||
//config: Optionally ignore device numbers when creating archives.
|
||||
//config:
|
||||
//config:config FEATURE_CPIO_RENUMBER_INODES
|
||||
//config: bool "Support --renumber-inodes like GNU cpio"
|
||||
//config: default y
|
||||
//config: depends on FEATURE_CPIO_O && LONG_OPTS
|
||||
//config: help
|
||||
//config: Optionally renumber inodes when creating archives.
|
||||
|
||||
//applet:IF_CPIO(APPLET(cpio, BB_DIR_BIN, BB_SUID_DROP))
|
||||
|
||||
@@ -78,22 +64,14 @@
|
||||
//usage: "\n -p DIR Copy files to DIR"
|
||||
//usage: )
|
||||
//usage: "\nOptions:"
|
||||
//usage: IF_FEATURE_CPIO_O(
|
||||
//usage: "\n -H newc Archive format"
|
||||
//usage: )
|
||||
//usage: "\n -d Make leading directories"
|
||||
//usage: "\n -m Restore mtime"
|
||||
//usage: "\n -m Preserve mtime"
|
||||
//usage: "\n -v Verbose"
|
||||
//usage: "\n -u Overwrite"
|
||||
//usage: "\n -F FILE Input (-t,-i,-p) or output (-o) file"
|
||||
//usage: "\n -R USER[:GRP] Set owner of created files"
|
||||
//usage: "\n -L Dereference symlinks"
|
||||
//usage: "\n -0 NUL terminated input"
|
||||
//usage: IF_FEATURE_CPIO_IGNORE_DEVNO(
|
||||
//usage: "\n --ignore-devno"
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_CPIO_RENUMBER_INODES(
|
||||
//usage: "\n --renumber-inodes"
|
||||
//usage: IF_FEATURE_CPIO_O(
|
||||
//usage: "\n -H newc Archive format"
|
||||
//usage: )
|
||||
|
||||
/* GNU cpio 2.9 --help (abridged):
|
||||
@@ -182,22 +160,17 @@ enum {
|
||||
IF_FEATURE_CPIO_P(OPTBIT_PASSTHROUGH,)
|
||||
IF_LONG_OPTS( OPTBIT_QUIET ,)
|
||||
IF_LONG_OPTS( OPTBIT_2STDOUT ,)
|
||||
IF_FEATURE_CPIO_IGNORE_DEVNO(OPTBIT_IGNORE_DEVNO,)
|
||||
IF_FEATURE_CPIO_RENUMBER_INODES(OPTBIT_RENUMBER_INODES,)
|
||||
OPT_CREATE = IF_FEATURE_CPIO_O((1 << OPTBIT_CREATE )) + 0,
|
||||
OPT_FORMAT = IF_FEATURE_CPIO_O((1 << OPTBIT_FORMAT )) + 0,
|
||||
OPT_PASSTHROUGH = IF_FEATURE_CPIO_P((1 << OPTBIT_PASSTHROUGH)) + 0,
|
||||
OPT_QUIET = IF_LONG_OPTS( (1 << OPTBIT_QUIET )) + 0,
|
||||
OPT_2STDOUT = IF_LONG_OPTS( (1 << OPTBIT_2STDOUT )) + 0,
|
||||
OPT_IGNORE_DEVNO = IF_FEATURE_CPIO_IGNORE_DEVNO((1 << OPTBIT_IGNORE_DEVNO)) + 0,
|
||||
OPT_RENUMBER_INODES = IF_FEATURE_CPIO_RENUMBER_INODES((1 << OPTBIT_RENUMBER_INODES)) + 0,
|
||||
};
|
||||
|
||||
#define OPTION_STR "it0uvdmLF:R:"
|
||||
|
||||
struct globals {
|
||||
struct bb_uidgid_t owner_ugid;
|
||||
ino_t next_inode;
|
||||
} FIX_ALIASING;
|
||||
#define G (*(struct globals*)bb_common_bufsiz1)
|
||||
void BUG_cpio_globals_too_big(void);
|
||||
@@ -231,9 +204,6 @@ static NOINLINE int cpio_o(void)
|
||||
struct inodes_s *next;
|
||||
struct name_s *names;
|
||||
struct stat st;
|
||||
#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
|
||||
ino_t mapped_inode;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct inodes_s *links = NULL;
|
||||
@@ -288,10 +258,6 @@ static NOINLINE int cpio_o(void)
|
||||
l = xzalloc(sizeof(*l));
|
||||
l->st = st;
|
||||
l->next = links;
|
||||
#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
|
||||
if (option_mask32 & OPT_RENUMBER_INODES)
|
||||
l->mapped_inode = ++G.next_inode;
|
||||
#endif
|
||||
links = l;
|
||||
break;
|
||||
}
|
||||
@@ -310,11 +276,6 @@ static NOINLINE int cpio_o(void)
|
||||
free(line);
|
||||
continue;
|
||||
}
|
||||
#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
|
||||
else if (option_mask32 & OPT_RENUMBER_INODES) {
|
||||
st.st_ino = ++G.next_inode;
|
||||
}
|
||||
#endif
|
||||
} else { /* line == NULL: EOF */
|
||||
next_link:
|
||||
if (links) {
|
||||
@@ -322,10 +283,6 @@ static NOINLINE int cpio_o(void)
|
||||
st = links->st;
|
||||
name = links->names->name;
|
||||
links->names = links->names->next;
|
||||
#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
|
||||
if (links->mapped_inode)
|
||||
st.st_ino = links->mapped_inode;
|
||||
#endif
|
||||
/* GNU cpio is reported to emit file data
|
||||
* only for the last instance. Mimic that. */
|
||||
if (links->names == NULL)
|
||||
@@ -345,11 +302,6 @@ static NOINLINE int cpio_o(void)
|
||||
}
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_CPIO_IGNORE_DEVNO
|
||||
if (option_mask32 & OPT_IGNORE_DEVNO)
|
||||
st.st_dev = st.st_rdev = 0;
|
||||
#endif
|
||||
|
||||
bytes += printf("070701"
|
||||
"%08X%08X%08X%08X%08X%08X%08X"
|
||||
"%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
|
||||
@@ -422,15 +374,8 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
#endif
|
||||
"owner\0" Required_argument "R"
|
||||
"verbose\0" No_argument "v"
|
||||
"null\0" No_argument "0"
|
||||
"quiet\0" No_argument "\xff"
|
||||
"to-stdout\0" No_argument "\xfe"
|
||||
#if ENABLE_FEATURE_CPIO_IGNORE_DEVNO
|
||||
"ignore-devno\0" No_argument "\xfd"
|
||||
#endif
|
||||
#if ENABLE_FEATURE_CPIO_RENUMBER_INODES
|
||||
"renumber-inodes\0" No_argument "\xfc"
|
||||
#endif
|
||||
;
|
||||
#endif
|
||||
|
||||
@@ -470,8 +415,7 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (argv[0] == NULL)
|
||||
bb_show_usage();
|
||||
if (opt & OPT_CREATE_LEADING_DIR)
|
||||
/* GNU cpio 2.13: "cpio -d -p a/b/c" works */
|
||||
bb_make_directory(argv[0], -1, FILEUTILS_RECUR);
|
||||
mkdir(argv[0], 0777);
|
||||
/* Crude existence check:
|
||||
* close(xopen(argv[0], O_RDONLY | O_DIRECTORY));
|
||||
* We can also xopen, fstat, IS_DIR, later fchdir.
|
||||
@@ -481,11 +425,6 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
* a diffrerent problem earlier.
|
||||
* This is good enough for now.
|
||||
*/
|
||||
//FIXME: GNU cpio -d -p DIR does not immediately create DIR -
|
||||
//it just prepends "DIR/" to the names of files to be created.
|
||||
//The first file (fails to) be copied, and then the -d logic
|
||||
//triggers and creates all necessary directories.
|
||||
//IOW: bare "cpio -d -p DIR" + ^C shouldn't create anything.
|
||||
#if !BB_MMU
|
||||
pp.rd = 3;
|
||||
pp.wr = 4;
|
||||
@@ -569,12 +508,9 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
|
||||
continue;
|
||||
|
||||
create_links_from_list(archive_handle->link_placeholders);
|
||||
|
||||
if (archive_handle->cpio__blocks != (off_t)-1
|
||||
&& !(opt & OPT_QUIET)
|
||||
) {
|
||||
fflush_all();
|
||||
fprintf(stderr, "%"OFF_FMT"u blocks\n", archive_handle->cpio__blocks);
|
||||
}
|
||||
|
||||
|
||||
+9
-32
@@ -26,7 +26,7 @@
|
||||
* - (unknown, please let me know when you find any)
|
||||
*/
|
||||
//config:config DPKG
|
||||
//config: bool "dpkg (43 kb)"
|
||||
//config: bool "dpkg (44 kb)"
|
||||
//config: default y
|
||||
//config: select FEATURE_SEAMLESS_GZ
|
||||
//config: help
|
||||
@@ -487,7 +487,7 @@ static void add_split_dependencies(common_node_t *parent_node, const char *whole
|
||||
} else if (strncmp(version, ">=", offset_ch) == 0) {
|
||||
edge->operator = VER_MORE_EQUAL;
|
||||
} else {
|
||||
bb_simple_error_msg_and_die("illegal operator");
|
||||
bb_error_msg_and_die("illegal operator");
|
||||
}
|
||||
}
|
||||
/* skip to start of version numbers */
|
||||
@@ -730,7 +730,7 @@ static void set_status(const unsigned status_node_num, const char *new_value, co
|
||||
status = new_value_num;
|
||||
break;
|
||||
default:
|
||||
bb_simple_error_msg_and_die("DEBUG ONLY: this shouldnt happen");
|
||||
bb_error_msg_and_die("DEBUG ONLY: this shouldnt happen");
|
||||
}
|
||||
|
||||
new_status = xasprintf("%s %s %s", name_hashtable[want], name_hashtable[flag], name_hashtable[status]);
|
||||
@@ -944,10 +944,10 @@ static void write_status_file(deb_file_t **deb_file)
|
||||
/* Create a separate backfile to dpkg */
|
||||
if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) {
|
||||
if (errno != ENOENT)
|
||||
bb_simple_error_msg_and_die("can't create backup status file");
|
||||
bb_error_msg_and_die("can't create backup status file");
|
||||
/* Its ok if renaming the status file fails because status
|
||||
* file doesn't exist, maybe we are starting from scratch */
|
||||
bb_simple_error_msg("no status file found, creating new one");
|
||||
bb_error_msg("no status file found, creating new one");
|
||||
}
|
||||
|
||||
xrename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status");
|
||||
@@ -1204,27 +1204,6 @@ static char **create_list(const char *filename)
|
||||
return file_list;
|
||||
}
|
||||
|
||||
/** This tests if the filename is an "important" directory, which might be symlinked.
|
||||
* Debians dpkg test if directories are used by other packages, this
|
||||
* implementation doesn't and would remove for ex. an lib -> usr/lib symlink.
|
||||
*/
|
||||
static int is_builtin_exclude(const char *name)
|
||||
{
|
||||
if (*name++ != '/')
|
||||
return 0;
|
||||
if (index_in_strings(".\0" "etc\0" "opt\0" "srv\0" "var\0" "var/lib\0",
|
||||
name) >= 0)
|
||||
return 1;
|
||||
if (is_prefixed_with(name, "usr/")) {
|
||||
name += sizeof("usr/") - 1;
|
||||
if (is_prefixed_with(name, "local/"))
|
||||
name += sizeof("local/") - 1;
|
||||
}
|
||||
|
||||
return index_in_strings("bin\0" "lib\0" "lib32\0" "lib64\0" "sbin\0",
|
||||
name) >= 0;
|
||||
}
|
||||
|
||||
/* maybe i should try and hook this into remove_file.c somehow */
|
||||
static int remove_file_array(char **remove_names, char **exclude_names)
|
||||
{
|
||||
@@ -1236,8 +1215,6 @@ static int remove_file_array(char **remove_names, char **exclude_names)
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; remove_names[i] != NULL; i++) {
|
||||
if (is_builtin_exclude(remove_names[i]))
|
||||
continue;
|
||||
if (exclude_names != NULL) {
|
||||
for (j = 0; exclude_names[j] != NULL; j++) {
|
||||
if (strcmp(remove_names[i], exclude_names[j]) == 0) {
|
||||
@@ -1304,7 +1281,7 @@ postrm abort-install <old_version>
|
||||
postrm abort-upgrade <old_version>
|
||||
postrm disappear <overwriter> <version>
|
||||
*/
|
||||
static const char *const all_control_files[] ALIGN_PTR = {
|
||||
static const char *const all_control_files[] = {
|
||||
"preinst", "postinst", "prerm", "postrm",
|
||||
"list", "md5sums", "shlibs", "conffiles",
|
||||
"config", "templates"
|
||||
@@ -1839,7 +1816,7 @@ int dpkg_main(int argc UNUSED_PARAM, char **argv)
|
||||
init_archive_deb_control(archive_handle);
|
||||
deb_file[deb_count]->control_file = deb_extract_control_file_to_buffer(archive_handle, control_list);
|
||||
if (deb_file[deb_count]->control_file == NULL) {
|
||||
bb_simple_error_msg_and_die("can't extract control file");
|
||||
bb_error_msg_and_die("can't extract control file");
|
||||
}
|
||||
deb_file[deb_count]->filename = xstrdup(argv[0]);
|
||||
package_num = fill_package_struct(deb_file[deb_count]->control_file);
|
||||
@@ -1902,13 +1879,13 @@ int dpkg_main(int argc UNUSED_PARAM, char **argv)
|
||||
argv++;
|
||||
}
|
||||
if (!deb_count)
|
||||
bb_simple_error_msg_and_die("no package files specified");
|
||||
bb_error_msg_and_die("no package files specified");
|
||||
deb_file[deb_count] = NULL;
|
||||
|
||||
/* Check that the deb file arguments are installable */
|
||||
if (!(opt & OPT_force_ignore_depends)) {
|
||||
if (!check_deps(deb_file, 0 /*, deb_count*/)) {
|
||||
bb_simple_error_msg_and_die("dependency check failed");
|
||||
bb_error_msg_and_die("dependency check failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config DPKG_DEB
|
||||
//config: bool "dpkg-deb (30 kb)"
|
||||
//config: bool "dpkg_deb"
|
||||
//config: default y
|
||||
//config: select FEATURE_SEAMLESS_GZ
|
||||
//config: help
|
||||
@@ -28,7 +28,7 @@
|
||||
//usage: "\n -f Print control fields"
|
||||
//usage: "\n -e Extract control files to DIR (default: ./DEBIAN)"
|
||||
//usage: "\n -x Extract files to DIR (no default)"
|
||||
//usage: "\n -X Verbose extract"
|
||||
//usage: "\n -X Verbose -x"
|
||||
//usage:
|
||||
//usage:#define dpkg_deb_example_usage
|
||||
//usage: "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
|
||||
|
||||
+271
-276
File diff suppressed because it is too large.
Load diff
@@ -91,7 +91,6 @@ lib-$(CONFIG_FEATURE_SEAMLESS_LZMA) += open_transformer.o decompress_unlzma.
|
||||
lib-$(CONFIG_FEATURE_SEAMLESS_XZ) += open_transformer.o decompress_unxz.o
|
||||
lib-$(CONFIG_FEATURE_COMPRESS_USAGE) += open_transformer.o decompress_bunzip2.o
|
||||
lib-$(CONFIG_FEATURE_COMPRESS_BBCONFIG) += open_transformer.o decompress_bunzip2.o
|
||||
lib-$(CONFIG_FEATURE_SH_EMBEDDED_SCRIPTS) += open_transformer.o decompress_bunzip2.o
|
||||
|
||||
ifneq ($(lib-y),)
|
||||
lib-y += $(COMMON_FILES)
|
||||
|
||||
+153
-160
@@ -107,14 +107,15 @@ void fallbackSimpleSort(uint32_t* fmap,
|
||||
#define FALLBACK_QSORT_SMALL_THRESH 10
|
||||
#define FALLBACK_QSORT_STACK_SIZE 100
|
||||
|
||||
static NOINLINE
|
||||
static
|
||||
void fallbackQSort3(uint32_t* fmap,
|
||||
uint32_t* eclass,
|
||||
int32_t loSt,
|
||||
int32_t hiSt)
|
||||
{
|
||||
int32_t sp;
|
||||
uint32_t r;
|
||||
int32_t unLo, unHi, ltLo, gtHi, n, m;
|
||||
int32_t sp, lo, hi;
|
||||
uint32_t med, r, r3;
|
||||
int32_t stackLo[FALLBACK_QSORT_STACK_SIZE];
|
||||
int32_t stackHi[FALLBACK_QSORT_STACK_SIZE];
|
||||
|
||||
@@ -124,11 +125,6 @@ void fallbackQSort3(uint32_t* fmap,
|
||||
fpush(loSt, hiSt);
|
||||
|
||||
while (sp > 0) {
|
||||
int32_t unLo, unHi, ltLo, gtHi, n, m;
|
||||
int32_t lo, hi;
|
||||
uint32_t med;
|
||||
uint32_t r3;
|
||||
|
||||
AssertH(sp < FALLBACK_QSORT_STACK_SIZE - 1, 1004);
|
||||
|
||||
fpop(lo, hi);
|
||||
@@ -165,7 +161,7 @@ void fallbackQSort3(uint32_t* fmap,
|
||||
ltLo++;
|
||||
unLo++;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if (n > 0) break;
|
||||
unLo++;
|
||||
}
|
||||
@@ -176,7 +172,7 @@ void fallbackQSort3(uint32_t* fmap,
|
||||
mswap(fmap[unHi], fmap[gtHi]);
|
||||
gtHi--; unHi--;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if (n < 0) break;
|
||||
unHi--;
|
||||
}
|
||||
@@ -231,19 +227,17 @@ void fallbackQSort3(uint32_t* fmap,
|
||||
#define UNALIGNED_BH(zz) ((zz) & 0x01f)
|
||||
|
||||
static
|
||||
void fallbackSort(EState* state)
|
||||
void fallbackSort(uint32_t* fmap,
|
||||
uint32_t* eclass,
|
||||
uint32_t* bhtab,
|
||||
int32_t nblock)
|
||||
{
|
||||
int32_t ftab[257];
|
||||
int32_t ftabCopy[256];
|
||||
int32_t H, i, j, k, l, r, cc, cc1;
|
||||
int32_t nNotDone;
|
||||
int32_t nBhtab;
|
||||
/* params */
|
||||
uint32_t *const fmap = state->arr1;
|
||||
uint32_t *const eclass = state->arr2;
|
||||
#define eclass8 ((uint8_t*)eclass)
|
||||
uint32_t *const bhtab = state->ftab;
|
||||
const int32_t nblock = state->nblock;
|
||||
uint8_t* eclass8 = (uint8_t*)eclass;
|
||||
|
||||
/*
|
||||
* Initial 1-char radix sort to generate
|
||||
@@ -332,7 +326,7 @@ void fallbackSort(EState* state)
|
||||
if (cc != cc1) {
|
||||
SET_BH(i);
|
||||
cc = cc1;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -355,7 +349,6 @@ void fallbackSort(EState* state)
|
||||
eclass8[fmap[i]] = (uint8_t)j;
|
||||
}
|
||||
AssertH(j < 256, 1005);
|
||||
#undef eclass8
|
||||
}
|
||||
|
||||
#undef SET_BH
|
||||
@@ -374,25 +367,25 @@ void fallbackSort(EState* state)
|
||||
/*---------------------------------------------*/
|
||||
static
|
||||
NOINLINE
|
||||
int mainGtU(EState* state,
|
||||
int mainGtU(
|
||||
uint32_t i1,
|
||||
uint32_t i2)
|
||||
uint32_t i2,
|
||||
uint8_t* block,
|
||||
uint16_t* quadrant,
|
||||
uint32_t nblock,
|
||||
int32_t* budget)
|
||||
{
|
||||
int32_t k;
|
||||
uint8_t c1, c2;
|
||||
uint16_t s1, s2;
|
||||
|
||||
uint8_t *const block = state->block;
|
||||
uint16_t *const quadrant = state->quadrant;
|
||||
const int32_t nblock = state->nblock;
|
||||
|
||||
/* Loop unrolling here is actually very useful
|
||||
* (generated code is much simpler),
|
||||
* code size increase is only 270 bytes (i386)
|
||||
* but speeds up compression 10% overall
|
||||
*/
|
||||
|
||||
#if BZIP2_SPEED >= 1
|
||||
#if CONFIG_BZIP2_FAST >= 1
|
||||
|
||||
#define TIMES_8(code) \
|
||||
code; code; code; code; \
|
||||
@@ -442,7 +435,7 @@ int mainGtU(EState* state,
|
||||
if (i1 >= nblock) i1 -= nblock;
|
||||
if (i2 >= nblock) i2 -= nblock;
|
||||
|
||||
state->budget--;
|
||||
(*budget)--;
|
||||
k -= 8;
|
||||
} while (k >= 0);
|
||||
|
||||
@@ -459,45 +452,42 @@ int mainGtU(EState* state,
|
||||
* usually small, typically <= 20.
|
||||
*/
|
||||
static
|
||||
const uint32_t incs[14] ALIGN4 = {
|
||||
const int32_t incs[14] = {
|
||||
1, 4, 13, 40, 121, 364, 1093, 3280,
|
||||
9841, 29524, 88573, 265720,
|
||||
797161, 2391484
|
||||
};
|
||||
|
||||
static
|
||||
void mainSimpleSort(EState* state,
|
||||
void mainSimpleSort(uint32_t* ptr,
|
||||
uint8_t* block,
|
||||
uint16_t* quadrant,
|
||||
int32_t nblock,
|
||||
int32_t lo,
|
||||
int32_t hi,
|
||||
int32_t d)
|
||||
int32_t d,
|
||||
int32_t* budget)
|
||||
{
|
||||
uint32_t *const ptr = state->ptr;
|
||||
int32_t i, j, h, bigN, hp;
|
||||
uint32_t v;
|
||||
|
||||
/* At which increment to start? */
|
||||
int hp = 0;
|
||||
{
|
||||
int bigN = hi - lo;
|
||||
if (bigN <= 0)
|
||||
return;
|
||||
while (incs[hp] <= bigN)
|
||||
hp++;
|
||||
hp--;
|
||||
}
|
||||
bigN = hi - lo + 1;
|
||||
if (bigN < 2) return;
|
||||
|
||||
hp = 0;
|
||||
while (incs[hp] < bigN) hp++;
|
||||
hp--;
|
||||
|
||||
for (; hp >= 0; hp--) {
|
||||
int32_t i;
|
||||
unsigned h;
|
||||
|
||||
h = incs[hp];
|
||||
|
||||
i = lo + h;
|
||||
while (1) {
|
||||
unsigned j;
|
||||
unsigned v;
|
||||
|
||||
/*-- copy 1 --*/
|
||||
if (i > hi) break;
|
||||
v = ptr[i];
|
||||
j = i;
|
||||
while (mainGtU(state, ptr[j-h]+d, v+d)) {
|
||||
while (mainGtU(ptr[j-h]+d, v+d, block, quadrant, nblock, budget)) {
|
||||
ptr[j] = ptr[j-h];
|
||||
j = j - h;
|
||||
if (j <= (lo + h - 1)) break;
|
||||
@@ -506,23 +496,24 @@ void mainSimpleSort(EState* state,
|
||||
i++;
|
||||
|
||||
/* 1.5% overall speedup, +290 bytes */
|
||||
#if BZIP2_SPEED >= 3
|
||||
#if CONFIG_BZIP2_FAST >= 3
|
||||
/*-- copy 2 --*/
|
||||
if (i > hi) break;
|
||||
v = ptr[i];
|
||||
j = i;
|
||||
while (mainGtU(state, ptr[j-h]+d, v+d)) {
|
||||
while (mainGtU(ptr[j-h]+d, v+d, block, quadrant, nblock, budget)) {
|
||||
ptr[j] = ptr[j-h];
|
||||
j = j - h;
|
||||
if (j <= (lo + h - 1)) break;
|
||||
}
|
||||
ptr[j] = v;
|
||||
i++;
|
||||
|
||||
/*-- copy 3 --*/
|
||||
if (i > hi) break;
|
||||
v = ptr[i];
|
||||
j = i;
|
||||
while (mainGtU(state, ptr[j-h]+d, v+d)) {
|
||||
while (mainGtU(ptr[j-h]+d, v+d, block, quadrant, nblock, budget)) {
|
||||
ptr[j] = ptr[j-h];
|
||||
j = j - h;
|
||||
if (j <= (lo + h - 1)) break;
|
||||
@@ -530,7 +521,7 @@ void mainSimpleSort(EState* state,
|
||||
ptr[j] = v;
|
||||
i++;
|
||||
#endif
|
||||
if (state->budget < 0) return;
|
||||
if (*budget < 0) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -554,7 +545,7 @@ uint8_t mmed3(uint8_t a, uint8_t b, uint8_t c)
|
||||
t = a;
|
||||
a = b;
|
||||
b = t;
|
||||
}
|
||||
};
|
||||
/* here b >= a */
|
||||
if (b > c) {
|
||||
b = c;
|
||||
@@ -595,12 +586,15 @@ uint8_t mmed3(uint8_t a, uint8_t b, uint8_t c)
|
||||
#define MAIN_QSORT_STACK_SIZE 100
|
||||
|
||||
static NOINLINE
|
||||
void mainQSort3(EState* state,
|
||||
void mainQSort3(uint32_t* ptr,
|
||||
uint8_t* block,
|
||||
uint16_t* quadrant,
|
||||
int32_t nblock,
|
||||
int32_t loSt,
|
||||
int32_t hiSt
|
||||
/*int32_t dSt*/)
|
||||
int32_t hiSt,
|
||||
int32_t dSt,
|
||||
int32_t* budget)
|
||||
{
|
||||
enum { dSt = BZ_N_RADIX };
|
||||
int32_t unLo, unHi, ltLo, gtHi, n, m, med;
|
||||
int32_t sp, lo, hi, d;
|
||||
|
||||
@@ -612,9 +606,6 @@ void mainQSort3(EState* state,
|
||||
int32_t nextHi[3];
|
||||
int32_t nextD [3];
|
||||
|
||||
uint32_t *const ptr = state->ptr;
|
||||
uint8_t *const block = state->block;
|
||||
|
||||
sp = 0;
|
||||
mpush(loSt, hiSt, dSt);
|
||||
|
||||
@@ -625,8 +616,8 @@ void mainQSort3(EState* state,
|
||||
if (hi - lo < MAIN_QSORT_SMALL_THRESH
|
||||
|| d > MAIN_QSORT_DEPTH_THRESH
|
||||
) {
|
||||
mainSimpleSort(state, lo, hi, d);
|
||||
if (state->budget < 0)
|
||||
mainSimpleSort(ptr, block, quadrant, nblock, lo, hi, d, budget);
|
||||
if (*budget < 0)
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
@@ -647,8 +638,8 @@ void mainQSort3(EState* state,
|
||||
ltLo++;
|
||||
unLo++;
|
||||
continue;
|
||||
}
|
||||
if (n > 0) break;
|
||||
};
|
||||
if (n > 0) break;
|
||||
unLo++;
|
||||
}
|
||||
while (1) {
|
||||
@@ -660,8 +651,8 @@ void mainQSort3(EState* state,
|
||||
gtHi--;
|
||||
unHi--;
|
||||
continue;
|
||||
}
|
||||
if (n < 0) break;
|
||||
};
|
||||
if (n < 0) break;
|
||||
unHi--;
|
||||
}
|
||||
if (unLo > unHi)
|
||||
@@ -730,24 +721,28 @@ void mainQSort3(EState* state,
|
||||
#define CLEARMASK (~(SETMASK))
|
||||
|
||||
static NOINLINE
|
||||
void mainSort(EState* state)
|
||||
void mainSort(EState* state,
|
||||
uint32_t* ptr,
|
||||
uint8_t* block,
|
||||
uint16_t* quadrant,
|
||||
uint32_t* ftab,
|
||||
int32_t nblock,
|
||||
int32_t* budget)
|
||||
{
|
||||
int32_t i, j;
|
||||
int32_t i, j, k, ss, sb;
|
||||
uint8_t c1;
|
||||
int32_t numQSorted;
|
||||
uint16_t s;
|
||||
Bool bigDone[256];
|
||||
uint8_t runningOrder[256];
|
||||
/* bbox: moved to EState to save stack
|
||||
int32_t runningOrder[256];
|
||||
int32_t copyStart[256];
|
||||
int32_t copyEnd [256];
|
||||
*/
|
||||
#define runningOrder (state->mainSort__runningOrder)
|
||||
#define copyStart (state->mainSort__copyStart)
|
||||
#define copyEnd (state->mainSort__copyEnd)
|
||||
|
||||
uint32_t *const ptr = state->ptr;
|
||||
uint8_t *const block = state->block;
|
||||
uint32_t *const ftab = state->ftab;
|
||||
const int32_t nblock = state->nblock;
|
||||
uint16_t *const quadrant = state->quadrant;
|
||||
|
||||
/*-- set up the 2-byte frequency table --*/
|
||||
/* was: for (i = 65536; i >= 0; i--) ftab[i] = 0; */
|
||||
memset(ftab, 0, 65537 * sizeof(ftab[0]));
|
||||
@@ -755,25 +750,25 @@ void mainSort(EState* state)
|
||||
j = block[0] << 8;
|
||||
i = nblock - 1;
|
||||
/* 3%, +300 bytes */
|
||||
#if BZIP2_SPEED >= 2
|
||||
#if CONFIG_BZIP2_FAST >= 2
|
||||
for (; i >= 3; i -= 4) {
|
||||
quadrant[i] = 0;
|
||||
j = (j >> 8) | (((unsigned)block[i]) << 8);
|
||||
j = (j >> 8) | (((uint16_t)block[i]) << 8);
|
||||
ftab[j]++;
|
||||
quadrant[i-1] = 0;
|
||||
j = (j >> 8) | (((unsigned)block[i-1]) << 8);
|
||||
j = (j >> 8) | (((uint16_t)block[i-1]) << 8);
|
||||
ftab[j]++;
|
||||
quadrant[i-2] = 0;
|
||||
j = (j >> 8) | (((unsigned)block[i-2]) << 8);
|
||||
j = (j >> 8) | (((uint16_t)block[i-2]) << 8);
|
||||
ftab[j]++;
|
||||
quadrant[i-3] = 0;
|
||||
j = (j >> 8) | (((unsigned)block[i-3]) << 8);
|
||||
j = (j >> 8) | (((uint16_t)block[i-3]) << 8);
|
||||
ftab[j]++;
|
||||
}
|
||||
#endif
|
||||
for (; i >= 0; i--) {
|
||||
quadrant[i] = 0;
|
||||
j = (j >> 8) | (((unsigned)block[i]) << 8);
|
||||
j = (j >> 8) | (((uint16_t)block[i]) << 8);
|
||||
ftab[j]++;
|
||||
}
|
||||
|
||||
@@ -790,36 +785,33 @@ void mainSort(EState* state)
|
||||
ftab[i] = j;
|
||||
}
|
||||
|
||||
{
|
||||
unsigned s;
|
||||
s = block[0] << 8;
|
||||
i = nblock - 1;
|
||||
#if BZIP2_SPEED >= 2
|
||||
for (; i >= 3; i -= 4) {
|
||||
s = (s >> 8) | (block[i] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i;
|
||||
s = (s >> 8) | (block[i-1] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i-1;
|
||||
s = (s >> 8) | (block[i-2] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i-2;
|
||||
s = (s >> 8) | (block[i-3] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i-3;
|
||||
}
|
||||
s = block[0] << 8;
|
||||
i = nblock - 1;
|
||||
#if CONFIG_BZIP2_FAST >= 2
|
||||
for (; i >= 3; i -= 4) {
|
||||
s = (s >> 8) | (block[i] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i;
|
||||
s = (s >> 8) | (block[i-1] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i-1;
|
||||
s = (s >> 8) | (block[i-2] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i-2;
|
||||
s = (s >> 8) | (block[i-3] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i-3;
|
||||
}
|
||||
#endif
|
||||
for (; i >= 0; i--) {
|
||||
s = (s >> 8) | (block[i] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i;
|
||||
}
|
||||
for (; i >= 0; i--) {
|
||||
s = (s >> 8) | (block[i] << 8);
|
||||
j = ftab[s] - 1;
|
||||
ftab[s] = j;
|
||||
ptr[j] = i;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -833,23 +825,24 @@ void mainSort(EState* state)
|
||||
}
|
||||
|
||||
{
|
||||
int32_t vv;
|
||||
/* bbox: was: int32_t h = 1; */
|
||||
/* do h = 3 * h + 1; while (h <= 256); */
|
||||
unsigned h = 364;
|
||||
uint32_t h = 364;
|
||||
|
||||
do {
|
||||
/*h = h / 3;*/
|
||||
h = (h * 171) >> 9; /* bbox: fast h/3 */
|
||||
for (i = h; i <= 255; i++) {
|
||||
unsigned vv, jh;
|
||||
vv = runningOrder[i]; /* uint8[] */
|
||||
vv = runningOrder[i];
|
||||
j = i;
|
||||
while (jh = j - h, BIGFREQ(runningOrder[jh]) > BIGFREQ(vv)) {
|
||||
runningOrder[j] = runningOrder[jh];
|
||||
j = jh;
|
||||
if (j < h)
|
||||
break;
|
||||
while (BIGFREQ(runningOrder[j-h]) > BIGFREQ(vv)) {
|
||||
runningOrder[j] = runningOrder[j-h];
|
||||
j = j - h;
|
||||
if (j <= (h - 1))
|
||||
goto zero;
|
||||
}
|
||||
zero:
|
||||
runningOrder[j] = vv;
|
||||
}
|
||||
} while (h != 1);
|
||||
@@ -859,8 +852,9 @@ void mainSort(EState* state)
|
||||
* The main sorting loop.
|
||||
*/
|
||||
|
||||
for (i = 0; /*i <= 255*/; i++) {
|
||||
unsigned ss;
|
||||
numQSorted = 0;
|
||||
|
||||
for (i = 0; i <= 255; i++) {
|
||||
|
||||
/*
|
||||
* Process big buckets, starting with the least full.
|
||||
@@ -880,14 +874,17 @@ void mainSort(EState* state)
|
||||
*/
|
||||
for (j = 0; j <= 255; j++) {
|
||||
if (j != ss) {
|
||||
unsigned sb;
|
||||
sb = (ss << 8) + j;
|
||||
if (!(ftab[sb] & SETMASK)) {
|
||||
int32_t lo = ftab[sb] /*& CLEARMASK (redundant)*/;
|
||||
int32_t lo = ftab[sb] & CLEARMASK;
|
||||
int32_t hi = (ftab[sb+1] & CLEARMASK) - 1;
|
||||
if (hi > lo) {
|
||||
mainQSort3(state, lo, hi /*,BZ_N_RADIX*/);
|
||||
if (state->budget < 0) return;
|
||||
mainQSort3(
|
||||
ptr, block, quadrant, nblock,
|
||||
lo, hi, BZ_N_RADIX, budget
|
||||
);
|
||||
if (*budget < 0) return;
|
||||
numQSorted += (hi - lo + 1);
|
||||
}
|
||||
}
|
||||
ftab[sb] |= SETMASK;
|
||||
@@ -909,8 +906,6 @@ void mainSort(EState* state)
|
||||
copyEnd [j] = (ftab[(j << 8) + ss + 1] & CLEARMASK) - 1;
|
||||
}
|
||||
for (j = ftab[ss << 8] & CLEARMASK; j < copyStart[ss]; j++) {
|
||||
unsigned c1;
|
||||
int32_t k;
|
||||
k = ptr[j] - 1;
|
||||
if (k < 0)
|
||||
k += nblock;
|
||||
@@ -919,8 +914,6 @@ void mainSort(EState* state)
|
||||
ptr[copyStart[c1]++] = k;
|
||||
}
|
||||
for (j = (ftab[(ss+1) << 8] & CLEARMASK) - 1; j > copyEnd[ss]; j--) {
|
||||
unsigned c1;
|
||||
int32_t k;
|
||||
k = ptr[j]-1;
|
||||
if (k < 0)
|
||||
k += nblock;
|
||||
@@ -940,9 +933,6 @@ void mainSort(EState* state)
|
||||
for (j = 0; j <= 255; j++)
|
||||
ftab[(j << 8) + ss] |= SETMASK;
|
||||
|
||||
if (i == 255)
|
||||
break;
|
||||
|
||||
/*
|
||||
* Step 3:
|
||||
* The [ss] big bucket is now done. Record this fact,
|
||||
@@ -984,15 +974,15 @@ void mainSort(EState* state)
|
||||
*/
|
||||
bigDone[ss] = True;
|
||||
|
||||
{
|
||||
unsigned bbStart = ftab[ss << 8] & CLEARMASK;
|
||||
unsigned bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
|
||||
unsigned shifts = 0;
|
||||
if (i < 255) {
|
||||
int32_t bbStart = ftab[ss << 8] & CLEARMASK;
|
||||
int32_t bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
|
||||
int32_t shifts = 0;
|
||||
|
||||
while ((bbSize >> shifts) > 65534) shifts++;
|
||||
|
||||
for (j = bbSize-1; j >= 0; j--) {
|
||||
unsigned a2update = ptr[bbStart + j]; /* uint32[] */
|
||||
int32_t a2update = ptr[bbStart + j];
|
||||
uint16_t qVal = (uint16_t)(j >> shifts);
|
||||
quadrant[a2update] = qVal;
|
||||
if (a2update < BZ_N_OVERSHOOT)
|
||||
@@ -1025,24 +1015,31 @@ void mainSort(EState* state)
|
||||
* arr1[0 .. nblock-1] holds sorted order
|
||||
*/
|
||||
static NOINLINE
|
||||
int32_t BZ2_blockSort(EState* state)
|
||||
void BZ2_blockSort(EState* s)
|
||||
{
|
||||
/* In original bzip2 1.0.4, it's a parameter, but 30
|
||||
* (which was the default) should work ok. */
|
||||
enum { wfact = 30 };
|
||||
unsigned i;
|
||||
int32_t origPtr = origPtr;
|
||||
|
||||
if (state->nblock >= 10000) {
|
||||
uint32_t* ptr = s->ptr;
|
||||
uint8_t* block = s->block;
|
||||
uint32_t* ftab = s->ftab;
|
||||
int32_t nblock = s->nblock;
|
||||
uint16_t* quadrant;
|
||||
int32_t budget;
|
||||
int32_t i;
|
||||
|
||||
if (nblock < 10000) {
|
||||
fallbackSort(s->arr1, s->arr2, ftab, nblock);
|
||||
} else {
|
||||
/* Calculate the location for quadrant, remembering to get
|
||||
* the alignment right. Assumes that &(block[0]) is at least
|
||||
* 2-byte aligned -- this should be ok since block is really
|
||||
* the first section of arr2.
|
||||
*/
|
||||
i = state->nblock + BZ_N_OVERSHOOT;
|
||||
if (i & 1)
|
||||
i++;
|
||||
state->quadrant = (uint16_t*) &(state->block[i]);
|
||||
i = nblock + BZ_N_OVERSHOOT;
|
||||
if (i & 1) i++;
|
||||
quadrant = (uint16_t*)(&(block[i]));
|
||||
|
||||
/* (wfact-1) / 3 puts the default-factor-30
|
||||
* transition point at very roughly the same place as
|
||||
@@ -1051,26 +1048,22 @@ int32_t BZ2_blockSort(EState* state)
|
||||
* resulting compressed stream is now the same regardless
|
||||
* of whether or not we use the main sort or fallback sort.
|
||||
*/
|
||||
state->budget = state->nblock * ((wfact-1) / 3);
|
||||
mainSort(state);
|
||||
if (state->budget >= 0)
|
||||
goto good;
|
||||
}
|
||||
fallbackSort(state);
|
||||
good:
|
||||
budget = nblock * ((wfact-1) / 3);
|
||||
|
||||
#if BZ_LIGHT_DEBUG
|
||||
origPtr = -1;
|
||||
#endif
|
||||
for (i = 0; i < state->nblock; i++) {
|
||||
if (state->ptr[i] == 0) {
|
||||
origPtr = i;
|
||||
break;
|
||||
mainSort(s, ptr, block, quadrant, ftab, nblock, &budget);
|
||||
if (budget < 0) {
|
||||
fallbackSort(s->arr1, s->arr2, ftab, nblock);
|
||||
}
|
||||
}
|
||||
|
||||
AssertH(origPtr != -1, 1003);
|
||||
return origPtr;
|
||||
s->origPtr = -1;
|
||||
for (i = 0; i < s->nblock; i++)
|
||||
if (ptr[i] == 0) {
|
||||
s->origPtr = i;
|
||||
break;
|
||||
};
|
||||
|
||||
AssertH(s->origPtr != -1, 1003);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -55,9 +55,8 @@ void prepare_new_block(EState* s)
|
||||
{
|
||||
int i;
|
||||
s->nblock = 0;
|
||||
//indexes into s->zbits[], initialzation moved to init of s->zbits
|
||||
//s->posZ = s->zbits; // was: s->numZ = 0;
|
||||
//s->state_out_pos = s->zbits;
|
||||
s->numZ = 0;
|
||||
s->state_out_pos = 0;
|
||||
BZ_INITIALISE_CRC(s->blockCRC);
|
||||
/* inlined memset would be nice to have here */
|
||||
for (i = 0; i < 256; i++)
|
||||
@@ -87,7 +86,7 @@ int isempty_RL(EState* s)
|
||||
static
|
||||
void BZ2_bzCompressInit(bz_stream *strm, int blockSize100k)
|
||||
{
|
||||
unsigned n;
|
||||
int32_t n;
|
||||
EState* s;
|
||||
|
||||
s = xzalloc(sizeof(EState));
|
||||
@@ -99,8 +98,9 @@ void BZ2_bzCompressInit(bz_stream *strm, int blockSize100k)
|
||||
s->ptr = (uint32_t*)s->arr1;
|
||||
s->arr2 = xmalloc((n + BZ_N_OVERSHOOT) * sizeof(uint32_t));
|
||||
s->block = (uint8_t*)s->arr2;
|
||||
s->ftab = xmalloc(65537 * sizeof(uint32_t));
|
||||
|
||||
crc32_filltable(s->crc32table, 1);
|
||||
s->crc32table = crc32_filltable(NULL, 1);
|
||||
|
||||
s->state = BZ_S_INPUT;
|
||||
s->mode = BZ_M_RUNNING;
|
||||
@@ -237,10 +237,11 @@ void /*Bool*/ copy_output_until_stop(EState* s)
|
||||
if (s->strm->avail_out == 0) break;
|
||||
|
||||
/*-- block done? --*/
|
||||
if (s->state_out_pos >= s->posZ) break;
|
||||
if (s->state_out_pos >= s->numZ) break;
|
||||
|
||||
/*progress_out = True;*/
|
||||
*(s->strm->next_out) = *s->state_out_pos++;
|
||||
*(s->strm->next_out) = s->zbits[s->state_out_pos];
|
||||
s->state_out_pos++;
|
||||
s->strm->avail_out--;
|
||||
s->strm->next_out++;
|
||||
s->strm->total_out++;
|
||||
@@ -260,7 +261,7 @@ void /*Bool*/ handle_compress(bz_stream *strm)
|
||||
while (1) {
|
||||
if (s->state == BZ_S_OUTPUT) {
|
||||
/*progress_out |=*/ copy_output_until_stop(s);
|
||||
if (s->state_out_pos < s->posZ) break;
|
||||
if (s->state_out_pos < s->numZ) break;
|
||||
if (s->mode == BZ_M_FINISHING
|
||||
//# && s->avail_in_expect == 0
|
||||
&& s->strm->avail_in == 0
|
||||
@@ -335,7 +336,7 @@ int BZ2_bzCompress(bz_stream *strm, int action)
|
||||
/*if (s->avail_in_expect != s->strm->avail_in)
|
||||
return BZ_SEQUENCE_ERROR;*/
|
||||
/*progress =*/ handle_compress(strm);
|
||||
if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
|
||||
if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ)
|
||||
return BZ_FLUSH_OK;
|
||||
s->mode = BZ_M_RUNNING;
|
||||
return BZ_RUN_OK;
|
||||
@@ -348,9 +349,9 @@ int BZ2_bzCompress(bz_stream *strm, int action)
|
||||
return BZ_SEQUENCE_ERROR;*/
|
||||
/*progress =*/ handle_compress(strm);
|
||||
/*if (!progress) return BZ_SEQUENCE_ERROR;*/
|
||||
//#if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
|
||||
//#if (s->avail_in_expect > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ)
|
||||
//# return BZ_FINISH_OK;
|
||||
if (s->strm->avail_in > 0 || !isempty_RL(s) || s->state_out_pos < s->posZ)
|
||||
if (s->strm->avail_in > 0 || !isempty_RL(s) || s->state_out_pos < s->numZ)
|
||||
return BZ_FINISH_OK;
|
||||
/*s->mode = BZ_M_IDLE;*/
|
||||
return BZ_STREAM_END;
|
||||
@@ -368,8 +369,8 @@ void BZ2_bzCompressEnd(bz_stream *strm)
|
||||
s = strm->state;
|
||||
free(s->arr1);
|
||||
free(s->arr2);
|
||||
//free(s->ftab); // made it array member of s
|
||||
//free(s->crc32table); // ditto
|
||||
free(s->ftab);
|
||||
free(s->crc32table);
|
||||
free(s);
|
||||
}
|
||||
|
||||
|
||||
@@ -120,11 +120,8 @@ typedef struct EState {
|
||||
|
||||
/* mode this stream is in, and whether inputting */
|
||||
/* or outputting data */
|
||||
uint8_t mode;
|
||||
uint8_t state;
|
||||
|
||||
/* misc administratium */
|
||||
uint8_t blockSize100k;
|
||||
int32_t mode;
|
||||
int32_t state;
|
||||
|
||||
/* remembers avail_in when flush/finish requested */
|
||||
/* bbox: not needed, strm->avail_in always has the same value */
|
||||
@@ -132,12 +129,10 @@ typedef struct EState {
|
||||
/* uint32_t avail_in_expect; */
|
||||
|
||||
/* for doing the block sorting */
|
||||
int32_t origPtr;
|
||||
uint32_t *arr1;
|
||||
uint32_t *arr2;
|
||||
//uint32_t *ftab; //moved into this struct, see below
|
||||
|
||||
uint16_t *quadrant;
|
||||
int32_t budget;
|
||||
uint32_t *ftab;
|
||||
|
||||
/* aliases for arr1 and arr2 */
|
||||
uint32_t *ptr;
|
||||
@@ -145,6 +140,9 @@ typedef struct EState {
|
||||
uint16_t *mtfv;
|
||||
uint8_t *zbits;
|
||||
|
||||
/* guess what */
|
||||
uint32_t *crc32table;
|
||||
|
||||
/* run-length-encoding of the input */
|
||||
uint32_t state_in_ch;
|
||||
int32_t state_in_len;
|
||||
@@ -152,9 +150,8 @@ typedef struct EState {
|
||||
/* input and output limits and current posns */
|
||||
int32_t nblock;
|
||||
int32_t nblockMAX;
|
||||
//int32_t numZ; // index into s->zbits[], replaced by pointer:
|
||||
uint8_t *posZ;
|
||||
uint8_t *state_out_pos;
|
||||
int32_t numZ;
|
||||
int32_t state_out_pos;
|
||||
|
||||
/* the buffer for bit stream creation */
|
||||
uint32_t bsBuff;
|
||||
@@ -166,6 +163,7 @@ typedef struct EState {
|
||||
|
||||
/* misc administratium */
|
||||
int32_t blockNo;
|
||||
int32_t blockSize100k;
|
||||
|
||||
/* stuff for coding the MTF values */
|
||||
int32_t nMTF;
|
||||
@@ -182,16 +180,10 @@ typedef struct EState {
|
||||
|
||||
uint8_t len[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
|
||||
|
||||
/* guess what */
|
||||
uint32_t crc32table[256];
|
||||
|
||||
/* for doing the block sorting */
|
||||
uint32_t ftab[65537];
|
||||
|
||||
/* stack-saving measures: these can be local, but they are too big */
|
||||
int32_t sendMTFValues__code [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
|
||||
int32_t sendMTFValues__rfreq[BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
|
||||
#if BZIP2_SPEED >= 5
|
||||
#if CONFIG_BZIP2_FAST >= 5
|
||||
/* second dimension: only 3 needed; 4 makes index calculations faster */
|
||||
uint32_t sendMTFValues__len_pack[BZ_MAX_ALPHA_SIZE][4];
|
||||
#endif
|
||||
@@ -199,6 +191,7 @@ typedef struct EState {
|
||||
int32_t BZ2_hbMakeCodeLengths__weight[BZ_MAX_ALPHA_SIZE * 2];
|
||||
int32_t BZ2_hbMakeCodeLengths__parent[BZ_MAX_ALPHA_SIZE * 2];
|
||||
|
||||
int32_t mainSort__runningOrder[256];
|
||||
int32_t mainSort__copyStart[256];
|
||||
int32_t mainSort__copyEnd[256];
|
||||
} EState;
|
||||
@@ -206,7 +199,7 @@ typedef struct EState {
|
||||
|
||||
/*-- compression. --*/
|
||||
|
||||
static int32_t
|
||||
static void
|
||||
BZ2_blockSort(EState*);
|
||||
|
||||
static void
|
||||
|
||||
+132
-207
@@ -32,12 +32,6 @@ in the file LICENSE.
|
||||
|
||||
/* #include "bzlib_private.h" */
|
||||
|
||||
#if BZIP2_SPEED >= 5
|
||||
# define ALWAYS_INLINE_5 ALWAYS_INLINE
|
||||
#else
|
||||
# define ALWAYS_INLINE_5 /*nothing*/
|
||||
#endif
|
||||
|
||||
/*---------------------------------------------------*/
|
||||
/*--- Bit stream I/O ---*/
|
||||
/*---------------------------------------------------*/
|
||||
@@ -56,7 +50,8 @@ static NOINLINE
|
||||
void bsFinishWrite(EState* s)
|
||||
{
|
||||
while (s->bsLive > 0) {
|
||||
*s->posZ++ = (uint8_t)(s->bsBuff >> 24);
|
||||
s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24);
|
||||
s->numZ++;
|
||||
s->bsBuff <<= 8;
|
||||
s->bsLive -= 8;
|
||||
}
|
||||
@@ -66,74 +61,39 @@ void bsFinishWrite(EState* s)
|
||||
/*---------------------------------------------------*/
|
||||
static
|
||||
/* Helps only on level 5, on other levels hurts. ? */
|
||||
ALWAYS_INLINE_5
|
||||
#if CONFIG_BZIP2_FAST >= 5
|
||||
ALWAYS_INLINE
|
||||
#endif
|
||||
void bsW(EState* s, int32_t n, uint32_t v)
|
||||
{
|
||||
while (s->bsLive >= 8) {
|
||||
*s->posZ++ = (uint8_t)(s->bsBuff >> 24);
|
||||
s->zbits[s->numZ] = (uint8_t)(s->bsBuff >> 24);
|
||||
s->numZ++;
|
||||
s->bsBuff <<= 8;
|
||||
s->bsLive -= 8;
|
||||
}
|
||||
s->bsBuff |= (v << (32 - s->bsLive - n));
|
||||
s->bsLive += n;
|
||||
}
|
||||
/* Same with n == 16: */
|
||||
static
|
||||
ALWAYS_INLINE_5
|
||||
void bsW16(EState* s, uint32_t v)
|
||||
{
|
||||
while (s->bsLive >= 8) {
|
||||
*s->posZ++ = (uint8_t)(s->bsBuff >> 24);
|
||||
s->bsBuff <<= 8;
|
||||
s->bsLive -= 8;
|
||||
}
|
||||
s->bsBuff |= (v << (16 - s->bsLive));
|
||||
s->bsLive += 16;
|
||||
}
|
||||
/* Same with n == 1: */
|
||||
static
|
||||
ALWAYS_INLINE /* one callsite */
|
||||
void bsW1_1(EState* s)
|
||||
{
|
||||
/* need space for only 1 bit, no need for loop freeing > 8 bits */
|
||||
if (s->bsLive >= 8) {
|
||||
*s->posZ++ = (uint8_t)(s->bsBuff >> 24);
|
||||
s->bsBuff <<= 8;
|
||||
s->bsLive -= 8;
|
||||
}
|
||||
s->bsBuff |= (1 << (31 - s->bsLive));
|
||||
s->bsLive += 1;
|
||||
}
|
||||
static
|
||||
ALWAYS_INLINE_5
|
||||
void bsW1_0(EState* s)
|
||||
{
|
||||
/* need space for only 1 bit, no need for loop freeing > 8 bits */
|
||||
if (s->bsLive >= 8) {
|
||||
*s->posZ++ = (uint8_t)(s->bsBuff >> 24);
|
||||
s->bsBuff <<= 8;
|
||||
s->bsLive -= 8;
|
||||
}
|
||||
//s->bsBuff |= (0 << (31 - s->bsLive));
|
||||
s->bsLive += 1;
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------*/
|
||||
static ALWAYS_INLINE
|
||||
void bsPutU16(EState* s, unsigned u)
|
||||
{
|
||||
bsW16(s, u);
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------*/
|
||||
static
|
||||
void bsPutU32(EState* s, unsigned u)
|
||||
{
|
||||
//bsW(s, 32, u); // can't use: may try "uint32 << -n"
|
||||
bsW16(s, (u >> 16) & 0xffff);
|
||||
bsW16(s, u & 0xffff);
|
||||
bsW(s, 8, (u >> 24) & 0xff);
|
||||
bsW(s, 8, (u >> 16) & 0xff);
|
||||
bsW(s, 8, (u >> 8) & 0xff);
|
||||
bsW(s, 8, u & 0xff);
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------*/
|
||||
static
|
||||
void bsPutU16(EState* s, unsigned u)
|
||||
{
|
||||
bsW(s, 8, (u >> 8) & 0xff);
|
||||
bsW(s, 8, u & 0xff);
|
||||
}
|
||||
|
||||
|
||||
@@ -146,57 +106,25 @@ static
|
||||
void makeMaps_e(EState* s)
|
||||
{
|
||||
int i;
|
||||
unsigned cnt = 0;
|
||||
s->nInUse = 0;
|
||||
for (i = 0; i < 256; i++) {
|
||||
if (s->inUse[i]) {
|
||||
s->unseqToSeq[i] = cnt;
|
||||
cnt++;
|
||||
s->unseqToSeq[i] = s->nInUse;
|
||||
s->nInUse++;
|
||||
}
|
||||
}
|
||||
s->nInUse = cnt;
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------*/
|
||||
/*
|
||||
* This bit of code is performance-critical.
|
||||
* On 32bit x86, gcc-6.3.0 was observed to spill ryy_j to stack,
|
||||
* resulting in abysmal performance (x3 slowdown).
|
||||
* Forcing it into a separate function alleviates register pressure,
|
||||
* and spillage no longer happens.
|
||||
* Other versions of gcc do not exhibit this problem, but out-of-line code
|
||||
* seems to be helping them too (code is both smaller and faster).
|
||||
* Therefore NOINLINE is enabled for the entire 32bit x86 arch for now,
|
||||
* without a check for gcc version.
|
||||
*/
|
||||
static
|
||||
#if defined __i386__
|
||||
NOINLINE
|
||||
#endif
|
||||
int inner_loop(uint8_t *yy, uint8_t ll_i)
|
||||
{
|
||||
register uint8_t rtmp;
|
||||
register uint8_t* ryy_j;
|
||||
rtmp = yy[1];
|
||||
yy[1] = yy[0];
|
||||
ryy_j = &(yy[1]);
|
||||
while (ll_i != rtmp) {
|
||||
register uint8_t rtmp2;
|
||||
ryy_j++;
|
||||
rtmp2 = rtmp;
|
||||
rtmp = *ryy_j;
|
||||
*ryy_j = rtmp2;
|
||||
}
|
||||
yy[0] = rtmp;
|
||||
return ryy_j - &(yy[0]);
|
||||
}
|
||||
static NOINLINE
|
||||
void generateMTFValues(EState* s)
|
||||
{
|
||||
uint8_t yy[256];
|
||||
int i;
|
||||
int zPend;
|
||||
int32_t i, j;
|
||||
int32_t zPend;
|
||||
int32_t wr;
|
||||
int32_t EOB;
|
||||
|
||||
/*
|
||||
* After sorting (eg, here),
|
||||
@@ -220,74 +148,95 @@ void generateMTFValues(EState* s)
|
||||
* compressBlock().
|
||||
*/
|
||||
uint32_t* ptr = s->ptr;
|
||||
uint8_t* block = s->block;
|
||||
uint16_t* mtfv = s->mtfv;
|
||||
|
||||
makeMaps_e(s);
|
||||
EOB = s->nInUse+1;
|
||||
|
||||
for (i = 0; i <= EOB; i++)
|
||||
s->mtfFreq[i] = 0;
|
||||
|
||||
wr = 0;
|
||||
zPend = 0;
|
||||
for (i = 0; i <= s->nInUse+1; i++)
|
||||
s->mtfFreq[i] = 0;
|
||||
|
||||
for (i = 0; i < s->nInUse; i++)
|
||||
yy[i] = (uint8_t) i;
|
||||
|
||||
for (i = 0; i < s->nblock; i++) {
|
||||
uint8_t ll_i = ll_i; /* gcc 4.3.1 thinks it may be used w/o init */
|
||||
int32_t j;
|
||||
|
||||
uint8_t ll_i;
|
||||
AssertD(wr <= i, "generateMTFValues(1)");
|
||||
j = ptr[i] - 1;
|
||||
if (j < 0)
|
||||
j += s->nblock;
|
||||
ll_i = s->unseqToSeq[s->block[j]];
|
||||
ll_i = s->unseqToSeq[block[j]];
|
||||
AssertD(ll_i < s->nInUse, "generateMTFValues(2a)");
|
||||
|
||||
if (yy[0] == ll_i) {
|
||||
zPend++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (zPend > 0) {
|
||||
process_zPend:
|
||||
zPend--;
|
||||
while (1) {
|
||||
#if 0
|
||||
if (zPend & 1) {
|
||||
s->mtfv[wr] = BZ_RUNB; wr++;
|
||||
s->mtfFreq[BZ_RUNB]++;
|
||||
} else {
|
||||
s->mtfv[wr] = BZ_RUNA; wr++;
|
||||
s->mtfFreq[BZ_RUNA]++;
|
||||
}
|
||||
#else /* same as above, since BZ_RUNA is 0 and BZ_RUNB is 1 */
|
||||
unsigned run = zPend & 1;
|
||||
s->mtfv[wr] = run;
|
||||
} else {
|
||||
if (zPend > 0) {
|
||||
zPend--;
|
||||
while (1) {
|
||||
if (zPend & 1) {
|
||||
mtfv[wr] = BZ_RUNB; wr++;
|
||||
s->mtfFreq[BZ_RUNB]++;
|
||||
} else {
|
||||
mtfv[wr] = BZ_RUNA; wr++;
|
||||
s->mtfFreq[BZ_RUNA]++;
|
||||
}
|
||||
if (zPend < 2) break;
|
||||
zPend = (uint32_t)(zPend - 2) / 2;
|
||||
/* bbox: unsigned div is easier */
|
||||
};
|
||||
zPend = 0;
|
||||
}
|
||||
{
|
||||
register uint8_t rtmp;
|
||||
register uint8_t* ryy_j;
|
||||
register uint8_t rll_i;
|
||||
rtmp = yy[1];
|
||||
yy[1] = yy[0];
|
||||
ryy_j = &(yy[1]);
|
||||
rll_i = ll_i;
|
||||
while (rll_i != rtmp) {
|
||||
register uint8_t rtmp2;
|
||||
ryy_j++;
|
||||
rtmp2 = rtmp;
|
||||
rtmp = *ryy_j;
|
||||
*ryy_j = rtmp2;
|
||||
};
|
||||
yy[0] = rtmp;
|
||||
j = ryy_j - &(yy[0]);
|
||||
mtfv[wr] = j+1;
|
||||
wr++;
|
||||
s->mtfFreq[run]++;
|
||||
#endif
|
||||
zPend -= 2;
|
||||
if (zPend < 0)
|
||||
break;
|
||||
zPend = (unsigned)zPend / 2;
|
||||
/* bbox: unsigned div is easier */
|
||||
s->mtfFreq[j+1]++;
|
||||
}
|
||||
if (i < 0) /* came via "goto process_zPend"? exit */
|
||||
goto end;
|
||||
zPend = 0;
|
||||
}
|
||||
j = inner_loop(yy, ll_i);
|
||||
s->mtfv[wr] = j+1;
|
||||
wr++;
|
||||
s->mtfFreq[j+1]++;
|
||||
}
|
||||
|
||||
i = -1;
|
||||
if (zPend > 0)
|
||||
goto process_zPend; /* "process it and come back here" */
|
||||
end:
|
||||
s->mtfv[wr] = s->nInUse+1;
|
||||
if (zPend > 0) {
|
||||
zPend--;
|
||||
while (1) {
|
||||
if (zPend & 1) {
|
||||
mtfv[wr] = BZ_RUNB;
|
||||
wr++;
|
||||
s->mtfFreq[BZ_RUNB]++;
|
||||
} else {
|
||||
mtfv[wr] = BZ_RUNA;
|
||||
wr++;
|
||||
s->mtfFreq[BZ_RUNA]++;
|
||||
}
|
||||
if (zPend < 2)
|
||||
break;
|
||||
zPend = (uint32_t)(zPend - 2) / 2;
|
||||
/* bbox: unsigned div is easier */
|
||||
};
|
||||
zPend = 0;
|
||||
}
|
||||
|
||||
mtfv[wr] = EOB;
|
||||
wr++;
|
||||
s->mtfFreq[s->nInUse+1]++;
|
||||
s->mtfFreq[EOB]++;
|
||||
|
||||
s->nMTF = wr;
|
||||
}
|
||||
@@ -300,11 +249,8 @@ void generateMTFValues(EState* s)
|
||||
static NOINLINE
|
||||
void sendMTFValues(EState* s)
|
||||
{
|
||||
int32_t t, i;
|
||||
unsigned iter;
|
||||
unsigned gs;
|
||||
int32_t alphaSize;
|
||||
unsigned nSelectors, selCtr;
|
||||
int32_t v, t, i, j, gs, ge, bt, bc, iter;
|
||||
int32_t nSelectors, alphaSize, minLen, maxLen, selCtr;
|
||||
int32_t nGroups;
|
||||
|
||||
/*
|
||||
@@ -320,49 +266,39 @@ void sendMTFValues(EState* s)
|
||||
#define rfreq sendMTFValues__rfreq
|
||||
#define len_pack sendMTFValues__len_pack
|
||||
|
||||
unsigned /*uint16_t*/ cost[BZ_N_GROUPS];
|
||||
uint16_t cost[BZ_N_GROUPS];
|
||||
int32_t fave[BZ_N_GROUPS];
|
||||
|
||||
uint16_t* mtfv = s->mtfv;
|
||||
|
||||
alphaSize = s->nInUse + 2;
|
||||
for (t = 0; t < BZ_N_GROUPS; t++) {
|
||||
unsigned v;
|
||||
for (t = 0; t < BZ_N_GROUPS; t++)
|
||||
for (v = 0; v < alphaSize; v++)
|
||||
s->len[t][v] = BZ_GREATER_ICOST;
|
||||
}
|
||||
|
||||
/*--- Decide how many coding tables to use ---*/
|
||||
AssertH(s->nMTF > 0, 3001);
|
||||
// 1..199 = 2
|
||||
// 200..599 = 3
|
||||
// 600..1199 = 4
|
||||
// 1200..2399 = 5
|
||||
// 2400..99999 = 6
|
||||
nGroups = 2;
|
||||
nGroups += (s->nMTF >= 200);
|
||||
nGroups += (s->nMTF >= 600);
|
||||
nGroups += (s->nMTF >= 1200);
|
||||
nGroups += (s->nMTF >= 2400);
|
||||
if (s->nMTF < 200) nGroups = 2; else
|
||||
if (s->nMTF < 600) nGroups = 3; else
|
||||
if (s->nMTF < 1200) nGroups = 4; else
|
||||
if (s->nMTF < 2400) nGroups = 5; else
|
||||
nGroups = 6;
|
||||
|
||||
/*--- Generate an initial set of coding tables ---*/
|
||||
{
|
||||
unsigned nPart, remF;
|
||||
int32_t nPart, remF, tFreq, aFreq;
|
||||
|
||||
nPart = nGroups;
|
||||
remF = s->nMTF;
|
||||
gs = 0;
|
||||
while (nPart > 0) {
|
||||
unsigned v;
|
||||
unsigned ge;
|
||||
unsigned tFreq, aFreq;
|
||||
|
||||
tFreq = remF / nPart;
|
||||
ge = gs;
|
||||
ge = gs - 1;
|
||||
aFreq = 0;
|
||||
while (aFreq < tFreq && ge < alphaSize) {
|
||||
aFreq += s->mtfFreq[ge++];
|
||||
while (aFreq < tFreq && ge < alphaSize-1) {
|
||||
ge++;
|
||||
aFreq += s->mtfFreq[ge];
|
||||
}
|
||||
ge--;
|
||||
|
||||
if (ge > gs
|
||||
&& nPart != nGroups && nPart != 1
|
||||
@@ -388,19 +324,19 @@ void sendMTFValues(EState* s)
|
||||
* Iterate up to BZ_N_ITERS times to improve the tables.
|
||||
*/
|
||||
for (iter = 0; iter < BZ_N_ITERS; iter++) {
|
||||
for (t = 0; t < nGroups; t++) {
|
||||
unsigned v;
|
||||
for (t = 0; t < nGroups; t++)
|
||||
fave[t] = 0;
|
||||
|
||||
for (t = 0; t < nGroups; t++)
|
||||
for (v = 0; v < alphaSize; v++)
|
||||
s->rfreq[t][v] = 0;
|
||||
}
|
||||
|
||||
#if BZIP2_SPEED >= 5
|
||||
#if CONFIG_BZIP2_FAST >= 5
|
||||
/*
|
||||
* Set up an auxiliary length table which is used to fast-track
|
||||
* the common case (nGroups == 6).
|
||||
*/
|
||||
if (nGroups == 6) {
|
||||
unsigned v;
|
||||
for (v = 0; v < alphaSize; v++) {
|
||||
s->len_pack[v][0] = (s->len[1][v] << 16) | s->len[0][v];
|
||||
s->len_pack[v][1] = (s->len[3][v] << 16) | s->len[2][v];
|
||||
@@ -411,9 +347,6 @@ void sendMTFValues(EState* s)
|
||||
nSelectors = 0;
|
||||
gs = 0;
|
||||
while (1) {
|
||||
unsigned ge;
|
||||
unsigned bt, bc;
|
||||
|
||||
/*--- Set group start & end marks. --*/
|
||||
if (gs >= s->nMTF)
|
||||
break;
|
||||
@@ -427,7 +360,7 @@ void sendMTFValues(EState* s)
|
||||
*/
|
||||
for (t = 0; t < nGroups; t++)
|
||||
cost[t] = 0;
|
||||
#if BZIP2_SPEED >= 5
|
||||
#if CONFIG_BZIP2_FAST >= 5
|
||||
if (nGroups == 6 && 50 == ge-gs+1) {
|
||||
/*--- fast track the common case ---*/
|
||||
register uint32_t cost01, cost23, cost45;
|
||||
@@ -457,7 +390,7 @@ void sendMTFValues(EState* s)
|
||||
{
|
||||
/*--- slow version which correctly handles all situations ---*/
|
||||
for (i = gs; i <= ge; i++) {
|
||||
unsigned /*uint16_t*/ icv = mtfv[i];
|
||||
uint16_t icv = mtfv[i];
|
||||
for (t = 0; t < nGroups; t++)
|
||||
cost[t] += s->len[t][icv];
|
||||
}
|
||||
@@ -476,6 +409,7 @@ void sendMTFValues(EState* s)
|
||||
bt = t;
|
||||
}
|
||||
}
|
||||
fave[bt]++;
|
||||
s->selector[nSelectors] = bt;
|
||||
nSelectors++;
|
||||
|
||||
@@ -483,7 +417,7 @@ void sendMTFValues(EState* s)
|
||||
* Increment the symbol frequencies for the selected table.
|
||||
*/
|
||||
/* 1% faster compress. +800 bytes */
|
||||
#if BZIP2_SPEED >= 4
|
||||
#if CONFIG_BZIP2_FAST >= 4
|
||||
if (nGroups == 6 && 50 == ge-gs+1) {
|
||||
/*--- fast track the common case ---*/
|
||||
#define BZ_ITUR(nn) s->rfreq[bt][mtfv[gs + (nn)]]++
|
||||
@@ -530,7 +464,6 @@ void sendMTFValues(EState* s)
|
||||
for (i = 0; i < nGroups; i++)
|
||||
pos[i] = i;
|
||||
for (i = 0; i < nSelectors; i++) {
|
||||
unsigned j;
|
||||
ll_i = s->selector[i];
|
||||
j = 0;
|
||||
tmp = pos[j];
|
||||
@@ -539,16 +472,16 @@ void sendMTFValues(EState* s)
|
||||
tmp2 = tmp;
|
||||
tmp = pos[j];
|
||||
pos[j] = tmp2;
|
||||
}
|
||||
};
|
||||
pos[0] = tmp;
|
||||
s->selectorMtf[i] = j;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*--- Assign actual codes for the tables. --*/
|
||||
for (t = 0; t < nGroups; t++) {
|
||||
unsigned minLen = 32; //todo: s->len[t][0];
|
||||
unsigned maxLen = 0; //todo: s->len[t][0];
|
||||
minLen = 32;
|
||||
maxLen = 0;
|
||||
for (i = 0; i < alphaSize; i++) {
|
||||
if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
|
||||
if (s->len[t][i] < minLen) minLen = s->len[t][i];
|
||||
@@ -576,16 +509,15 @@ void sendMTFValues(EState* s)
|
||||
}
|
||||
}
|
||||
|
||||
bsW16(s, inUse16);
|
||||
bsW(s, 16, inUse16);
|
||||
|
||||
inUse16 <<= (sizeof(int)*8 - 16); /* move 15th bit into sign bit */
|
||||
for (i = 0; i < 16; i++) {
|
||||
if (inUse16 < 0) {
|
||||
unsigned v16 = 0;
|
||||
unsigned j;
|
||||
for (j = 0; j < 16; j++)
|
||||
v16 = v16*2 + s->inUse[i * 16 + j];
|
||||
bsW16(s, v16);
|
||||
bsW(s, 16, v16);
|
||||
}
|
||||
inUse16 <<= 1;
|
||||
}
|
||||
@@ -595,20 +527,19 @@ void sendMTFValues(EState* s)
|
||||
bsW(s, 3, nGroups);
|
||||
bsW(s, 15, nSelectors);
|
||||
for (i = 0; i < nSelectors; i++) {
|
||||
unsigned j;
|
||||
for (j = 0; j < s->selectorMtf[i]; j++)
|
||||
bsW1_1(s);
|
||||
bsW1_0(s);
|
||||
bsW(s, 1, 1);
|
||||
bsW(s, 1, 0);
|
||||
}
|
||||
|
||||
/*--- Now the coding tables. ---*/
|
||||
for (t = 0; t < nGroups; t++) {
|
||||
unsigned curr = s->len[t][0];
|
||||
int32_t curr = s->len[t][0];
|
||||
bsW(s, 5, curr);
|
||||
for (i = 0; i < alphaSize; i++) {
|
||||
while (curr < s->len[t][i]) { bsW(s, 2, 2); curr++; /* 10 */ }
|
||||
while (curr > s->len[t][i]) { bsW(s, 2, 3); curr--; /* 11 */ }
|
||||
bsW1_0(s);
|
||||
while (curr < s->len[t][i]) { bsW(s, 2, 2); curr++; /* 10 */ };
|
||||
while (curr > s->len[t][i]) { bsW(s, 2, 3); curr--; /* 11 */ };
|
||||
bsW(s, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -616,8 +547,6 @@ void sendMTFValues(EState* s)
|
||||
selCtr = 0;
|
||||
gs = 0;
|
||||
while (1) {
|
||||
unsigned ge;
|
||||
|
||||
if (gs >= s->nMTF)
|
||||
break;
|
||||
ge = gs + BZ_G_SIZE - 1;
|
||||
@@ -676,21 +605,17 @@ void sendMTFValues(EState* s)
|
||||
static
|
||||
void BZ2_compressBlock(EState* s, int is_last_block)
|
||||
{
|
||||
int32_t origPtr = origPtr;
|
||||
|
||||
if (s->nblock > 0) {
|
||||
BZ_FINALISE_CRC(s->blockCRC);
|
||||
s->combinedCRC = (s->combinedCRC << 1) | (s->combinedCRC >> 31);
|
||||
s->combinedCRC ^= s->blockCRC;
|
||||
if (s->blockNo > 1)
|
||||
s->posZ = s->zbits; // was: s->numZ = 0;
|
||||
s->numZ = 0;
|
||||
|
||||
origPtr = BZ2_blockSort(s);
|
||||
BZ2_blockSort(s);
|
||||
}
|
||||
|
||||
s->zbits = &((uint8_t*)s->arr2)[s->nblock];
|
||||
s->posZ = s->zbits;
|
||||
s->state_out_pos = s->zbits;
|
||||
|
||||
/*-- If this is the first block, create the stream header. --*/
|
||||
if (s->blockNo == 1) {
|
||||
@@ -724,9 +649,9 @@ void BZ2_compressBlock(EState* s, int is_last_block)
|
||||
* so as to maintain backwards compatibility with
|
||||
* older versions of bzip2.
|
||||
*/
|
||||
bsW1_0(s);
|
||||
bsW(s, 1, 0);
|
||||
|
||||
bsW(s, 24, origPtr);
|
||||
bsW(s, 24, s->origPtr);
|
||||
generateMTFValues(s);
|
||||
sendMTFValues(s);
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ in the file LICENSE.
|
||||
|
||||
|
||||
/* 90 bytes, 0.3% of overall compress speed */
|
||||
#if BZIP2_SPEED >= 1
|
||||
#if CONFIG_BZIP2_FAST >= 1
|
||||
|
||||
/* macro works better than inline (gcc 4.2.1) */
|
||||
#define DOWNHEAP1(heap, weight, Heap) \
|
||||
@@ -217,7 +217,7 @@ void BZ2_hbAssignCodes(int32_t *code,
|
||||
if (length[i] == n) {
|
||||
code[i] = vec;
|
||||
vec++;
|
||||
}
|
||||
};
|
||||
}
|
||||
vec <<= 1;
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
struct stat existing_sb;
|
||||
if (lstat(dst_name, &existing_sb) == -1) {
|
||||
if (errno != ENOENT) {
|
||||
bb_simple_perror_msg_and_die("can't stat old file");
|
||||
bb_perror_msg_and_die("can't stat old file");
|
||||
}
|
||||
}
|
||||
else if (existing_sb.st_mtime >= file_header->mtime) {
|
||||
@@ -122,10 +122,13 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
|
||||
/* Handle hard links separately */
|
||||
if (hard_link) {
|
||||
create_or_remember_link(&archive_handle->link_placeholders,
|
||||
hard_link,
|
||||
dst_name,
|
||||
1);
|
||||
res = link(hard_link, dst_name);
|
||||
if (res != 0) {
|
||||
/* shared message */
|
||||
bb_perror_msg("can't create %slink '%s' to '%s'",
|
||||
"hard", dst_name, hard_link
|
||||
);
|
||||
}
|
||||
/* Hardlinks have no separate mode/ownership, skip chown/chmod */
|
||||
goto ret;
|
||||
}
|
||||
@@ -159,10 +162,6 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
break;
|
||||
}
|
||||
case S_IFDIR:
|
||||
//TODO: this causes problems if tarball contains a r-xr-xr-x directory:
|
||||
// we create this directory, and then fail to create files inside it
|
||||
// (if tar xf isn't run as root).
|
||||
// GNU tar works around this by chmod-ing directories *after* all files are extracted.
|
||||
res = mkdir(dst_name, file_header->mode);
|
||||
if ((res != 0)
|
||||
&& (errno != EISDIR) /* btw, Linux doesn't return this */
|
||||
@@ -196,10 +195,9 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
*
|
||||
* Untarring bug.tar would otherwise place evil.py in '/tmp'.
|
||||
*/
|
||||
create_or_remember_link(&archive_handle->link_placeholders,
|
||||
create_or_remember_symlink(&archive_handle->symlink_placeholders,
|
||||
file_header->link_target,
|
||||
dst_name,
|
||||
0);
|
||||
dst_name);
|
||||
break;
|
||||
case S_IFSOCK:
|
||||
case S_IFBLK:
|
||||
@@ -211,7 +209,7 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
}
|
||||
break;
|
||||
default:
|
||||
bb_simple_error_msg_and_die("unrecognized file type");
|
||||
bb_error_msg_and_die("unrecognized file type");
|
||||
}
|
||||
|
||||
if (!S_ISLNK(file_header->mode)) {
|
||||
|
||||
@@ -20,7 +20,7 @@ enum {
|
||||
TAR_MAX,
|
||||
};
|
||||
|
||||
static const char *const tar_var[] ALIGN_PTR = {
|
||||
static const char *const tar_var[] = {
|
||||
// "FILETYPE",
|
||||
"MODE",
|
||||
"FILENAME",
|
||||
@@ -37,7 +37,7 @@ static const char *const tar_var[] ALIGN_PTR = {
|
||||
static void xputenv(char *str)
|
||||
{
|
||||
if (putenv(str))
|
||||
bb_die_memory_exhausted();
|
||||
bb_error_msg_and_die(bb_msg_memory_exhausted);
|
||||
}
|
||||
|
||||
static void str2env(char *env[], int idx, const char *str)
|
||||
|
||||
@@ -100,14 +100,14 @@ struct bunzip_data {
|
||||
unsigned dbufSize;
|
||||
|
||||
/* For I/O error handling */
|
||||
jmp_buf *jmpbuf;
|
||||
jmp_buf jmpbuf;
|
||||
|
||||
/* Big things go last (register-relative addressing can be larger for big offsets) */
|
||||
uint32_t crc32Table[256];
|
||||
uint8_t selectors[32768]; /* nSelectors=15 bits */
|
||||
struct group_data groups[MAX_GROUPS]; /* Huffman coding tables */
|
||||
};
|
||||
typedef struct bunzip_data bunzip_data;
|
||||
/* typedef struct bunzip_data bunzip_data; -- done in .h file */
|
||||
|
||||
|
||||
/* Return the next nnn bits of input. All reads from the compressed input
|
||||
@@ -127,7 +127,7 @@ static unsigned get_bits(bunzip_data *bd, int bits_wanted)
|
||||
/* if "no input fd" case: in_fd == -1, read fails, we jump */
|
||||
bd->inbufCount = read(bd->in_fd, bd->inbuf, IOBUF_SIZE);
|
||||
if (bd->inbufCount <= 0)
|
||||
longjmp(*bd->jmpbuf, RETVAL_UNEXPECTED_INPUT_EOF);
|
||||
longjmp(bd->jmpbuf, RETVAL_UNEXPECTED_INPUT_EOF);
|
||||
bd->inbufPos = 0;
|
||||
}
|
||||
|
||||
@@ -151,12 +151,12 @@ static unsigned get_bits(bunzip_data *bd, int bits_wanted)
|
||||
|
||||
return bits;
|
||||
}
|
||||
//#define get_bits(bd, n) (dbg("%d:get_bits()", __LINE__), get_bits(bd, n))
|
||||
|
||||
/* Unpacks the next block and sets up for the inverse Burrows-Wheeler step. */
|
||||
static int get_next_block(bunzip_data *bd)
|
||||
{
|
||||
int groupCount, selector,
|
||||
struct group_data *hufGroup;
|
||||
int groupCount, *base, *limit, selector,
|
||||
i, j, symCount, symTotal, nSelectors, byteCount[256];
|
||||
uint8_t uc, symToByte[256], mtfSymbol[256], *selectors;
|
||||
uint32_t *dbuf;
|
||||
@@ -179,19 +179,15 @@ static int get_next_block(bunzip_data *bd)
|
||||
i = get_bits(bd, 24);
|
||||
j = get_bits(bd, 24);
|
||||
bd->headerCRC = get_bits(bd, 32);
|
||||
if ((i == 0x177245) && (j == 0x385090))
|
||||
return RETVAL_LAST_BLOCK;
|
||||
if ((i != 0x314159) || (j != 0x265359))
|
||||
return RETVAL_NOT_BZIP_DATA;
|
||||
if ((i == 0x177245) && (j == 0x385090)) return RETVAL_LAST_BLOCK;
|
||||
if ((i != 0x314159) || (j != 0x265359)) return RETVAL_NOT_BZIP_DATA;
|
||||
|
||||
/* We can add support for blockRandomised if anybody complains. There was
|
||||
some code for this in busybox 1.0.0-pre3, but nobody ever noticed that
|
||||
it didn't actually work. */
|
||||
if (get_bits(bd, 1))
|
||||
return RETVAL_OBSOLETE_INPUT;
|
||||
if (get_bits(bd, 1)) return RETVAL_OBSOLETE_INPUT;
|
||||
origPtr = get_bits(bd, 24);
|
||||
if (origPtr > bd->dbufSize)
|
||||
return RETVAL_DATA_ERROR;
|
||||
if (origPtr > bd->dbufSize) return RETVAL_DATA_ERROR;
|
||||
|
||||
/* mapping table: if some byte values are never used (encoding things
|
||||
like ascii text), the compression code removes the gaps to have fewer
|
||||
@@ -235,21 +231,13 @@ static int get_next_block(bunzip_data *bd)
|
||||
/* Get next value */
|
||||
int n = 0;
|
||||
while (get_bits(bd, 1)) {
|
||||
if (n >= groupCount) return RETVAL_DATA_ERROR;
|
||||
n++;
|
||||
if (n >= groupCount)
|
||||
return RETVAL_DATA_ERROR;
|
||||
}
|
||||
/* Decode MTF to get the next selector */
|
||||
tmp_byte = mtfSymbol[n];
|
||||
while (--n >= 0)
|
||||
mtfSymbol[n + 1] = mtfSymbol[n];
|
||||
//We catch it later, in the second loop where we use selectors[i].
|
||||
//Maybe this is a better place, though?
|
||||
// if (tmp_byte >= groupCount) {
|
||||
// dbg("%d: selectors[%d]:%d groupCount:%d",
|
||||
// __LINE__, i, tmp_byte, groupCount);
|
||||
// return RETVAL_DATA_ERROR;
|
||||
// }
|
||||
mtfSymbol[0] = selectors[i] = tmp_byte;
|
||||
}
|
||||
|
||||
@@ -260,8 +248,6 @@ static int get_next_block(bunzip_data *bd)
|
||||
uint8_t length[MAX_SYMBOLS];
|
||||
/* 8 bits is ALMOST enough for temp[], see below */
|
||||
unsigned temp[MAX_HUFCODE_BITS+1];
|
||||
struct group_data *hufGroup;
|
||||
int *base, *limit;
|
||||
int minLen, maxLen, pp, len_m1;
|
||||
|
||||
/* Read Huffman code lengths for each symbol. They're stored in
|
||||
@@ -297,10 +283,8 @@ static int get_next_block(bunzip_data *bd)
|
||||
/* Find largest and smallest lengths in this group */
|
||||
minLen = maxLen = length[0];
|
||||
for (i = 1; i < symCount; i++) {
|
||||
if (length[i] > maxLen)
|
||||
maxLen = length[i];
|
||||
else if (length[i] < minLen)
|
||||
minLen = length[i];
|
||||
if (length[i] > maxLen) maxLen = length[i];
|
||||
else if (length[i] < minLen) minLen = length[i];
|
||||
}
|
||||
|
||||
/* Calculate permute[], base[], and limit[] tables from length[].
|
||||
@@ -336,8 +320,7 @@ static int get_next_block(bunzip_data *bd)
|
||||
/* Count symbols coded for at each bit length */
|
||||
/* NB: in pathological cases, temp[8] can end ip being 256.
|
||||
* That's why uint8_t is too small for temp[]. */
|
||||
for (i = 0; i < symCount; i++)
|
||||
temp[length[i]]++;
|
||||
for (i = 0; i < symCount; i++) temp[length[i]]++;
|
||||
|
||||
/* Calculate limit[] (the largest symbol-coding value at each bit
|
||||
* length, which is (previous limit<<1)+symbols at this level), and
|
||||
@@ -380,22 +363,12 @@ static int get_next_block(bunzip_data *bd)
|
||||
|
||||
runPos = dbufCount = selector = 0;
|
||||
for (;;) {
|
||||
struct group_data *hufGroup;
|
||||
int *base, *limit;
|
||||
int nextSym;
|
||||
uint8_t ngrp;
|
||||
|
||||
/* Fetch next Huffman coding group from list. */
|
||||
symCount = GROUP_SIZE - 1;
|
||||
if (selector >= nSelectors)
|
||||
return RETVAL_DATA_ERROR;
|
||||
ngrp = selectors[selector++];
|
||||
if (ngrp >= groupCount) {
|
||||
dbg("%d selectors[%d]:%d groupCount:%d",
|
||||
__LINE__, selector-1, ngrp, groupCount);
|
||||
return RETVAL_DATA_ERROR;
|
||||
}
|
||||
hufGroup = bd->groups + ngrp;
|
||||
if (selector >= nSelectors) return RETVAL_DATA_ERROR;
|
||||
hufGroup = bd->groups + selectors[selector++];
|
||||
base = hufGroup->base - 1;
|
||||
limit = hufGroup->limit - 1;
|
||||
|
||||
@@ -430,8 +403,7 @@ static int get_next_block(bunzip_data *bd)
|
||||
}
|
||||
/* Figure how many bits are in next symbol and unget extras */
|
||||
i = hufGroup->minLen;
|
||||
while (nextSym > limit[i])
|
||||
++i;
|
||||
while (nextSym > limit[i]) ++i;
|
||||
j = hufGroup->maxLen - i;
|
||||
if (j < 0)
|
||||
return RETVAL_DATA_ERROR;
|
||||
@@ -575,7 +547,7 @@ static int get_next_block(bunzip_data *bd)
|
||||
in outbuf. IOW: on EOF returns len ("all bytes are not filled"), not 0.
|
||||
(Why? This allows to get rid of one local variable)
|
||||
*/
|
||||
static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
|
||||
int FAST_FUNC read_bunzip(bunzip_data *bd, char *outbuf, int len)
|
||||
{
|
||||
const uint32_t *dbuf;
|
||||
int pos, current, previous;
|
||||
@@ -699,10 +671,7 @@ static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
|
||||
/* Because bunzip2 is used for help text unpacking, and because bb_show_usage()
|
||||
should work for NOFORK applets too, we must be extremely careful to not leak
|
||||
any allocations! */
|
||||
static int FAST_FUNC start_bunzip(
|
||||
void *jmpbuf,
|
||||
bunzip_data **bdp,
|
||||
int in_fd,
|
||||
int FAST_FUNC start_bunzip(bunzip_data **bdp, int in_fd,
|
||||
const void *inbuf, int len)
|
||||
{
|
||||
bunzip_data *bd;
|
||||
@@ -714,14 +683,11 @@ static int FAST_FUNC start_bunzip(
|
||||
|
||||
/* Figure out how much data to allocate */
|
||||
i = sizeof(bunzip_data);
|
||||
if (in_fd != -1)
|
||||
i += IOBUF_SIZE;
|
||||
if (in_fd != -1) i += IOBUF_SIZE;
|
||||
|
||||
/* Allocate bunzip_data. Most fields initialize to zero. */
|
||||
bd = *bdp = xzalloc(i);
|
||||
|
||||
bd->jmpbuf = jmpbuf;
|
||||
|
||||
/* Setup input buffer */
|
||||
bd->in_fd = in_fd;
|
||||
if (-1 == in_fd) {
|
||||
@@ -736,6 +702,10 @@ static int FAST_FUNC start_bunzip(
|
||||
/* Init the CRC32 table (big endian) */
|
||||
crc32_filltable(bd->crc32Table, 1);
|
||||
|
||||
/* Setup for I/O error handling via longjmp */
|
||||
i = setjmp(bd->jmpbuf);
|
||||
if (i) return i;
|
||||
|
||||
/* Ensure that file starts with "BZh['1'-'9']." */
|
||||
/* Update: now caller verifies 1st two bytes, makes .gz/.bz2
|
||||
* integration easier */
|
||||
@@ -759,7 +729,7 @@ static int FAST_FUNC start_bunzip(
|
||||
return RETVAL_OK;
|
||||
}
|
||||
|
||||
static void FAST_FUNC dealloc_bunzip(bunzip_data *bd)
|
||||
void FAST_FUNC dealloc_bunzip(bunzip_data *bd)
|
||||
{
|
||||
free(bd->dbuf);
|
||||
free(bd);
|
||||
@@ -782,12 +752,8 @@ unpack_bz2_stream(transformer_state_t *xstate)
|
||||
outbuf = xmalloc(IOBUF_SIZE);
|
||||
len = 0;
|
||||
while (1) { /* "Process one BZ... stream" loop */
|
||||
jmp_buf jmpbuf;
|
||||
|
||||
/* Setup for I/O error handling via longjmp */
|
||||
i = setjmp(jmpbuf);
|
||||
if (i == 0)
|
||||
i = start_bunzip(&jmpbuf, &bd, xstate->src_fd, outbuf + 2, len);
|
||||
i = start_bunzip(&bd, xstate->src_fd, outbuf + 2, len);
|
||||
|
||||
if (i == 0) {
|
||||
while (1) { /* "Produce some output bytes" loop */
|
||||
@@ -809,7 +775,7 @@ unpack_bz2_stream(transformer_state_t *xstate)
|
||||
/* Observed case when i == RETVAL_OK:
|
||||
* "bzcat z.bz2", where "z.bz2" is a bzipped zero-length file
|
||||
* (to be exact, z.bz2 is exactly these 14 bytes:
|
||||
* 42 5a 68 39 17 72 45 38 50 90 00 00 00 00).
|
||||
* 42 5a 68 39 17 72 45 38 50 90 00 00 00 00).
|
||||
*/
|
||||
&& i != RETVAL_OK
|
||||
) {
|
||||
@@ -817,7 +783,7 @@ unpack_bz2_stream(transformer_state_t *xstate)
|
||||
break;
|
||||
}
|
||||
if (bd->headerCRC != bd->totalCRC) {
|
||||
bb_simple_error_msg("CRC error");
|
||||
bb_error_msg("CRC error");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -847,36 +813,6 @@ unpack_bz2_stream(transformer_state_t *xstate)
|
||||
return i ? i : IF_DESKTOP(total_written) + 0;
|
||||
}
|
||||
|
||||
char* FAST_FUNC
|
||||
unpack_bz2_data(const char *packed, int packed_len, int unpacked_len)
|
||||
{
|
||||
char *outbuf = NULL;
|
||||
bunzip_data *bd;
|
||||
int i;
|
||||
jmp_buf jmpbuf;
|
||||
|
||||
/* Setup for I/O error handling via longjmp */
|
||||
i = setjmp(jmpbuf);
|
||||
if (i == 0) {
|
||||
i = start_bunzip(&jmpbuf,
|
||||
&bd,
|
||||
/* src_fd: */ -1,
|
||||
/* inbuf: */ packed,
|
||||
/* len: */ packed_len
|
||||
);
|
||||
}
|
||||
/* read_bunzip can longjmp and end up here with i != 0
|
||||
* on read data errors! Not trivial */
|
||||
if (i == 0) {
|
||||
/* Cannot use xmalloc: will leak bd in NOFORK case! */
|
||||
outbuf = malloc_or_warn(unpacked_len);
|
||||
if (outbuf)
|
||||
read_bunzip(bd, outbuf, unpacked_len);
|
||||
}
|
||||
dealloc_bunzip(bd);
|
||||
return outbuf;
|
||||
}
|
||||
|
||||
#ifdef TESTING
|
||||
|
||||
static char *const bunzip_errors[] = {
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
*
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
#include <setjmp.h>
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
@@ -39,8 +40,7 @@ typedef struct huft_t {
|
||||
unsigned char e; /* number of extra bits or operation */
|
||||
unsigned char b; /* number of bits in this code or subcode */
|
||||
union {
|
||||
unsigned n; /* literal, length base, or distance base */
|
||||
/* ^^^^^ was "unsigned short", but that results in larger code */
|
||||
unsigned short n; /* literal, length base, or distance base */
|
||||
struct huft_t *t; /* pointer to next level of table */
|
||||
} v;
|
||||
} huft_t;
|
||||
@@ -185,26 +185,29 @@ static const uint16_t mask_bits[] ALIGN2 = {
|
||||
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
|
||||
};
|
||||
|
||||
/* Put lengths/offsets and extra bits in a struct of arrays
|
||||
* to make calls to huft_build() have one fewer parameter.
|
||||
*/
|
||||
struct cp_ext {
|
||||
uint16_t cp[31];
|
||||
uint8_t ext[31];
|
||||
/* Copy lengths for literal codes 257..285 */
|
||||
static const uint16_t cplens[] ALIGN2 = {
|
||||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
|
||||
67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
|
||||
};
|
||||
/* Copy lengths and extra bits for literal codes 257..285 */
|
||||
|
||||
/* note: see note #13 above about the 258 in this list. */
|
||||
static const struct cp_ext lit ALIGN2 = {
|
||||
/*257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 */
|
||||
/*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 */
|
||||
{ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 } /* 99 == invalid */
|
||||
/* Extra bits for literal codes 257..285 */
|
||||
static const uint8_t cplext[] ALIGN1 = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5,
|
||||
5, 5, 5, 0, 99, 99
|
||||
}; /* 99 == invalid */
|
||||
|
||||
/* Copy offsets for distance codes 0..29 */
|
||||
static const uint16_t cpdist[] ALIGN2 = {
|
||||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
|
||||
769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
|
||||
};
|
||||
/* Copy offsets and extra bits for distance codes 0..29 */
|
||||
static const struct cp_ext dist ALIGN2 = {
|
||||
/*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 */
|
||||
{ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 },
|
||||
{ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }
|
||||
|
||||
/* Extra bits for distance codes */
|
||||
static const uint8_t cpdext[] ALIGN1 = {
|
||||
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
|
||||
11, 11, 12, 12, 13, 13
|
||||
};
|
||||
|
||||
/* Tables for deflate from PKZIP's appnote.txt. */
|
||||
@@ -220,19 +223,10 @@ static const uint8_t border[] ALIGN1 = {
|
||||
* each table.
|
||||
* t: table to free
|
||||
*/
|
||||
#define BAD_HUFT(p) ((uintptr_t)(p) & 1)
|
||||
#define ERR_RET ((huft_t*)(uintptr_t)1)
|
||||
static void huft_free(huft_t *p)
|
||||
{
|
||||
huft_t *q;
|
||||
|
||||
/*
|
||||
* If 'p' has the error bit set we have to clear it, otherwise we might run
|
||||
* into a segmentation fault or an invalid pointer to free(p)
|
||||
*/
|
||||
//if (BAD_HUFT(p)) // commented out, since bit clearing has effect only if condition is true
|
||||
p = (huft_t*)((uintptr_t)p & ~(uintptr_t)ERR_RET);
|
||||
|
||||
/* Go through linked list, freeing from the malloced (t[-1]) address. */
|
||||
while (p) {
|
||||
q = (--p)->v.t;
|
||||
@@ -284,23 +278,22 @@ static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current
|
||||
|
||||
|
||||
/* Given a list of code lengths and a maximum table size, make a set of
|
||||
* tables to decode that set of codes.
|
||||
* tables to decode that set of codes. Return zero on success, one if
|
||||
* the given code set is incomplete (the tables are still built in this
|
||||
* case), two if the input is invalid (all zero length codes or an
|
||||
* oversubscribed set of lengths) - in this case stores NULL in *t.
|
||||
*
|
||||
* b: code lengths in bits (all assumed <= BMAX)
|
||||
* n: number of codes (assumed <= N_MAX)
|
||||
* s: number of simple-valued codes (0..s-1)
|
||||
* cp_ext->cp,ext: list of base values/extra bits for non-simple codes
|
||||
* d: list of base values for non-simple codes
|
||||
* e: list of extra bits for non-simple codes
|
||||
* t: result: starting table
|
||||
* m: maximum lookup bits, returns actual
|
||||
* result: starting table
|
||||
*
|
||||
* On error, returns a value with lowest-bit set on error.
|
||||
* It can be just the value of 0x1,
|
||||
* or a valid pointer to a Huffman table, ORed with 0x1 if incompete table
|
||||
* is given: "fixed inflate" decoder feeds us such data.
|
||||
*/
|
||||
static huft_t* huft_build(const unsigned *b, const unsigned n,
|
||||
const unsigned s, const struct cp_ext *cp_ext,
|
||||
unsigned *m)
|
||||
static int huft_build(const unsigned *b, const unsigned n,
|
||||
const unsigned s, const unsigned short *d,
|
||||
const unsigned char *e, huft_t **t, unsigned *m)
|
||||
{
|
||||
unsigned a; /* counter for codes of length k */
|
||||
unsigned c[BMAX + 1]; /* bit length count table */
|
||||
@@ -322,12 +315,12 @@ static huft_t* huft_build(const unsigned *b, const unsigned n,
|
||||
unsigned *xp; /* pointer into x */
|
||||
int y; /* number of dummy codes added */
|
||||
unsigned z; /* number of entries in current table */
|
||||
huft_t *result;
|
||||
huft_t **t;
|
||||
|
||||
/* Length of EOB code, if any */
|
||||
eob_len = n > 256 ? b[256] : BMAX;
|
||||
|
||||
*t = NULL;
|
||||
|
||||
/* Generate counts for each bit length */
|
||||
memset(c, 0, sizeof(c));
|
||||
p = b;
|
||||
@@ -337,14 +330,8 @@ static huft_t* huft_build(const unsigned *b, const unsigned n,
|
||||
p++; /* can't combine with above line (Solaris bug) */
|
||||
} while (--i);
|
||||
if (c[0] == n) { /* null input - all zero length codes */
|
||||
q = xzalloc(3 * sizeof(*q));
|
||||
//q[0].v.t = NULL;
|
||||
q[1].e = 99; /* invalid code marker */
|
||||
q[1].b = 1;
|
||||
q[2].e = 99; /* invalid code marker */
|
||||
q[2].b = 1;
|
||||
*m = 1;
|
||||
return q + 1;
|
||||
*m = 0;
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* Find minimum and maximum length, bound *m by those */
|
||||
@@ -360,11 +347,11 @@ static huft_t* huft_build(const unsigned *b, const unsigned n,
|
||||
for (y = 1 << j; j < i; j++, y <<= 1) {
|
||||
y -= c[j];
|
||||
if (y < 0)
|
||||
return ERR_RET; /* bad input: more codes than bits */
|
||||
return 2; /* bad input: more codes than bits */
|
||||
}
|
||||
y -= c[i];
|
||||
if (y < 0)
|
||||
return ERR_RET;
|
||||
return 2;
|
||||
c[i] += y;
|
||||
|
||||
/* Generate starting offsets into the value table for each length */
|
||||
@@ -391,8 +378,6 @@ static huft_t* huft_build(const unsigned *b, const unsigned n,
|
||||
} while (++i < n);
|
||||
|
||||
/* Generate the Huffman codes and for each, make the table entries */
|
||||
result = ERR_RET;
|
||||
t = &result;
|
||||
x[0] = i = 0; /* first Huffman code is zero */
|
||||
p = v; /* grab values in bit order */
|
||||
htl = -1; /* no tables yet--level -1 */
|
||||
@@ -458,8 +443,8 @@ static huft_t* huft_build(const unsigned *b, const unsigned n,
|
||||
r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */
|
||||
r.v.n = (unsigned short) (*p++); /* simple code is just the value */
|
||||
} else {
|
||||
r.e = (unsigned char) cp_ext->ext[*p - s]; /* non-simple--look up in lists */
|
||||
r.v.n = cp_ext->cp[*p++ - s];
|
||||
r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
|
||||
r.v.n = d[*p++ - s];
|
||||
}
|
||||
|
||||
/* fill code-like entries with r */
|
||||
@@ -484,11 +469,8 @@ static huft_t* huft_build(const unsigned *b, const unsigned n,
|
||||
/* return actual size of base table */
|
||||
*m = ws[1];
|
||||
|
||||
if (y != 0 && g != 1) /* we were given an incomplete table */
|
||||
/* return "result" ORed with 1 */
|
||||
return (void*)((uintptr_t)result | 1);
|
||||
|
||||
return result;
|
||||
/* Return 1 if we were given an incomplete table */
|
||||
return y != 0 && g != 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -789,17 +771,14 @@ static int inflate_block(STATE_PARAM smallint *e)
|
||||
for (; i < 288; i++) /* make a complete, but wrong code set */
|
||||
ll[i] = 8;
|
||||
bl = 7;
|
||||
inflate_codes_tl = huft_build(ll, 288, 257, &lit, &bl);
|
||||
/* ^^^ never returns error here - we use known data */
|
||||
huft_build(ll, 288, 257, cplens, cplext, &inflate_codes_tl, &bl);
|
||||
/* huft_build() never return nonzero - we use known data */
|
||||
|
||||
/* set up distance table */
|
||||
for (i = 0; i < 30; i++) /* make an incomplete code set */
|
||||
ll[i] = 5;
|
||||
bd = 5;
|
||||
inflate_codes_td = huft_build(ll, 30, 0, &dist, &bd);
|
||||
/* ^^^ does return error here! (lsb bit is set) - we gave it incomplete code set */
|
||||
/* clearing error bit: */
|
||||
inflate_codes_td = (void*)((uintptr_t)inflate_codes_td & ~(uintptr_t)1);
|
||||
huft_build(ll, 30, 0, cpdist, cpdext, &inflate_codes_td, &bd);
|
||||
|
||||
/* set up data for inflate_codes() */
|
||||
inflate_codes_setup(PASS_STATE bl, bd);
|
||||
@@ -865,9 +844,9 @@ static int inflate_block(STATE_PARAM smallint *e)
|
||||
|
||||
/* build decoding table for trees - single level, 7 bit lookup */
|
||||
bl = 7;
|
||||
inflate_codes_tl = huft_build(ll, 19, 19, NULL, &bl);
|
||||
if (BAD_HUFT(inflate_codes_tl)) {
|
||||
abort_unzip(PASS_STATE_ONLY); /* incomplete code set */
|
||||
i = huft_build(ll, 19, 19, NULL, NULL, &inflate_codes_tl, &bl);
|
||||
if (i != 0) {
|
||||
abort_unzip(PASS_STATE_ONLY); //return i; /* incomplete code set */
|
||||
}
|
||||
|
||||
/* read in literal and distance code lengths */
|
||||
@@ -930,13 +909,14 @@ static int inflate_block(STATE_PARAM smallint *e)
|
||||
|
||||
/* build the decoding tables for literal/length and distance codes */
|
||||
bl = lbits;
|
||||
inflate_codes_tl = huft_build(ll, nl, 257, &lit, &bl);
|
||||
if (BAD_HUFT(inflate_codes_tl)) {
|
||||
|
||||
i = huft_build(ll, nl, 257, cplens, cplext, &inflate_codes_tl, &bl);
|
||||
if (i != 0) {
|
||||
abort_unzip(PASS_STATE_ONLY);
|
||||
}
|
||||
bd = dbits;
|
||||
inflate_codes_td = huft_build(ll + nl, nd, 0, &dist, &bd);
|
||||
if (BAD_HUFT(inflate_codes_td)) {
|
||||
i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &inflate_codes_td, &bd);
|
||||
if (i != 0) {
|
||||
abort_unzip(PASS_STATE_ONLY);
|
||||
}
|
||||
|
||||
@@ -1020,13 +1000,13 @@ inflate_unzip_internal(STATE_PARAM transformer_state_t *xstate)
|
||||
gunzip_bb = 0;
|
||||
|
||||
/* Create the crc table */
|
||||
gunzip_crc_table = crc32_new_table_le();
|
||||
gunzip_crc_table = crc32_filltable(NULL, 0);
|
||||
gunzip_crc = ~0;
|
||||
|
||||
error_msg = "corrupted data";
|
||||
if (setjmp(error_jmp)) {
|
||||
/* Error from deep inside zip machinery */
|
||||
bb_simple_error_msg(error_msg);
|
||||
bb_error_msg(error_msg);
|
||||
n = -1;
|
||||
goto ret;
|
||||
}
|
||||
@@ -1099,7 +1079,7 @@ static int top_up(STATE_PARAM unsigned n)
|
||||
bytebuffer_offset = 0;
|
||||
bytebuffer_size = full_read(gunzip_src_fd, &bytebuffer[count], bytebuffer_max - count);
|
||||
if ((int)bytebuffer_size < 0) {
|
||||
bb_simple_error_msg(bb_msg_read_error);
|
||||
bb_error_msg(bb_msg_read_error);
|
||||
return 0;
|
||||
}
|
||||
bytebuffer_size += count;
|
||||
@@ -1225,7 +1205,7 @@ unpack_gz_stream(transformer_state_t *xstate)
|
||||
|
||||
if (full_read(xstate->src_fd, &magic2, 2) != 2) {
|
||||
bad_magic:
|
||||
bb_simple_error_msg("invalid magic");
|
||||
bb_error_msg("invalid magic");
|
||||
return -1;
|
||||
}
|
||||
if (magic2 == COMPRESS_MAGIC) {
|
||||
@@ -1247,7 +1227,7 @@ unpack_gz_stream(transformer_state_t *xstate)
|
||||
|
||||
again:
|
||||
if (!check_header_gzip(PASS_STATE xstate)) {
|
||||
bb_simple_error_msg("corrupted data");
|
||||
bb_error_msg("corrupted data");
|
||||
total = -1;
|
||||
goto ret;
|
||||
}
|
||||
@@ -1260,7 +1240,7 @@ unpack_gz_stream(transformer_state_t *xstate)
|
||||
total += n;
|
||||
|
||||
if (!top_up(PASS_STATE 8)) {
|
||||
bb_simple_error_msg("corrupted data");
|
||||
bb_error_msg("corrupted data");
|
||||
total = -1;
|
||||
goto ret;
|
||||
}
|
||||
@@ -1268,7 +1248,7 @@ unpack_gz_stream(transformer_state_t *xstate)
|
||||
/* Validate decompression - crc */
|
||||
v32 = buffer_read_le_u32(PASS_STATE_ONLY);
|
||||
if ((~gunzip_crc) != v32) {
|
||||
bb_simple_error_msg("crc error");
|
||||
bb_error_msg("crc error");
|
||||
total = -1;
|
||||
goto ret;
|
||||
}
|
||||
@@ -1276,7 +1256,7 @@ unpack_gz_stream(transformer_state_t *xstate)
|
||||
/* Validate decompression - size */
|
||||
v32 = buffer_read_le_u32(PASS_STATE_ONLY);
|
||||
if ((uint32_t)gunzip_bytes_out != v32) {
|
||||
bb_simple_error_msg("incorrect length");
|
||||
bb_error_msg("incorrect length");
|
||||
total = -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ unpack_Z_stream(transformer_state_t *xstate)
|
||||
/* xread isn't good here, we have to return - caller may want
|
||||
* to do some cleanup (e.g. delete incomplete unpacked file etc) */
|
||||
if (full_read(xstate->src_fd, inbuf, 1) != 1) {
|
||||
bb_simple_error_msg("short read");
|
||||
bb_error_msg("short read");
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ unpack_Z_stream(transformer_state_t *xstate)
|
||||
if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
|
||||
rsize = safe_read(xstate->src_fd, inbuf + insize, IBUFSIZ);
|
||||
if (rsize < 0)
|
||||
bb_simple_error_msg_and_die(bb_msg_read_error);
|
||||
bb_error_msg_and_die(bb_msg_read_error);
|
||||
insize += rsize;
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ unpack_Z_stream(transformer_state_t *xstate)
|
||||
|
||||
if (oldcode == -1) {
|
||||
if (code >= 256)
|
||||
bb_simple_error_msg_and_die("corrupted data"); /* %ld", code); */
|
||||
bb_error_msg_and_die("corrupted data"); /* %ld", code); */
|
||||
oldcode = code;
|
||||
finchar = (int) oldcode;
|
||||
outbuf[outpos++] = (unsigned char) finchar;
|
||||
@@ -236,7 +236,7 @@ unpack_Z_stream(transformer_state_t *xstate)
|
||||
insize, posbits, p[-1], p[0], p[1], p[2], p[3],
|
||||
(posbits & 07));
|
||||
*/
|
||||
bb_simple_error_msg("corrupted data");
|
||||
bb_error_msg("corrupted data");
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ unpack_Z_stream(transformer_state_t *xstate)
|
||||
/* Generate output characters in reverse order */
|
||||
while (code >= 256) {
|
||||
if (stackp <= &htabof(0))
|
||||
bb_simple_error_msg_and_die("corrupted data");
|
||||
bb_error_msg_and_die("corrupted data");
|
||||
*--stackp = tab_suffixof(code);
|
||||
code = tab_prefixof(code);
|
||||
}
|
||||
|
||||
@@ -11,13 +11,6 @@
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
#if 0
|
||||
# define dbg(...) bb_error_msg(__VA_ARGS__)
|
||||
#else
|
||||
# define dbg(...) ((void)0)
|
||||
#endif
|
||||
|
||||
|
||||
#if ENABLE_FEATURE_LZMA_FAST
|
||||
# define speed_inline ALWAYS_INLINE
|
||||
# define size_inline
|
||||
@@ -59,7 +52,7 @@ static void rc_read(rc_t *rc)
|
||||
//TODO: return -1 instead
|
||||
//This will make unlzma delete broken unpacked file on unpack errors
|
||||
if (buffer_size <= 0)
|
||||
bb_simple_error_msg_and_die("unexpected EOF");
|
||||
bb_error_msg_and_die("unexpected EOF");
|
||||
rc->buffer_end = RC_BUFFER + buffer_size;
|
||||
rc->ptr = RC_BUFFER;
|
||||
}
|
||||
@@ -224,7 +217,6 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
rc_t *rc;
|
||||
int i;
|
||||
uint8_t *buffer;
|
||||
uint32_t buffer_size;
|
||||
uint8_t previous_byte = 0;
|
||||
size_t buffer_pos = 0, global_pos = 0;
|
||||
int len = 0;
|
||||
@@ -234,7 +226,7 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header)
|
||||
|| header.pos >= (9 * 5 * 5)
|
||||
) {
|
||||
bb_simple_error_msg("bad lzma header");
|
||||
bb_error_msg("bad lzma header");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -254,8 +246,7 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
if (header.dict_size == 0)
|
||||
header.dict_size++;
|
||||
|
||||
buffer_size = MIN(header.dst_size, header.dict_size);
|
||||
buffer = xmalloc(buffer_size);
|
||||
buffer = xmalloc(MIN(header.dst_size, header.dict_size));
|
||||
|
||||
{
|
||||
int num_probs;
|
||||
@@ -290,11 +281,8 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
uint32_t pos;
|
||||
|
||||
pos = buffer_pos - rep0;
|
||||
if ((int32_t)pos < 0) {
|
||||
if ((int32_t)pos < 0)
|
||||
pos += header.dict_size;
|
||||
if ((int32_t)pos < 0)
|
||||
goto bad;
|
||||
}
|
||||
match_byte = buffer[pos];
|
||||
do {
|
||||
int bit;
|
||||
@@ -353,14 +341,8 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
|
||||
|
||||
pos = buffer_pos - rep0;
|
||||
if ((int32_t)pos < 0) {
|
||||
if ((int32_t)pos < 0)
|
||||
pos += header.dict_size;
|
||||
/* see unzip_bad_lzma_2.zip: */
|
||||
if (pos >= buffer_size) {
|
||||
dbg("%d pos:%d buffer_size:%d", __LINE__, pos, buffer_size);
|
||||
goto bad;
|
||||
}
|
||||
}
|
||||
previous_byte = buffer[pos];
|
||||
goto one_byte1;
|
||||
#else
|
||||
@@ -435,9 +417,6 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
|
||||
rep0 = (rep0 << 1) | rc_direct_bit(rc);
|
||||
rep0 <<= LZMA_NUM_ALIGN_BITS;
|
||||
// Note: (int32_t)rep0 may be < 0 here
|
||||
// (I have linux-3.3.4.tar.lzma which has it).
|
||||
// I moved the check after "++rep0 == 0" check below.
|
||||
prob3 = p + LZMA_ALIGN;
|
||||
}
|
||||
i2 = 1;
|
||||
@@ -448,13 +427,8 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
i2 <<= 1;
|
||||
}
|
||||
}
|
||||
rep0++;
|
||||
if ((int32_t)rep0 <= 0) {
|
||||
if (rep0 == 0)
|
||||
break;
|
||||
dbg("%d rep0:%d", __LINE__, rep0);
|
||||
goto bad;
|
||||
}
|
||||
if (++rep0 == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
len += LZMA_MATCH_MIN_LEN;
|
||||
@@ -479,10 +453,7 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
if ((int32_t)pos < 0) {
|
||||
pos += header.dict_size;
|
||||
/* bug 10436 has an example file where this triggers: */
|
||||
//if ((int32_t)pos < 0)
|
||||
// goto bad;
|
||||
/* more stringent test (see unzip_bad_lzma_1.zip): */
|
||||
if (pos >= buffer_size)
|
||||
if ((int32_t)pos < 0)
|
||||
goto bad;
|
||||
}
|
||||
previous_byte = buffer[pos];
|
||||
@@ -511,12 +482,6 @@ unpack_lzma_stream(transformer_state_t *xstate)
|
||||
IF_DESKTOP(total_written += buffer_pos;)
|
||||
if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) {
|
||||
bad:
|
||||
/* One of our users, bbunpack(), expects _us_ to emit
|
||||
* the error message (since it's the best place to give
|
||||
* potentially more detailed information).
|
||||
* Do not fail silently.
|
||||
*/
|
||||
bb_simple_error_msg("corrupted data");
|
||||
total_written = -1; /* failure */
|
||||
}
|
||||
rc_free(rc);
|
||||
|
||||
@@ -52,7 +52,7 @@ unpack_xz_stream(transformer_state_t *xstate)
|
||||
IF_DESKTOP(long long) int total = 0;
|
||||
|
||||
if (!global_crc32_table)
|
||||
global_crc32_new_table_le();
|
||||
global_crc32_table = crc32_filltable(NULL, /*endian:*/ 0);
|
||||
|
||||
memset(&iobuf, 0, sizeof(iobuf));
|
||||
membuf = xmalloc(2 * BUFSIZ);
|
||||
@@ -74,7 +74,7 @@ unpack_xz_stream(transformer_state_t *xstate)
|
||||
if (iobuf.in_pos == iobuf.in_size) {
|
||||
int rd = safe_read(xstate->src_fd, membuf, BUFSIZ);
|
||||
if (rd < 0) {
|
||||
bb_simple_error_msg(bb_msg_read_error);
|
||||
bb_error_msg(bb_msg_read_error);
|
||||
total = -1;
|
||||
break;
|
||||
}
|
||||
@@ -96,24 +96,6 @@ unpack_xz_stream(transformer_state_t *xstate)
|
||||
*/
|
||||
do {
|
||||
if (membuf[iobuf.in_pos] != 0) {
|
||||
/* There is more data, but is it XZ data?
|
||||
* Example: dpkg-deb -f busybox_1.30.1-4_amd64.deb
|
||||
* reads control.tar.xz "control" file
|
||||
* inside the ar archive, but tar.xz
|
||||
* extraction code reaches end of xz data,
|
||||
* reached this code and reads the beginning
|
||||
* of data.tar.xz's ar header, which isn't xz data,
|
||||
* and prints "corrupted data".
|
||||
* The correct solution is to not read
|
||||
* past nested archive (to simulate EOF).
|
||||
* This is a workaround:
|
||||
*/
|
||||
if (membuf[iobuf.in_pos] != 0xfd) {
|
||||
/* It's definitely not a xz signature
|
||||
* (which is 0xfd,"7zXZ",0x00).
|
||||
*/
|
||||
goto end;
|
||||
}
|
||||
xz_dec_reset(state);
|
||||
goto do_run;
|
||||
}
|
||||
@@ -141,12 +123,12 @@ unpack_xz_stream(transformer_state_t *xstate)
|
||||
continue;
|
||||
}
|
||||
if (xz_result != XZ_OK && xz_result != XZ_UNSUPPORTED_CHECK) {
|
||||
bb_simple_error_msg("corrupted data");
|
||||
bb_error_msg("corrupted data");
|
||||
total = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
end:
|
||||
|
||||
xz_dec_end(state);
|
||||
free(membuf);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
#include "ar_.h"
|
||||
#include "ar.h"
|
||||
|
||||
/* WARNING: Clobbers str[len], so fields must be read in reverse order! */
|
||||
static unsigned read_num(char *str, int base, int len)
|
||||
@@ -22,7 +22,7 @@ static unsigned read_num(char *str, int base, int len)
|
||||
* on misformatted numbers bb_strtou returns all-ones */
|
||||
err = bb_strtou(str, NULL, base);
|
||||
if (err == -1)
|
||||
bb_simple_error_msg_and_die("invalid ar header");
|
||||
bb_error_msg_and_die("invalid ar header");
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,10 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
|
||||
char raw[60];
|
||||
struct ar_header formatted;
|
||||
} ar;
|
||||
#if ENABLE_FEATURE_AR_LONG_FILENAMES
|
||||
static char *ar_long_names;
|
||||
static unsigned ar_long_name_size;
|
||||
#endif
|
||||
|
||||
/* dont use xread as we want to handle the error ourself */
|
||||
if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
|
||||
@@ -53,7 +57,7 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
|
||||
archive_handle->offset += 60;
|
||||
|
||||
if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
|
||||
bb_simple_error_msg_and_die("invalid ar header");
|
||||
bb_error_msg_and_die("invalid ar header");
|
||||
|
||||
/*
|
||||
* Note that the fields MUST be read in reverse order as
|
||||
@@ -77,27 +81,23 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
|
||||
* stores long filename for multiple entries, they are stored
|
||||
* in static variable long_names for use in future entries
|
||||
*/
|
||||
archive_handle->ar__long_name_size = size;
|
||||
free(archive_handle->ar__long_names);
|
||||
archive_handle->ar__long_names = xzalloc(size + 1);
|
||||
xread(archive_handle->src_fd, archive_handle->ar__long_names, size);
|
||||
ar_long_name_size = size;
|
||||
free(ar_long_names);
|
||||
ar_long_names = xmalloc(size);
|
||||
xread(archive_handle->src_fd, ar_long_names, size);
|
||||
archive_handle->offset += size;
|
||||
/* Return next header */
|
||||
return get_header_ar(archive_handle);
|
||||
}
|
||||
#else
|
||||
bb_simple_error_msg_and_die("long filenames not supported");
|
||||
bb_error_msg_and_die("long filenames not supported");
|
||||
#endif
|
||||
}
|
||||
/* Only size is always present, the rest may be missing in
|
||||
* long filename pseudo file. Thus we decode the rest
|
||||
* after dealing with long filename pseudo file.
|
||||
*
|
||||
* GNU binutils in deterministic mode hard codes mode to 0644 (NOT
|
||||
* 0100644). AR archives can only contain files, so force file
|
||||
* mode.
|
||||
*/
|
||||
typed->mode = read_num(ar.formatted.mode, 8, sizeof(ar.formatted.mode)) | S_IFREG;
|
||||
typed->mode = read_num(ar.formatted.mode, 8, sizeof(ar.formatted.mode));
|
||||
typed->gid = read_num(ar.formatted.gid, 10, sizeof(ar.formatted.gid));
|
||||
typed->uid = read_num(ar.formatted.uid, 10, sizeof(ar.formatted.uid));
|
||||
typed->mtime = read_num(ar.formatted.date, 10, sizeof(ar.formatted.date));
|
||||
@@ -107,13 +107,13 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
|
||||
unsigned long_offset;
|
||||
|
||||
/* The number after the '/' indicates the offset in the ar data section
|
||||
* (saved in ar__long_names) that contains the real filename */
|
||||
* (saved in ar_long_names) that conatains the real filename */
|
||||
long_offset = read_num(&ar.formatted.name[1], 10,
|
||||
sizeof(ar.formatted.name) - 1);
|
||||
if (long_offset >= archive_handle->ar__long_name_size) {
|
||||
bb_simple_error_msg_and_die("can't resolve long filename");
|
||||
if (long_offset >= ar_long_name_size) {
|
||||
bb_error_msg_and_die("can't resolve long filename");
|
||||
}
|
||||
typed->name = xstrdup(archive_handle->ar__long_names + long_offset);
|
||||
typed->name = xstrdup(ar_long_names + long_offset);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
@@ -127,10 +127,8 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
|
||||
archive_handle->action_header(typed);
|
||||
#if ENABLE_DPKG || ENABLE_DPKG_DEB
|
||||
if (archive_handle->dpkg__sub_archive) {
|
||||
struct archive_handle_t *sa = archive_handle->dpkg__sub_archive;
|
||||
while (archive_handle->dpkg__action_data_subarchive(sa) == EXIT_SUCCESS)
|
||||
while (archive_handle->dpkg__action_data_subarchive(archive_handle->dpkg__sub_archive) == EXIT_SUCCESS)
|
||||
continue;
|
||||
create_links_from_list(sa->link_placeholders);
|
||||
} else
|
||||
#endif
|
||||
archive_handle->action_data(archive_handle);
|
||||
|
||||
@@ -20,7 +20,7 @@ typedef struct hardlinks_t {
|
||||
char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
|
||||
{
|
||||
file_header_t *file_header = archive_handle->file_header;
|
||||
char cpio_header[111];
|
||||
char cpio_header[110];
|
||||
int namesize;
|
||||
int major, minor, nlink, mode, inode;
|
||||
unsigned size, uid, gid, mtime;
|
||||
@@ -33,17 +33,16 @@ char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
|
||||
goto create_hardlinks;
|
||||
}
|
||||
if (size != 110) {
|
||||
bb_simple_error_msg_and_die("short read");
|
||||
bb_error_msg_and_die("short read");
|
||||
}
|
||||
archive_handle->offset += 110;
|
||||
|
||||
if (!is_prefixed_with(&cpio_header[0], "07070")
|
||||
|| (cpio_header[5] != '1' && cpio_header[5] != '2')
|
||||
) {
|
||||
bb_simple_error_msg_and_die("unsupported cpio format, use newc or crc");
|
||||
bb_error_msg_and_die("unsupported cpio format, use newc or crc");
|
||||
}
|
||||
|
||||
cpio_header[110] = '\0'; /* sscanf may call strlen which may break without this */
|
||||
if (sscanf(cpio_header + 6,
|
||||
"%8x" "%8x" "%8x" "%8x"
|
||||
"%8x" "%8x" "%8x" /*maj,min:*/ "%*16c"
|
||||
@@ -51,7 +50,7 @@ char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
|
||||
&inode, &mode, &uid, &gid,
|
||||
&nlink, &mtime, &size,
|
||||
&major, &minor, &namesize) != 10)
|
||||
bb_simple_error_msg_and_die("damaged cpio file");
|
||||
bb_error_msg_and_die("damaged cpio file");
|
||||
file_header->mode = mode;
|
||||
/* "cpio -R USER:GRP" support: */
|
||||
if (archive_handle->cpio__owner.uid != (uid_t)-1L)
|
||||
|
||||
@@ -32,7 +32,7 @@ static unsigned long long getOctal(char *str, int len)
|
||||
if (*end != '\0' && *end != ' ') {
|
||||
int8_t first = str[0];
|
||||
if (!(first & 0x80))
|
||||
bb_simple_error_msg_and_die("corrupted octal value in tar header");
|
||||
bb_error_msg_and_die("corrupted octal value in tar header");
|
||||
/*
|
||||
* GNU tar uses "base-256 encoding" for very large numbers.
|
||||
* Encoding is binary, with highest bit always set as a marker
|
||||
@@ -100,7 +100,7 @@ static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int g
|
||||
|| errno != EINVAL
|
||||
|| *end != ' '
|
||||
) {
|
||||
bb_simple_error_msg("malformed extended header, skipped");
|
||||
bb_error_msg("malformed extended header, skipped");
|
||||
// More verbose version:
|
||||
//bb_error_msg("malformed extended header at %"OFF_FMT"d, skipped",
|
||||
// archive_handle->offset - (sz + len));
|
||||
@@ -147,12 +147,6 @@ static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int g
|
||||
#endif
|
||||
}
|
||||
|
||||
static void die_if_bad_fnamesize(off_t sz)
|
||||
{
|
||||
if ((uoff_t)sz > 0xfff) /* more than 4k?! no funny business please */
|
||||
bb_simple_error_msg_and_die("bad archive");
|
||||
}
|
||||
|
||||
char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
{
|
||||
file_header_t *file_header = archive_handle->file_header;
|
||||
@@ -200,13 +194,13 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
* the very first read fails. Grrr.
|
||||
*/
|
||||
if (archive_handle->offset == 0)
|
||||
bb_simple_error_msg("short read");
|
||||
bb_error_msg("short read");
|
||||
/* this merely signals end of archive, not exit(1): */
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (i != 512) {
|
||||
IF_FEATURE_TAR_AUTODETECT(goto autodetect;)
|
||||
bb_simple_error_msg_and_die("short read");
|
||||
bb_error_msg_and_die("short read");
|
||||
}
|
||||
|
||||
#else
|
||||
@@ -249,11 +243,11 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
goto err;
|
||||
if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 0) != 0)
|
||||
err:
|
||||
bb_simple_error_msg_and_die("invalid tar magic");
|
||||
bb_error_msg_and_die("invalid tar magic");
|
||||
archive_handle->offset = 0;
|
||||
goto again_after_align;
|
||||
#endif
|
||||
bb_simple_error_msg_and_die("invalid tar magic");
|
||||
bb_error_msg_and_die("invalid tar magic");
|
||||
}
|
||||
|
||||
/* Do checksum on headers.
|
||||
@@ -288,7 +282,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
if (sum_u != sum
|
||||
IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)
|
||||
) {
|
||||
bb_simple_error_msg_and_die("invalid tar header checksum");
|
||||
bb_error_msg_and_die("invalid tar header checksum");
|
||||
}
|
||||
|
||||
/* GET_OCTAL trashes subsequent field, therefore we call it
|
||||
@@ -337,6 +331,8 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
file_header->name = xstrdup(tar.name);
|
||||
}
|
||||
|
||||
/* Set bits 12-15 of the files mode */
|
||||
/* (typeflag was not trashed because chksum does not use getOctal) */
|
||||
switch (tar_typeflag) {
|
||||
case '1': /* hardlink */
|
||||
/* we mark hardlinks as regular files with zero size and a link name */
|
||||
@@ -345,7 +341,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
* ... For tar archives written by pre POSIX.1-1988
|
||||
* implementations, the size field usually contains the size of
|
||||
* the file and needs to be ignored as no data may follow this
|
||||
* header type. For POSIX.1-1988 compliant archives, the size
|
||||
* header type. For POSIX.1- 1988 compliant archives, the size
|
||||
* field needs to be 0. For POSIX.1-2001 compliant archives,
|
||||
* the size field may be non zero, indicating that file data is
|
||||
* included in the archive.
|
||||
@@ -356,7 +352,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
/* case 0: */
|
||||
case '0':
|
||||
#if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY
|
||||
if (file_header->name && last_char_is(file_header->name, '/')) {
|
||||
if (last_char_is(file_header->name, '/')) {
|
||||
goto set_dir;
|
||||
}
|
||||
#endif
|
||||
@@ -394,7 +390,6 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
/* free: paranoia: tar with several consecutive longnames */
|
||||
free(p_longname);
|
||||
/* For paranoia reasons we allocate extra NUL char */
|
||||
die_if_bad_fnamesize(file_header->size);
|
||||
p_longname = xzalloc(file_header->size + 1);
|
||||
/* We read ASCIZ string, including NUL */
|
||||
xread(archive_handle->src_fd, p_longname, file_header->size);
|
||||
@@ -405,7 +400,6 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
goto again;
|
||||
case 'K':
|
||||
free(p_linkname);
|
||||
die_if_bad_fnamesize(file_header->size);
|
||||
p_linkname = xzalloc(file_header->size + 1);
|
||||
xread(archive_handle->src_fd, p_linkname, file_header->size);
|
||||
archive_handle->offset += file_header->size;
|
||||
@@ -420,8 +414,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
// case 'D': /* GNU dump dir */
|
||||
// case 'M': /* Continuation of multi volume archive */
|
||||
// case 'N': /* Old GNU for names > 100 characters */
|
||||
case 'V': /* Volume header */
|
||||
; /* Fall through to skip it */
|
||||
// case 'V': /* Volume header */
|
||||
#endif
|
||||
}
|
||||
skip_ext_hdr:
|
||||
|
||||
@@ -9,7 +9,6 @@ void FAST_FUNC header_verbose_list(const file_header_t *file_header)
|
||||
{
|
||||
struct tm tm_time;
|
||||
struct tm *ptm = &tm_time; //localtime(&file_header->mtime);
|
||||
char modestr[12];
|
||||
|
||||
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
||||
char uid[sizeof(int)*3 + 2];
|
||||
@@ -30,7 +29,7 @@ void FAST_FUNC header_verbose_list(const file_header_t *file_header)
|
||||
group = utoa(file_header->gid);
|
||||
}
|
||||
printf("%s %s/%s %9"OFF_FMT"u %4u-%02u-%02u %02u:%02u:%02u %s",
|
||||
bb_mode_string(modestr, file_header->mode),
|
||||
bb_mode_string(file_header->mode),
|
||||
user,
|
||||
group,
|
||||
file_header->size,
|
||||
@@ -47,7 +46,7 @@ void FAST_FUNC header_verbose_list(const file_header_t *file_header)
|
||||
localtime_r(&file_header->mtime, ptm);
|
||||
|
||||
printf("%s %u/%u %9"OFF_FMT"u %4u-%02u-%02u %02u:%02u:%02u %s",
|
||||
bb_mode_string(modestr, file_header->mode),
|
||||
bb_mode_string(file_header->mode),
|
||||
(unsigned)file_header->uid,
|
||||
(unsigned)file_header->gid,
|
||||
file_header->size,
|
||||
|
||||
@@ -487,7 +487,7 @@ static int find_match(lzo1x_999_t *c, lzo_swd_p s,
|
||||
}
|
||||
|
||||
/* this is a public functions, but there is no prototype in a header file */
|
||||
static int lzo1x_999_compress_internal(const uint8_t *in, unsigned in_len,
|
||||
static int lzo1x_999_compress_internal(const uint8_t *in , unsigned in_len,
|
||||
uint8_t *out, unsigned *out_len,
|
||||
void *wrkmem,
|
||||
unsigned good_length,
|
||||
|
||||
@@ -31,7 +31,8 @@
|
||||
************************************************************************/
|
||||
/* safe decompression with overrun testing */
|
||||
int lzo1x_decompress_safe(const uint8_t* in, unsigned in_len,
|
||||
uint8_t* out, unsigned* out_len /*, void* wrkmem */)
|
||||
uint8_t* out, unsigned* out_len,
|
||||
void* wrkmem UNUSED_PARAM)
|
||||
{
|
||||
register uint8_t* op;
|
||||
register const uint8_t* ip;
|
||||
|
||||
@@ -15,7 +15,7 @@ int FAST_FUNC check_signature16(transformer_state_t *xstate, unsigned magic16)
|
||||
if (!xstate->signature_skipped) {
|
||||
uint16_t magic2;
|
||||
if (full_read(xstate->src_fd, &magic2, 2) != 2 || magic2 != magic16) {
|
||||
bb_simple_error_msg("invalid magic");
|
||||
bb_error_msg("invalid magic");
|
||||
return -1;
|
||||
}
|
||||
xstate->signature_skipped = 2;
|
||||
@@ -46,7 +46,7 @@ ssize_t FAST_FUNC transformer_write(transformer_state_t *xstate, const void *buf
|
||||
} else {
|
||||
nwrote = full_write(xstate->dst_fd, buf, bufsize);
|
||||
if (nwrote != (ssize_t)bufsize) {
|
||||
bb_simple_perror_msg("write");
|
||||
bb_perror_msg("write");
|
||||
nwrote = -1;
|
||||
goto ret;
|
||||
}
|
||||
@@ -159,44 +159,47 @@ void FAST_FUNC fork_transformer(int fd, const char *transform_prog)
|
||||
*/
|
||||
static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_compressed)
|
||||
{
|
||||
union {
|
||||
uint8_t b[4];
|
||||
uint16_t b16[2];
|
||||
uint32_t b32[1];
|
||||
} magic;
|
||||
transformer_state_t *xstate;
|
||||
|
||||
xstate = xzalloc(sizeof(*xstate));
|
||||
xstate->src_fd = fd;
|
||||
xstate->signature_skipped = 2;
|
||||
|
||||
/* .gz and .bz2 both have 2-byte signature, and their
|
||||
* unpack_XXX_stream wants this header skipped. */
|
||||
xstate->signature_skipped = 2;
|
||||
xread(fd, xstate->magic.b16, 2);
|
||||
xread(fd, magic.b16, sizeof(magic.b16[0]));
|
||||
if (ENABLE_FEATURE_SEAMLESS_GZ
|
||||
&& xstate->magic.b16[0] == GZIP_MAGIC
|
||||
&& magic.b16[0] == GZIP_MAGIC
|
||||
) {
|
||||
xstate->xformer = unpack_gz_stream;
|
||||
USE_FOR_NOMMU(xstate->xformer_prog = "gunzip";)
|
||||
goto found_magic;
|
||||
}
|
||||
if (ENABLE_FEATURE_SEAMLESS_Z
|
||||
&& xstate->magic.b16[0] == COMPRESS_MAGIC
|
||||
&& magic.b16[0] == COMPRESS_MAGIC
|
||||
) {
|
||||
xstate->xformer = unpack_Z_stream;
|
||||
USE_FOR_NOMMU(xstate->xformer_prog = "uncompress";)
|
||||
goto found_magic;
|
||||
}
|
||||
if (ENABLE_FEATURE_SEAMLESS_BZ2
|
||||
&& xstate->magic.b16[0] == BZIP2_MAGIC
|
||||
&& magic.b16[0] == BZIP2_MAGIC
|
||||
) {
|
||||
xstate->xformer = unpack_bz2_stream;
|
||||
USE_FOR_NOMMU(xstate->xformer_prog = "bunzip2";)
|
||||
goto found_magic;
|
||||
}
|
||||
if (ENABLE_FEATURE_SEAMLESS_XZ
|
||||
&& xstate->magic.b16[0] == XZ_MAGIC1
|
||||
&& magic.b16[0] == XZ_MAGIC1
|
||||
) {
|
||||
uint32_t v32;
|
||||
xstate->signature_skipped = 6;
|
||||
xread(fd, &xstate->magic.b16[1], 4);
|
||||
move_from_unaligned32(v32, &xstate->magic.b16[1]);
|
||||
if (v32 == XZ_MAGIC2) {
|
||||
xread(fd, magic.b32, sizeof(magic.b32[0]));
|
||||
if (magic.b32[0] == XZ_MAGIC2) {
|
||||
xstate->xformer = unpack_xz_stream;
|
||||
USE_FOR_NOMMU(xstate->xformer_prog = "unxz";)
|
||||
goto found_magic;
|
||||
@@ -205,7 +208,7 @@ static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_comp
|
||||
|
||||
/* No known magic seen */
|
||||
if (fail_if_not_compressed)
|
||||
bb_simple_error_msg_and_die("no gzip"
|
||||
bb_error_msg_and_die("no gzip"
|
||||
IF_FEATURE_SEAMLESS_BZ2("/bzip2")
|
||||
IF_FEATURE_SEAMLESS_XZ("/xz")
|
||||
" magic");
|
||||
@@ -275,7 +278,8 @@ static transformer_state_t *open_transformer(const char *fname, int fail_if_not_
|
||||
|
||||
if (ENABLE_FEATURE_SEAMLESS_LZMA) {
|
||||
/* .lzma has no header/signature, can only detect it by extension */
|
||||
if (is_suffixed_with(fname, ".lzma")) {
|
||||
char *sfx = strrchr(fname, '.');
|
||||
if (sfx && strcmp(sfx+1, "lzma") == 0) {
|
||||
xstate = xzalloc(sizeof(*xstate));
|
||||
xstate->src_fd = fd;
|
||||
xstate->xformer = unpack_lzma_stream;
|
||||
@@ -341,24 +345,11 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
|
||||
*maxsz_p = xstate->mem_output_size;
|
||||
}
|
||||
} else {
|
||||
/* File is not compressed.
|
||||
* We already read first few bytes, account for that.
|
||||
* Example where it happens:
|
||||
* "modinfo MODULE.ko" (not compressed)
|
||||
* open("MODULE.ko", O_RDONLY|O_LARGEFILE) = 4
|
||||
* read(4, "\177E", 2) = 2
|
||||
* fstat64(4, ...)
|
||||
* mmap(...)
|
||||
* read(4, "LF\2\1\1\0\0\0\0"...
|
||||
* ...and we avoided seeking on the fd! :)
|
||||
*/
|
||||
image = xmalloc_read_with_initial_buf(
|
||||
xstate->src_fd,
|
||||
maxsz_p,
|
||||
xmemdup(&xstate->magic, xstate->signature_skipped),
|
||||
xstate->signature_skipped
|
||||
);
|
||||
/* File is not compressed */
|
||||
//FIXME: avoid seek
|
||||
xlseek(xstate->src_fd, - xstate->signature_skipped, SEEK_CUR);
|
||||
xstate->signature_skipped = 0;
|
||||
image = xmalloc_read(xstate->src_fd, maxsz_p);
|
||||
}
|
||||
|
||||
if (!image)
|
||||
|
||||
@@ -13,6 +13,6 @@ void FAST_FUNC seek_by_jump(int fd, off_t amount)
|
||||
if (errno == ESPIPE)
|
||||
seek_by_read(fd, amount);
|
||||
else
|
||||
bb_simple_perror_msg_and_die("seek failure");
|
||||
bb_perror_msg_and_die("seek failure");
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
#include "ar_.h"
|
||||
#include "ar.h"
|
||||
|
||||
void FAST_FUNC unpack_ar_archive(archive_handle_t *ar_archive)
|
||||
{
|
||||
@@ -12,7 +12,7 @@ void FAST_FUNC unpack_ar_archive(archive_handle_t *ar_archive)
|
||||
|
||||
xread(ar_archive->src_fd, magic, AR_MAGIC_LEN);
|
||||
if (!is_prefixed_with(magic, AR_MAGIC)) {
|
||||
bb_simple_error_msg_and_die("invalid ar magic");
|
||||
bb_error_msg_and_die("invalid ar magic");
|
||||
}
|
||||
ar_archive->offset += AR_MAGIC_LEN;
|
||||
|
||||
|
||||
@@ -5,14 +5,13 @@
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
void FAST_FUNC create_or_remember_link(llist_t **link_placeholders,
|
||||
void FAST_FUNC create_or_remember_symlink(llist_t **symlink_placeholders,
|
||||
const char *target,
|
||||
const char *linkname,
|
||||
int hard_link)
|
||||
const char *linkname)
|
||||
{
|
||||
if (hard_link || target[0] == '/' || strstr(target, "..")) {
|
||||
llist_add_to_end(link_placeholders,
|
||||
xasprintf("%c%s%c%s", hard_link, linkname, '\0', target)
|
||||
if (target[0] == '/' || strstr(target, "..")) {
|
||||
llist_add_to(symlink_placeholders,
|
||||
xasprintf("%s%c%s", linkname, '\0', target)
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -24,17 +23,17 @@ void FAST_FUNC create_or_remember_link(llist_t **link_placeholders,
|
||||
}
|
||||
}
|
||||
|
||||
void FAST_FUNC create_links_from_list(llist_t *list)
|
||||
void FAST_FUNC create_symlinks_from_list(llist_t *list)
|
||||
{
|
||||
while (list) {
|
||||
char *target;
|
||||
|
||||
target = list->data + 1 + strlen(list->data + 1) + 1;
|
||||
if ((*list->data ? link : symlink) (target, list->data + 1)) {
|
||||
target = list->data + strlen(list->data) + 1;
|
||||
if (symlink(target, list->data)) {
|
||||
/* shared message */
|
||||
bb_error_msg_and_die("can't create %slink '%s' to '%s'",
|
||||
*list->data ? "hard" : "sym",
|
||||
list->data + 1, target
|
||||
"sym",
|
||||
list->data, target
|
||||
);
|
||||
}
|
||||
list = list->link;
|
||||
|
||||
@@ -545,7 +545,7 @@ static enum xz_ret XZ_FUNC dec_block_header(struct xz_dec *s)
|
||||
return XZ_OK;
|
||||
}
|
||||
|
||||
static NOINLINE enum xz_ret XZ_FUNC dec_main(struct xz_dec *s, struct xz_buf *b)
|
||||
static enum xz_ret XZ_FUNC dec_main(struct xz_dec *s, struct xz_buf *b)
|
||||
{
|
||||
enum xz_ret ret;
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
#define STREAM_HEADER_SIZE 12
|
||||
|
||||
#define HEADER_MAGIC "\375""7zXZ"
|
||||
#define HEADER_MAGIC "\3757zXZ"
|
||||
#define HEADER_MAGIC_SIZE 6
|
||||
|
||||
#define FOOTER_MAGIC "YZ"
|
||||
|
||||
+234
-225
@@ -25,7 +25,7 @@
|
||||
"Minimalized" for busybox by Alain Knaff
|
||||
*/
|
||||
//config:config LZOP
|
||||
//config: bool "lzop (12 kb)"
|
||||
//config: bool "lzop (13 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Lzop compression/decompresion.
|
||||
@@ -80,13 +80,12 @@
|
||||
//usage: "\n -F Don't verify checksum"
|
||||
//usage:
|
||||
//usage:#define unlzop_trivial_usage
|
||||
//usage: "[-cfUvF] [FILE]..."
|
||||
//usage: "[-cfkvF] [FILE]..."
|
||||
//usage:#define unlzop_full_usage "\n\n"
|
||||
//usage: " -c Write to stdout"
|
||||
//usage: "\n -f Force"
|
||||
//usage: "\n -U Delete input files"
|
||||
///////: "\n -k Keep input files" (default, so why bother documenting?)
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage: "\n -v Verbose"
|
||||
//usage: "\n -F Don't verify checksum"
|
||||
|
||||
@@ -142,7 +141,8 @@ static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off)
|
||||
#define TEST_OP (op <= op_end)
|
||||
|
||||
static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
uint8_t *out, unsigned *out_len /*, void* wrkmem */)
|
||||
uint8_t *out, unsigned *out_len,
|
||||
void* wrkmem UNUSED_PARAM)
|
||||
{
|
||||
uint8_t* op;
|
||||
uint8_t* ip;
|
||||
@@ -439,33 +439,36 @@ typedef struct chksum_t {
|
||||
} chksum_t;
|
||||
|
||||
typedef struct header_t {
|
||||
/* used to have auxiliary fields here */
|
||||
unsigned version;
|
||||
unsigned lib_version;
|
||||
unsigned version_needed_to_extract;
|
||||
uint32_t flags;
|
||||
uint32_t mode;
|
||||
uint32_t mtime;
|
||||
uint32_t gmtdiff;
|
||||
uint32_t header_checksum;
|
||||
|
||||
/* Starting from here, the layout and endianness
|
||||
* are exactly in on-disk format.
|
||||
*/
|
||||
uint16_t version_be16;
|
||||
uint16_t lib_version_be16;
|
||||
uint16_t version_needed_to_extract_be16;
|
||||
uint8_t method;
|
||||
uint8_t level;
|
||||
uint32_t flags32; /* be32 on disk, but we keep this field in native order */
|
||||
uint32_t mode_be32;
|
||||
uint32_t mtime_be32;
|
||||
uint32_t gmtdiff_be32;
|
||||
char len_and_name[1+255+1];
|
||||
uint32_t extra_field_len;
|
||||
uint32_t extra_field_checksum;
|
||||
|
||||
unsigned char method;
|
||||
unsigned char level;
|
||||
|
||||
/* info */
|
||||
char name[255+1];
|
||||
} header_t;
|
||||
|
||||
struct globals {
|
||||
/*const uint32_t *lzo_crc32_table;*/
|
||||
chksum_t chksum;
|
||||
chksum_t chksum_in;
|
||||
chksum_t chksum_out;
|
||||
} FIX_ALIASING;
|
||||
#define G (*(struct globals*)bb_common_bufsiz1)
|
||||
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
||||
//#define G (*ptr_to_globals)
|
||||
#define INIT_G() do { \
|
||||
setup_common_bufsiz(); \
|
||||
/*SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));*/ \
|
||||
} while (0)
|
||||
//#define INIT_G() do {
|
||||
// SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
|
||||
//} while (0)
|
||||
|
||||
|
||||
/**********************************************************************/
|
||||
@@ -546,24 +549,24 @@ lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
|
||||
}
|
||||
|
||||
/**********************************************************************/
|
||||
static void init_chksum(void)
|
||||
static void init_chksum(chksum_t *ct)
|
||||
{
|
||||
G.chksum.f_adler32 = ADLER32_INIT_VALUE;
|
||||
G.chksum.f_crc32 = CRC32_INIT_VALUE;
|
||||
ct->f_adler32 = ADLER32_INIT_VALUE;
|
||||
ct->f_crc32 = CRC32_INIT_VALUE;
|
||||
}
|
||||
|
||||
static void add_bytes_to_chksum(const void* buf, int cnt)
|
||||
static void add_bytes_to_chksum(chksum_t *ct, const void* buf, int cnt)
|
||||
{
|
||||
/* We need to handle the two checksums at once, because at the
|
||||
* beginning of the header, we don't know yet which one we'll
|
||||
* eventually need */
|
||||
G.chksum.f_adler32 = lzo_adler32(G.chksum.f_adler32, (const uint8_t*)buf, cnt);
|
||||
G.chksum.f_crc32 = lzo_crc32(G.chksum.f_crc32, (const uint8_t*)buf, cnt);
|
||||
ct->f_adler32 = lzo_adler32(ct->f_adler32, (const uint8_t*)buf, cnt);
|
||||
ct->f_crc32 = lzo_crc32(ct->f_crc32, (const uint8_t*)buf, cnt);
|
||||
}
|
||||
|
||||
static uint32_t chksum_getresult(uint32_t h_flags32)
|
||||
static uint32_t chksum_getresult(chksum_t *ct, const header_t *h)
|
||||
{
|
||||
return (h_flags32 & F_H_CRC32) ? G.chksum.f_crc32 : G.chksum.f_adler32;
|
||||
return (h->flags & F_H_CRC32) ? ct->f_crc32 : ct->f_adler32;
|
||||
}
|
||||
|
||||
/**********************************************************************/
|
||||
@@ -573,23 +576,50 @@ static uint32_t read32(void)
|
||||
xread(0, &v, 4);
|
||||
return ntohl(v);
|
||||
}
|
||||
|
||||
static void write32(uint32_t v)
|
||||
{
|
||||
v = htonl(v);
|
||||
xwrite(1, &v, 4);
|
||||
}
|
||||
|
||||
static void f_write(const void* buf, int cnt)
|
||||
{
|
||||
xwrite(1, buf, cnt);
|
||||
add_bytes_to_chksum(&G.chksum_out, buf, cnt);
|
||||
}
|
||||
|
||||
static void f_read(void* buf, int cnt)
|
||||
{
|
||||
xread(0, buf, cnt);
|
||||
add_bytes_to_chksum(buf, cnt);
|
||||
add_bytes_to_chksum(&G.chksum_in, buf, cnt);
|
||||
}
|
||||
//static int f_read8(void)
|
||||
//{
|
||||
// uint8_t v;
|
||||
// f_read(&v, 1);
|
||||
// return v;
|
||||
//}
|
||||
//static unsigned f_read16(void)
|
||||
//{
|
||||
// uint16_t v;
|
||||
// f_read(&v, 2);
|
||||
// return ntohs(v);
|
||||
//}
|
||||
|
||||
static int f_read8(void)
|
||||
{
|
||||
uint8_t v;
|
||||
f_read(&v, 1);
|
||||
return v;
|
||||
}
|
||||
|
||||
static void f_write8(uint8_t v)
|
||||
{
|
||||
f_write(&v, 1);
|
||||
}
|
||||
|
||||
static unsigned f_read16(void)
|
||||
{
|
||||
uint16_t v;
|
||||
f_read(&v, 2);
|
||||
return ntohs(v);
|
||||
}
|
||||
|
||||
static void f_write16(uint16_t v)
|
||||
{
|
||||
v = htons(v);
|
||||
f_write(&v, 2);
|
||||
}
|
||||
|
||||
static uint32_t f_read32(void)
|
||||
{
|
||||
uint32_t v;
|
||||
@@ -597,30 +627,34 @@ static uint32_t f_read32(void)
|
||||
return ntohl(v);
|
||||
}
|
||||
|
||||
static void write32(uint32_t v)
|
||||
static void f_write32(uint32_t v)
|
||||
{
|
||||
v = htonl(v);
|
||||
xwrite(1, &v, 4);
|
||||
f_write(&v, 4);
|
||||
}
|
||||
static void f_write(const void* buf, int cnt)
|
||||
|
||||
/**********************************************************************/
|
||||
static int lzo_get_method(header_t *h)
|
||||
{
|
||||
xwrite(1, buf, cnt);
|
||||
add_bytes_to_chksum(buf, cnt);
|
||||
/* check method */
|
||||
if (h->method == M_LZO1X_1) {
|
||||
if (h->level == 0)
|
||||
h->level = 3;
|
||||
} else if (h->method == M_LZO1X_1_15) {
|
||||
if (h->level == 0)
|
||||
h->level = 1;
|
||||
} else if (h->method == M_LZO1X_999) {
|
||||
if (h->level == 0)
|
||||
h->level = 9;
|
||||
} else
|
||||
return -1; /* not a LZO method */
|
||||
|
||||
/* check compression level */
|
||||
if (h->level < 1 || h->level > 9)
|
||||
return 15;
|
||||
|
||||
return 0;
|
||||
}
|
||||
//static void f_write8(uint8_t v)
|
||||
//{
|
||||
// f_write(&v, 1);
|
||||
//}
|
||||
//static void f_write16(uint16_t v)
|
||||
//{
|
||||
// v = htons(v);
|
||||
// f_write(&v, 2);
|
||||
//}
|
||||
//static void f_write32(uint32_t v)
|
||||
//{
|
||||
// v = htonl(v);
|
||||
// f_write(&v, 4);
|
||||
//}
|
||||
|
||||
/**********************************************************************/
|
||||
#define LZO_BLOCK_SIZE (256 * 1024l)
|
||||
@@ -638,101 +672,86 @@ static NOINLINE int lzo_compress(const header_t *h)
|
||||
int r = 0; /* LZO_E_OK */
|
||||
uint8_t *const b1 = xzalloc(block_size);
|
||||
uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size));
|
||||
unsigned src_len = 0, dst_len = 0;
|
||||
uint32_t d_adler32 = ADLER32_INIT_VALUE;
|
||||
uint32_t d_crc32 = CRC32_INIT_VALUE;
|
||||
int l;
|
||||
uint8_t *wrk_mem = NULL;
|
||||
|
||||
/* Only these methods are possible, see lzo_set_method():
|
||||
* -1: M_LZO1X_1_15
|
||||
* -2..6: M_LZO1X_1
|
||||
* -7..9: M_LZO1X_999 if ENABLE_LZOP_COMPR_HIGH
|
||||
*/
|
||||
if (h->method == M_LZO1X_1)
|
||||
wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS);
|
||||
else /* check only if it's not the only possibility */
|
||||
IF_LZOP_COMPR_HIGH(if (h->method == M_LZO1X_1_15))
|
||||
wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
|
||||
#if ENABLE_LZOP_COMPR_HIGH
|
||||
else /* must be h->method == M_LZO1X_999 */
|
||||
else if (h->method == M_LZO1X_1_15)
|
||||
wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
|
||||
else if (h->method == M_LZO1X_999)
|
||||
wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS);
|
||||
#endif
|
||||
|
||||
for (;;) {
|
||||
unsigned src_len, dst_len;
|
||||
int l;
|
||||
uint32_t wordbuf[6];
|
||||
uint32_t *wordptr = wordbuf;
|
||||
|
||||
/* read a block */
|
||||
l = full_read(0, b1, block_size);
|
||||
src_len = (l > 0 ? l : 0);
|
||||
|
||||
/* write uncompressed block size */
|
||||
write32(src_len);
|
||||
|
||||
/* exit if last block */
|
||||
if (src_len == 0) {
|
||||
write32(0);
|
||||
if (src_len == 0)
|
||||
break;
|
||||
}
|
||||
*wordptr++ = htonl(src_len);
|
||||
|
||||
/* compute checksum of uncompressed block */
|
||||
if (h->flags32 & F_ADLER32_D)
|
||||
if (h->flags & F_ADLER32_D)
|
||||
d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len);
|
||||
if (h->flags32 & F_CRC32_D)
|
||||
if (h->flags & F_CRC32_D)
|
||||
d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len);
|
||||
|
||||
/* compress */
|
||||
if (h->method == M_LZO1X_1)
|
||||
r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem);
|
||||
else IF_LZOP_COMPR_HIGH(if (h->method == M_LZO1X_1_15))
|
||||
else if (h->method == M_LZO1X_1_15)
|
||||
r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem);
|
||||
#if ENABLE_LZOP_COMPR_HIGH
|
||||
else /* must be h->method == M_LZO1X_999 */
|
||||
else if (h->method == M_LZO1X_999)
|
||||
r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len,
|
||||
wrk_mem, h->level);
|
||||
#endif
|
||||
else
|
||||
bb_error_msg_and_die("internal error");
|
||||
|
||||
if (r != 0) /* not LZO_E_OK */
|
||||
bb_error_msg_and_die("%s: %s", "internal error", "compression");
|
||||
bb_error_msg_and_die("internal error - compression failed");
|
||||
|
||||
/* write compressed block size */
|
||||
if (dst_len < src_len) {
|
||||
/* optimize */
|
||||
if (h->method == M_LZO1X_999) {
|
||||
unsigned new_len = src_len;
|
||||
r = lzo1x_optimize(b2, dst_len, b1, &new_len /*, NULL*/);
|
||||
r = lzo1x_optimize(b2, dst_len, b1, &new_len, NULL);
|
||||
if (r != 0 /*LZO_E_OK*/ || new_len != src_len)
|
||||
bb_error_msg_and_die("%s: %s", "internal error", "optimization");
|
||||
bb_error_msg_and_die("internal error - optimization failed");
|
||||
}
|
||||
*wordptr++ = htonl(dst_len);
|
||||
write32(dst_len);
|
||||
} else {
|
||||
/* data actually expanded => store data uncompressed */
|
||||
*wordptr++ = htonl(src_len);
|
||||
write32(src_len);
|
||||
}
|
||||
|
||||
/* write checksum of uncompressed block */
|
||||
if (h->flags32 & F_ADLER32_D)
|
||||
*wordptr++ = htonl(d_adler32);
|
||||
if (h->flags32 & F_CRC32_D)
|
||||
*wordptr++ = htonl(d_crc32);
|
||||
if (h->flags & F_ADLER32_D)
|
||||
write32(d_adler32);
|
||||
if (h->flags & F_CRC32_D)
|
||||
write32(d_crc32);
|
||||
|
||||
if (dst_len < src_len) {
|
||||
/* write checksum of compressed block */
|
||||
if (h->flags32 & F_ADLER32_C)
|
||||
*wordptr++ = htonl(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
|
||||
if (h->flags32 & F_CRC32_C)
|
||||
*wordptr++ = htonl(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
|
||||
}
|
||||
xwrite(1, wordbuf, ((char*)wordptr) - ((char*)wordbuf));
|
||||
if (dst_len < src_len) {
|
||||
if (h->flags & F_ADLER32_C)
|
||||
write32(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
|
||||
if (h->flags & F_CRC32_C)
|
||||
write32(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
|
||||
/* write compressed block data */
|
||||
xwrite(1, b2, dst_len);
|
||||
} else {
|
||||
/* write uncompressed block data */
|
||||
xwrite(1, b1, src_len);
|
||||
}
|
||||
// /* if full_read() was nevertheless "short", it was EOF */
|
||||
// if (src_len < block_size)
|
||||
// break;
|
||||
}
|
||||
|
||||
free(wrk_mem);
|
||||
@@ -753,15 +772,13 @@ static FAST_FUNC void lzo_check(
|
||||
*/
|
||||
uint32_t c = fn(init, buf, len);
|
||||
if (c != ref)
|
||||
bb_simple_error_msg_and_die("checksum error");
|
||||
bb_error_msg_and_die("checksum error");
|
||||
}
|
||||
|
||||
/**********************************************************************/
|
||||
// decompress a file
|
||||
/**********************************************************************/
|
||||
// used to have "const header_t *h" parameter, but since it uses
|
||||
// only flags32 field, changed to receive only that.
|
||||
static NOINLINE int lzo_decompress(uint32_t h_flags32)
|
||||
static NOINLINE int lzo_decompress(const header_t *h)
|
||||
{
|
||||
unsigned block_size = LZO_BLOCK_SIZE;
|
||||
int r;
|
||||
@@ -786,15 +803,15 @@ static NOINLINE int lzo_decompress(uint32_t h_flags32)
|
||||
/* error if split file */
|
||||
if (dst_len == 0xffffffffL)
|
||||
/* should not happen - not yet implemented */
|
||||
bb_simple_error_msg_and_die("this file is a split lzop file");
|
||||
bb_error_msg_and_die("this file is a split lzop file");
|
||||
|
||||
if (dst_len > MAX_BLOCK_SIZE)
|
||||
bb_simple_error_msg_and_die("corrupted data");
|
||||
bb_error_msg_and_die("corrupted data");
|
||||
|
||||
/* read compressed block size */
|
||||
src_len = read32();
|
||||
if (src_len <= 0 || src_len > dst_len)
|
||||
bb_simple_error_msg_and_die("corrupted data");
|
||||
bb_error_msg_and_die("corrupted data");
|
||||
|
||||
if (dst_len > block_size) {
|
||||
if (b2) {
|
||||
@@ -806,16 +823,16 @@ static NOINLINE int lzo_decompress(uint32_t h_flags32)
|
||||
}
|
||||
|
||||
/* read checksum of uncompressed block */
|
||||
if (h_flags32 & F_ADLER32_D)
|
||||
if (h->flags & F_ADLER32_D)
|
||||
d_adler32 = read32();
|
||||
if (h_flags32 & F_CRC32_D)
|
||||
if (h->flags & F_CRC32_D)
|
||||
d_crc32 = read32();
|
||||
|
||||
/* read checksum of compressed block */
|
||||
if (src_len < dst_len) {
|
||||
if (h_flags32 & F_ADLER32_C)
|
||||
if (h->flags & F_ADLER32_C)
|
||||
c_adler32 = read32();
|
||||
if (h_flags32 & F_CRC32_C)
|
||||
if (h->flags & F_CRC32_C)
|
||||
c_crc32 = read32();
|
||||
}
|
||||
|
||||
@@ -830,11 +847,11 @@ static NOINLINE int lzo_decompress(uint32_t h_flags32)
|
||||
|
||||
if (!(option_mask32 & OPT_F)) {
|
||||
/* verify checksum of compressed block */
|
||||
if (h_flags32 & F_ADLER32_C)
|
||||
if (h->flags & F_ADLER32_C)
|
||||
lzo_check(ADLER32_INIT_VALUE,
|
||||
b1, src_len,
|
||||
lzo_adler32, c_adler32);
|
||||
if (h_flags32 & F_CRC32_C)
|
||||
if (h->flags & F_CRC32_C)
|
||||
lzo_check(CRC32_INIT_VALUE,
|
||||
b1, src_len,
|
||||
lzo_crc32, c_crc32);
|
||||
@@ -842,12 +859,12 @@ static NOINLINE int lzo_decompress(uint32_t h_flags32)
|
||||
|
||||
/* decompress */
|
||||
// if (option_mask32 & OPT_F)
|
||||
// r = lzo1x_decompress(b1, src_len, b2, &d /*, NULL*/);
|
||||
// r = lzo1x_decompress(b1, src_len, b2, &d, NULL);
|
||||
// else
|
||||
r = lzo1x_decompress_safe(b1, src_len, b2, &d /*, NULL*/);
|
||||
r = lzo1x_decompress_safe(b1, src_len, b2, &d, NULL);
|
||||
|
||||
if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
|
||||
bb_simple_error_msg_and_die("corrupted data");
|
||||
bb_error_msg_and_die("corrupted data");
|
||||
}
|
||||
dst = b2;
|
||||
} else {
|
||||
@@ -857,11 +874,11 @@ static NOINLINE int lzo_decompress(uint32_t h_flags32)
|
||||
|
||||
if (!(option_mask32 & OPT_F)) {
|
||||
/* verify checksum of uncompressed block */
|
||||
if (h_flags32 & F_ADLER32_D)
|
||||
if (h->flags & F_ADLER32_D)
|
||||
lzo_check(ADLER32_INIT_VALUE,
|
||||
dst, dst_len,
|
||||
lzo_adler32, d_adler32);
|
||||
if (h_flags32 & F_CRC32_D)
|
||||
if (h->flags & F_CRC32_D)
|
||||
lzo_check(CRC32_INIT_VALUE,
|
||||
dst, dst_len,
|
||||
lzo_crc32, d_crc32);
|
||||
@@ -901,7 +918,7 @@ static NOINLINE int lzo_decompress(uint32_t h_flags32)
|
||||
* -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
|
||||
* +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
|
||||
* ^^^^^^^^^^^
|
||||
* chksum
|
||||
* chksum_out
|
||||
* The rest is identical.
|
||||
*/
|
||||
static const unsigned char lzop_magic[9] ALIGN1 = {
|
||||
@@ -914,144 +931,144 @@ static void check_magic(void)
|
||||
unsigned char magic[sizeof(lzop_magic)];
|
||||
xread(0, magic, sizeof(magic));
|
||||
if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
|
||||
bb_simple_error_msg_and_die("bad magic number");
|
||||
bb_error_msg_and_die("bad magic number");
|
||||
}
|
||||
|
||||
/**********************************************************************/
|
||||
// lzop file header
|
||||
/**********************************************************************/
|
||||
static void write_header(header_t *h)
|
||||
static void write_header(const header_t *h)
|
||||
{
|
||||
char *end;
|
||||
int l;
|
||||
|
||||
xwrite(1, lzop_magic, sizeof(lzop_magic));
|
||||
|
||||
init_chksum();
|
||||
init_chksum(&G.chksum_out);
|
||||
|
||||
/* Our caller leaves name zero-filled, so len == 0 */
|
||||
end = h->len_and_name+1 + 0; /* 0 is strlen(h->len_and_name+1) */
|
||||
/* Store length byte */
|
||||
/*h->len_and_name[0] = end - (h->len_and_name+1); - zero already */
|
||||
f_write16(h->version);
|
||||
f_write16(h->lib_version);
|
||||
f_write16(h->version_needed_to_extract);
|
||||
f_write8(h->method);
|
||||
f_write8(h->level);
|
||||
f_write32(h->flags);
|
||||
f_write32(h->mode);
|
||||
f_write32(h->mtime);
|
||||
f_write32(h->gmtdiff);
|
||||
|
||||
f_write(&h->version_be16, end - (char*)&h->version_be16);
|
||||
l = (int) strlen(h->name);
|
||||
f_write8(l);
|
||||
if (l)
|
||||
f_write(h->name, l);
|
||||
|
||||
h->flags32 = htonl(h->flags32); /* native endianness for lzo_compress() */
|
||||
|
||||
write32(chksum_getresult(h->flags32));
|
||||
f_write32(chksum_getresult(&G.chksum_out, h));
|
||||
}
|
||||
|
||||
static int read_header(header_t *h)
|
||||
{
|
||||
int r;
|
||||
int l;
|
||||
uint32_t checksum;
|
||||
/* As it stands now, only h->flags32 is used by our caller.
|
||||
* Therefore we don't store many fields in h->FIELD.
|
||||
*/
|
||||
unsigned h_version;
|
||||
unsigned h_version_needed_to_extract;
|
||||
|
||||
init_chksum();
|
||||
memset(h, 0, sizeof(*h));
|
||||
h->version_needed_to_extract = 0x0900; /* first lzop version */
|
||||
h->level = 0;
|
||||
|
||||
/* We don't support versions < 0.94, since 0.94
|
||||
* came only 2 months after 0.90:
|
||||
* 0.90 (10 Aug 1997): First public release of lzop
|
||||
* 0.94 (15 Oct 1997): Header format change
|
||||
*/
|
||||
init_chksum(&G.chksum_in);
|
||||
|
||||
/* Read up to and including name length byte */
|
||||
f_read(&h->version_be16, ((char*)&h->len_and_name[1]) - ((char*)&h->version_be16));
|
||||
|
||||
h_version = htons(h->version_be16);
|
||||
if (h_version < 0x0940)
|
||||
return 3;
|
||||
h_version_needed_to_extract = htons(h->version_needed_to_extract_be16);
|
||||
if (h_version_needed_to_extract > LZOP_VERSION)
|
||||
return 16;
|
||||
if (h_version_needed_to_extract < 0x0940)
|
||||
h->version = f_read16();
|
||||
if (h->version < 0x0900)
|
||||
return 3;
|
||||
h->lib_version = f_read16();
|
||||
if (h->version >= 0x0940) {
|
||||
h->version_needed_to_extract = f_read16();
|
||||
if (h->version_needed_to_extract > LZOP_VERSION)
|
||||
return 16;
|
||||
if (h->version_needed_to_extract < 0x0900)
|
||||
return 3;
|
||||
}
|
||||
h->method = f_read8();
|
||||
if (h->version >= 0x0940)
|
||||
h->level = f_read8();
|
||||
h->flags = f_read32();
|
||||
if (h->flags & F_H_FILTER)
|
||||
return 16; /* filter not supported */
|
||||
h->mode = f_read32();
|
||||
h->mtime = f_read32();
|
||||
if (h->version >= 0x0940)
|
||||
h->gmtdiff = f_read32();
|
||||
|
||||
l = f_read8();
|
||||
if (l > 0)
|
||||
f_read(h->name, l);
|
||||
h->name[l] = 0;
|
||||
|
||||
checksum = chksum_getresult(&G.chksum_in, h);
|
||||
h->header_checksum = f_read32();
|
||||
if (h->header_checksum != checksum)
|
||||
return 2;
|
||||
|
||||
if (h->method <= 0)
|
||||
return 14;
|
||||
r = lzo_get_method(h);
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
/* former lzo_get_method(h): */
|
||||
if (h->method == M_LZO1X_1) {
|
||||
if (h->level == 0)
|
||||
h->level = 3;
|
||||
} else if (h->method == M_LZO1X_1_15) {
|
||||
if (h->level == 0)
|
||||
h->level = 1;
|
||||
} else if (h->method == M_LZO1X_999) {
|
||||
if (h->level == 0)
|
||||
h->level = 9;
|
||||
} else
|
||||
return -1; /* not a LZO method */
|
||||
/* check compression level */
|
||||
if (h->level < 1 || h->level > 9)
|
||||
return 15;
|
||||
|
||||
h->flags32 = ntohl(h->flags32);
|
||||
if (h->flags32 & F_H_FILTER)
|
||||
return 16; /* filter not supported */
|
||||
/* check reserved flags */
|
||||
if (h->flags32 & F_RESERVED)
|
||||
if (h->flags & F_RESERVED)
|
||||
return -13;
|
||||
|
||||
l = h->len_and_name[0];
|
||||
if (l > 0)
|
||||
/* UNUSED */ f_read(h->len_and_name+1, l);
|
||||
/* UNUSED h->len_and_name[1+l] = 0; */
|
||||
|
||||
checksum = chksum_getresult(h->flags32);
|
||||
if (read32() != checksum)
|
||||
return 2;
|
||||
|
||||
/* skip extra field [not used yet] */
|
||||
if (h->flags32 & F_H_EXTRA_FIELD) {
|
||||
uint32_t extra_field_len;
|
||||
uint32_t extra_field_checksum;
|
||||
if (h->flags & F_H_EXTRA_FIELD) {
|
||||
uint32_t k;
|
||||
char dummy;
|
||||
|
||||
/* note: the checksum also covers the length */
|
||||
init_chksum();
|
||||
extra_field_len = f_read32();
|
||||
for (k = 0; k < extra_field_len; k++)
|
||||
f_read(&dummy, 1);
|
||||
checksum = chksum_getresult(h->flags32);
|
||||
extra_field_checksum = read32();
|
||||
if (extra_field_checksum != checksum)
|
||||
init_chksum(&G.chksum_in);
|
||||
h->extra_field_len = f_read32();
|
||||
for (k = 0; k < h->extra_field_len; k++)
|
||||
f_read8();
|
||||
checksum = chksum_getresult(&G.chksum_in, h);
|
||||
h->extra_field_checksum = f_read32();
|
||||
if (h->extra_field_checksum != checksum)
|
||||
return 3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void p_header(header_t *h)
|
||||
{
|
||||
int r;
|
||||
|
||||
r = read_header(h);
|
||||
if (r == 0)
|
||||
return;
|
||||
bb_error_msg_and_die("header_error %d", r);
|
||||
}
|
||||
|
||||
/**********************************************************************/
|
||||
// compress
|
||||
/**********************************************************************/
|
||||
static void lzo_set_method(header_t *h)
|
||||
{
|
||||
smallint level;
|
||||
|
||||
/* levels 2..6 or none (defaults to level 3) */
|
||||
h->method = M_LZO1X_1;
|
||||
level = 5; /* levels 2-6 are actually the same */
|
||||
int level = 1;
|
||||
|
||||
if (option_mask32 & OPT_1) {
|
||||
h->method = M_LZO1X_1_15;
|
||||
level = 1;
|
||||
}
|
||||
if (option_mask32 & OPT_789) {
|
||||
} else if (option_mask32 & OPT_789) {
|
||||
#if ENABLE_LZOP_COMPR_HIGH
|
||||
h->method = M_LZO1X_999;
|
||||
level = 9;
|
||||
if (option_mask32 & OPT_7)
|
||||
level = 7;
|
||||
else if (option_mask32 & OPT_8)
|
||||
level = 8;
|
||||
else
|
||||
level = 9;
|
||||
#else
|
||||
bb_simple_error_msg_and_die("high compression not compiled in");
|
||||
bb_error_msg_and_die("high compression not compiled in");
|
||||
#endif
|
||||
} else { /* levels 2..6 or none (defaults to level 3) */
|
||||
h->method = M_LZO1X_1;
|
||||
level = 5; /* levels 2-6 are actually the same */
|
||||
}
|
||||
|
||||
h->level = level;
|
||||
@@ -1066,21 +1083,18 @@ static int do_lzo_compress(void)
|
||||
|
||||
lzo_set_method(h);
|
||||
|
||||
h->version_be16 = htons(LZOP_VERSION & 0xffff);
|
||||
h->version_needed_to_extract_be16 = htons(0x0940);
|
||||
h->lib_version_be16 = htons(lzo_version() & 0xffff);
|
||||
h->version = (LZOP_VERSION & 0xffff);
|
||||
h->version_needed_to_extract = 0x0940;
|
||||
h->lib_version = lzo_version() & 0xffff;
|
||||
|
||||
h->flags32 = htonl((F_OS & F_OS_MASK) | (F_CS & F_CS_MASK));
|
||||
h->flags = (F_OS & F_OS_MASK) | (F_CS & F_CS_MASK);
|
||||
|
||||
if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
|
||||
h->flags32 |= htonl(F_ADLER32_D);
|
||||
h->flags |= F_ADLER32_D;
|
||||
if (option_mask32 & OPT_C)
|
||||
h->flags32 |= htonl(F_ADLER32_C);
|
||||
h->flags |= F_ADLER32_C;
|
||||
}
|
||||
|
||||
/* write_header() also converts h->flags32 to native endianness */
|
||||
write_header(h);
|
||||
|
||||
return lzo_compress(h);
|
||||
#undef h
|
||||
}
|
||||
@@ -1090,14 +1104,11 @@ static int do_lzo_compress(void)
|
||||
/**********************************************************************/
|
||||
static int do_lzo_decompress(void)
|
||||
{
|
||||
int r;
|
||||
header_t header;
|
||||
|
||||
check_magic();
|
||||
r = read_header(&header);
|
||||
if (r != 0)
|
||||
bb_error_msg_and_die("header_error %d", r);
|
||||
return lzo_decompress(header.flags32);
|
||||
p_header(&header);
|
||||
return lzo_decompress(&header);
|
||||
}
|
||||
|
||||
static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
|
||||
@@ -1122,8 +1133,6 @@ static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate
|
||||
int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int lzop_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
INIT_G();
|
||||
|
||||
getopt32(argv, OPTION_STRING);
|
||||
argv += optind;
|
||||
/* -U is "anti -k", invert bit for bbunpack(): */
|
||||
@@ -1140,6 +1149,6 @@ int lzop_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (ENABLE_UNLZOP && applet_name[4] == 'o')
|
||||
option_mask32 |= OPT_DECOMPRESS;
|
||||
|
||||
global_crc32_new_table_le();
|
||||
global_crc32_table = crc32_filltable(NULL, 0);
|
||||
return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);
|
||||
}
|
||||
+8
-10
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config RPM
|
||||
//config: bool "rpm (32 kb)"
|
||||
//config: bool "rpm (33 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Mini RPM applet - queries and extracts RPM packages.
|
||||
@@ -83,9 +83,7 @@ struct globals {
|
||||
void *map;
|
||||
rpm_index *mytags;
|
||||
int tagcount;
|
||||
unsigned mapsize;
|
||||
IF_VARIABLE_ARCH_PAGESIZE(unsigned pagesize;)
|
||||
#define G_pagesize cached_pagesize(G.pagesize)
|
||||
unsigned mapsize, pagesize;
|
||||
} FIX_ALIASING;
|
||||
#define G (*(struct globals*)bb_common_bufsiz1)
|
||||
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
||||
@@ -143,11 +141,11 @@ static int rpm_gettags(const char *filename)
|
||||
G.mytags = tags;
|
||||
|
||||
/* Map the store */
|
||||
storepos = (storepos + G_pagesize) & -(int)G_pagesize;
|
||||
storepos = (storepos + G.pagesize) & -(int)G.pagesize;
|
||||
/* remember size for munmap */
|
||||
G.mapsize = storepos;
|
||||
/* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */
|
||||
G.map = mmap_read(fd, storepos);
|
||||
G.map = mmap(0, storepos, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (G.map == MAP_FAILED)
|
||||
bb_perror_msg_and_die("mmap '%s'", filename);
|
||||
|
||||
@@ -358,7 +356,7 @@ int rpm_main(int argc, char **argv)
|
||||
int opt, func = 0;
|
||||
|
||||
INIT_G();
|
||||
INIT_PAGESIZE(G.pagesize);
|
||||
G.pagesize = getpagesize();
|
||||
|
||||
while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
|
||||
switch (opt) {
|
||||
@@ -501,7 +499,7 @@ int rpm_main(int argc, char **argv)
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config RPM2CPIO
|
||||
//config: bool "rpm2cpio (21 kb)"
|
||||
//config: bool "rpm2cpio (20 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Converts a RPM file into a CPIO archive.
|
||||
@@ -525,7 +523,7 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
int rpm_fd;
|
||||
|
||||
INIT_G();
|
||||
INIT_PAGESIZE(G.pagesize);
|
||||
G.pagesize = getpagesize();
|
||||
|
||||
rpm_fd = rpm_gettags(argv[1]);
|
||||
|
||||
@@ -545,7 +543,7 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
}
|
||||
|
||||
if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
|
||||
bb_simple_error_msg_and_die("error unpacking");
|
||||
bb_error_msg_and_die("error unpacking");
|
||||
|
||||
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||
close(rpm_fd);
|
||||
|
||||
+136
-154
@@ -23,7 +23,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config TAR
|
||||
//config: bool "tar (39 kb)"
|
||||
//config: bool "tar (40 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: tar is an archiving program. It's commonly used with gzip to
|
||||
@@ -232,7 +232,7 @@ static HardLinkInfo *findHardLinkInfo(HardLinkInfo *hlInfo, struct stat *statbuf
|
||||
}
|
||||
|
||||
/* Put an octal string into the specified buffer.
|
||||
* The number is zero padded and possibly NUL terminated.
|
||||
* The number is zero padded and possibly null terminated.
|
||||
* Stores low-order bits only if whole value does not fit. */
|
||||
static void putOctal(char *cp, int len, off_t value)
|
||||
{
|
||||
@@ -254,37 +254,61 @@ static void putOctal(char *cp, int len, off_t value)
|
||||
}
|
||||
#define PUT_OCTAL(a, b) putOctal((a), sizeof(a), (b))
|
||||
|
||||
static void chksum_and_xwrite(int fd, struct tar_header_t* hp)
|
||||
{
|
||||
/* POSIX says that checksum is done on unsigned bytes
|
||||
* (Sun and HP-UX gets it wrong... more details in
|
||||
* GNU tar source) */
|
||||
const unsigned char *cp;
|
||||
int chksum, size;
|
||||
|
||||
strcpy(hp->magic, "ustar ");
|
||||
|
||||
/* Calculate and store the checksum (i.e., the sum of all of the bytes of
|
||||
* the header). The checksum field must be filled with blanks for the
|
||||
* calculation. The checksum field is formatted differently from the
|
||||
* other fields: it has 6 digits, a null, then a space -- rather than
|
||||
* digits, followed by a null like the other fields... */
|
||||
memset(hp->chksum, ' ', sizeof(hp->chksum));
|
||||
cp = (const unsigned char *) hp;
|
||||
chksum = 0;
|
||||
size = sizeof(*hp);
|
||||
do { chksum += *cp++; } while (--size);
|
||||
putOctal(hp->chksum, sizeof(hp->chksum)-1, chksum);
|
||||
|
||||
/* Now write the header out to disk */
|
||||
xwrite(fd, hp, sizeof(*hp));
|
||||
}
|
||||
|
||||
# if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
||||
static void writeLongname(int fd, int type, const char *name, int dir)
|
||||
{
|
||||
struct prefilled {
|
||||
static const struct {
|
||||
char mode[8]; /* 100-107 */
|
||||
char uid[8]; /* 108-115 */
|
||||
char gid[8]; /* 116-123 */
|
||||
char size[12]; /* 124-135 */
|
||||
char mtime[12]; /* 136-147 */
|
||||
} prefilled = {
|
||||
"0000000",
|
||||
"0000000",
|
||||
"0000000",
|
||||
"00000000000",
|
||||
"00000000000",
|
||||
};
|
||||
struct tar_header_t header;
|
||||
int size;
|
||||
|
||||
memset(&header, 0, sizeof(header));
|
||||
header.typeflag = type;
|
||||
strcpy(header.name, "././@LongLink");
|
||||
/* This sets mode/uid/gid/mtime to "00...00<NUL>" strings */
|
||||
memset((char*)&header + offsetof(struct tar_header_t, mode), /* make gcc-9.x happy */
|
||||
'0', sizeof(struct prefilled));
|
||||
header.mode [sizeof(header.mode ) - 1] = '\0';
|
||||
header.uid [sizeof(header.uid ) - 1] = '\0';
|
||||
header.gid [sizeof(header.gid ) - 1] = '\0';
|
||||
/* header.size is filled by '0' now, will be corrected below */
|
||||
header.mtime[sizeof(header.mtime) - 1] = '\0';
|
||||
|
||||
dir = !!dir; /* normalize: 0/1 */
|
||||
size = strlen(name) + 1 + dir; /* GNU tar uses strlen+1 */
|
||||
/* + dir: account for possible '/' */
|
||||
|
||||
memset(&header, 0, sizeof(header));
|
||||
strcpy(header.name, "././@LongLink");
|
||||
memcpy(header.mode, prefilled.mode, sizeof(prefilled));
|
||||
PUT_OCTAL(header.size, size);
|
||||
chksum_and_xwrite_tar_header(fd, &header);
|
||||
header.typeflag = type;
|
||||
chksum_and_xwrite(fd, &header);
|
||||
|
||||
/* Write filename[/] and pad the block. */
|
||||
/* dir=0: writes 'name<NUL>', pads */
|
||||
@@ -345,7 +369,7 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
|
||||
/* If it is larger than 100 bytes, bail out */
|
||||
if (header.linkname[sizeof(header.linkname)-1]) {
|
||||
free(lpath);
|
||||
bb_simple_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
|
||||
bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
|
||||
return FALSE;
|
||||
}
|
||||
# endif
|
||||
@@ -415,7 +439,8 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
|
||||
header_name, S_ISDIR(statbuf->st_mode));
|
||||
# endif
|
||||
|
||||
chksum_and_xwrite_tar_header(tbInfo->tarFd, &header);
|
||||
/* Now write the header out to disk */
|
||||
chksum_and_xwrite(tbInfo->tarFd, &header);
|
||||
|
||||
/* Now do the verbose thing (or not) */
|
||||
if (tbInfo->verboseFlag) {
|
||||
@@ -464,11 +489,10 @@ static int exclude_file(const llist_t *excluded_files, const char *file)
|
||||
# define exclude_file(excluded_files, file) 0
|
||||
# endif
|
||||
|
||||
static int FAST_FUNC writeFileToTarball(struct recursive_state *state,
|
||||
const char *fileName,
|
||||
struct stat *statbuf)
|
||||
static int FAST_FUNC writeFileToTarball(const char *fileName, struct stat *statbuf,
|
||||
void *userData, int depth UNUSED_PARAM)
|
||||
{
|
||||
struct TarBallInfo *tbInfo = (struct TarBallInfo *) state->userData;
|
||||
struct TarBallInfo *tbInfo = (struct TarBallInfo *) userData;
|
||||
const char *header_name;
|
||||
int inputFileFd = -1;
|
||||
|
||||
@@ -480,9 +504,6 @@ static int FAST_FUNC writeFileToTarball(struct recursive_state *state,
|
||||
if (header_name[0] == '\0')
|
||||
return TRUE;
|
||||
|
||||
if (exclude_file(tbInfo->excludeList, header_name))
|
||||
return SKIP; /* "do not recurse on this directory", no error message printed */
|
||||
|
||||
/* It is against the rules to archive a socket */
|
||||
if (S_ISSOCK(statbuf->st_mode)) {
|
||||
bb_error_msg("%s: socket ignored", fileName);
|
||||
@@ -516,9 +537,12 @@ static int FAST_FUNC writeFileToTarball(struct recursive_state *state,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (exclude_file(tbInfo->excludeList, header_name))
|
||||
return SKIP;
|
||||
|
||||
# if !ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
||||
if (strlen(header_name) >= NAME_SIZE) {
|
||||
bb_simple_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
|
||||
bb_error_msg("names longer than "NAME_SIZE_STR" chars not supported");
|
||||
return TRUE;
|
||||
}
|
||||
# endif
|
||||
@@ -528,13 +552,13 @@ static int FAST_FUNC writeFileToTarball(struct recursive_state *state,
|
||||
/* open the file we want to archive, and make sure all is well */
|
||||
inputFileFd = open_or_warn(fileName, O_RDONLY);
|
||||
if (inputFileFd < 0) {
|
||||
return FALSE; /* make recursive_action() return FALSE */
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/* Add an entry to the tarball */
|
||||
if (writeTarHeader(tbInfo, header_name, fileName, statbuf) == FALSE) {
|
||||
return FALSE; /* make recursive_action() return FALSE */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* If it was a regular file, write out the body */
|
||||
@@ -572,71 +596,56 @@ static int FAST_FUNC writeFileToTarball(struct recursive_state *state,
|
||||
/* Don't inline: vfork scares gcc and pessimizes code */
|
||||
static void NOINLINE vfork_compressor(int tar_fd, const char *gzip)
|
||||
{
|
||||
pid_t gzipPid;
|
||||
|
||||
// On Linux, vfork never unpauses parent early, although standard
|
||||
// allows for that. Do we want to waste bytes checking for it?
|
||||
# define WAIT_FOR_CHILD 0
|
||||
volatile int vfork_exec_errno = 0;
|
||||
struct fd_pair data;
|
||||
struct fd_pair gzipDataPipe;
|
||||
# if WAIT_FOR_CHILD
|
||||
struct fd_pair status;
|
||||
xpiped_pair(status);
|
||||
struct fd_pair gzipStatusPipe;
|
||||
xpiped_pair(gzipStatusPipe);
|
||||
# endif
|
||||
xpiped_pair(data);
|
||||
xpiped_pair(gzipDataPipe);
|
||||
|
||||
signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
|
||||
|
||||
if (xvfork() == 0) {
|
||||
gzipPid = xvfork();
|
||||
|
||||
if (gzipPid == 0) {
|
||||
/* child */
|
||||
int tfd;
|
||||
/* NB: close _first_, then move fds! */
|
||||
close(data.wr);
|
||||
close(gzipDataPipe.wr);
|
||||
# if WAIT_FOR_CHILD
|
||||
close(status.rd);
|
||||
/* status.wr will close only on exec -
|
||||
close(gzipStatusPipe.rd);
|
||||
/* gzipStatusPipe.wr will close only on exec -
|
||||
* parent waits for this close to happen */
|
||||
fcntl(status.wr, F_SETFD, FD_CLOEXEC);
|
||||
fcntl(gzipStatusPipe.wr, F_SETFD, FD_CLOEXEC);
|
||||
# endif
|
||||
/* copy it: parent's tar_fd variable must not change */
|
||||
tfd = tar_fd;
|
||||
if (tfd == 0) {
|
||||
/* Output tar fd may be zero.
|
||||
* xmove_fd(data.rd, 0) would destroy it.
|
||||
* Reproducer:
|
||||
* exec 0>&-
|
||||
* exec 1>&-
|
||||
* tar czf Z.tar.gz FILE
|
||||
* Swapping move_fd's order wouldn't work:
|
||||
* data.rd is 1 and _it_ would be destroyed.
|
||||
*/
|
||||
tfd = dup(tfd);
|
||||
}
|
||||
xmove_fd(data.rd, 0);
|
||||
xmove_fd(tfd, 1);
|
||||
|
||||
/* exec gzip/bzip2/... program */
|
||||
//BB_EXECLP(gzip, gzip, "-f", (char *)0); - WRONG for "xz",
|
||||
// if xz is an enabled applet, it'll be a version which
|
||||
// can only decompress. We do need to execute external
|
||||
// program, not applet.
|
||||
execlp(gzip, gzip, "-f", (char *)0);
|
||||
|
||||
xmove_fd(gzipDataPipe.rd, 0);
|
||||
xmove_fd(tar_fd, 1);
|
||||
/* exec gzip/bzip2 program/applet */
|
||||
BB_EXECLP(gzip, gzip, "-f", (char *)0);
|
||||
vfork_exec_errno = errno;
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* parent */
|
||||
xmove_fd(data.wr, tar_fd);
|
||||
close(data.rd);
|
||||
xmove_fd(gzipDataPipe.wr, tar_fd);
|
||||
close(gzipDataPipe.rd);
|
||||
# if WAIT_FOR_CHILD
|
||||
close(status.wr);
|
||||
close(gzipStatusPipe.wr);
|
||||
while (1) {
|
||||
/* Wait until child execs (or fails to) */
|
||||
char buf;
|
||||
int n = full_read(status.rd, &buf, 1);
|
||||
int n;
|
||||
|
||||
/* Wait until child execs (or fails to) */
|
||||
n = full_read(gzipStatusPipe.rd, &buf, 1);
|
||||
if (n < 0 /* && errno == EAGAIN */)
|
||||
continue; /* try it again */
|
||||
}
|
||||
close(status.rd);
|
||||
close(gzipStatusPipe.rd);
|
||||
# endif
|
||||
if (vfork_exec_errno) {
|
||||
errno = vfork_exec_errno;
|
||||
@@ -674,7 +683,7 @@ static NOINLINE int writeTarFile(
|
||||
/* Read the directory/files and iterate over them one at a time */
|
||||
while (filelist) {
|
||||
if (!recursive_action(filelist->data, recurseFlags,
|
||||
writeFileToTarball, writeFileToTarball, tbInfo)
|
||||
writeFileToTarball, writeFileToTarball, tbInfo, 0)
|
||||
) {
|
||||
errorFlag = TRUE;
|
||||
}
|
||||
@@ -697,13 +706,13 @@ static NOINLINE int writeTarFile(
|
||||
freeHardLinkInfo(&tbInfo->hlInfoHead);
|
||||
|
||||
if (errorFlag)
|
||||
bb_simple_error_msg("error exit delayed from previous errors");
|
||||
bb_error_msg("error exit delayed from previous errors");
|
||||
|
||||
# if SEAMLESS_COMPRESSION
|
||||
if (gzip) {
|
||||
int status;
|
||||
if (safe_waitpid(-1, &status, 0) == -1)
|
||||
bb_simple_perror_msg("waitpid");
|
||||
bb_perror_msg("waitpid");
|
||||
else if (!WIFEXITED(status) || WEXITSTATUS(status))
|
||||
/* gzip was killed or has exited with nonzero! */
|
||||
errorFlag = TRUE;
|
||||
@@ -712,7 +721,11 @@ static NOINLINE int writeTarFile(
|
||||
return errorFlag;
|
||||
}
|
||||
|
||||
#endif /* FEATURE_TAR_CREATE */
|
||||
#else /* !FEATURE_TAR_CREATE */
|
||||
|
||||
# define writeTarFile(...) 0
|
||||
|
||||
#endif
|
||||
|
||||
#if ENABLE_FEATURE_TAR_FROM
|
||||
static llist_t *append_file_list_to_list(llist_t *list)
|
||||
@@ -743,18 +756,18 @@ static llist_t *append_file_list_to_list(llist_t *list)
|
||||
//usage: IF_FEATURE_SEAMLESS_GZ("z")
|
||||
//usage: IF_FEATURE_SEAMLESS_XZ("J")
|
||||
//usage: IF_FEATURE_SEAMLESS_BZ2("j")
|
||||
//usage: "a"
|
||||
//usage: IF_FEATURE_SEAMLESS_LZMA("a")
|
||||
//usage: IF_FEATURE_TAR_CREATE("h")
|
||||
//usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m")
|
||||
//usage: "vokO] "
|
||||
//usage: "vO] "
|
||||
//usage: "[-f TARFILE] [-C DIR] "
|
||||
//usage: IF_FEATURE_TAR_FROM("[-T FILE] [-X FILE] "IF_FEATURE_TAR_LONG_OPTIONS("[LONGOPT]... "))
|
||||
//usage: IF_FEATURE_TAR_FROM("[-T FILE] [-X FILE] "IF_FEATURE_TAR_LONG_OPTIONS("[--exclude PATTERN]... "))
|
||||
//usage: "[FILE]..."
|
||||
//usage:#define tar_full_usage "\n\n"
|
||||
//usage: IF_FEATURE_TAR_CREATE("Create, extract, ")
|
||||
//usage: IF_NOT_FEATURE_TAR_CREATE("Extract ")
|
||||
//usage: "or list files from a tar file"
|
||||
//usage: "\n"
|
||||
//usage: "or list files from a tar file\n"
|
||||
//usage: "\nOperation:"
|
||||
//usage: IF_FEATURE_TAR_CREATE(
|
||||
//usage: "\n c Create"
|
||||
//usage: )
|
||||
@@ -763,13 +776,6 @@ static llist_t *append_file_list_to_list(llist_t *list)
|
||||
//usage: "\n -f FILE Name of TARFILE ('-' for stdin/out)"
|
||||
//usage: "\n -C DIR Change to DIR before operation"
|
||||
//usage: "\n -v Verbose"
|
||||
//usage: "\n -O Extract to stdout"
|
||||
//usage: IF_FEATURE_TAR_NOPRESERVE_TIME(
|
||||
//usage: "\n -m Don't restore mtime"
|
||||
//usage: )
|
||||
//usage: "\n -o Don't restore user:group"
|
||||
///////:-p - accepted but ignored, restores mode (aliases in GNU tar: --preserve-permissions, --same-permissions)
|
||||
//usage: "\n -k Don't replace existing files"
|
||||
//usage: IF_FEATURE_SEAMLESS_Z(
|
||||
//usage: "\n -Z (De)compress using compress"
|
||||
//usage: )
|
||||
@@ -783,14 +789,15 @@ static llist_t *append_file_list_to_list(llist_t *list)
|
||||
//usage: "\n -j (De)compress using bzip2"
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_SEAMLESS_LZMA(
|
||||
//usage: IF_FEATURE_TAR_LONG_OPTIONS(
|
||||
//usage: "\n --lzma (De)compress using lzma"
|
||||
//usage: "\n -a (De)compress using lzma"
|
||||
//usage: )
|
||||
//usage: )
|
||||
//usage: "\n -a (De)compress based on extension"
|
||||
//usage: "\n -O Extract to stdout"
|
||||
//usage: IF_FEATURE_TAR_CREATE(
|
||||
//usage: "\n -h Follow symlinks"
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_TAR_NOPRESERVE_TIME(
|
||||
//usage: "\n -m Don't restore mtime"
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_TAR_FROM(
|
||||
//usage: "\n -T FILE File with names to include"
|
||||
//usage: "\n -X FILE File with glob patterns to exclude"
|
||||
@@ -798,36 +805,37 @@ static llist_t *append_file_list_to_list(llist_t *list)
|
||||
//usage: "\n --exclude PATTERN Glob pattern to exclude"
|
||||
//usage: )
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_TAR_LONG_OPTIONS(
|
||||
//usage: "\n --overwrite Replace existing files"
|
||||
//usage: "\n --strip-components NUM NUM of leading components to strip"
|
||||
//usage: "\n --no-recursion Don't descend in directories"
|
||||
//usage: "\n --numeric-owner Use numeric user:group"
|
||||
//usage: "\n --no-same-permissions Don't restore access permissions"
|
||||
//usage: IF_FEATURE_TAR_TO_COMMAND(
|
||||
//usage: "\n --to-command COMMAND Pipe files to COMMAND"
|
||||
//usage: )
|
||||
//usage: )
|
||||
//usage:
|
||||
//usage:#define tar_example_usage
|
||||
//usage: "$ zcat /tmp/tarball.tar.gz | tar -xf -\n"
|
||||
//usage: "$ tar -cf /tmp/tarball.tar /usr/local\n"
|
||||
|
||||
// Supported but aren't in --help:
|
||||
// o no-same-owner
|
||||
// p same-permissions
|
||||
// k keep-old
|
||||
// no-recursion
|
||||
// numeric-owner
|
||||
// no-same-permissions
|
||||
// overwrite
|
||||
//IF_FEATURE_TAR_TO_COMMAND(
|
||||
// to-command
|
||||
//)
|
||||
|
||||
enum {
|
||||
OPTBIT_KEEP_OLD = 8,
|
||||
IF_FEATURE_TAR_CREATE( OPTBIT_CREATE ,)
|
||||
IF_FEATURE_TAR_CREATE( OPTBIT_DEREFERENCE ,)
|
||||
IF_FEATURE_SEAMLESS_BZ2( OPTBIT_BZIP2 ,)
|
||||
IF_FEATURE_SEAMLESS_LZMA(OPTBIT_LZMA ,)
|
||||
IF_FEATURE_TAR_FROM( OPTBIT_INCLUDE_FROM,)
|
||||
IF_FEATURE_TAR_FROM( OPTBIT_EXCLUDE_FROM,)
|
||||
IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,)
|
||||
IF_FEATURE_SEAMLESS_XZ( OPTBIT_XZ ,)
|
||||
IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,) // 16th bit
|
||||
OPTBIT_AUTOCOMPRESS_BY_EXT,
|
||||
IF_FEATURE_SEAMLESS_XZ( OPTBIT_XZ ,) // 16th bit
|
||||
IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,)
|
||||
IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
|
||||
#if ENABLE_FEATURE_TAR_LONG_OPTIONS
|
||||
OPTBIT_STRIP_COMPONENTS,
|
||||
IF_FEATURE_SEAMLESS_LZMA(OPTBIT_LZMA ,)
|
||||
OPTBIT_NORECURSION,
|
||||
IF_FEATURE_TAR_TO_COMMAND(OPTBIT_2COMMAND ,)
|
||||
OPTBIT_NUMERIC_OWNER,
|
||||
@@ -846,15 +854,14 @@ enum {
|
||||
OPT_CREATE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_CREATE )) + 0, // c
|
||||
OPT_DEREFERENCE = IF_FEATURE_TAR_CREATE( (1 << OPTBIT_DEREFERENCE )) + 0, // h
|
||||
OPT_BZIP2 = IF_FEATURE_SEAMLESS_BZ2( (1 << OPTBIT_BZIP2 )) + 0, // j
|
||||
OPT_LZMA = IF_FEATURE_SEAMLESS_LZMA((1 << OPTBIT_LZMA )) + 0, // a
|
||||
OPT_INCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_INCLUDE_FROM)) + 0, // T
|
||||
OPT_EXCLUDE_FROM = IF_FEATURE_TAR_FROM( (1 << OPTBIT_EXCLUDE_FROM)) + 0, // X
|
||||
OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z
|
||||
OPT_XZ = IF_FEATURE_SEAMLESS_XZ( (1 << OPTBIT_XZ )) + 0, // J
|
||||
OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z
|
||||
OPT_AUTOCOMPRESS_BY_EXT = 1 << OPTBIT_AUTOCOMPRESS_BY_EXT, // a
|
||||
OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
|
||||
OPT_STRIP_COMPONENTS = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_STRIP_COMPONENTS)) + 0, // strip-components
|
||||
OPT_LZMA = IF_FEATURE_TAR_LONG_OPTIONS(IF_FEATURE_SEAMLESS_LZMA((1 << OPTBIT_LZMA))) + 0, // lzma
|
||||
OPT_NORECURSION = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NORECURSION )) + 0, // no-recursion
|
||||
OPT_2COMMAND = IF_FEATURE_TAR_TO_COMMAND( (1 << OPTBIT_2COMMAND )) + 0, // to-command
|
||||
OPT_NUMERIC_OWNER = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER )) + 0, // numeric-owner
|
||||
@@ -884,6 +891,9 @@ static const char tar_longopts[] ALIGN1 =
|
||||
# if ENABLE_FEATURE_SEAMLESS_BZ2
|
||||
"bzip2\0" No_argument "j"
|
||||
# endif
|
||||
# if ENABLE_FEATURE_SEAMLESS_LZMA
|
||||
"lzma\0" No_argument "a"
|
||||
# endif
|
||||
# if ENABLE_FEATURE_TAR_FROM
|
||||
"files-from\0" Required_argument "T"
|
||||
"exclude-from\0" Required_argument "X"
|
||||
@@ -897,14 +907,10 @@ static const char tar_longopts[] ALIGN1 =
|
||||
# if ENABLE_FEATURE_SEAMLESS_Z
|
||||
"compress\0" No_argument "Z"
|
||||
# endif
|
||||
"auto-compress\0" No_argument "a"
|
||||
# if ENABLE_FEATURE_TAR_NOPRESERVE_TIME
|
||||
"touch\0" No_argument "m"
|
||||
# endif
|
||||
"strip-components\0" Required_argument "\xf8"
|
||||
# if ENABLE_FEATURE_SEAMLESS_LZMA
|
||||
"lzma\0" No_argument "\xf9"
|
||||
# endif
|
||||
"strip-components\0" Required_argument "\xf9"
|
||||
"no-recursion\0" No_argument "\xfa"
|
||||
# if ENABLE_FEATURE_TAR_TO_COMMAND
|
||||
"to-command\0" Required_argument "\xfb"
|
||||
@@ -993,13 +999,13 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
"txC:f:Oopvk"
|
||||
IF_FEATURE_TAR_CREATE( "ch" )
|
||||
IF_FEATURE_SEAMLESS_BZ2( "j" )
|
||||
IF_FEATURE_SEAMLESS_LZMA("a" )
|
||||
IF_FEATURE_TAR_FROM( "T:*X:*")
|
||||
IF_FEATURE_SEAMLESS_GZ( "z" )
|
||||
IF_FEATURE_SEAMLESS_XZ( "J" )
|
||||
IF_FEATURE_SEAMLESS_Z( "Z" )
|
||||
"a"
|
||||
IF_FEATURE_TAR_NOPRESERVE_TIME("m")
|
||||
IF_FEATURE_TAR_LONG_OPTIONS("\xf8:") // --strip-components
|
||||
IF_FEATURE_TAR_LONG_OPTIONS("\xf9:") // --strip-components
|
||||
"\0"
|
||||
"tt:vv:" // count -t,-v
|
||||
#if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
|
||||
@@ -1009,7 +1015,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
IF_FEATURE_TAR_CREATE("c--tx:t--cx:x--ct") // mutually exclusive
|
||||
IF_NOT_FEATURE_TAR_CREATE("t--x:x--t") // mutually exclusive
|
||||
#if ENABLE_FEATURE_TAR_LONG_OPTIONS
|
||||
":\xf8+" // --strip-components=NUM
|
||||
":\xf9+" // --strip-components=NUM
|
||||
#endif
|
||||
LONGOPTS
|
||||
, &base_dir // -C dir
|
||||
@@ -1028,7 +1034,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
);
|
||||
#if DBG_OPTION_PARSING
|
||||
bb_error_msg("opt: 0x%08x", opt);
|
||||
# define showopt(o) bb_error_msg("opt & %s(%x):\t%x", #o, o, opt & o);
|
||||
# define showopt(o) bb_error_msg("opt & %s(%x): %x", #o, o, opt & o);
|
||||
showopt(OPT_TEST );
|
||||
showopt(OPT_EXTRACT );
|
||||
showopt(OPT_BASEDIR );
|
||||
@@ -1041,15 +1047,14 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
showopt(OPT_CREATE );
|
||||
showopt(OPT_DEREFERENCE );
|
||||
showopt(OPT_BZIP2 );
|
||||
showopt(OPT_LZMA );
|
||||
showopt(OPT_INCLUDE_FROM );
|
||||
showopt(OPT_EXCLUDE_FROM );
|
||||
showopt(OPT_GZIP );
|
||||
showopt(OPT_XZ );
|
||||
showopt(OPT_COMPRESS );
|
||||
showopt(OPT_AUTOCOMPRESS_BY_EXT);
|
||||
showopt(OPT_NOPRESERVE_TIME );
|
||||
showopt(OPT_STRIP_COMPONENTS);
|
||||
showopt(OPT_LZMA );
|
||||
showopt(OPT_NORECURSION );
|
||||
showopt(OPT_2COMMAND );
|
||||
showopt(OPT_NUMERIC_OWNER );
|
||||
@@ -1105,15 +1110,14 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
tar_handle->ah_flags &= ~ARCHIVE_RESTORE_DATE;
|
||||
|
||||
#if ENABLE_FEATURE_TAR_FROM
|
||||
/* Convert each -X EXCLFILE to list of to-be-rejected glob patterns */
|
||||
tar_handle->reject = append_file_list_to_list(tar_handle->reject);
|
||||
# if ENABLE_FEATURE_TAR_LONG_OPTIONS
|
||||
/* Append --exclude=GLOBPATTERNs to reject */
|
||||
if (excludes) {
|
||||
llist_t **p2next = &tar_handle->reject;
|
||||
while (*p2next)
|
||||
p2next = &((*p2next)->link);
|
||||
*p2next = excludes;
|
||||
/* Append excludes to reject */
|
||||
while (excludes) {
|
||||
llist_t *next = excludes->link;
|
||||
excludes->link = tar_handle->reject;
|
||||
tar_handle->reject = excludes;
|
||||
excludes = next;
|
||||
}
|
||||
# endif
|
||||
tar_handle->accept = append_file_list_to_list(tar_handle->accept);
|
||||
@@ -1141,7 +1145,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (opt & OPT_CREATE) {
|
||||
/* Make sure there is at least one file to tar up */
|
||||
if (tar_handle->accept == NULL)
|
||||
bb_simple_error_msg_and_die("empty archive");
|
||||
bb_error_msg_and_die("empty archive");
|
||||
|
||||
tar_fd = STDOUT_FILENO;
|
||||
/* Mimicking GNU tar 1.15.1: */
|
||||
@@ -1151,39 +1155,17 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (LONE_DASH(tar_filename)) {
|
||||
tar_handle->src_fd = tar_fd;
|
||||
tar_handle->seek = seek_by_read;
|
||||
} else
|
||||
if (ENABLE_FEATURE_TAR_AUTODETECT
|
||||
&& ENABLE_FEATURE_SEAMLESS_LZMA
|
||||
&& flags == O_RDONLY
|
||||
&& !(opt & OPT_ANY_COMPRESS)
|
||||
&& is_suffixed_with(tar_filename, ".lzma")
|
||||
/* We do this only for .lzma files, they have no signature.
|
||||
* All other compression formats are recognized in
|
||||
* get_header_tar() when first tar block has invalid format.
|
||||
* Doing it here for all filenames would falsely trigger
|
||||
* on e.g. tarball with 1st file named "BZh5".
|
||||
*/
|
||||
) {
|
||||
tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
|
||||
if (tar_handle->src_fd < 0)
|
||||
bb_perror_msg_and_die("can't open '%s'", tar_filename);
|
||||
} else {
|
||||
tar_handle->src_fd = xopen(tar_filename, flags);
|
||||
#if ENABLE_FEATURE_TAR_CREATE
|
||||
if ((OPT_GZIP | OPT_BZIP2 | OPT_XZ | OPT_LZMA) != 0 /* at least one is config-enabled */
|
||||
&& (opt & OPT_AUTOCOMPRESS_BY_EXT)
|
||||
&& flags != O_RDONLY
|
||||
if (ENABLE_FEATURE_TAR_AUTODETECT
|
||||
&& flags == O_RDONLY
|
||||
&& !(opt & OPT_ANY_COMPRESS)
|
||||
) {
|
||||
if (OPT_GZIP != 0 && is_suffixed_with(tar_filename, "gz"))
|
||||
opt |= OPT_GZIP;
|
||||
if (OPT_BZIP2 != 0 && is_suffixed_with(tar_filename, "bz2"))
|
||||
opt |= OPT_BZIP2;
|
||||
if (OPT_XZ != 0 && is_suffixed_with(tar_filename, "xz"))
|
||||
opt |= OPT_XZ;
|
||||
if (OPT_LZMA != 0 && is_suffixed_with(tar_filename, "lzma"))
|
||||
opt |= OPT_LZMA;
|
||||
tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
|
||||
if (tar_handle->src_fd < 0)
|
||||
bb_perror_msg_and_die("can't open '%s'", tar_filename);
|
||||
} else {
|
||||
tar_handle->src_fd = xopen(tar_filename, flags);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1262,7 +1244,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
while (get_header_tar(tar_handle) == EXIT_SUCCESS)
|
||||
bb_got_signal = EXIT_SUCCESS; /* saw at least one header, good */
|
||||
|
||||
create_links_from_list(tar_handle->link_placeholders);
|
||||
create_symlinks_from_list(tar_handle->symlink_placeholders);
|
||||
|
||||
/* Check that every file that should have been extracted was */
|
||||
while (tar_handle->accept) {
|
||||
|
||||
+42
-92
@@ -17,7 +17,7 @@
|
||||
* Zip64 + other methods
|
||||
*/
|
||||
//config:config UNZIP
|
||||
//config: bool "unzip (26 kb)"
|
||||
//config: bool "unzip (24 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: unzip will list or extract files from a ZIP archive,
|
||||
@@ -56,15 +56,14 @@
|
||||
//kbuild:lib-$(CONFIG_UNZIP) += unzip.o
|
||||
|
||||
//usage:#define unzip_trivial_usage
|
||||
//usage: "[-lnojpq] FILE[.zip] [FILE]... [-x FILE]... [-d DIR]"
|
||||
//usage: "[-lnojpq] FILE[.zip] [FILE]... [-x FILE...] [-d DIR]"
|
||||
//usage:#define unzip_full_usage "\n\n"
|
||||
//usage: "Extract FILEs from ZIP archive\n"
|
||||
//usage: "\n -l List contents (with -q for short form)"
|
||||
//usage: "\n -n Never overwrite files (default: ask)"
|
||||
//usage: "\n -o Overwrite"
|
||||
//usage: "\n -j Do not restore paths"
|
||||
//usage: "\n -p Write to stdout"
|
||||
//usage: "\n -t Test"
|
||||
//usage: "\n -p Print to stdout"
|
||||
//usage: "\n -q Quiet"
|
||||
//usage: "\n -x FILE Exclude FILEs"
|
||||
//usage: "\n -d DIR Extract into DIR"
|
||||
@@ -83,13 +82,11 @@ enum {
|
||||
ZIP_FILEHEADER_MAGIC = 0x504b0304,
|
||||
ZIP_CDF_MAGIC = 0x504b0102, /* CDF item */
|
||||
ZIP_CDE_MAGIC = 0x504b0506, /* End of CDF */
|
||||
ZIP64_CDE_MAGIC = 0x504b0606, /* End of Zip64 CDF */
|
||||
ZIP_DD_MAGIC = 0x504b0708,
|
||||
#else
|
||||
ZIP_FILEHEADER_MAGIC = 0x04034b50,
|
||||
ZIP_CDF_MAGIC = 0x02014b50,
|
||||
ZIP_CDE_MAGIC = 0x06054b50,
|
||||
ZIP64_CDE_MAGIC = 0x06064b50,
|
||||
ZIP_DD_MAGIC = 0x08074b50,
|
||||
#endif
|
||||
};
|
||||
@@ -156,15 +153,15 @@ typedef union {
|
||||
#define FIX_ENDIANNESS_CDF(cdf) \
|
||||
do { if (BB_BIG_ENDIAN) { \
|
||||
(cdf).fmt.version_made_by = SWAP_LE16((cdf).fmt.version_made_by); \
|
||||
(cdf).fmt.version_needed = SWAP_LE16((cdf).fmt.version_needed ); \
|
||||
(cdf).fmt.method = SWAP_LE16((cdf).fmt.method ); \
|
||||
(cdf).fmt.modtime = SWAP_LE16((cdf).fmt.modtime ); \
|
||||
(cdf).fmt.moddate = SWAP_LE16((cdf).fmt.moddate ); \
|
||||
(cdf).fmt.crc32 = SWAP_LE32((cdf).fmt.crc32 ); \
|
||||
(cdf).fmt.cmpsize = SWAP_LE32((cdf).fmt.cmpsize ); \
|
||||
(cdf).fmt.ucmpsize = SWAP_LE32((cdf).fmt.ucmpsize ); \
|
||||
(cdf).fmt.filename_len = SWAP_LE16((cdf).fmt.filename_len ); \
|
||||
(cdf).fmt.extra_len = SWAP_LE16((cdf).fmt.extra_len ); \
|
||||
(cdf).fmt.version_needed = SWAP_LE16((cdf).fmt.version_needed); \
|
||||
(cdf).fmt.method = SWAP_LE16((cdf).fmt.method ); \
|
||||
(cdf).fmt.modtime = SWAP_LE16((cdf).fmt.modtime ); \
|
||||
(cdf).fmt.moddate = SWAP_LE16((cdf).fmt.moddate ); \
|
||||
(cdf).fmt.crc32 = SWAP_LE32((cdf).fmt.crc32 ); \
|
||||
(cdf).fmt.cmpsize = SWAP_LE32((cdf).fmt.cmpsize ); \
|
||||
(cdf).fmt.ucmpsize = SWAP_LE32((cdf).fmt.ucmpsize ); \
|
||||
(cdf).fmt.filename_len = SWAP_LE16((cdf).fmt.filename_len); \
|
||||
(cdf).fmt.extra_len = SWAP_LE16((cdf).fmt.extra_len ); \
|
||||
(cdf).fmt.file_comment_length = SWAP_LE16((cdf).fmt.file_comment_length); \
|
||||
(cdf).fmt.external_attributes = SWAP_LE32((cdf).fmt.external_attributes); \
|
||||
}} while (0)
|
||||
@@ -263,12 +260,6 @@ static uint32_t find_cdf_offset(void)
|
||||
continue;
|
||||
/* we found CDE! */
|
||||
memcpy(cde.raw, p + 1, CDE_LEN);
|
||||
dbg("cde.this_disk_no:%d", cde.fmt.this_disk_no );
|
||||
dbg("cde.disk_with_cdf_no:%d", cde.fmt.disk_with_cdf_no );
|
||||
dbg("cde.cdf_entries_on_this_disk:%d", cde.fmt.cdf_entries_on_this_disk);
|
||||
dbg("cde.cdf_entries_total:%d", cde.fmt.cdf_entries_total );
|
||||
dbg("cde.cdf_size:%d", cde.fmt.cdf_size );
|
||||
dbg("cde.cdf_offset:%x", cde.fmt.cdf_offset );
|
||||
FIX_ENDIANNESS_CDE(cde);
|
||||
/*
|
||||
* I've seen .ZIP files with seemingly valid CDEs
|
||||
@@ -311,27 +302,19 @@ static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf)
|
||||
dbg("got ZIP_CDE_MAGIC");
|
||||
return 0; /* EOF */
|
||||
}
|
||||
if (magic == ZIP64_CDE_MAGIC) { /* seen in .zip with >4GB files */
|
||||
dbg("got ZIP64_CDE_MAGIC");
|
||||
return 0; /* EOF */
|
||||
}
|
||||
xread(zip_fd, cdf->raw, CDF_HEADER_LEN);
|
||||
|
||||
FIX_ENDIANNESS_CDF(*cdf);
|
||||
dbg(" magic:%08x filename_len:%u extra_len:%u file_comment_length:%u",
|
||||
magic,
|
||||
dbg(" filename_len:%u extra_len:%u file_comment_length:%u",
|
||||
(unsigned)cdf->fmt.filename_len,
|
||||
(unsigned)cdf->fmt.extra_len,
|
||||
(unsigned)cdf->fmt.file_comment_length
|
||||
);
|
||||
//TODO: require that magic == ZIP_CDF_MAGIC?
|
||||
|
||||
cdf_offset += 4 + CDF_HEADER_LEN
|
||||
+ cdf->fmt.filename_len
|
||||
+ cdf->fmt.extra_len
|
||||
+ cdf->fmt.file_comment_length;
|
||||
|
||||
dbg("Next cdf_offset 0x%x", cdf_offset);
|
||||
return cdf_offset;
|
||||
};
|
||||
#endif
|
||||
@@ -339,7 +322,7 @@ static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf)
|
||||
static void die_if_bad_fnamesize(unsigned sz)
|
||||
{
|
||||
if (sz > 0xfff) /* more than 4k?! no funny business please */
|
||||
bb_simple_error_msg_and_die("bad archive");
|
||||
bb_error_msg_and_die("bad archive");
|
||||
}
|
||||
|
||||
static void unzip_skip(off_t skip)
|
||||
@@ -353,9 +336,7 @@ static void unzip_create_leading_dirs(const char *fn)
|
||||
{
|
||||
/* Create all leading directories */
|
||||
char *name = xstrdup(fn);
|
||||
|
||||
/* mode of -1: set mode according to umask */
|
||||
if (bb_make_directory(dirname(name), -1, FILEUTILS_RECUR)) {
|
||||
if (bb_make_directory(dirname(name), 0777, FILEUTILS_RECUR)) {
|
||||
xfunc_die(); /* bb_make_directory is noisy */
|
||||
}
|
||||
free(name);
|
||||
@@ -376,7 +357,7 @@ static void unzip_extract_symlink(llist_t **symlink_placeholders,
|
||||
xread(zip_fd, target, zip->fmt.ucmpsize);
|
||||
} else {
|
||||
#if 1
|
||||
bb_simple_error_msg_and_die("compressed symlink is not supported");
|
||||
bb_error_msg_and_die("compressed symlink is not supported");
|
||||
#else
|
||||
transformer_state_t xstate;
|
||||
init_transformer_state(&xstate);
|
||||
@@ -389,10 +370,9 @@ static void unzip_extract_symlink(llist_t **symlink_placeholders,
|
||||
target[xstate.mem_output_size] = '\0';
|
||||
#endif
|
||||
}
|
||||
create_or_remember_link(symlink_placeholders,
|
||||
create_or_remember_symlink(symlink_placeholders,
|
||||
target,
|
||||
dst_fn,
|
||||
0);
|
||||
dst_fn);
|
||||
free(target);
|
||||
}
|
||||
#endif
|
||||
@@ -416,10 +396,10 @@ static void unzip_extract(zip_header_t *zip, int dst_fd)
|
||||
if (zip->fmt.method == 8) {
|
||||
/* Method 8 - inflate */
|
||||
if (inflate_unzip(&xstate) < 0)
|
||||
bb_simple_error_msg_and_die("inflate error");
|
||||
bb_error_msg_and_die("inflate error");
|
||||
/* Validate decompression - crc */
|
||||
if (zip->fmt.crc32 != (xstate.crc32 ^ 0xffffffffL)) {
|
||||
bb_simple_error_msg_and_die("crc error");
|
||||
bb_error_msg_and_die("crc error");
|
||||
}
|
||||
}
|
||||
#if ENABLE_FEATURE_UNZIP_BZIP2
|
||||
@@ -429,7 +409,7 @@ static void unzip_extract(zip_header_t *zip, int dst_fd)
|
||||
*/
|
||||
xstate.bytes_out = unpack_bz2_stream(&xstate);
|
||||
if (xstate.bytes_out < 0)
|
||||
bb_simple_error_msg_and_die("inflate error");
|
||||
bb_error_msg_and_die("inflate error");
|
||||
}
|
||||
#endif
|
||||
#if ENABLE_FEATURE_UNZIP_LZMA
|
||||
@@ -437,7 +417,7 @@ static void unzip_extract(zip_header_t *zip, int dst_fd)
|
||||
/* Not tested yet */
|
||||
xstate.bytes_out = unpack_lzma_stream(&xstate);
|
||||
if (xstate.bytes_out < 0)
|
||||
bb_simple_error_msg_and_die("inflate error");
|
||||
bb_error_msg_and_die("inflate error");
|
||||
}
|
||||
#endif
|
||||
#if ENABLE_FEATURE_UNZIP_XZ
|
||||
@@ -445,7 +425,7 @@ static void unzip_extract(zip_header_t *zip, int dst_fd)
|
||||
/* Not tested yet */
|
||||
xstate.bytes_out = unpack_xz_stream(&xstate);
|
||||
if (xstate.bytes_out < 0)
|
||||
bb_simple_error_msg_and_die("inflate error");
|
||||
bb_error_msg_and_die("inflate error");
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
@@ -453,12 +433,10 @@ static void unzip_extract(zip_header_t *zip, int dst_fd)
|
||||
}
|
||||
|
||||
/* Validate decompression - size */
|
||||
if (zip->fmt.ucmpsize != 0xffffffff /* seen on files with >4GB uncompressed data */
|
||||
&& zip->fmt.ucmpsize != xstate.bytes_out
|
||||
) {
|
||||
if (zip->fmt.ucmpsize != xstate.bytes_out) {
|
||||
/* Don't die. Who knows, maybe len calculation
|
||||
* was botched somewhere. After all, crc matched! */
|
||||
bb_simple_error_msg("bad length");
|
||||
bb_error_msg("bad length");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -466,7 +444,7 @@ static void my_fgets80(char *buf80)
|
||||
{
|
||||
fflush_all();
|
||||
if (!fgets(buf80, 80, stdin)) {
|
||||
bb_simple_perror_msg_and_die("can't read standard input");
|
||||
bb_perror_msg_and_die("can't read standard input");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -475,9 +453,7 @@ static int get_lstat_mode(const char *dst_fn)
|
||||
struct stat stat_buf;
|
||||
if (lstat(dst_fn, &stat_buf) == -1) {
|
||||
if (errno != ENOENT) {
|
||||
bb_perror_msg_and_die("can't stat '%s'",
|
||||
dst_fn
|
||||
);
|
||||
bb_perror_msg_and_die("can't stat '%s'", dst_fn);
|
||||
}
|
||||
/* File does not exist */
|
||||
return -1;
|
||||
@@ -557,7 +533,7 @@ int unzip_main(int argc, char **argv)
|
||||
|
||||
opts = 0;
|
||||
/* '-' makes getopt return 1 for non-options */
|
||||
while ((i = getopt(argc, argv, "-d:lnotpqxjv")) != -1) {
|
||||
while ((i = getopt(argc, argv, "-d:lnopqxjv")) != -1) {
|
||||
switch (i) {
|
||||
case 'd': /* Extract to base directory */
|
||||
base_dir = optarg;
|
||||
@@ -575,13 +551,8 @@ int unzip_main(int argc, char **argv)
|
||||
overwrite = O_ALWAYS;
|
||||
break;
|
||||
|
||||
case 't': /* Extract files to /dev/null */
|
||||
xmove_fd(xopen("/dev/null", O_WRONLY), STDOUT_FILENO);
|
||||
/*fallthrough*/
|
||||
|
||||
case 'p': /* Extract files to stdout */
|
||||
case 'p': /* Extract files to stdout and fall through to set verbosity */
|
||||
dst_fd = STDOUT_FILENO;
|
||||
/*fallthrough*/
|
||||
|
||||
case 'q': /* Be quiet */
|
||||
quiet++;
|
||||
@@ -660,9 +631,7 @@ int unzip_main(int argc, char **argv)
|
||||
break;
|
||||
if (++i > 2) {
|
||||
*ext = '\0';
|
||||
bb_error_msg_and_die("can't open %s[.zip]",
|
||||
src_fn
|
||||
);
|
||||
bb_error_msg_and_die("can't open %s[.zip]", src_fn);
|
||||
}
|
||||
strcpy(ext, extn[i - 1]);
|
||||
}
|
||||
@@ -670,21 +639,12 @@ int unzip_main(int argc, char **argv)
|
||||
}
|
||||
|
||||
/* Change dir if necessary */
|
||||
if (base_dir) {
|
||||
/* -p DIR: try to create, errors don't matter.
|
||||
* UnZip 6.00 does no multi-level mkdir (-p DIR1/DIR2 syntax),
|
||||
* not using bb_make_directory() here (yet?)
|
||||
*/
|
||||
mkdir(base_dir, 0777);
|
||||
if (base_dir)
|
||||
xchdir(base_dir);
|
||||
}
|
||||
|
||||
if (quiet <= 1) { /* not -qq */
|
||||
if (quiet == 0) {
|
||||
printf("Archive: %s\n",
|
||||
printable_string(src_fn)
|
||||
);
|
||||
}
|
||||
if (quiet == 0)
|
||||
printf("Archive: %s\n", src_fn);
|
||||
if (opts & OPT_l) {
|
||||
puts(verbose ?
|
||||
" Length Method Size Cmpr Date Time CRC-32 Name\n"
|
||||
@@ -868,8 +828,7 @@ int unzip_main(int argc, char **argv)
|
||||
printf( "%9u " "%s " "%s\n",
|
||||
(unsigned)zip.fmt.ucmpsize,
|
||||
dtbuf,
|
||||
printable_string(dst_fn)
|
||||
);
|
||||
dst_fn);
|
||||
} else {
|
||||
char method6[7];
|
||||
unsigned long percents;
|
||||
@@ -898,8 +857,7 @@ int unzip_main(int argc, char **argv)
|
||||
(unsigned)percents,
|
||||
dtbuf,
|
||||
zip.fmt.crc32,
|
||||
printable_string(dst_fn)
|
||||
);
|
||||
dst_fn);
|
||||
total_size += zip.fmt.cmpsize;
|
||||
}
|
||||
total_usize += zip.fmt.ucmpsize;
|
||||
@@ -925,7 +883,7 @@ int unzip_main(int argc, char **argv)
|
||||
mode = get_lstat_mode(dst_fn);
|
||||
if (mode == -1) { /* ENOENT */
|
||||
if (!quiet) {
|
||||
printf(" creating: %s\n", printable_string(dst_fn));
|
||||
printf(" creating: %s\n", dst_fn);
|
||||
}
|
||||
unzip_create_leading_dirs(dst_fn);
|
||||
if (bb_make_directory(dst_fn, dir_mode, FILEUTILS_IGNORE_CHMOD_ERR)) {
|
||||
@@ -934,9 +892,7 @@ int unzip_main(int argc, char **argv)
|
||||
} else {
|
||||
if (!S_ISDIR(mode)) {
|
||||
bb_error_msg_and_die("'%s' exists but is not a %s",
|
||||
printable_string(dst_fn),
|
||||
"directory"
|
||||
);
|
||||
dst_fn, "directory");
|
||||
}
|
||||
}
|
||||
goto skip_cmpsize;
|
||||
@@ -955,16 +911,12 @@ int unzip_main(int argc, char **argv)
|
||||
if (!S_ISREG(mode)) {
|
||||
fishy:
|
||||
bb_error_msg_and_die("'%s' exists but is not a %s",
|
||||
printable_string(dst_fn),
|
||||
"regular file"
|
||||
);
|
||||
dst_fn, "regular file");
|
||||
}
|
||||
if (overwrite == O_ALWAYS) {
|
||||
goto do_open_and_extract;
|
||||
}
|
||||
printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ",
|
||||
printable_string(dst_fn)
|
||||
);
|
||||
printf("replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ", dst_fn);
|
||||
my_fgets80(key_buf);
|
||||
/* User input could take a long time. Is it still a regular file? */
|
||||
mode = get_lstat_mode(dst_fn);
|
||||
@@ -990,14 +942,12 @@ int unzip_main(int argc, char **argv)
|
||||
/* O_NOFOLLOW defends against symlink attacks */
|
||||
dst_fd = xopen(dst_fn, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW);
|
||||
#endif
|
||||
do_extract:
|
||||
if (!quiet) {
|
||||
printf(/* zip.fmt.method == 0
|
||||
? " extracting: %s\n"
|
||||
: */ " inflating: %s\n",
|
||||
printable_string(dst_fn)
|
||||
);
|
||||
: */ " inflating: %s\n", dst_fn);
|
||||
}
|
||||
do_extract:
|
||||
#if ENABLE_FEATURE_UNZIP_CDF
|
||||
if (S_ISLNK(file_mode)) {
|
||||
if (dst_fd != STDOUT_FILENO) /* not -p? */
|
||||
@@ -1038,7 +988,7 @@ int unzip_main(int argc, char **argv)
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_UNZIP_CDF
|
||||
create_links_from_list(symlink_placeholders);
|
||||
create_symlinks_from_list(symlink_placeholders);
|
||||
#endif
|
||||
|
||||
if ((opts & OPT_l) && quiet <= 1) {
|
||||
|
||||
@@ -16,6 +16,7 @@ CONFIG_HAVE_DOT_CONFIG=y
|
||||
# CONFIG_EXTRA_COMPAT is not set
|
||||
# CONFIG_INCLUDE_SUSv2 is not set
|
||||
# CONFIG_USE_PORTABLE_CODE is not set
|
||||
CONFIG_PLATFORM_LINUX=y
|
||||
CONFIG_FEATURE_BUFFERS_USE_MALLOC=y
|
||||
# CONFIG_FEATURE_BUFFERS_GO_ON_STACK is not set
|
||||
# CONFIG_FEATURE_BUFFERS_GO_IN_BSS is not set
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
## Assorted header problems:
|
||||
## # CONFIG_HOSTID is not set
|
||||
## # CONFIG_FEATURE_SYNC_FANCY is not set - syncfs()
|
||||
## # CONFIG_FEATURE_TOUCH_NODEREF is not set - lutimes()
|
||||
## # CONFIG_LOGNAME is not set - getlogin_r()
|
||||
## # CONFIG_LOADFONT is not set
|
||||
## # CONFIG_SETFONT is not set
|
||||
@@ -86,6 +87,7 @@ CONFIG_DESKTOP=y
|
||||
# CONFIG_EXTRA_COMPAT is not set
|
||||
CONFIG_INCLUDE_SUSv2=y
|
||||
# CONFIG_USE_PORTABLE_CODE is not set
|
||||
CONFIG_PLATFORM_LINUX=y
|
||||
CONFIG_FEATURE_BUFFERS_USE_MALLOC=y
|
||||
# CONFIG_FEATURE_BUFFERS_GO_ON_STACK is not set
|
||||
# CONFIG_FEATURE_BUFFERS_GO_IN_BSS is not set
|
||||
@@ -268,6 +270,7 @@ CONFIG_SYNC=y
|
||||
CONFIG_TEST=y
|
||||
CONFIG_FEATURE_TEST_64=y
|
||||
CONFIG_TOUCH=y
|
||||
# CONFIG_FEATURE_TOUCH_NODEREF is not set
|
||||
CONFIG_FEATURE_TOUCH_SUSV3=y
|
||||
CONFIG_TR=y
|
||||
CONFIG_FEATURE_TR_CLASSES=y
|
||||
|
||||
@@ -16,6 +16,7 @@ CONFIG_DESKTOP=y
|
||||
# CONFIG_EXTRA_COMPAT is not set
|
||||
# CONFIG_INCLUDE_SUSv2 is not set
|
||||
# CONFIG_USE_PORTABLE_CODE is not set
|
||||
CONFIG_PLATFORM_LINUX=y
|
||||
CONFIG_FEATURE_BUFFERS_USE_MALLOC=y
|
||||
# CONFIG_FEATURE_BUFFERS_GO_ON_STACK is not set
|
||||
# CONFIG_FEATURE_BUFFERS_GO_IN_BSS is not set
|
||||
|
||||
@@ -16,6 +16,7 @@ CONFIG_DESKTOP=y
|
||||
# CONFIG_EXTRA_COMPAT is not set
|
||||
# CONFIG_INCLUDE_SUSv2 is not set
|
||||
# CONFIG_USE_PORTABLE_CODE is not set
|
||||
CONFIG_PLATFORM_LINUX=y
|
||||
CONFIG_FEATURE_BUFFERS_USE_MALLOC=y
|
||||
# CONFIG_FEATURE_BUFFERS_GO_ON_STACK is not set
|
||||
# CONFIG_FEATURE_BUFFERS_GO_IN_BSS is not set
|
||||
@@ -193,6 +194,7 @@ CONFIG_SYNC=y
|
||||
CONFIG_TEST=y
|
||||
CONFIG_FEATURE_TEST_64=y
|
||||
CONFIG_TOUCH=y
|
||||
# CONFIG_FEATURE_TOUCH_NODEREF is not set
|
||||
CONFIG_FEATURE_TOUCH_SUSV3=y
|
||||
CONFIG_TR=y
|
||||
CONFIG_FEATURE_TR_CLASSES=y
|
||||
|
||||
@@ -16,6 +16,7 @@ CONFIG_DESKTOP=y
|
||||
# CONFIG_EXTRA_COMPAT is not set
|
||||
CONFIG_INCLUDE_SUSv2=y
|
||||
# CONFIG_USE_PORTABLE_CODE is not set
|
||||
CONFIG_PLATFORM_LINUX=y
|
||||
CONFIG_FEATURE_BUFFERS_USE_MALLOC=y
|
||||
# CONFIG_FEATURE_BUFFERS_GO_ON_STACK is not set
|
||||
# CONFIG_FEATURE_BUFFERS_GO_IN_BSS is not set
|
||||
|
||||
@@ -16,6 +16,7 @@ CONFIG_HAVE_DOT_CONFIG=y
|
||||
# CONFIG_EXTRA_COMPAT is not set
|
||||
CONFIG_INCLUDE_SUSv2=y
|
||||
CONFIG_USE_PORTABLE_CODE=y
|
||||
# CONFIG_PLATFORM_LINUX is not set
|
||||
CONFIG_FEATURE_BUFFERS_USE_MALLOC=y
|
||||
# CONFIG_FEATURE_BUFFERS_GO_ON_STACK is not set
|
||||
# CONFIG_FEATURE_BUFFERS_GO_IN_BSS is not set
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see docs/Kconfig-language.txt.
|
||||
# see scripts/kbuild/config-language.txt.
|
||||
#
|
||||
|
||||
menu "Console Utilities"
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
//config:config CHVT
|
||||
//config: bool "chvt (2 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program is used to change to another terminal.
|
||||
//config: Example: chvt 4 (change to terminal /dev/tty4)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
//config:config DEALLOCVT
|
||||
//config: bool "deallocvt (1.9 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program deallocates unused virtual consoles.
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config DUMPKMAP
|
||||
//config: bool "dumpkmap (1.6 kb)"
|
||||
//config: bool "dumpkmap (1.3 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program dumps the kernel's keyboard translation table to
|
||||
//config: stdout, in binary format. You can then use loadkmap to load it.
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config FGCONSOLE
|
||||
//config: bool "fgconsole (1.5 kb)"
|
||||
//config: bool "fgconsole (1.6 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program prints active (foreground) console number.
|
||||
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config KBD_MODE
|
||||
//config: bool "kbd_mode (4.1 kb)"
|
||||
//config: bool "kbd_mode (4 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program reports and sets keyboard mode.
|
||||
|
||||
|
||||
+15
-13
@@ -10,14 +10,16 @@
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config LOADFONT
|
||||
//config: bool "loadfont (5.2 kb)"
|
||||
//config: bool "loadfont (5.4 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program loads a console font from standard input.
|
||||
//config:
|
||||
//config:config SETFONT
|
||||
//config: bool "setfont (24 kb)"
|
||||
//config: bool "setfont (26 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: Allows to load console screen map. Useful for i18n.
|
||||
//config:
|
||||
@@ -223,7 +225,7 @@ static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize,
|
||||
if (unicode == PSF2_SEPARATOR) {
|
||||
break;
|
||||
} else if (unicode == PSF2_STARTSEQ) {
|
||||
bb_simple_error_msg_and_die("unicode sequences not implemented");
|
||||
bb_error_msg_and_die("unicode sequences not implemented");
|
||||
} else if (unicode >= 0xC0) {
|
||||
if (unicode >= 0xFC)
|
||||
unicode &= 0x01, maxct = 5;
|
||||
@@ -237,12 +239,12 @@ static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize,
|
||||
unicode &= 0x1F, maxct = 1;
|
||||
do {
|
||||
if (tailsz <= 0 || *inbuf < 0x80 || *inbuf > 0xBF)
|
||||
bb_simple_error_msg_and_die("illegal UTF-8 character");
|
||||
bb_error_msg_and_die("illegal UTF-8 character");
|
||||
--tailsz;
|
||||
unicode = (unicode << 6) + (*inbuf++ & 0x3F);
|
||||
} while (--maxct > 0);
|
||||
} else if (unicode >= 0x80) {
|
||||
bb_simple_error_msg_and_die("illegal UTF-8 character");
|
||||
bb_error_msg_and_die("illegal UTF-8 character");
|
||||
}
|
||||
#else
|
||||
return;
|
||||
@@ -279,7 +281,7 @@ static void do_load(int fd, unsigned char *buffer, size_t len)
|
||||
|
||||
if (len >= sizeof(struct psf1_header) && PSF1_MAGIC_OK(psf1h(buffer))) {
|
||||
if (psf1h(buffer)->mode > PSF1_MAXMODE)
|
||||
bb_simple_error_msg_and_die("unsupported psf file mode");
|
||||
bb_error_msg_and_die("unsupported psf file mode");
|
||||
if (psf1h(buffer)->mode & PSF1_MODE512)
|
||||
fontsize = 512;
|
||||
if (psf1h(buffer)->mode & PSF1_MODEHASTAB)
|
||||
@@ -290,7 +292,7 @@ static void do_load(int fd, unsigned char *buffer, size_t len)
|
||||
#if ENABLE_FEATURE_LOADFONT_PSF2
|
||||
if (len >= sizeof(struct psf2_header) && PSF2_MAGIC_OK(psf2h(buffer))) {
|
||||
if (psf2h(buffer)->version > PSF2_MAXVERSION)
|
||||
bb_simple_error_msg_and_die("unsupported psf file version");
|
||||
bb_error_msg_and_die("unsupported psf file version");
|
||||
fontsize = psf2h(buffer)->length;
|
||||
if (psf2h(buffer)->flags & PSF2_HAS_UNICODE_TABLE)
|
||||
has_table = 2;
|
||||
@@ -309,19 +311,19 @@ static void do_load(int fd, unsigned char *buffer, size_t len)
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
bb_simple_error_msg_and_die("input file: bad length or unsupported font type");
|
||||
bb_error_msg_and_die("input file: bad length or unsupported font type");
|
||||
}
|
||||
|
||||
#if !defined(PIO_FONTX) || defined(__sparc__)
|
||||
if (fontsize != 256)
|
||||
bb_simple_error_msg_and_die("only fontsize 256 supported");
|
||||
bb_error_msg_and_die("only fontsize 256 supported");
|
||||
#endif
|
||||
|
||||
table = font + fontsize * charsize;
|
||||
buffer += len;
|
||||
|
||||
if (table > buffer || (!has_table && table != buffer))
|
||||
bb_simple_error_msg_and_die("input file: bad length");
|
||||
bb_error_msg_and_die("input file: bad length");
|
||||
|
||||
do_loadfont(fd, font, height, width, charsize, fontsize);
|
||||
|
||||
@@ -359,7 +361,7 @@ int loadfont_main(int argc UNUSED_PARAM, char **argv)
|
||||
buffer = xmalloc_read(STDIN_FILENO, &len);
|
||||
// xmalloc_open_zipped_read_close(filename, &len);
|
||||
if (!buffer)
|
||||
bb_simple_perror_msg_and_die("error reading input font");
|
||||
bb_perror_msg_and_die("error reading input font");
|
||||
do_load(get_console_fd_or_die(), buffer, len);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
@@ -399,7 +401,7 @@ setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig]
|
||||
-V Version
|
||||
*/
|
||||
//usage:#define setfont_trivial_usage
|
||||
//usage: "[-m MAPFILE] [-C TTY] FILE"
|
||||
//usage: "FONT [-m MAPFILE] [-C TTY]"
|
||||
//usage:#define setfont_full_usage "\n\n"
|
||||
//usage: "Load a console font\n"
|
||||
//usage: "\n -m MAPFILE Load console screen map"
|
||||
@@ -500,7 +502,7 @@ int setfont_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (a < 0 || a >= E_TABSZ
|
||||
|| b < 0 || b > 65535
|
||||
) {
|
||||
bb_simple_error_msg_and_die("map format");
|
||||
bb_error_msg_and_die("map format");
|
||||
}
|
||||
// patch map
|
||||
unicodes[a] = b;
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config LOADKMAP
|
||||
//config: bool "loadkmap (1.8 kb)"
|
||||
//config: bool "loadkmap (1.5 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program loads a keyboard translation table from
|
||||
//config: standard input.
|
||||
@@ -68,7 +69,7 @@ int loadkmap_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
xread(STDIN_FILENO, flags, 7);
|
||||
if (!is_prefixed_with(flags, BINARY_KEYMAP_MAGIC))
|
||||
bb_simple_error_msg_and_die("not a valid binary keymap");
|
||||
bb_error_msg_and_die("not a valid binary keymap");
|
||||
|
||||
xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
|
||||
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config OPENVT
|
||||
//config: bool "openvt (7.2 kb)"
|
||||
//config: bool "openvt (7 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program is used to start a command on an unused
|
||||
//config: virtual terminal.
|
||||
@@ -86,7 +87,7 @@ static int get_vt_fd(void)
|
||||
fd = open(DEV_CONSOLE, O_RDONLY | O_NONBLOCK);
|
||||
if (fd >= 0 && !not_vt_fd(fd))
|
||||
return fd;
|
||||
bb_simple_error_msg_and_die("can't find open VT");
|
||||
bb_error_msg_and_die("can't find open VT");
|
||||
}
|
||||
|
||||
static int find_free_vtno(void)
|
||||
@@ -97,7 +98,7 @@ static int find_free_vtno(void)
|
||||
errno = 0;
|
||||
/*xfunc_error_retval = 3; - do we need compat? */
|
||||
if (ioctl(fd, VT_OPENQRY, &vtno) != 0 || vtno <= 0)
|
||||
bb_simple_perror_msg_and_die("can't find open VT");
|
||||
bb_perror_msg_and_die("can't find open VT");
|
||||
// Not really needed, grep for DAEMON_CLOSE_EXTRA_FDS
|
||||
// if (fd > 2)
|
||||
// close(fd);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config RESET
|
||||
//config: bool "reset (345 bytes)"
|
||||
//config: bool "reset (275 bytes)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: This program is used to reset the terminal screen, if it
|
||||
|
||||
+2
-14
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config RESIZE
|
||||
//config: bool "resize (903 bytes)"
|
||||
//config: bool "resize (756 bytes)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: This program is used to (re)set the width and height of your current
|
||||
@@ -74,19 +74,7 @@ int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
||||
+ (1 << SIGTERM)
|
||||
+ (1 << SIGALRM)
|
||||
, onintr);
|
||||
/* Users report:
|
||||
* The resize command messes up the terminal.
|
||||
* In my case it looks like it is hanging and
|
||||
* I need to press ctrl-c to get a prompt.
|
||||
* Actually the program does not hang but just
|
||||
* the terminal is messed up.
|
||||
* Replaced TCSANOW with TCSAFLUSH:
|
||||
* "the change occurs after all output written to fd
|
||||
* has been transmitted, and all input that has been
|
||||
* received but not read will be discarded before
|
||||
* the change is made.
|
||||
*/
|
||||
tcsetattr(STDERR_FILENO, TCSAFLUSH, &new);
|
||||
tcsetattr(STDERR_FILENO, TCSANOW, &new);
|
||||
|
||||
/* save_cursor_pos 7
|
||||
* scroll_whole_screen [r
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config SETCONSOLE
|
||||
//config: bool "setconsole (3.6 kb)"
|
||||
//config: bool "setconsole (3.7 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: Redirect writes to /dev/console to another device,
|
||||
//config: like the current tty while logged in via telnet.
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config SETKEYCODES
|
||||
//config: bool "setkeycodes (2.1 kb)"
|
||||
//config: bool "setkeycodes (1.7 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program loads entries into the kernel's scancode-to-keycode
|
||||
//config: map, allowing unusual keyboards to generate usable keycodes.
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
//config:config SETLOGCONS
|
||||
//config: bool "setlogcons (1.8 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program redirects the output console of kernel messages.
|
||||
|
||||
@@ -40,7 +41,6 @@
|
||||
int setlogcons_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int setlogcons_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
char *devname;
|
||||
struct {
|
||||
char fn;
|
||||
char subarg;
|
||||
@@ -52,14 +52,7 @@ int setlogcons_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (argv[1])
|
||||
arg.subarg = xatou_range(argv[1], 0, 63);
|
||||
|
||||
/* Can just call it on "/dev/tty1" always, but...
|
||||
* in my testing, inactive (never opened) VTs are not
|
||||
* redirected to, despite ioctl not failing.
|
||||
*
|
||||
* By using "/dev/ttyN", ensure it is activated.
|
||||
*/
|
||||
devname = xasprintf("/dev/tty%u", arg.subarg);
|
||||
xioctl(xopen(devname, O_RDONLY), TIOCLINUX, &arg);
|
||||
xioctl(xopen(VC_1, O_RDONLY), TIOCLINUX, &arg);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
//config:config SHOWKEY
|
||||
//config: bool "showkey (4.7 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: Shows keys pressed.
|
||||
|
||||
@@ -55,7 +56,7 @@ static void xset1(struct termios *t)
|
||||
{
|
||||
int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
|
||||
if (ret) {
|
||||
bb_simple_perror_msg("can't tcsetattr for stdin");
|
||||
bb_perror_msg("can't tcsetattr for stdin");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +107,7 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
|
||||
xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
|
||||
|
||||
// we should exit on any signal; signals should interrupt read
|
||||
bb_signals_norestart(BB_FATAL_SIGS, record_signo);
|
||||
bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
|
||||
|
||||
// inform user that program ends after time of inactivity
|
||||
printf(press_keys, "10s after last keypress");
|
||||
|
||||
+5
-16
@@ -1,10 +1,14 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see docs/Kconfig-language.txt.
|
||||
# see scripts/kbuild/config-language.txt.
|
||||
#
|
||||
|
||||
menu "Coreutils"
|
||||
|
||||
INSERT
|
||||
|
||||
comment "Common options"
|
||||
|
||||
config FEATURE_VERBOSE
|
||||
bool "Support verbose options (usually -v) for various applets"
|
||||
default y
|
||||
@@ -13,19 +17,6 @@ config FEATURE_VERBOSE
|
||||
Also enables long option (--verbose) if it exists.
|
||||
Without this option, -v is accepted but ignored.
|
||||
|
||||
comment "Common options for date and touch"
|
||||
|
||||
config FEATURE_TIMEZONE
|
||||
bool "Allow timezone in dates"
|
||||
default y
|
||||
depends on DESKTOP
|
||||
help
|
||||
Permit the use of timezones when parsing user-provided data
|
||||
strings, e.g. '1996-04-09 12:45:00 -0500'.
|
||||
|
||||
This requires support for the '%z' extension to strptime() which
|
||||
may not be available in all implementations.
|
||||
|
||||
comment "Common options for cp and mv"
|
||||
depends on CP || MV
|
||||
|
||||
@@ -46,6 +37,4 @@ config FEATURE_HUMAN_READABLE
|
||||
help
|
||||
Allow df, du, and ls to have human readable output.
|
||||
|
||||
INSERT
|
||||
|
||||
endmenu
|
||||
+25
-39
@@ -14,7 +14,7 @@
|
||||
* 3) Save some space by using strcmp(). Calling strncmp() here was silly.
|
||||
*/
|
||||
//config:config BASENAME
|
||||
//config: bool "basename (438 bytes)"
|
||||
//config: bool "basename (371 bytes)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: basename is used to strip the directory and suffix from filenames,
|
||||
@@ -29,11 +29,9 @@
|
||||
/* http://www.opengroup.org/onlinepubs/007904975/utilities/basename.html */
|
||||
|
||||
//usage:#define basename_trivial_usage
|
||||
//usage: "FILE [SUFFIX] | -a FILE... | -s SUFFIX FILE..."
|
||||
//usage: "FILE [SUFFIX]"
|
||||
//usage:#define basename_full_usage "\n\n"
|
||||
//usage: "Strip directory path and SUFFIX from FILE\n"
|
||||
//usage: "\n -a All arguments are FILEs"
|
||||
//usage: "\n -s SUFFIX Remove SUFFIX (implies -a)"
|
||||
//usage: "Strip directory path and .SUFFIX from FILE"
|
||||
//usage:
|
||||
//usage:#define basename_example_usage
|
||||
//usage: "$ basename /usr/local/bin/foo\n"
|
||||
@@ -50,43 +48,31 @@
|
||||
int basename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int basename_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
unsigned opts;
|
||||
const char *suffix = NULL;
|
||||
size_t m, n;
|
||||
char *s;
|
||||
|
||||
/* '+': stop at first non-option */
|
||||
opts = getopt32(argv, "^+" "as:"
|
||||
"\0" "-1" /* At least one argument */
|
||||
, &suffix
|
||||
);
|
||||
argv += optind;
|
||||
if (argv[1] && strcmp(argv[1], "--") == 0) {
|
||||
argv++;
|
||||
}
|
||||
if (!argv[1])
|
||||
bb_show_usage();
|
||||
|
||||
do {
|
||||
char *s;
|
||||
size_t m;
|
||||
/* It should strip slash: /abc/def/ -> def */
|
||||
s = bb_get_last_path_component_strip(*++argv);
|
||||
|
||||
/* It should strip slash: /abc/def/ -> def */
|
||||
s = bb_get_last_path_component_strip(*argv++);
|
||||
m = strlen(s);
|
||||
if (!opts) {
|
||||
if (*argv) {
|
||||
suffix = *argv;
|
||||
if (argv[1])
|
||||
bb_show_usage();
|
||||
}
|
||||
m = strlen(s);
|
||||
if (*++argv) {
|
||||
if (argv[1])
|
||||
bb_show_usage();
|
||||
n = strlen(*argv);
|
||||
if ((m > n) && (strcmp(s+m-n, *argv) == 0)) {
|
||||
m -= n;
|
||||
/*s[m] = '\0'; - redundant */
|
||||
}
|
||||
if (suffix) {
|
||||
size_t n = strlen(suffix);
|
||||
if ((m > n) && (strcmp(s + m - n, suffix) == 0)) {
|
||||
m -= n;
|
||||
/*s[m] = '\0'; - redundant */
|
||||
}
|
||||
}
|
||||
/* puts(s) will do, but we can do without stdio this way: */
|
||||
s[m++] = '\n';
|
||||
/* NB: != is correct here: */
|
||||
if (full_write(STDOUT_FILENO, s, m) != (ssize_t)m)
|
||||
return EXIT_FAILURE;
|
||||
} while (opts && *argv);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
/* puts(s) will do, but we can do without stdio this way: */
|
||||
s[m++] = '\n';
|
||||
/* NB: != is correct here: */
|
||||
return full_write(STDOUT_FILENO, s, m) != (ssize_t)m;
|
||||
}
|
||||
+7
-9
@@ -112,10 +112,10 @@ static int catv(unsigned opts, char **argv)
|
||||
int retval = EXIT_SUCCESS;
|
||||
int fd;
|
||||
#if ENABLE_FEATURE_CATN
|
||||
bool eol_seen = (opts & (CAT_OPT_n|CAT_OPT_b));
|
||||
unsigned eol_char = (eol_seen ? '\n' : 0x100);
|
||||
unsigned skip_num_on = (opts & CAT_OPT_b) ? '\n' : 0x100;
|
||||
unsigned lineno = 0;
|
||||
unsigned eol_char = (opts & (CAT_OPT_n|CAT_OPT_b)) ? '\n' : 0x100;
|
||||
unsigned skip_num_on = (opts & CAT_OPT_b) ? '\n' : 0x100;
|
||||
bool eol_seen = 1;
|
||||
#endif
|
||||
|
||||
BUILD_BUG_ON(CAT_OPT_e != VISIBLE_ENDLINE);
|
||||
@@ -152,7 +152,7 @@ static int catv(unsigned opts, char **argv)
|
||||
eol_seen = (c == eol_char);
|
||||
#endif
|
||||
visible(c, buf, opts);
|
||||
fputs_stdout(buf);
|
||||
fputs(buf, stdout);
|
||||
}
|
||||
}
|
||||
if (ENABLE_FEATURE_CLEAN_UP && fd)
|
||||
@@ -195,20 +195,18 @@ int cat_main(int argc UNUSED_PARAM, char **argv)
|
||||
# define CAT_OPT_b (1<<1)
|
||||
if (opts & (CAT_OPT_n|CAT_OPT_b)) { /* -n or -b */
|
||||
struct number_state ns;
|
||||
int exitcode;
|
||||
|
||||
ns.width = 6;
|
||||
ns.start = 1;
|
||||
ns.inc = 1;
|
||||
ns.sep = "\t";
|
||||
ns.empty_str = NULL;
|
||||
ns.empty_str = "\n";
|
||||
ns.all = !(opts & CAT_OPT_b); /* -n without -b */
|
||||
ns.nonempty = (opts & CAT_OPT_b); /* -b (with or without -n) */
|
||||
exitcode = EXIT_SUCCESS;
|
||||
do {
|
||||
exitcode |= print_numbered_lines(&ns, *argv);
|
||||
print_numbered_lines(&ns, *argv);
|
||||
} while (*++argv);
|
||||
fflush_stdout_and_exit(exitcode);
|
||||
fflush_stdout_and_exit(EXIT_SUCCESS);
|
||||
}
|
||||
/*opts >>= 2;*/
|
||||
#endif
|
||||
|
||||
+4
-8
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CHGRP
|
||||
//config: bool "chgrp (7.6 kb)"
|
||||
//config: bool "chgrp (7.2 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: chgrp is used to change the group ownership of files.
|
||||
@@ -21,18 +21,14 @@
|
||||
/* http://www.opengroup.org/onlinepubs/007904975/utilities/chgrp.html */
|
||||
|
||||
//usage:#define chgrp_trivial_usage
|
||||
//usage: "[-Rh"IF_DESKTOP("LHPcvf")"]... GROUP FILE..."
|
||||
//usage: "[-RhLHP"IF_DESKTOP("cvf")"]... GROUP FILE..."
|
||||
//usage:#define chgrp_full_usage "\n\n"
|
||||
//usage: "Change the group membership of FILEs to GROUP"
|
||||
//usage: "\n"
|
||||
//usage: "Change the group membership of each FILE to GROUP\n"
|
||||
//usage: "\n -R Recurse"
|
||||
//usage: "\n -h Affect symlinks instead of symlink targets"
|
||||
//usage: IF_DESKTOP(
|
||||
//usage: "\n -L Traverse all symlinks to directories"
|
||||
//usage: "\n -H Traverse symlinks on command line only"
|
||||
//usage: "\n -P Don't traverse symlinks (default)"
|
||||
//usage: )
|
||||
//next 4 options are the same for chmod/chown/chgrp:
|
||||
//usage: "\n -R Recurse"
|
||||
//usage: IF_DESKTOP(
|
||||
//usage: "\n -c List changed files"
|
||||
//usage: "\n -v Verbose"
|
||||
|
||||
+12
-16
@@ -10,7 +10,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CHMOD
|
||||
//config: bool "chmod (5.5 kb)"
|
||||
//config: bool "chmod (5.1 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: chmod is used to change the access permission of files.
|
||||
@@ -26,13 +26,12 @@
|
||||
//usage:#define chmod_trivial_usage
|
||||
//usage: "[-R"IF_DESKTOP("cvf")"] MODE[,MODE]... FILE..."
|
||||
//usage:#define chmod_full_usage "\n\n"
|
||||
//usage: "MODE is octal number (bit pattern sstrwxrwxrwx) or [ugoa]{+|-|=}[rwxXst]"
|
||||
//usage: "\n"
|
||||
//next 4 options are the same for chmod/chown/chgrp:
|
||||
//usage: "Each MODE is one or more of the letters ugoa, one of the\n"
|
||||
//usage: "symbols +-= and one or more of the letters rwxst\n"
|
||||
//usage: "\n -R Recurse"
|
||||
//usage: IF_DESKTOP(
|
||||
//usage: "\n -c List changed files"
|
||||
//usage: "\n -v Verbose"
|
||||
//usage: "\n -v List all files"
|
||||
//usage: "\n -f Hide errors"
|
||||
//usage: )
|
||||
//usage:
|
||||
@@ -66,14 +65,12 @@
|
||||
* symbolic links encountered during recursive directory traversals.
|
||||
*/
|
||||
|
||||
static int FAST_FUNC fileAction(struct recursive_state *state,
|
||||
const char *fileName,
|
||||
struct stat *statbuf)
|
||||
static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void* param, int depth)
|
||||
{
|
||||
mode_t newmode;
|
||||
|
||||
/* match coreutils behavior */
|
||||
if (state->depth == 0) {
|
||||
if (depth == 0) {
|
||||
/* statbuf holds lstat result, but we need stat (follow link) */
|
||||
if (stat(fileName, statbuf))
|
||||
goto err;
|
||||
@@ -82,18 +79,16 @@ static int FAST_FUNC fileAction(struct recursive_state *state,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
newmode = bb_parse_mode((char *)state->userData, statbuf->st_mode);
|
||||
newmode = bb_parse_mode((char *)param, statbuf->st_mode);
|
||||
if (newmode == (mode_t)-1)
|
||||
bb_error_msg_and_die("invalid mode '%s'", (char *)state->userData);
|
||||
bb_error_msg_and_die("invalid mode '%s'", (char *)param);
|
||||
|
||||
if (chmod(fileName, newmode) == 0) {
|
||||
if (OPT_VERBOSE
|
||||
|| (OPT_CHANGED
|
||||
&& (statbuf->st_mode & 07777) != (newmode & 07777))
|
||||
|| (OPT_CHANGED && statbuf->st_mode != newmode)
|
||||
) {
|
||||
char modestr[12];
|
||||
printf("mode of '%s' changed to %04o (%s)\n", fileName,
|
||||
newmode & 07777, bb_mode_string(modestr, newmode)+1);
|
||||
newmode & 07777, bb_mode_string(newmode)+1);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
@@ -141,7 +136,8 @@ int chmod_main(int argc UNUSED_PARAM, char **argv)
|
||||
OPT_RECURSE, // recurse
|
||||
fileAction, // file action
|
||||
fileAction, // dir action
|
||||
smode) // user data
|
||||
smode, // user data
|
||||
0) // depth
|
||||
) {
|
||||
retval = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
+9
-12
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CHOWN
|
||||
//config: bool "chown (7.6 kb)"
|
||||
//config: bool "chown (7.2 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: chown is used to change the user and/or group ownership
|
||||
@@ -28,19 +28,15 @@
|
||||
//usage:#define chown_trivial_usage
|
||||
//usage: "[-Rh"IF_DESKTOP("LHPcvf")"]... USER[:[GRP]] FILE..."
|
||||
//usage:#define chown_full_usage "\n\n"
|
||||
//usage: "Change the owner and/or group of FILEs to USER and/or GRP"
|
||||
//usage: "\n"
|
||||
//usage: "Change the owner and/or group of each FILE to USER and/or GRP\n"
|
||||
//usage: "\n -R Recurse"
|
||||
//usage: "\n -h Affect symlinks instead of symlink targets"
|
||||
//usage: IF_DESKTOP(
|
||||
//usage: "\n -L Traverse all symlinks to directories"
|
||||
//usage: "\n -H Traverse symlinks on command line only"
|
||||
//usage: "\n -P Don't traverse symlinks (default)"
|
||||
//usage: )
|
||||
//next 4 options are the same for chmod/chown/chgrp:
|
||||
//usage: "\n -R Recurse"
|
||||
//usage: IF_DESKTOP(
|
||||
//usage: "\n -c List changed files"
|
||||
//usage: "\n -v Verbose"
|
||||
//usage: "\n -v List all files"
|
||||
//usage: "\n -f Hide errors"
|
||||
//usage: )
|
||||
//usage:
|
||||
@@ -101,10 +97,10 @@ struct param_t {
|
||||
chown_fptr chown_func;
|
||||
};
|
||||
|
||||
static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
|
||||
const char *fileName, struct stat *statbuf)
|
||||
static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
|
||||
void *vparam, int depth UNUSED_PARAM)
|
||||
{
|
||||
#define param (*(struct param_t*)state->userData)
|
||||
#define param (*(struct param_t*)vparam)
|
||||
#define opt option_mask32
|
||||
uid_t u = (param.ugid.uid == (uid_t)-1L) ? statbuf->st_uid : param.ugid.uid;
|
||||
gid_t g = (param.ugid.gid == (gid_t)-1L) ? statbuf->st_gid : param.ugid.gid;
|
||||
@@ -163,7 +159,8 @@ int chown_main(int argc UNUSED_PARAM, char **argv)
|
||||
flags, /* flags */
|
||||
fileAction, /* file action */
|
||||
fileAction, /* dir action */
|
||||
¶m) /* user data */
|
||||
¶m, /* user data */
|
||||
0) /* depth */
|
||||
) {
|
||||
retval = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
+31
-54
@@ -7,42 +7,37 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CKSUM
|
||||
//config: bool "cksum (4.1 kb)"
|
||||
//config: default y
|
||||
//config:
|
||||
//config:config CRC32
|
||||
//config: bool "crc32 (4.1 kb)"
|
||||
//config: bool "cksum (4.2 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: cksum is used to calculate the CRC32 checksum of a file.
|
||||
|
||||
// APPLET_NOEXEC:name main location suid_type help
|
||||
//applet:IF_CKSUM(APPLET_NOEXEC(cksum, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
|
||||
//applet:IF_CRC32(APPLET_NOEXEC(crc32, cksum, BB_DIR_USR_BIN, BB_SUID_DROP, cksum))
|
||||
/* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
|
||||
|
||||
//kbuild:lib-$(CONFIG_CKSUM) += cksum.o
|
||||
//kbuild:lib-$(CONFIG_CRC32) += cksum.o
|
||||
|
||||
//usage:#define cksum_trivial_usage
|
||||
//usage: "FILE..."
|
||||
//usage:#define cksum_full_usage "\n\n"
|
||||
//usage: "Calculate CRC32 checksum of FILEs"
|
||||
//usage: "Calculate the CRC32 checksums of FILEs"
|
||||
|
||||
#include "libbb.h"
|
||||
#include "common_bufsiz.h"
|
||||
|
||||
/* This is a NOEXEC applet. Be very careful! */
|
||||
|
||||
#define IS_CKSUM (ENABLE_CKSUM && (!ENABLE_CRC32 || applet_name[1] == 'k'))
|
||||
#define IS_CRC32 (ENABLE_CRC32 && (!ENABLE_CKSUM || applet_name[1] == 'r'))
|
||||
|
||||
int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int cksum_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
uint32_t *crc32_table = crc32_filltable(NULL, IS_CKSUM);
|
||||
uint32_t *crc32_table = crc32_filltable(NULL, 1);
|
||||
uint32_t crc;
|
||||
off_t length, filesize;
|
||||
int bytes_read;
|
||||
int exit_code = EXIT_SUCCESS;
|
||||
|
||||
#if ENABLE_DESKTOP
|
||||
getopt32(argv, ""); /* cksum coreutils 6.9 compat */
|
||||
getopt32(argv, ""); /* coreutils 6.9 compat */
|
||||
argv += optind;
|
||||
#else
|
||||
argv++;
|
||||
@@ -50,56 +45,38 @@ int cksum_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
setup_common_bufsiz();
|
||||
do {
|
||||
uint32_t crc;
|
||||
IF_CKSUM(off_t filesize;)
|
||||
const char *fname = *argv ? *argv : bb_msg_standard_input;
|
||||
int fd = open_or_warn_stdin(fname);
|
||||
int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
|
||||
|
||||
if (fd < 0) {
|
||||
exit_code = EXIT_FAILURE;
|
||||
continue;
|
||||
}
|
||||
crc = 0;
|
||||
length = 0;
|
||||
|
||||
crc = IS_CKSUM ? 0 : 0xffffffff;
|
||||
IF_CKSUM(filesize = 0;)
|
||||
#define read_buf bb_common_bufsiz1
|
||||
for (;;) {
|
||||
int bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE);
|
||||
if (bytes_read < 0)
|
||||
bb_simple_perror_msg_and_die(fname);
|
||||
if (bytes_read > 0) {
|
||||
IF_CKSUM(filesize += bytes_read;)
|
||||
} else {
|
||||
IF_CKSUM(uoff_t t;)
|
||||
|
||||
close(fd);
|
||||
if (IS_CRC32)
|
||||
break;
|
||||
#if ENABLE_CKSUM
|
||||
fd = -1; /* break flag */
|
||||
/* Checksum filesize bytes, LSB first */
|
||||
t = filesize;
|
||||
/*bytes_read = 0; - already is */
|
||||
while (t != 0) {
|
||||
read_buf[bytes_read++] = (uint8_t)t;
|
||||
t >>= 8;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
crc = (IS_CKSUM ? crc32_block_endian1 : crc32_block_endian0)(crc, read_buf, bytes_read, crc32_table);
|
||||
if (ENABLE_CKSUM && fd < 0)
|
||||
break;
|
||||
while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) {
|
||||
length += bytes_read;
|
||||
crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
|
||||
}
|
||||
close(fd);
|
||||
|
||||
filesize = length;
|
||||
|
||||
while (length) {
|
||||
crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length];
|
||||
/* must ensure that shift is unsigned! */
|
||||
if (sizeof(length) <= sizeof(unsigned))
|
||||
length = (unsigned)length >> 8;
|
||||
else if (sizeof(length) <= sizeof(unsigned long))
|
||||
length = (unsigned long)length >> 8;
|
||||
else
|
||||
length = (unsigned long long)length >> 8;
|
||||
}
|
||||
crc = ~crc;
|
||||
#if ENABLE_CKSUM
|
||||
if (IS_CKSUM)
|
||||
printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
|
||||
(unsigned)crc, filesize, *argv);
|
||||
else
|
||||
#endif
|
||||
printf((*argv ? "%08x %s\n" : "%08x\n"),
|
||||
(unsigned)crc, *argv);
|
||||
|
||||
printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"),
|
||||
crc, filesize, *argv);
|
||||
} while (*argv && *++argv);
|
||||
|
||||
fflush_stdout_and_exit(exit_code);
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config COMM
|
||||
//config: bool "comm (4.2 kb)"
|
||||
//config: bool "comm (3.9 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: comm is used to compare two files line by line and return
|
||||
|
||||
+98
-136
@@ -12,7 +12,7 @@
|
||||
* Size reduction.
|
||||
*/
|
||||
//config:config CP
|
||||
//config: bool "cp (10 kb)"
|
||||
//config: bool "cp (9.7 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: cp is used to copy files and directories.
|
||||
@@ -24,70 +24,17 @@
|
||||
//config: help
|
||||
//config: Enable long options.
|
||||
//config: Also add support for --parents option.
|
||||
//config:
|
||||
//config:config FEATURE_CP_REFLINK
|
||||
//config: bool "Enable --reflink[=auto]"
|
||||
//config: default y
|
||||
//config: depends on FEATURE_CP_LONG_OPTIONS
|
||||
|
||||
//applet:IF_CP(APPLET_NOEXEC(cp, cp, BB_DIR_BIN, BB_SUID_DROP, cp))
|
||||
/* NOEXEC despite cases when it can be a "runner" (cp -r LARGE_DIR NEW_DIR) */
|
||||
|
||||
//kbuild:lib-$(CONFIG_CP) += cp.o
|
||||
|
||||
/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
|
||||
|
||||
// Options of cp from GNU coreutils 6.10:
|
||||
// -a, --archive
|
||||
// -f, --force
|
||||
// -i, --interactive
|
||||
// -l, --link
|
||||
// -L, --dereference
|
||||
// -P, --no-dereference
|
||||
// -R, -r, --recursive
|
||||
// -s, --symbolic-link
|
||||
// -v, --verbose
|
||||
// -H follow command-line symbolic links in SOURCE
|
||||
// -d same as --no-dereference --preserve=links
|
||||
// -p same as --preserve=mode,ownership,timestamps
|
||||
// -c same as --preserve=context
|
||||
// -u, --update
|
||||
// copy only when the SOURCE file is newer than the destination
|
||||
// file or when the destination file is missing
|
||||
// --remove-destination
|
||||
// remove each existing destination file before attempting to open
|
||||
// --parents
|
||||
// use full source file name under DIRECTORY
|
||||
// -T, --no-target-directory
|
||||
// treat DEST as a normal file
|
||||
// NOT SUPPORTED IN BBOX:
|
||||
// --backup[=CONTROL]
|
||||
// make a backup of each existing destination file
|
||||
// -b like --backup but does not accept an argument
|
||||
// --copy-contents
|
||||
// copy contents of special files when recursive
|
||||
// --preserve[=ATTR_LIST]
|
||||
// preserve attributes (default: mode,ownership,timestamps),
|
||||
// if possible additional attributes: security context,links,all
|
||||
// --no-preserve=ATTR_LIST
|
||||
// --sparse=WHEN
|
||||
// control creation of sparse files
|
||||
// --strip-trailing-slashes
|
||||
// remove any trailing slashes from each SOURCE argument
|
||||
// -S, --suffix=SUFFIX
|
||||
// override the usual backup suffix
|
||||
// -t, --target-directory=DIRECTORY
|
||||
// copy all SOURCE arguments into DIRECTORY
|
||||
// -x, --one-file-system
|
||||
// stay on this file system
|
||||
// -Z, --context=CONTEXT
|
||||
// (SELinux) set SELinux security context of copy to CONTEXT
|
||||
|
||||
//usage:#define cp_trivial_usage
|
||||
//usage: "[-arPLHpfinlsTu] SOURCE DEST\n"
|
||||
//usage: "or: cp [-arPLHpfinlsu] SOURCE... { -t DIRECTORY | DIRECTORY }"
|
||||
//usage: "[OPTIONS] SOURCE... DEST"
|
||||
//usage:#define cp_full_usage "\n\n"
|
||||
//usage: "Copy SOURCEs to DEST\n"
|
||||
//usage: "Copy SOURCE(s) to DEST\n"
|
||||
//usage: "\n -a Same as -dpR"
|
||||
//usage: IF_SELINUX(
|
||||
//usage: "\n -c Preserve security context"
|
||||
@@ -99,10 +46,8 @@
|
||||
//usage: "\n -p Preserve file attributes if possible"
|
||||
//usage: "\n -f Overwrite"
|
||||
//usage: "\n -i Prompt before overwrite"
|
||||
//usage: "\n -n Don't overwrite"
|
||||
//usage: "\n -l,-s Create (sym)links"
|
||||
//usage: "\n -T Refuse to copy if DEST is a directory"
|
||||
//usage: "\n -t DIR Copy all SOURCEs into DIR"
|
||||
//usage: "\n -T Treat DEST as a normal file"
|
||||
//usage: "\n -u Copy only newer files"
|
||||
|
||||
#include "libbb.h"
|
||||
@@ -121,68 +66,92 @@ int cp_main(int argc, char **argv)
|
||||
int d_flags;
|
||||
int flags;
|
||||
int status;
|
||||
#if ENABLE_FEATURE_CP_LONG_OPTIONS
|
||||
enum {
|
||||
/*OPT_rmdest = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTBITS */
|
||||
OPT_parents = 1 << (FILEUTILS_CP_OPTBITS+1),
|
||||
OPT_reflink = 1 << (FILEUTILS_CP_OPTBITS+2),
|
||||
FILEUTILS_CP_OPTNUM = sizeof(FILEUTILS_CP_OPTSTR)-1,
|
||||
#if ENABLE_FEATURE_CP_LONG_OPTIONS
|
||||
/*OPT_rmdest = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTNUM */
|
||||
OPT_parents = 1 << (FILEUTILS_CP_OPTNUM+1),
|
||||
#endif
|
||||
};
|
||||
# if ENABLE_FEATURE_CP_REFLINK
|
||||
char *reflink = NULL;
|
||||
# endif
|
||||
|
||||
#if ENABLE_FEATURE_CP_LONG_OPTIONS
|
||||
flags = getopt32long(argv, "^"
|
||||
FILEUTILS_CP_OPTSTR
|
||||
"\0"
|
||||
// Need at least one argument. (Usually two+, but -t DIR can have only one)
|
||||
// Need at least two arguments
|
||||
// Soft- and hardlinking doesn't mix
|
||||
// -P and -d are the same (-P is POSIX, -d is GNU)
|
||||
// -r and -R are the same
|
||||
// -R (and therefore -r) turns on -d (coreutils does this)
|
||||
// -a = -pdR
|
||||
// -i overrides -n and vice versa (last wins)
|
||||
"-1:l--s:s--l:Pd:rRd:Rd:apdR:i-n:n-i",
|
||||
"-2:l--s:s--l:Pd:rRd:Rd:apdR",
|
||||
"archive\0" No_argument "a"
|
||||
"force\0" No_argument "f"
|
||||
"interactive\0" No_argument "i"
|
||||
"no-clobber\0" No_argument "n"
|
||||
"link\0" No_argument "l"
|
||||
"dereference\0" No_argument "L"
|
||||
"no-dereference\0" No_argument "P"
|
||||
"recursive\0" No_argument "R"
|
||||
"symbolic-link\0" No_argument "s"
|
||||
"no-target-directory\0" No_argument "T"
|
||||
"target-directory\0" Required_argument "t"
|
||||
"verbose\0" No_argument "v"
|
||||
"update\0" No_argument "u"
|
||||
"remove-destination\0" No_argument "\xff"
|
||||
"parents\0" No_argument "\xfe"
|
||||
# if ENABLE_FEATURE_CP_REFLINK
|
||||
"reflink\0" Optional_argument "\xfd"
|
||||
# endif
|
||||
, &last
|
||||
# if ENABLE_FEATURE_CP_REFLINK
|
||||
, &reflink
|
||||
# endif
|
||||
);
|
||||
# if ENABLE_FEATURE_CP_REFLINK
|
||||
BUILD_BUG_ON((int)OPT_reflink != (int)FILEUTILS_REFLINK);
|
||||
if (flags & FILEUTILS_REFLINK) {
|
||||
if (!reflink)
|
||||
flags |= FILEUTILS_REFLINK_ALWAYS;
|
||||
else if (strcmp(reflink, "always") == 0)
|
||||
flags |= FILEUTILS_REFLINK_ALWAYS;
|
||||
else if (strcmp(reflink, "auto") != 0)
|
||||
bb_show_usage();
|
||||
}
|
||||
# endif
|
||||
#else
|
||||
flags = getopt32(argv, "^"
|
||||
FILEUTILS_CP_OPTSTR
|
||||
"\0"
|
||||
"-1:l--s:s--l:Pd:rRd:Rd:apdR"
|
||||
, &last
|
||||
"-2:l--s:s--l:Pd:rRd:Rd:apdR"
|
||||
);
|
||||
#endif
|
||||
/* Options of cp from GNU coreutils 6.10:
|
||||
* -a, --archive
|
||||
* -f, --force
|
||||
* -i, --interactive
|
||||
* -l, --link
|
||||
* -L, --dereference
|
||||
* -P, --no-dereference
|
||||
* -R, -r, --recursive
|
||||
* -s, --symbolic-link
|
||||
* -v, --verbose
|
||||
* -H follow command-line symbolic links in SOURCE
|
||||
* -d same as --no-dereference --preserve=links
|
||||
* -p same as --preserve=mode,ownership,timestamps
|
||||
* -c same as --preserve=context
|
||||
* -u, --update
|
||||
* copy only when the SOURCE file is newer than the destination
|
||||
* file or when the destination file is missing
|
||||
* --remove-destination
|
||||
* remove each existing destination file before attempting to open
|
||||
* --parents
|
||||
* use full source file name under DIRECTORY
|
||||
* -T, --no-target-directory
|
||||
* treat DEST as a normal file
|
||||
* NOT SUPPORTED IN BBOX:
|
||||
* --backup[=CONTROL]
|
||||
* make a backup of each existing destination file
|
||||
* -b like --backup but does not accept an argument
|
||||
* --copy-contents
|
||||
* copy contents of special files when recursive
|
||||
* --preserve[=ATTR_LIST]
|
||||
* preserve attributes (default: mode,ownership,timestamps),
|
||||
* if possible additional attributes: security context,links,all
|
||||
* --no-preserve=ATTR_LIST
|
||||
* --sparse=WHEN
|
||||
* control creation of sparse files
|
||||
* --strip-trailing-slashes
|
||||
* remove any trailing slashes from each SOURCE argument
|
||||
* -S, --suffix=SUFFIX
|
||||
* override the usual backup suffix
|
||||
* -t, --target-directory=DIRECTORY
|
||||
* copy all SOURCE arguments into DIRECTORY
|
||||
* -x, --one-file-system
|
||||
* stay on this file system
|
||||
* -Z, --context=CONTEXT
|
||||
* (SELinux) set SELinux security context of copy to CONTEXT
|
||||
*/
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
/* Reverse this bit. If there is -d, bit is not set: */
|
||||
@@ -201,56 +170,49 @@ int cp_main(int argc, char **argv)
|
||||
#endif
|
||||
|
||||
status = EXIT_SUCCESS;
|
||||
if (!(flags & FILEUTILS_TARGET_DIR)) {
|
||||
last = argv[argc - 1];
|
||||
if (argc < 2)
|
||||
bb_show_usage();
|
||||
if (argc != 2) {
|
||||
if (flags & FILEUTILS_NO_TARGET_DIR)
|
||||
bb_show_usage();
|
||||
/* "cp A B C... DIR" - target must be dir */
|
||||
} else /* argc == 2 */ {
|
||||
/* "cp A B" - only case where target can be not a dir */
|
||||
s_flags = cp_mv_stat2(*argv, &source_stat,
|
||||
(flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
|
||||
if (s_flags < 0) /* error other than ENOENT */
|
||||
return EXIT_FAILURE;
|
||||
d_flags = cp_mv_stat(last, &dest_stat);
|
||||
if (d_flags < 0) /* error other than ENOENT */
|
||||
return EXIT_FAILURE;
|
||||
last = argv[argc - 1];
|
||||
/* If there are only two arguments and... */
|
||||
if (argc == 2) {
|
||||
s_flags = cp_mv_stat2(*argv, &source_stat,
|
||||
(flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
|
||||
if (s_flags < 0)
|
||||
return EXIT_FAILURE;
|
||||
d_flags = cp_mv_stat(last, &dest_stat);
|
||||
if (d_flags < 0)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if (flags & FILEUTILS_NO_TARGET_DIR) { /* -T */
|
||||
if (!(s_flags & 2) && (d_flags & 2))
|
||||
/* cp -T NOTDIR DIR */
|
||||
bb_error_msg_and_die("'%s' is a directory", last);
|
||||
}
|
||||
if (flags & FILEUTILS_NO_TARGET_DIR) { /* -T */
|
||||
if (!(s_flags & 2) && (d_flags & 2))
|
||||
/* cp -T NOTDIR DIR */
|
||||
bb_error_msg_and_die("'%s' is a directory", last);
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_CP_LONG_OPTIONS
|
||||
//bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
|
||||
// flags, FILEUTILS_RMDEST, OPT_parents);
|
||||
if (flags & OPT_parents) {
|
||||
if (!(d_flags & 2)) {
|
||||
bb_simple_error_msg_and_die("with --parents, the destination must be a directory");
|
||||
}
|
||||
}
|
||||
if (flags & FILEUTILS_RMDEST) {
|
||||
flags |= FILEUTILS_FORCE;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ...if neither is a directory... */
|
||||
if (!((s_flags | d_flags) & 2)
|
||||
/* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
|
||||
|| ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
|
||||
|| (flags & FILEUTILS_NO_TARGET_DIR)
|
||||
) {
|
||||
/* Do a simple copy */
|
||||
dest = last;
|
||||
goto DO_COPY; /* NB: argc==2 -> *++argv==last */
|
||||
//bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
|
||||
// flags, FILEUTILS_RMDEST, OPT_parents);
|
||||
if (flags & OPT_parents) {
|
||||
if (!(d_flags & 2)) {
|
||||
bb_error_msg_and_die("with --parents, the destination must be a directory");
|
||||
}
|
||||
}
|
||||
if (flags & FILEUTILS_RMDEST) {
|
||||
flags |= FILEUTILS_FORCE;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ...if neither is a directory... */
|
||||
if (!((s_flags | d_flags) & 2)
|
||||
/* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
|
||||
|| ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
|
||||
|| (flags & FILEUTILS_NO_TARGET_DIR)
|
||||
) {
|
||||
/* Do a simple copy */
|
||||
dest = last;
|
||||
goto DO_COPY; /* NB: argc==2 -> *++argv==last */
|
||||
}
|
||||
} else if (flags & FILEUTILS_NO_TARGET_DIR) {
|
||||
bb_error_msg_and_die("too many arguments");
|
||||
}
|
||||
/* else: last is DIR from "-t DIR" */
|
||||
|
||||
while (1) {
|
||||
#if ENABLE_FEATURE_CP_LONG_OPTIONS
|
||||
@@ -272,7 +234,7 @@ int cp_main(int argc, char **argv)
|
||||
if (copy_file(*argv, dest, flags) < 0) {
|
||||
status = EXIT_FAILURE;
|
||||
}
|
||||
if (!*++argv || *argv == last) {
|
||||
if (*++argv == last) {
|
||||
/* possibly leaking dest... */
|
||||
break;
|
||||
}
|
||||
|
||||
+99
-113
@@ -9,18 +9,11 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CUT
|
||||
//config: bool "cut (5.8 kb)"
|
||||
//config: bool "cut (5.3 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: cut is used to print selected parts of lines from
|
||||
//config: each file to stdout.
|
||||
//config:
|
||||
//config:config FEATURE_CUT_REGEX
|
||||
//config: bool "cut -F"
|
||||
//config: default y
|
||||
//config: depends on CUT
|
||||
//config: help
|
||||
//config: Allow regex based delimiters.
|
||||
|
||||
//applet:IF_CUT(APPLET_NOEXEC(cut, cut, BB_DIR_USR_BIN, BB_SUID_DROP, cut))
|
||||
|
||||
@@ -29,19 +22,13 @@
|
||||
//usage:#define cut_trivial_usage
|
||||
//usage: "[OPTIONS] [FILE]..."
|
||||
//usage:#define cut_full_usage "\n\n"
|
||||
//usage: "Print selected fields from FILEs to stdout\n"
|
||||
//usage: "Print selected fields from each input FILE to stdout\n"
|
||||
//usage: "\n -b LIST Output only bytes from LIST"
|
||||
//usage: "\n -c LIST Output only characters from LIST"
|
||||
//usage: "\n -d SEP Field delimiter for input (default -f TAB, -F run of whitespace)"
|
||||
//usage: "\n -O SEP Field delimeter for output (default = -d for -f, one space for -F)"
|
||||
//usage: "\n -D Don't sort/collate sections or match -fF lines without delimeter"
|
||||
//usage: "\n -f LIST Print only these fields (-d is single char)"
|
||||
//usage: IF_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -F LIST Print only these fields (-d is regex)"
|
||||
//usage: )
|
||||
//usage: "\n -s Output only lines containing delimiter"
|
||||
//usage: "\n -d CHAR Use CHAR instead of tab as the field delimiter"
|
||||
//usage: "\n -s Output only the lines containing delimiter"
|
||||
//usage: "\n -f N Print only these fields"
|
||||
//usage: "\n -n Ignored"
|
||||
//(manpage:-n with -b: don't split multibyte characters)
|
||||
//usage:
|
||||
//usage:#define cut_example_usage
|
||||
//usage: "$ echo \"Hello world\" | cut -f 1 -d ' '\n"
|
||||
@@ -51,49 +38,38 @@
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
#include "xregex.h"
|
||||
#else
|
||||
#define regex_t int
|
||||
typedef struct { int rm_eo, rm_so; } regmatch_t;
|
||||
#define xregcomp(x, ...) *(x) = 0
|
||||
#define regexec(...) 0
|
||||
#endif
|
||||
|
||||
/* This is a NOEXEC applet. Be very careful! */
|
||||
|
||||
|
||||
/* option vars */
|
||||
#define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
|
||||
#define OPT_STR "b:c:f:d:sn"
|
||||
#define CUT_OPT_BYTE_FLGS (1 << 0)
|
||||
#define CUT_OPT_CHAR_FLGS (1 << 1)
|
||||
#define CUT_OPT_FIELDS_FLGS (1 << 2)
|
||||
#define CUT_OPT_DELIM_FLGS (1 << 3)
|
||||
#define CUT_OPT_ODELIM_FLGS (1 << 4)
|
||||
#define CUT_OPT_SUPPRESS_FLGS (1 << 5)
|
||||
#define CUT_OPT_NOSORT_FLGS (1 << 6)
|
||||
#define CUT_OPT_REGEX_FLGS ((1 << 7) * ENABLE_FEATURE_CUT_REGEX)
|
||||
#define CUT_OPT_SUPPRESS_FLGS (1 << 4)
|
||||
|
||||
struct cut_list {
|
||||
int startpos;
|
||||
int endpos;
|
||||
};
|
||||
|
||||
enum {
|
||||
BOL = 0,
|
||||
EOL = INT_MAX,
|
||||
NON_RANGE = -1
|
||||
};
|
||||
|
||||
static int cmpfunc(const void *a, const void *b)
|
||||
{
|
||||
return (((struct cut_list *) a)->startpos -
|
||||
((struct cut_list *) b)->startpos);
|
||||
}
|
||||
|
||||
static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||
const struct cut_list *cut_lists, unsigned nlists)
|
||||
static void cut_file(FILE *file, char delim, const struct cut_list *cut_lists, unsigned nlists)
|
||||
{
|
||||
char *line;
|
||||
unsigned linenum = 0; /* keep these zero-based to be consistent */
|
||||
regex_t reg;
|
||||
int spos, shoe = option_mask32 & CUT_OPT_REGEX_FLGS;
|
||||
|
||||
if (shoe) xregcomp(®, delim, REG_EXTENDED);
|
||||
|
||||
/* go through every line in the file */
|
||||
while ((line = xmalloc_fgetline(file)) != NULL) {
|
||||
@@ -103,22 +79,29 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||
char *printed = xzalloc(linelen + 1);
|
||||
char *orig_line = line;
|
||||
unsigned cl_pos = 0;
|
||||
int spos;
|
||||
|
||||
/* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
|
||||
if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) {
|
||||
/* print the chars specified in each cut list */
|
||||
for (; cl_pos < nlists; cl_pos++) {
|
||||
for (spos = cut_lists[cl_pos].startpos; spos < linelen;) {
|
||||
spos = cut_lists[cl_pos].startpos;
|
||||
while (spos < linelen) {
|
||||
if (!printed[spos]) {
|
||||
printed[spos] = 'X';
|
||||
putchar(line[spos]);
|
||||
}
|
||||
if (++spos > cut_lists[cl_pos].endpos) {
|
||||
spos++;
|
||||
if (spos > cut_lists[cl_pos].endpos
|
||||
/* NON_RANGE is -1, so if below is true,
|
||||
* the above was true too (spos is >= 0) */
|
||||
/* || cut_lists[cl_pos].endpos == NON_RANGE */
|
||||
) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (*delim == '\n') { /* cut by lines */
|
||||
} else if (delim == '\n') { /* cut by lines */
|
||||
spos = cut_lists[cl_pos].startpos;
|
||||
|
||||
/* get out if we have no more lists to process or if the lines
|
||||
@@ -131,7 +114,9 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||
while (spos < (int)linenum) {
|
||||
spos++;
|
||||
/* go to the next list if we're at the end of this one */
|
||||
if (spos > cut_lists[cl_pos].endpos) {
|
||||
if (spos > cut_lists[cl_pos].endpos
|
||||
|| cut_lists[cl_pos].endpos == NON_RANGE
|
||||
) {
|
||||
cl_pos++;
|
||||
/* get out if there's no more lists to process */
|
||||
if (cl_pos >= nlists)
|
||||
@@ -149,56 +134,55 @@ static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||
puts(line);
|
||||
goto next_line;
|
||||
} else { /* cut by fields */
|
||||
unsigned uu = 0, start = 0, end = 0, out = 0;
|
||||
int dcount = 0;
|
||||
int ndelim = -1; /* zero-based / one-based problem */
|
||||
int nfields_printed = 0;
|
||||
char *field = NULL;
|
||||
char delimiter[2];
|
||||
|
||||
/* Loop through bytes, finding next delimiter */
|
||||
for (;;) {
|
||||
/* End of current range? */
|
||||
if (end == linelen || dcount > cut_lists[cl_pos].endpos) {
|
||||
if (++cl_pos >= nlists) break;
|
||||
if (option_mask32 & CUT_OPT_NOSORT_FLGS)
|
||||
start = dcount = uu = 0;
|
||||
end = 0;
|
||||
}
|
||||
/* End of current line? */
|
||||
if (uu == linelen) {
|
||||
/* If we've seen no delimiters, check -s */
|
||||
if (!cl_pos && !dcount && !shoe) {
|
||||
if (option_mask32 & CUT_OPT_SUPPRESS_FLGS)
|
||||
goto next_line;
|
||||
} else if (dcount<cut_lists[cl_pos].startpos)
|
||||
start = linelen;
|
||||
end = linelen;
|
||||
} else {
|
||||
/* Find next delimiter */
|
||||
if (shoe) {
|
||||
regmatch_t rr = {-1, -1};
|
||||
delimiter[0] = delim;
|
||||
delimiter[1] = 0;
|
||||
|
||||
if (!regexec(®, line+uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) {
|
||||
end = uu + rr.rm_so;
|
||||
uu += rr.rm_eo;
|
||||
} else {
|
||||
uu = linelen;
|
||||
continue;
|
||||
}
|
||||
} else if (line[end = uu++] != *delim)
|
||||
continue;
|
||||
/* does this line contain any delimiters? */
|
||||
if (strchr(line, delim) == NULL) {
|
||||
if (!(option_mask32 & CUT_OPT_SUPPRESS_FLGS))
|
||||
puts(line);
|
||||
goto next_line;
|
||||
}
|
||||
|
||||
/* Got delimiter. Loop if not yet within range. */
|
||||
if (dcount++ < cut_lists[cl_pos].startpos) {
|
||||
start = uu;
|
||||
continue;
|
||||
/* process each list on this line, for as long as we've got
|
||||
* a line to process */
|
||||
for (; cl_pos < nlists && line; cl_pos++) {
|
||||
spos = cut_lists[cl_pos].startpos;
|
||||
do {
|
||||
/* find the field we're looking for */
|
||||
while (line && ndelim < spos) {
|
||||
field = strsep(&line, delimiter);
|
||||
ndelim++;
|
||||
}
|
||||
}
|
||||
if (end != start || !shoe)
|
||||
printf("%s%.*s", out++ ? odelim : "", end-start, line + start);
|
||||
start = uu;
|
||||
if (!dcount)
|
||||
break;
|
||||
|
||||
/* we found it, and it hasn't been printed yet */
|
||||
if (field && ndelim == spos && !printed[ndelim]) {
|
||||
/* if this isn't our first time through, we need to
|
||||
* print the delimiter after the last field that was
|
||||
* printed */
|
||||
if (nfields_printed > 0)
|
||||
putchar(delim);
|
||||
fputs(field, stdout);
|
||||
printed[ndelim] = 'X';
|
||||
nfields_printed++; /* shouldn't overflow.. */
|
||||
}
|
||||
|
||||
spos++;
|
||||
|
||||
/* keep going as long as we have a line to work with,
|
||||
* this is a list, and we're not at the end of that
|
||||
* list */
|
||||
} while (spos <= cut_lists[cl_pos].endpos && line
|
||||
&& cut_lists[cl_pos].endpos != NON_RANGE);
|
||||
}
|
||||
}
|
||||
/* if we printed anything, finish with newline */
|
||||
/* if we printed anything at all, we need to finish it with a
|
||||
* newline cuz we were handed a chomped line */
|
||||
putchar('\n');
|
||||
next_line:
|
||||
linenum++;
|
||||
@@ -213,35 +197,37 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* growable array holding a series of lists */
|
||||
struct cut_list *cut_lists = NULL;
|
||||
unsigned nlists = 0; /* number of elements in above list */
|
||||
char delim = '\t'; /* delimiter, default is tab */
|
||||
char *sopt, *ltok;
|
||||
const char *delim = NULL;
|
||||
const char *odelim = NULL;
|
||||
unsigned opt;
|
||||
|
||||
#define ARG "bcf"IF_FEATURE_CUT_REGEX("F")
|
||||
opt = getopt32(argv, "^"
|
||||
OPT_STR // = "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
|
||||
"\0" "b--"ARG":c--"ARG":f--"ARG IF_FEATURE_CUT_REGEX("F--"ARG),
|
||||
&sopt, &sopt, &sopt, &delim, &odelim IF_FEATURE_CUT_REGEX(, &sopt)
|
||||
OPT_STR
|
||||
"\0" "b--bcf:c--bcf:f--bcf",
|
||||
&sopt, &sopt, &sopt, <ok
|
||||
);
|
||||
if (!delim || !*delim)
|
||||
delim = (opt & CUT_OPT_REGEX_FLGS) ? "[[:space:]]+" : "\t";
|
||||
if (!odelim) odelim = (opt & CUT_OPT_REGEX_FLGS) ? " " : delim;
|
||||
|
||||
// argc -= optind;
|
||||
argv += optind;
|
||||
if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS | CUT_OPT_REGEX_FLGS)))
|
||||
bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
|
||||
if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS)))
|
||||
bb_error_msg_and_die("expected a list of bytes, characters, or fields");
|
||||
|
||||
if (opt & CUT_OPT_DELIM_FLGS) {
|
||||
if (ltok[0] && ltok[1]) { /* more than 1 char? */
|
||||
bb_error_msg_and_die("the delimiter must be a single character");
|
||||
}
|
||||
delim = ltok[0];
|
||||
}
|
||||
|
||||
/* non-field (char or byte) cutting has some special handling */
|
||||
if (!(opt & (CUT_OPT_FIELDS_FLGS|CUT_OPT_REGEX_FLGS))) {
|
||||
if (!(opt & CUT_OPT_FIELDS_FLGS)) {
|
||||
static const char _op_on_field[] ALIGN1 = " only when operating on fields";
|
||||
|
||||
if (opt & CUT_OPT_SUPPRESS_FLGS) {
|
||||
bb_error_msg_and_die
|
||||
("suppressing non-delimited lines makes sense%s", _op_on_field);
|
||||
("suppressing non-delimited lines makes sense%s",
|
||||
_op_on_field);
|
||||
}
|
||||
if (opt & CUT_OPT_DELIM_FLGS) {
|
||||
if (delim != '\t') {
|
||||
bb_error_msg_and_die
|
||||
("a delimiter may be specified%s", _op_on_field);
|
||||
}
|
||||
@@ -266,7 +252,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* get the start pos */
|
||||
ntok = strsep(<ok, "-");
|
||||
if (!ntok[0]) {
|
||||
s = 0;
|
||||
s = BOL;
|
||||
} else {
|
||||
s = xatoi_positive(ntok);
|
||||
/* account for the fact that arrays are zero based, while
|
||||
@@ -277,23 +263,24 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
/* get the end pos */
|
||||
if (ltok == NULL) {
|
||||
e = s;
|
||||
e = NON_RANGE;
|
||||
} else if (!ltok[0]) {
|
||||
e = INT_MAX;
|
||||
e = EOL;
|
||||
} else {
|
||||
e = xatoi_positive(ltok);
|
||||
/* if the user specified and end position of 0,
|
||||
* that means "til the end of the line" */
|
||||
if (!*ltok)
|
||||
e = INT_MAX;
|
||||
else if (e < s)
|
||||
bb_error_msg_and_die("%d<%d", e, s);
|
||||
if (e == 0)
|
||||
e = EOL;
|
||||
e--; /* again, arrays are zero based, lines are 1 based */
|
||||
if (e == s)
|
||||
e = NON_RANGE;
|
||||
}
|
||||
|
||||
/* add the new list */
|
||||
cut_lists = xrealloc_vector(cut_lists, 4, nlists);
|
||||
/* NB: startpos is always >= 0 */
|
||||
/* NB: startpos is always >= 0,
|
||||
* while endpos may be = NON_RANGE (-1) */
|
||||
cut_lists[nlists].startpos = s;
|
||||
cut_lists[nlists].endpos = e;
|
||||
nlists++;
|
||||
@@ -301,13 +288,12 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
/* make sure we got some cut positions out of all that */
|
||||
if (nlists == 0)
|
||||
bb_simple_error_msg_and_die("missing list of positions");
|
||||
bb_error_msg_and_die("missing list of positions");
|
||||
|
||||
/* now that the lists are parsed, we need to sort them to make life
|
||||
* easier on us when it comes time to print the chars / fields / lines
|
||||
*/
|
||||
if (!(opt & CUT_OPT_NOSORT_FLGS))
|
||||
qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc);
|
||||
qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -322,7 +308,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
retval = EXIT_FAILURE;
|
||||
continue;
|
||||
}
|
||||
cut_file(file, delim, odelim, cut_lists, nlists);
|
||||
cut_file(file, delim, cut_lists, nlists);
|
||||
fclose_if_not_stdin(file);
|
||||
} while (*++argv);
|
||||
|
||||
|
||||
+67
-58
@@ -19,7 +19,7 @@
|
||||
much as possible, missed out a lot of bounds checking */
|
||||
|
||||
//config:config DATE
|
||||
//config: bool "date (7 kb)"
|
||||
//config: bool "date (7.1 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: date is used to set the system date or display the
|
||||
@@ -33,10 +33,12 @@
|
||||
//config: Enable option (-I) to output an ISO-8601 compliant
|
||||
//config: date/time string.
|
||||
//config:
|
||||
//config:# defaults to "no": stat's nanosecond field is a bit non-portable
|
||||
//config:config FEATURE_DATE_NANO
|
||||
//config: bool "Support %[num]N nanosecond format specifier"
|
||||
//config: default n # stat's nanosecond field is a bit non-portable
|
||||
//config: default n # syscall(__NR_clock_gettime)
|
||||
//config: depends on DATE
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: Support %[num]N format specifier. Adds ~250 bytes of code.
|
||||
//config:
|
||||
@@ -52,7 +54,7 @@
|
||||
//config: date -s (and other commands like touch -d) use more sensible
|
||||
//config: formats (for one, ISO format YYYY-MM-DD hh:mm:ss.ssssss).
|
||||
//config:
|
||||
//config: With this option off, 'date DATE' and 'date -s DATE' support
|
||||
//config: With this option off, 'date DATE' is 'date -s DATE' support
|
||||
//config: the same format. With it on, 'date DATE' additionally supports
|
||||
//config: MMDDhhmm[[YY]YY][.ss] format.
|
||||
|
||||
@@ -93,25 +95,36 @@
|
||||
*/
|
||||
|
||||
//usage:#define date_trivial_usage
|
||||
//usage: "[OPTIONS] [+FMT] [[-s] TIME]"
|
||||
//usage: "[OPTIONS] [+FMT] [TIME]"
|
||||
//usage:#define date_full_usage "\n\n"
|
||||
//usage: "Display time (using +FMT), or set time\n"
|
||||
//usage: "\n -u Work in UTC (don't convert to local time)"
|
||||
//usage: IF_NOT_LONG_OPTS(
|
||||
//usage: "\n [-s] TIME Set time to TIME"
|
||||
//usage: "\n -d TIME Display TIME, not 'now'"
|
||||
//usage: IF_FEATURE_DATE_ISOFMT(
|
||||
//usage: "\n -D FMT FMT (strptime format) for -s/-d TIME conversion"
|
||||
////////^^^^^^^^^^^^^^^^^^^^^^ busybox invention, not compat
|
||||
//usage: "\n -u Work in UTC (don't convert to local time)"
|
||||
//usage: "\n -R Output RFC-2822 compliant date string"
|
||||
//usage: ) IF_LONG_OPTS(
|
||||
//usage: "\n [-s,--set] TIME Set time to TIME"
|
||||
//usage: "\n -u,--utc Work in UTC (don't convert to local time)"
|
||||
//usage: "\n -R,--rfc-2822 Output RFC-2822 compliant date string"
|
||||
//usage: )
|
||||
//usage: "\n -r FILE Display last modification time of FILE"
|
||||
//usage: "\n -R Output RFC-2822 date"
|
||||
//usage: IF_FEATURE_DATE_ISOFMT(
|
||||
//usage: "\n -I[SPEC] Output ISO-8601 date"
|
||||
//usage: "\n SPEC=date (default), hours, minutes, seconds or ns"
|
||||
//usage: "\n -I[SPEC] Output ISO-8601 compliant date string"
|
||||
//usage: "\n SPEC='date' (default) for date only,"
|
||||
//usage: "\n 'hours', 'minutes', or 'seconds' for date and"
|
||||
//usage: "\n time to the indicated precision"
|
||||
//usage: )
|
||||
//usage: IF_NOT_LONG_OPTS(
|
||||
//usage: "\n -r FILE Display last modification time of FILE"
|
||||
//usage: "\n -d TIME Display TIME, not 'now'"
|
||||
//usage: ) IF_LONG_OPTS(
|
||||
//usage: "\n -r,--reference FILE Display last modification time of FILE"
|
||||
//usage: "\n -d,--date TIME Display TIME, not 'now'"
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_DATE_ISOFMT(
|
||||
//usage: "\n -D FMT Use FMT for -d TIME conversion"
|
||||
//usage: )
|
||||
//usage: "\n"
|
||||
//usage: "\nRecognized TIME formats:"
|
||||
//usage: "\n @seconds_since_1970"
|
||||
//usage: "\n hh:mm[:ss]"
|
||||
//usage: "\n [YYYY.]MM.DD-hh:mm[:ss]"
|
||||
//usage: "\n YYYY-MM-DD hh:mm[:ss]"
|
||||
@@ -136,8 +149,8 @@ enum {
|
||||
OPT_UTC = (1 << 2), /* u */
|
||||
OPT_DATE = (1 << 3), /* d */
|
||||
OPT_REFERENCE = (1 << 4), /* r */
|
||||
OPT_ISO8601 = (1 << 5) * ENABLE_FEATURE_DATE_ISOFMT, /* I */
|
||||
OPT_STR2DT = (1 << 6) * ENABLE_FEATURE_DATE_ISOFMT, /* D */
|
||||
OPT_TIMESPEC = (1 << 5) * ENABLE_FEATURE_DATE_ISOFMT, /* I */
|
||||
OPT_HINT = (1 << 6) * ENABLE_FEATURE_DATE_ISOFMT, /* D */
|
||||
};
|
||||
|
||||
#if ENABLE_LONG_OPTS
|
||||
@@ -159,6 +172,12 @@ static const char date_longopts[] ALIGN1 =
|
||||
* - after xasprintf we use other xfuncs
|
||||
*/
|
||||
|
||||
static void maybe_set_utc(int opt)
|
||||
{
|
||||
if (opt & OPT_UTC)
|
||||
putenv((char*)"TZ=UTC0");
|
||||
}
|
||||
|
||||
int date_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
@@ -166,7 +185,7 @@ int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
struct tm tm_time;
|
||||
char buf_fmt_dt2str[64];
|
||||
unsigned opt;
|
||||
int isofmt = -1;
|
||||
int ifmt = -1;
|
||||
char *date_str;
|
||||
char *fmt_dt2str;
|
||||
char *fmt_str2dt;
|
||||
@@ -185,16 +204,15 @@ int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
);
|
||||
argv += optind;
|
||||
|
||||
if (opt & OPT_UTC)
|
||||
putenv((char*)"TZ=UTC0");
|
||||
maybe_set_utc(opt);
|
||||
|
||||
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & OPT_ISO8601)) {
|
||||
isofmt = 0; /* default is date */
|
||||
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & OPT_TIMESPEC)) {
|
||||
ifmt = 0; /* default is date */
|
||||
if (isofmt_arg) {
|
||||
static const char isoformats[] ALIGN1 =
|
||||
"date\0""hours\0""minutes\0""seconds\0ns\0";
|
||||
isofmt = index_in_substrings(isoformats, isofmt_arg);
|
||||
if (isofmt < 0)
|
||||
"date\0""hours\0""minutes\0""seconds\0"; /* ns? */
|
||||
ifmt = index_in_substrings(isoformats, isofmt_arg);
|
||||
if (ifmt < 0)
|
||||
bb_show_usage();
|
||||
}
|
||||
}
|
||||
@@ -204,7 +222,7 @@ int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
fmt_dt2str = &argv[0][1]; /* skip over the '+' */
|
||||
argv++;
|
||||
}
|
||||
if (!(opt & (OPT_SET | OPT_DATE))) { /* neither -s TIME nor -d TIME? */
|
||||
if (!(opt & (OPT_SET | OPT_DATE))) {
|
||||
opt |= OPT_SET;
|
||||
date_str = argv[0]; /* can be NULL */
|
||||
if (date_str) {
|
||||
@@ -254,42 +272,39 @@ int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
#endif
|
||||
} else {
|
||||
#if ENABLE_FEATURE_DATE_NANO
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
/* libc has incredibly messy way of doing this,
|
||||
* typically requiring -lrt. We just skip all this mess */
|
||||
syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts);
|
||||
#else
|
||||
time(&ts.tv_sec);
|
||||
#endif
|
||||
}
|
||||
#if !ENABLE_FEATURE_DATE_NANO
|
||||
ts.tv_nsec = 0;
|
||||
#endif
|
||||
localtime_r(&ts.tv_sec, &tm_time);
|
||||
|
||||
/* If date string is given, update tm_time, and maybe set date */
|
||||
if (date_str != NULL) {
|
||||
int check_dst = 1;
|
||||
/* Zero out fields - take her back to midnight! */
|
||||
tm_time.tm_sec = 0;
|
||||
tm_time.tm_min = 0;
|
||||
tm_time.tm_hour = 0;
|
||||
|
||||
/* Process any date input to UNIX time since 1 Jan 1970 */
|
||||
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & OPT_STR2DT)) {
|
||||
if (ENABLE_FEATURE_DATE_ISOFMT && (opt & OPT_HINT)) {
|
||||
if (strptime(date_str, fmt_str2dt, &tm_time) == NULL)
|
||||
bb_error_msg_and_die(bb_msg_invalid_date, date_str);
|
||||
} else {
|
||||
check_dst = parse_datestr(date_str, &tm_time);
|
||||
parse_datestr(date_str, &tm_time);
|
||||
}
|
||||
|
||||
/* Correct any day of week and day of year etc. fields */
|
||||
/* Be sure to recheck dst (but not if date is UTC) */
|
||||
if (check_dst)
|
||||
/* Be sure to recheck dst (but not if date is time_t format) */
|
||||
if (date_str[0] != '@')
|
||||
tm_time.tm_isdst = -1;
|
||||
ts.tv_sec = validate_tm_time(date_str, &tm_time);
|
||||
ts.tv_nsec = 0;
|
||||
|
||||
/* if setting time, set it */
|
||||
if ((opt & OPT_SET) && clock_settime(CLOCK_REALTIME, &ts) < 0) {
|
||||
bb_simple_perror_msg("can't set date");
|
||||
if ((opt & OPT_SET) && stime(&ts.tv_sec) < 0) {
|
||||
bb_perror_msg("can't set date");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,34 +314,24 @@ int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (fmt_dt2str == NULL) {
|
||||
int i;
|
||||
fmt_dt2str = buf_fmt_dt2str;
|
||||
if (ENABLE_FEATURE_DATE_ISOFMT && isofmt >= 0) {
|
||||
/* -I[SPEC]: 0:date 1:hours 2:minutes 3:seconds 4:ns*/
|
||||
if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) {
|
||||
/* -I[SPEC]: 0:date 1:hours 2:minutes 3:seconds */
|
||||
strcpy(fmt_dt2str, "%Y-%m-%dT%H:%M:%S");
|
||||
i = 8 + 3 * isofmt;
|
||||
if (isofmt != 0) {
|
||||
int n;
|
||||
if (isofmt == 4) {
|
||||
i -= 3;
|
||||
i += sprintf(&fmt_dt2str[i], ",%09u", (unsigned)ts.tv_nsec);
|
||||
}
|
||||
/* %z prints "+hhmm" timezone, but coreutils-8.30 prints "+hh:mm"! */
|
||||
/* ...therefore this atrocity: */
|
||||
n = strftime(&fmt_dt2str[i], 8, "%z", &tm_time);
|
||||
i += n;
|
||||
if (n == 5 && (fmt_dt2str[i-5] == '+' || fmt_dt2str[i-5] == '-')) {
|
||||
/* "mm" -> ":mm" */
|
||||
fmt_dt2str[i ] = fmt_dt2str[i - 1];
|
||||
fmt_dt2str[i - 1] = fmt_dt2str[i - 2];
|
||||
fmt_dt2str[i - 2] = ':';
|
||||
i++;
|
||||
}
|
||||
i = 8 + 3 * ifmt;
|
||||
if (ifmt != 0) {
|
||||
/* TODO: if (ifmt==4) i += sprintf(&fmt_dt2str[i], ",%09u", nanoseconds); */
|
||||
format_utc:
|
||||
fmt_dt2str[i++] = '%';
|
||||
fmt_dt2str[i++] = (opt & OPT_UTC) ? 'Z' : 'z';
|
||||
}
|
||||
fmt_dt2str[i] = '\0';
|
||||
} else if (opt & OPT_RFC2822) {
|
||||
/* -R. undo busybox.c setlocale */
|
||||
if (ENABLE_LOCALE_SUPPORT)
|
||||
setlocale(LC_TIME, "C");
|
||||
fmt_dt2str = (char*)"%a, %d %b %Y %H:%M:%S %z";
|
||||
strcpy(fmt_dt2str, "%a, %d %b %Y %H:%M:%S ");
|
||||
i = sizeof("%a, %d %b %Y %H:%M:%S ")-1;
|
||||
goto format_utc;
|
||||
} else { /* default case */
|
||||
fmt_dt2str = (char*)"%a %b %e %H:%M:%S %Z %Y";
|
||||
}
|
||||
@@ -378,6 +383,10 @@ int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* With no format string, just print a blank line */
|
||||
date_buf[0] = '\0';
|
||||
} else {
|
||||
/* Handle special conversions */
|
||||
if (is_prefixed_with(fmt_dt2str, "%f")) {
|
||||
fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S";
|
||||
}
|
||||
/* Generate output string */
|
||||
strftime(date_buf, COMMON_BUFSIZE, fmt_dt2str, &tm_time);
|
||||
}
|
||||
|
||||
+50
-156
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config DD
|
||||
//config: bool "dd (7.5 kb)"
|
||||
//config: bool "dd (7.1 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: dd copies a file (from standard input to standard output,
|
||||
@@ -37,7 +37,7 @@
|
||||
//config: elapsed time and speed.
|
||||
//config:
|
||||
//config:config FEATURE_DD_IBS_OBS
|
||||
//config: bool "Enable ibs, obs, iflag, oflag and conv options"
|
||||
//config: bool "Enable ibs, obs and conv options"
|
||||
//config: default y
|
||||
//config: depends on DD
|
||||
//config: help
|
||||
@@ -56,11 +56,8 @@
|
||||
//kbuild:lib-$(CONFIG_DD) += dd.o
|
||||
|
||||
//usage:#define dd_trivial_usage
|
||||
//usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]"
|
||||
//usage: IF_FEATURE_DD_IBS_OBS("\n"
|
||||
//usage: " [conv=notrunc|noerror|sync|fsync]\n"
|
||||
//usage: " [iflag=skip_bytes|count_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]"
|
||||
//usage: )
|
||||
//usage: "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n"
|
||||
//usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync] [iflag=skip_bytes]")
|
||||
//usage:#define dd_full_usage "\n\n"
|
||||
//usage: "Copy a file with converting and formatting\n"
|
||||
//usage: "\n if=FILE Read from FILE instead of stdin"
|
||||
@@ -82,12 +79,6 @@
|
||||
//usage: "\n conv=fsync Physically write data out before finishing"
|
||||
//usage: "\n conv=swab Swap every pair of bytes"
|
||||
//usage: "\n iflag=skip_bytes skip=N is in bytes"
|
||||
//usage: "\n iflag=count_bytes count=N is in bytes"
|
||||
//usage: "\n oflag=seek_bytes seek=N is in bytes"
|
||||
//usage: "\n iflag=direct O_DIRECT input"
|
||||
//usage: "\n oflag=direct O_DIRECT output"
|
||||
//usage: "\n iflag=fullblock Read full blocks"
|
||||
//usage: "\n oflag=append Open output in append mode"
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_DD_STATUS(
|
||||
//usage: "\n status=noxfer Suppress rate output"
|
||||
@@ -137,22 +128,13 @@ enum {
|
||||
FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
/* end of conv flags */
|
||||
/* start of input flags */
|
||||
FLAG_IFLAG_SHIFT = 5,
|
||||
FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_COUNT_BYTES = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_FULLBLOCK = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_IDIRECT = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_IFLAG_SHIFT = 5,
|
||||
FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
/* end of input flags */
|
||||
/* start of output flags */
|
||||
FLAG_OFLAG_SHIFT = 9,
|
||||
FLAG_SEEK_BYTES = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_APPEND = (1 << 10) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_ODIRECT = (1 << 11) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
/* end of output flags */
|
||||
FLAG_TWOBUFS = (1 << 12) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_COUNT = 1 << 13,
|
||||
FLAG_STATUS_NONE = 1 << 14,
|
||||
FLAG_STATUS_NOXFER = 1 << 15,
|
||||
FLAG_TWOBUFS = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_COUNT = 1 << 7,
|
||||
FLAG_STATUS_NONE = 1 << 8,
|
||||
FLAG_STATUS_NOXFER = 1 << 9,
|
||||
};
|
||||
|
||||
static void dd_output_status(int UNUSED_PARAM cur_signal)
|
||||
@@ -177,9 +159,8 @@ static void dd_output_status(int UNUSED_PARAM cur_signal)
|
||||
//So far we react to it (we print the stats),
|
||||
//status=none only suppresses final, non-USR1 generated status message.
|
||||
# endif
|
||||
fprintf(stderr, /*G.total_bytes < 1024
|
||||
? "%llu bytes copied, " : */ "%llu bytes (%sB) copied, "
|
||||
, G.total_bytes,
|
||||
fprintf(stderr, "%llu bytes (%sB) copied, ",
|
||||
G.total_bytes,
|
||||
/* show fractional digit, use suffixes */
|
||||
make_human_readable_str(G.total_bytes, 1, 0)
|
||||
);
|
||||
@@ -199,71 +180,29 @@ static void dd_output_status(int UNUSED_PARAM cur_signal)
|
||||
#endif
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
static int clear_O_DIRECT(int fd)
|
||||
static ssize_t full_write_or_warn(const void *buf, size_t len,
|
||||
const char *const filename)
|
||||
{
|
||||
if (errno == EINVAL) {
|
||||
int fl = fcntl(fd, F_GETFL);
|
||||
if (fl & O_DIRECT) {
|
||||
fcntl(fd, F_SETFL, fl & ~O_DIRECT);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static ssize_t dd_read(void *ibuf, size_t ibs)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
read_again:
|
||||
if (G.flags & FLAG_FULLBLOCK)
|
||||
n = full_read(ifd, ibuf, ibs);
|
||||
else
|
||||
#endif
|
||||
n = safe_read(ifd, ibuf, ibs);
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (n < 0 && (G.flags & FLAG_IDIRECT) && clear_O_DIRECT(ifd))
|
||||
goto read_again;
|
||||
#endif
|
||||
ssize_t n = full_write(ofd, buf, len);
|
||||
if (n < 0)
|
||||
bb_perror_msg("writing '%s'", filename);
|
||||
return n;
|
||||
}
|
||||
|
||||
static bool write_and_stats(const void *buf, size_t len, size_t obs,
|
||||
const char *filename)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
IF_FEATURE_DD_IBS_OBS(write_again:)
|
||||
n = full_write(ofd, buf, len);
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (n < 0 && (G.flags & FLAG_ODIRECT) && clear_O_DIRECT(ofd))
|
||||
goto write_again;
|
||||
#endif
|
||||
|
||||
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
||||
if (n > 0)
|
||||
G.total_bytes += n;
|
||||
#endif
|
||||
if ((size_t)n == obs) {
|
||||
ssize_t n = full_write_or_warn(buf, len, filename);
|
||||
if (n < 0)
|
||||
return 1;
|
||||
if ((size_t)n == obs)
|
||||
G.out_full++;
|
||||
return 0;
|
||||
}
|
||||
if ((size_t)n == len) {
|
||||
else if (n) /* > 0 */
|
||||
G.out_part++;
|
||||
return 0;
|
||||
}
|
||||
/* n is < len (and possibly is -1).
|
||||
* Even if n >= 0, errno is usually set correctly.
|
||||
* For example, if writing to block device and getting ENOSPC,
|
||||
* full_write() first sees a short write, then tries to write
|
||||
* the remainder and gets errno set to ENOSPC.
|
||||
* It returns n > 0 (the amount which it did write).
|
||||
*/
|
||||
bb_perror_msg("error writing '%s'", filename);
|
||||
return 1;
|
||||
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
||||
G.total_bytes += n;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if ENABLE_LFS
|
||||
@@ -299,30 +238,20 @@ static int parse_comma_flags(char *val, const char *words, const char *error_in)
|
||||
}
|
||||
#endif
|
||||
|
||||
static void *alloc_buf(size_t size)
|
||||
{
|
||||
/* Important for "{i,o}flag=direct" - buffers must be page aligned */
|
||||
if (size >= bb_getpagesize())
|
||||
return xmmap_anon(size);
|
||||
return xmalloc(size);
|
||||
}
|
||||
|
||||
int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
static const char keywords[] ALIGN1 =
|
||||
"bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
"ibs\0""obs\0""conv\0""iflag\0""oflag\0"
|
||||
"ibs\0""obs\0""conv\0""iflag\0"
|
||||
#endif
|
||||
;
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
static const char conv_words[] ALIGN1 =
|
||||
"notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
|
||||
static const char iflag_words[] ALIGN1 =
|
||||
"skip_bytes\0""count_bytes\0""fullblock\0""direct\0";
|
||||
static const char oflag_words[] ALIGN1 =
|
||||
"seek_bytes\0append\0""direct\0";
|
||||
"skip_bytes\0";
|
||||
#endif
|
||||
#if ENABLE_FEATURE_DD_STATUS
|
||||
static const char status_words[] ALIGN1 =
|
||||
@@ -341,7 +270,6 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
OP_obs,
|
||||
OP_conv,
|
||||
OP_iflag,
|
||||
OP_oflag,
|
||||
/* Must be in the same order as FLAG_XXX! */
|
||||
OP_conv_notrunc = 0,
|
||||
OP_conv_sync,
|
||||
@@ -362,11 +290,6 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* Partially implemented: */
|
||||
//swab swap every pair of input bytes: will abort on non-even reads
|
||||
OP_iflag_skip_bytes,
|
||||
OP_iflag_count_bytes,
|
||||
OP_iflag_fullblock,
|
||||
OP_iflag_direct,
|
||||
OP_oflag_seek_bytes,
|
||||
OP_oflag_direct,
|
||||
#endif
|
||||
};
|
||||
smallint exitcode = EXIT_FAILURE;
|
||||
@@ -382,13 +305,13 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
#endif
|
||||
/* These are all zeroed at once! */
|
||||
struct {
|
||||
IF_FEATURE_DD_IBS_OBS(size_t ocount;)
|
||||
size_t oc;
|
||||
ssize_t prev_read_size; /* for detecting swab failure */
|
||||
off_t count;
|
||||
off_t seek, skip;
|
||||
const char *infile, *outfile;
|
||||
} Z;
|
||||
#define ocount (Z.ocount )
|
||||
#define oc (Z.oc )
|
||||
#define prev_read_size (Z.prev_read_size)
|
||||
#define count (Z.count )
|
||||
#define seek (Z.seek )
|
||||
@@ -438,10 +361,6 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
|
||||
/*continue;*/
|
||||
}
|
||||
if (what == OP_oflag) {
|
||||
G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT;
|
||||
/*continue;*/
|
||||
}
|
||||
#endif
|
||||
if (what == OP_bs) {
|
||||
ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
|
||||
@@ -482,12 +401,13 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
#endif
|
||||
} /* end of "for (argv[i])" */
|
||||
|
||||
ibuf = alloc_buf(ibs);
|
||||
//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
|
||||
ibuf = xmalloc(ibs);
|
||||
obuf = ibuf;
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (ibs != obs) {
|
||||
G.flags |= FLAG_TWOBUFS;
|
||||
obuf = alloc_buf(obs);
|
||||
obuf = xmalloc(obs);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -499,12 +419,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
#endif
|
||||
|
||||
if (infile) {
|
||||
int iflag = O_RDONLY;
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (G.flags & FLAG_IDIRECT)
|
||||
iflag |= O_DIRECT;
|
||||
#endif
|
||||
xmove_fd(xopen(infile, iflag), ifd);
|
||||
xmove_fd(xopen(infile, O_RDONLY), ifd);
|
||||
} else {
|
||||
infile = bb_msg_standard_input;
|
||||
}
|
||||
@@ -513,17 +428,11 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
if (!seek && !(G.flags & FLAG_NOTRUNC))
|
||||
oflag |= O_TRUNC;
|
||||
if (G.flags & FLAG_APPEND)
|
||||
oflag |= O_APPEND;
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (G.flags & FLAG_ODIRECT)
|
||||
oflag |= O_DIRECT;
|
||||
#endif
|
||||
|
||||
xmove_fd(xopen(outfile, oflag), ofd);
|
||||
|
||||
if (seek && !(G.flags & FLAG_NOTRUNC)) {
|
||||
size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
|
||||
if (ftruncate(ofd, seek * blocksz) < 0) {
|
||||
if (ftruncate(ofd, seek * obs) < 0) {
|
||||
struct stat st;
|
||||
|
||||
if (fstat(ofd, &st) < 0
|
||||
@@ -541,7 +450,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
|
||||
if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
|
||||
do {
|
||||
ssize_t n = dd_read(ibuf, blocksz);
|
||||
ssize_t n = safe_read(ifd, ibuf, blocksz);
|
||||
if (n < 0)
|
||||
goto die_infile;
|
||||
if (n == 0)
|
||||
@@ -550,22 +459,14 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
}
|
||||
}
|
||||
if (seek) {
|
||||
size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
|
||||
if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0)
|
||||
if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
|
||||
goto die_outfile;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
ssize_t n = ibs;
|
||||
while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
|
||||
ssize_t n;
|
||||
|
||||
if (G.flags & FLAG_COUNT) {
|
||||
if (count == 0)
|
||||
break;
|
||||
if ((G.flags & FLAG_COUNT_BYTES) && count < ibs)
|
||||
n = count;
|
||||
}
|
||||
|
||||
n = dd_read(ibuf, n);
|
||||
n = safe_read(ifd, ibuf, ibs);
|
||||
if (n == 0)
|
||||
break;
|
||||
if (n < 0) {
|
||||
@@ -600,7 +501,6 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
p16++;
|
||||
}
|
||||
}
|
||||
count -= (G.flags & FLAG_COUNT_BYTES) ? n : 1;
|
||||
if ((size_t)n == ibs)
|
||||
G.in_full++;
|
||||
else {
|
||||
@@ -610,26 +510,24 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
n = ibs;
|
||||
}
|
||||
}
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (G.flags & FLAG_TWOBUFS) {
|
||||
char *tmp = ibuf;
|
||||
while (n) {
|
||||
size_t d = obs - ocount;
|
||||
size_t d = obs - oc;
|
||||
|
||||
if (d > (size_t)n)
|
||||
d = n;
|
||||
memcpy(obuf + ocount, tmp, d);
|
||||
memcpy(obuf + oc, tmp, d);
|
||||
n -= d;
|
||||
tmp += d;
|
||||
ocount += d;
|
||||
if (ocount == obs) {
|
||||
oc += d;
|
||||
if (oc == obs) {
|
||||
if (write_and_stats(obuf, obs, obs, outfile))
|
||||
goto out_status;
|
||||
ocount = 0;
|
||||
oc = 0;
|
||||
}
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
} else {
|
||||
if (write_and_stats(ibuf, n, obs, outfile))
|
||||
goto out_status;
|
||||
}
|
||||
@@ -640,12 +538,10 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
goto die_outfile;
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (ocount != 0) {
|
||||
if (write_and_stats(obuf, ocount, obs, outfile))
|
||||
if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
|
||||
if (write_and_stats(obuf, oc, obs, outfile))
|
||||
goto out_status;
|
||||
}
|
||||
#endif
|
||||
if (close(ifd) < 0) {
|
||||
die_infile:
|
||||
bb_simple_perror_msg_and_die(infile);
|
||||
@@ -661,13 +557,11 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
|
||||
dd_output_status(0);
|
||||
|
||||
#if 0 /* can't just free(), they can be mmap()ed */
|
||||
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||
free(obuf);
|
||||
if (G.flags & FLAG_TWOBUFS)
|
||||
free(ibuf);
|
||||
}
|
||||
#endif
|
||||
|
||||
return exitcode;
|
||||
}
|
||||
+35
-74
@@ -18,7 +18,7 @@
|
||||
* Implement -P and -B; better coreutils compat; cleanup
|
||||
*/
|
||||
//config:config DF
|
||||
//config: bool "df (6.8 kb)"
|
||||
//config: bool "df (7.5 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: df reports the amount of disk space used and available
|
||||
@@ -32,26 +32,6 @@
|
||||
//config: -a Show all filesystems
|
||||
//config: -i Inodes
|
||||
//config: -B <SIZE> Blocksize
|
||||
//config:
|
||||
//config:config FEATURE_SKIP_ROOTFS
|
||||
//config: bool "Skip rootfs in mount table"
|
||||
//config: default y
|
||||
//config: depends on DF
|
||||
//config: help
|
||||
//config: Ignore rootfs entry in mount table.
|
||||
//config:
|
||||
//config: In Linux, kernel has a special filesystem, rootfs, which is initially
|
||||
//config: mounted on /. It contains initramfs data, if kernel is configured
|
||||
//config: to have one. Usually, another file system is mounted over / early
|
||||
//config: in boot process, and therefore most tools which manipulate
|
||||
//config: mount table, such as df, will skip rootfs entry.
|
||||
//config:
|
||||
//config: However, some systems do not mount anything on /.
|
||||
//config: If you need to configure busybox for one of these systems,
|
||||
//config: you may find it useful to turn this option off to make df show
|
||||
//config: initramfs statistics.
|
||||
//config:
|
||||
//config: Otherwise, choose Y.
|
||||
|
||||
//applet:IF_DF(APPLET_NOEXEC(df, df, BB_DIR_BIN, BB_SUID_DROP, df))
|
||||
|
||||
@@ -65,7 +45,7 @@
|
||||
//usage: IF_FEATURE_HUMAN_READABLE("mh")
|
||||
//usage: "T"
|
||||
//usage: IF_FEATURE_DF_FANCY("ai] [-B SIZE")
|
||||
//usage: "] [-t TYPE] [FILESYSTEM]..."
|
||||
//usage: "] [FILESYSTEM]..."
|
||||
//usage:#define df_full_usage "\n\n"
|
||||
//usage: "Print filesystem usage statistics\n"
|
||||
//usage: "\n -P POSIX output format"
|
||||
@@ -75,7 +55,6 @@
|
||||
//usage: "\n -h Human readable (e.g. 1K 243M 2G)"
|
||||
//usage: )
|
||||
//usage: "\n -T Print filesystem type"
|
||||
//usage: "\n -t TYPE Print only mounts of this type"
|
||||
//usage: IF_FEATURE_DF_FANCY(
|
||||
//usage: "\n -a Show all filesystems"
|
||||
//usage: "\n -i Inodes"
|
||||
@@ -112,47 +91,40 @@ static unsigned long kscale(unsigned long b, unsigned long bs)
|
||||
int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
unsigned long blocks_used;
|
||||
unsigned blocks_percent_used;
|
||||
unsigned long df_disp_hr = 1024;
|
||||
int status = EXIT_SUCCESS;
|
||||
unsigned opt;
|
||||
FILE *mount_table;
|
||||
struct mntent *mount_entry;
|
||||
struct statvfs s;
|
||||
|
||||
enum {
|
||||
OPT_KILO = (1 << 0),
|
||||
OPT_POSIX = (1 << 1),
|
||||
OPT_FSTYPE = (1 << 2),
|
||||
OPT_t = (1 << 3),
|
||||
OPT_ALL = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
|
||||
OPT_INODE = (1 << 5) * ENABLE_FEATURE_DF_FANCY,
|
||||
OPT_BSIZE = (1 << 6) * ENABLE_FEATURE_DF_FANCY,
|
||||
OPT_HUMAN = (1 << (4 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
|
||||
OPT_HUMANDEC = (1 << (5 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
|
||||
OPT_MEGA = (1 << (6 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
|
||||
OPT_KILO = (1 << 0),
|
||||
OPT_POSIX = (1 << 1),
|
||||
OPT_FSTYPE = (1 << 2),
|
||||
OPT_ALL = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
|
||||
OPT_INODE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
|
||||
OPT_BSIZE = (1 << 5) * ENABLE_FEATURE_DF_FANCY,
|
||||
OPT_HUMAN = (1 << (3 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
|
||||
OPT_MEGA = (1 << (4 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
|
||||
};
|
||||
const char *disp_units_hdr = NULL;
|
||||
char *chp, *opt_t;
|
||||
char *chp;
|
||||
|
||||
init_unicode();
|
||||
|
||||
/* From the manpage of df from coreutils-6.10:
|
||||
* Disk space is shown in 1K blocks by default, unless the environment
|
||||
* variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
|
||||
*/
|
||||
if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
|
||||
df_disp_hr = 512;
|
||||
|
||||
opt = getopt32(argv, "^"
|
||||
"kPTt:"
|
||||
"kPT"
|
||||
IF_FEATURE_DF_FANCY("aiB:")
|
||||
IF_FEATURE_HUMAN_READABLE("hHm")
|
||||
IF_FEATURE_HUMAN_READABLE("hm")
|
||||
"\0"
|
||||
#if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY
|
||||
"k-mB:m-Bk:B-km"
|
||||
#elif ENABLE_FEATURE_HUMAN_READABLE
|
||||
"k-m:m-k"
|
||||
#endif
|
||||
, &opt_t
|
||||
IF_FEATURE_DF_FANCY(, &chp)
|
||||
);
|
||||
if (opt & OPT_MEGA)
|
||||
@@ -172,11 +144,15 @@ int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
got_it: ;
|
||||
}
|
||||
|
||||
if (opt & (OPT_HUMAN|OPT_HUMANDEC)) {
|
||||
/* From the manpage of df from coreutils-6.10:
|
||||
* Disk space is shown in 1K blocks by default, unless the environment
|
||||
* variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
|
||||
*/
|
||||
if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
|
||||
df_disp_hr = 512;
|
||||
|
||||
if (opt & OPT_HUMAN) {
|
||||
df_disp_hr = 0;
|
||||
//TODO: need to add support in make_human_readable_str() for "decimal human readable"
|
||||
//if (opt & OPT_HUMANDEC)
|
||||
// df_disp_hr--;
|
||||
disp_units_hdr = " Size";
|
||||
}
|
||||
if (opt & OPT_INODE)
|
||||
@@ -204,7 +180,7 @@ int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (!argv[0]) {
|
||||
mount_table = setmntent(bb_path_mtab_file, "r");
|
||||
if (!mount_table)
|
||||
bb_simple_perror_msg_and_die(bb_path_mtab_file);
|
||||
bb_perror_msg_and_die(bb_path_mtab_file);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
@@ -232,19 +208,9 @@ int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
}
|
||||
|
||||
device = mount_entry->mnt_fsname;
|
||||
|
||||
/* GNU coreutils 6.10 skips certain mounts, try to be compatible */
|
||||
if (ENABLE_FEATURE_SKIP_ROOTFS && strcmp(device, "rootfs") == 0)
|
||||
continue;
|
||||
|
||||
mount_point = mount_entry->mnt_dir;
|
||||
fs_type = mount_entry->mnt_type;
|
||||
|
||||
if (opt & OPT_t) {
|
||||
if (strcmp(fs_type, opt_t) != 0)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (statvfs(mount_point, &s) != 0) {
|
||||
bb_simple_perror_msg(mount_point);
|
||||
goto set_error;
|
||||
@@ -256,31 +222,26 @@ int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
s.f_frsize = s.f_bsize;
|
||||
|
||||
if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
|
||||
unsigned long long blocks_used;
|
||||
unsigned long long blocks_total;
|
||||
unsigned blocks_percent_used;
|
||||
|
||||
if (opt & OPT_INODE) {
|
||||
s.f_blocks = s.f_files;
|
||||
s.f_bavail = s.f_bfree = s.f_ffree;
|
||||
s.f_frsize = 1;
|
||||
|
||||
if (df_disp_hr)
|
||||
df_disp_hr = 1;
|
||||
}
|
||||
blocks_used = s.f_blocks - s.f_bfree;
|
||||
blocks_total = blocks_used + s.f_bavail;
|
||||
blocks_percent_used = blocks_total; /* 0% if blocks_total == 0, else... */
|
||||
if (blocks_total != 0) {
|
||||
/* Downscale sizes for narrower division */
|
||||
unsigned u;
|
||||
while (blocks_total >= INT_MAX / 101) {
|
||||
blocks_total >>= 1;
|
||||
blocks_used >>= 1;
|
||||
}
|
||||
u = (unsigned)blocks_used * 100u + (unsigned)blocks_total / 2;
|
||||
blocks_percent_used = u / (unsigned)blocks_total;
|
||||
blocks_percent_used = 0;
|
||||
if (blocks_used + s.f_bavail) {
|
||||
blocks_percent_used = (blocks_used * 100ULL
|
||||
+ (blocks_used + s.f_bavail)/2
|
||||
) / (blocks_used + s.f_bavail);
|
||||
}
|
||||
|
||||
/* GNU coreutils 6.10 skips certain mounts, try to be compatible. */
|
||||
if (ENABLE_FEATURE_SKIP_ROOTFS && strcmp(device, "rootfs") == 0)
|
||||
continue;
|
||||
|
||||
#ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
|
||||
if (strcmp(device, "/dev/root") == 0) {
|
||||
/* Adjusts device to be the real root device,
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config DIRNAME
|
||||
//config: bool "dirname (329 bytes)"
|
||||
//config: bool "dirname (289 bytes)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: dirname is used to strip a non-directory suffix from
|
||||
|
||||
@@ -12,14 +12,14 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config DOS2UNIX
|
||||
//config: bool "dos2unix (5.2 kb)"
|
||||
//config: bool "dos2unix (5.1 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: dos2unix is used to convert a text file from DOS format to
|
||||
//config: UNIX format, and vice versa.
|
||||
//config:
|
||||
//config:config UNIX2DOS
|
||||
//config: bool "unix2dos (5.2 kb)"
|
||||
//config: bool "unix2dos (5.1 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: unix2dos is used to convert a text file from UNIX format to
|
||||
|
||||
+19
-26
@@ -19,16 +19,18 @@
|
||||
* 4) Fixed busybox bug #1284 involving long overflow with human_readable.
|
||||
*/
|
||||
//config:config DU
|
||||
//config: bool "du (6.3 kb)"
|
||||
//config: bool "du (default blocksize of 512 bytes)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: du is used to report the amount of disk space used
|
||||
//config: for specified files.
|
||||
//config:
|
||||
//config:config FEATURE_DU_DEFAULT_BLOCKSIZE_1K
|
||||
//config: bool "Use default blocksize of 1024 bytes (else it's 512 bytes)"
|
||||
//config: bool "Use a default blocksize of 1024 bytes (1K)"
|
||||
//config: default y
|
||||
//config: depends on DU
|
||||
//config: help
|
||||
//config: Use a blocksize of (1K) instead of the default 512b.
|
||||
|
||||
//applet:IF_DU(APPLET(du, BB_DIR_USR_BIN, BB_SUID_DROP))
|
||||
|
||||
@@ -40,9 +42,8 @@
|
||||
//usage:#define du_trivial_usage
|
||||
//usage: "[-aHLdclsx" IF_FEATURE_HUMAN_READABLE("hm") "k] [FILE]..."
|
||||
//usage:#define du_full_usage "\n\n"
|
||||
//usage: "Summarize disk space used for FILEs (or directories)\n"
|
||||
//usage: "Summarize disk space used for each FILE and/or directory\n"
|
||||
//usage: "\n -a Show file sizes too"
|
||||
//usage: "\n -b Apparent size (including holes)"
|
||||
//usage: "\n -L Follow all symlinks"
|
||||
//usage: "\n -H Follow symlinks on command line"
|
||||
//usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
|
||||
@@ -85,9 +86,8 @@ enum {
|
||||
OPT_d_maxdepth = (1 << 6),
|
||||
OPT_l_hardlinks = (1 << 7),
|
||||
OPT_c_total = (1 << 8),
|
||||
OPT_b = (1 << 9),
|
||||
OPT_h_for_humans = (1 << 10),
|
||||
OPT_m_mbytes = (1 << 11),
|
||||
OPT_h_for_humans = (1 << 9),
|
||||
OPT_m_mbytes = (1 << 10),
|
||||
};
|
||||
|
||||
struct globals {
|
||||
@@ -111,7 +111,7 @@ static void print(unsigned long long size, const char *filename)
|
||||
/* TODO - May not want to defer error checking here. */
|
||||
#if ENABLE_FEATURE_HUMAN_READABLE
|
||||
# if ENABLE_DESKTOP
|
||||
/* ~30 bytes of code for extra compat:
|
||||
/* ~30 bytes of code for extra comtat:
|
||||
* coreutils' du rounds sizes up:
|
||||
* for example, 1025k file is shown as "2" by du -m.
|
||||
* We round to nearest if human-readable [too hard to fix],
|
||||
@@ -126,16 +126,12 @@ static void print(unsigned long long size, const char *filename)
|
||||
* If G.disp_unit == 0, show one fractional
|
||||
* and use suffixes
|
||||
*/
|
||||
make_human_readable_str(size, (option_mask32 & OPT_b) ? 1 : 512, G.disp_unit),
|
||||
make_human_readable_str(size, 512, G.disp_unit),
|
||||
filename);
|
||||
#else
|
||||
if (G.disp_k) {
|
||||
if (!(option_mask32 & OPT_b)) {
|
||||
size++;
|
||||
size >>= 1;
|
||||
} else {
|
||||
size >>= 10;
|
||||
}
|
||||
size++;
|
||||
size >>= 1;
|
||||
}
|
||||
printf("%llu\t%s\n", size, filename);
|
||||
#endif
|
||||
@@ -161,7 +157,7 @@ static unsigned long long du(const char *filename)
|
||||
}
|
||||
}
|
||||
|
||||
sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
|
||||
sum = statbuf.st_blocks;
|
||||
|
||||
if (S_ISLNK(statbuf.st_mode)) {
|
||||
if (G.slink_depth > G.du_depth) { /* -H or -L */
|
||||
@@ -170,7 +166,7 @@ static unsigned long long du(const char *filename)
|
||||
G.status = EXIT_FAILURE;
|
||||
return 0;
|
||||
}
|
||||
sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
|
||||
sum = statbuf.st_blocks;
|
||||
if (G.slink_depth == 1) {
|
||||
/* Convert -H to -L */
|
||||
G.slink_depth = INT_MAX;
|
||||
@@ -247,14 +243,11 @@ int du_main(int argc UNUSED_PARAM, char **argv)
|
||||
*/
|
||||
#if ENABLE_FEATURE_HUMAN_READABLE
|
||||
opt = getopt32(argv, "^"
|
||||
"aHkLsxd:+lcbhm"
|
||||
"aHkLsxd:+lchm"
|
||||
"\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s",
|
||||
&G.max_print_depth
|
||||
);
|
||||
argv += optind;
|
||||
if (opt & OPT_b) {
|
||||
G.disp_unit = 1;
|
||||
}
|
||||
if (opt & OPT_h_for_humans) {
|
||||
G.disp_unit = 0;
|
||||
}
|
||||
@@ -266,16 +259,16 @@ int du_main(int argc UNUSED_PARAM, char **argv)
|
||||
}
|
||||
#else
|
||||
opt = getopt32(argv, "^"
|
||||
"aHkLsxd:+lcb"
|
||||
"aHkLsxd:+lc"
|
||||
"\0" "H-L:L-H:s-d:d-s",
|
||||
&G.max_print_depth
|
||||
);
|
||||
argv += optind;
|
||||
# if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
|
||||
#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
|
||||
if (opt & OPT_k_kbytes) {
|
||||
G.disp_k = 1;
|
||||
}
|
||||
# endif
|
||||
#endif
|
||||
#endif
|
||||
if (opt & OPT_H_follow_links) {
|
||||
G.slink_depth = 1;
|
||||
@@ -299,11 +292,11 @@ int du_main(int argc UNUSED_PARAM, char **argv)
|
||||
total = 0;
|
||||
do {
|
||||
total += du(*argv);
|
||||
/* otherwise du /dir /dir won't show /dir twice: */
|
||||
reset_ino_dev_hashtable();
|
||||
G.slink_depth = slink_depth_save;
|
||||
} while (*++argv);
|
||||
|
||||
if (ENABLE_FEATURE_CLEAN_UP)
|
||||
reset_ino_dev_hashtable();
|
||||
if (opt & OPT_c_total)
|
||||
print(total, "total");
|
||||
|
||||
|
||||
+11
-11
@@ -19,10 +19,10 @@
|
||||
* The previous version did not allow 4-digit octals.
|
||||
*/
|
||||
//config:config ECHO
|
||||
//config: bool "echo (1.8 kb)"
|
||||
//config: bool "echo (basic SuSv3 version taking no options)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: echo prints a specified string to stdout.
|
||||
//config: echo is used to print a specified string to stdout.
|
||||
//config:
|
||||
//config:# this entry also appears in shell/Config.in, next to the echo builtin
|
||||
//config:config FEATURE_FANCY_ECHO
|
||||
@@ -43,10 +43,10 @@
|
||||
//usage:#define echo_trivial_usage
|
||||
//usage: IF_FEATURE_FANCY_ECHO("[-neE] ") "[ARG]..."
|
||||
//usage:#define echo_full_usage "\n\n"
|
||||
//usage: "Print ARGs to stdout"
|
||||
//usage: "Print the specified ARGs to stdout"
|
||||
//usage: IF_FEATURE_FANCY_ECHO( "\n"
|
||||
//usage: "\n -n No trailing newline"
|
||||
//usage: "\n -e Interpret backslash escapes (\\t=tab etc)"
|
||||
//usage: "\n -n Suppress trailing newline"
|
||||
//usage: "\n -e Interpret backslash escapes (i.e., \\t=tab)"
|
||||
//usage: "\n -E Don't interpret backslash escapes (default)"
|
||||
//usage: )
|
||||
//usage:
|
||||
@@ -87,7 +87,6 @@ int echo_main(int argc UNUSED_PARAM, char **argv)
|
||||
char *out;
|
||||
char *buffer;
|
||||
unsigned buflen;
|
||||
int err;
|
||||
#if !ENABLE_FEATURE_FANCY_ECHO
|
||||
enum {
|
||||
eflag = 0, /* 0 -- disable escape sequences */
|
||||
@@ -186,12 +185,13 @@ int echo_main(int argc UNUSED_PARAM, char **argv)
|
||||
do_write:
|
||||
/* Careful to error out on partial writes too (think ENOSPC!) */
|
||||
errno = 0;
|
||||
err = full_write(STDOUT_FILENO, buffer, out - buffer) != out - buffer;
|
||||
if (err) {
|
||||
bb_simple_perror_msg(bb_msg_write_error);
|
||||
}
|
||||
/*r =*/ full_write(STDOUT_FILENO, buffer, out - buffer);
|
||||
free(buffer);
|
||||
return err;
|
||||
if (/*WRONG:r < 0*/ errno) {
|
||||
bb_perror_msg(bb_msg_write_error);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
+9
-14
@@ -24,7 +24,7 @@
|
||||
* - use xfunc_error_retval
|
||||
*/
|
||||
//config:config ENV
|
||||
//config: bool "env (4 kb)"
|
||||
//config: bool "env (3.8 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: env is used to set an environment variable and run
|
||||
@@ -39,15 +39,12 @@
|
||||
/* http://www.opengroup.org/onlinepubs/007904975/utilities/env.html */
|
||||
|
||||
//usage:#define env_trivial_usage
|
||||
//usage: "[-i0] [-u NAME]... [-] [NAME=VALUE]... [PROG ARGS]"
|
||||
// The "-" can occur only once (unlike, say, -i): it terminates option processing,
|
||||
// so if it is followed by another "-" arg (or any option-looking arg),
|
||||
// that arg will be taken as PROG (or even as NAME=VALUE, example: "-z=QWE").
|
||||
//usage: "[-iu] [-] [name=value]... [PROG ARGS]"
|
||||
//usage:#define env_full_usage "\n\n"
|
||||
//usage: "Print current environment or run PROG after setting up environment\n"
|
||||
//usage: "\n -, -i Start with empty environment"
|
||||
//usage: "\n -0 NUL terminated output"
|
||||
//usage: "\n -u NAME Remove variable from environment"
|
||||
//usage: "Print the current environment or run PROG after setting up\n"
|
||||
//usage: "the specified environment\n"
|
||||
//usage: "\n -, -i Start with an empty environment"
|
||||
//usage: "\n -u Remove variable from the environment"
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
@@ -57,9 +54,8 @@ int env_main(int argc UNUSED_PARAM, char **argv)
|
||||
unsigned opts;
|
||||
llist_t *unset_env = NULL;
|
||||
|
||||
opts = getopt32long(argv, "+i0u:*",
|
||||
opts = getopt32long(argv, "+iu:*",
|
||||
"ignore-environment\0" No_argument "i"
|
||||
"null\0" No_argument "0"
|
||||
"unset\0" Required_argument "u"
|
||||
, &unset_env
|
||||
);
|
||||
@@ -83,7 +79,7 @@ int env_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
while (*argv && (strchr(*argv, '=') != NULL)) {
|
||||
if (putenv(*argv) < 0) {
|
||||
bb_simple_perror_msg_and_die("putenv");
|
||||
bb_perror_msg_and_die("putenv");
|
||||
}
|
||||
++argv;
|
||||
}
|
||||
@@ -94,9 +90,8 @@ int env_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
if (environ) { /* clearenv() may set environ == NULL! */
|
||||
char **ep;
|
||||
opts = (opts & 2) ? 0 : '\n';
|
||||
for (ep = environ; *ep; ep++) {
|
||||
printf("%s%c", *ep, opts);
|
||||
puts(*ep);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+15
-44
@@ -21,13 +21,13 @@
|
||||
* Caveat: this versions of expand and unexpand don't accept tab lists.
|
||||
*/
|
||||
//config:config EXPAND
|
||||
//config: bool "expand (5.1 kb)"
|
||||
//config: bool "expand (5.8 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: By default, convert all tabs to spaces.
|
||||
//config:
|
||||
//config:config UNEXPAND
|
||||
//config: bool "unexpand (5.3 kb)"
|
||||
//config: bool "unexpand (6 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: By default, convert only leading sequences of blanks to tabs.
|
||||
@@ -63,67 +63,38 @@ enum {
|
||||
OPT_ALL = 1 << 2,
|
||||
};
|
||||
|
||||
//FIXME: does not work properly with input containing NULs
|
||||
//coreutils 8.30 preserves NULs but treats them as chars of width zero:
|
||||
//AB<nul><tab>C will expand <tab> to 6 spaces, not 5.
|
||||
|
||||
#if ENABLE_EXPAND
|
||||
static void expand(FILE *file, unsigned tab_size, unsigned opt)
|
||||
{
|
||||
char *line;
|
||||
|
||||
for (;;) {
|
||||
char *line;
|
||||
while ((line = xmalloc_fgets(file)) != NULL) {
|
||||
unsigned char c;
|
||||
char *ptr;
|
||||
char *ptr_strbeg;
|
||||
//commented-out code handles NULs, +90 bytes of code, not tested much
|
||||
// size_t linelen;
|
||||
// unsigned len = 0;
|
||||
|
||||
// linelen = 1024 * 1024;
|
||||
// line = xmalloc_fgets_str_len(file, "\n", &linelen);
|
||||
line = xmalloc_fgets(file); //
|
||||
if (!line)
|
||||
break;
|
||||
ptr = ptr_strbeg = line;
|
||||
for (;;) {
|
||||
unsigned char c = *ptr;
|
||||
if (c == '\0') {
|
||||
// size_t rem = line + linelen - ptr;
|
||||
// if (rem > 0) {
|
||||
//# if ENABLE_UNICODE_SUPPORT
|
||||
// len += unicode_strwidth(ptr_strbeg);
|
||||
//# else
|
||||
// len += ptr - ptr_strbeg;
|
||||
//# endif
|
||||
// printf("%s%c", ptr_strbeg, '\0');
|
||||
// memmove(ptr, ptr + 1, rem + 1);
|
||||
// ptr_strbeg = ptr;
|
||||
// linelen--;
|
||||
// continue;
|
||||
// }
|
||||
break;
|
||||
}
|
||||
while ((c = *ptr) != '\0') {
|
||||
if ((opt & OPT_INITIAL) && !isblank(c)) {
|
||||
/* not space or tab */
|
||||
break;
|
||||
}
|
||||
if (c == '\t') {
|
||||
unsigned len = 0; //
|
||||
unsigned len;
|
||||
*ptr = '\0';
|
||||
# if ENABLE_UNICODE_SUPPORT
|
||||
len += unicode_strwidth(ptr_strbeg);
|
||||
len = unicode_strwidth(ptr_strbeg);
|
||||
# else
|
||||
len += ptr - ptr_strbeg;
|
||||
len = ptr - ptr_strbeg;
|
||||
# endif
|
||||
len = tab_size - (len % tab_size);
|
||||
/*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */
|
||||
printf("%s%*s", ptr_strbeg, len, "");
|
||||
// len = 0;
|
||||
ptr_strbeg = ptr + 1;
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
fputs_stdout(ptr_strbeg);
|
||||
fputs(ptr_strbeg, stdout);
|
||||
free(line);
|
||||
}
|
||||
}
|
||||
@@ -160,7 +131,7 @@ static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
|
||||
putchar('\t');
|
||||
}
|
||||
|
||||
if (!(opt & OPT_ALL) && ptr != line) {
|
||||
if ((opt & OPT_INITIAL) && ptr != line) {
|
||||
printf("%*s%s", len, "", ptr);
|
||||
break;
|
||||
}
|
||||
@@ -207,13 +178,13 @@ int expand_main(int argc UNUSED_PARAM, char **argv)
|
||||
"ft:a"
|
||||
"\0"
|
||||
"ta" /* -t NUM sets -a */,
|
||||
"first-only\0" No_argument "f"
|
||||
"first-only\0" No_argument "i"
|
||||
"tabs\0" Required_argument "t"
|
||||
"all\0" No_argument "a"
|
||||
, &opt_t
|
||||
);
|
||||
/* -t implies -a, but an explicit -f overrides */
|
||||
if (opt & OPT_INITIAL) opt &= ~OPT_ALL;
|
||||
/* -f --first-only is the default */
|
||||
if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
|
||||
}
|
||||
tab_size = xatou_range(opt_t, 1, UINT_MAX);
|
||||
|
||||
@@ -247,7 +218,7 @@ int expand_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* Now close stdin also */
|
||||
/* (if we didn't read from it, it's a no-op) */
|
||||
if (fclose(stdin))
|
||||
bb_simple_perror_msg_and_die(bb_msg_standard_input);
|
||||
bb_perror_msg_and_die(bb_msg_standard_input);
|
||||
|
||||
fflush_stdout_and_exit(exit_status);
|
||||
}
|
||||
+11
-11
@@ -23,7 +23,7 @@
|
||||
* provided they all associate ((x op x) op x).
|
||||
*/
|
||||
//config:config EXPR
|
||||
//config: bool "expr (6.6 kb)"
|
||||
//config: bool "expr (6.1 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: expr is used to calculate numbers and print the result
|
||||
@@ -45,7 +45,7 @@
|
||||
//usage:#define expr_trivial_usage
|
||||
//usage: "EXPRESSION"
|
||||
//usage:#define expr_full_usage "\n\n"
|
||||
//usage: "Print the value of EXPRESSION\n"
|
||||
//usage: "Print the value of EXPRESSION to stdout\n"
|
||||
//usage: "\n"
|
||||
//usage: "EXPRESSION may be:\n"
|
||||
//usage: " ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2\n"
|
||||
@@ -63,7 +63,7 @@
|
||||
//usage: " ARG1 % ARG2\n"
|
||||
//usage: " STRING : REGEXP Anchored pattern match of REGEXP in STRING\n"
|
||||
//usage: " match STRING REGEXP Same as STRING : REGEXP\n"
|
||||
//usage: " substr STRING POS LEN Substring of STRING, POS counts from 1\n"
|
||||
//usage: " substr STRING POS LENGTH Substring of STRING, POS counted from 1\n"
|
||||
//usage: " index STRING CHARS Index in STRING where any CHARS is found, or 0\n"
|
||||
//usage: " length STRING Length of STRING\n"
|
||||
//usage: " quote TOKEN Interpret TOKEN as a string, even if\n"
|
||||
@@ -249,7 +249,7 @@ static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
|
||||
arith_t li, ri;
|
||||
|
||||
if (!toarith(l) || !toarith(r))
|
||||
bb_simple_error_msg_and_die("non-numeric argument");
|
||||
bb_error_msg_and_die("non-numeric argument");
|
||||
li = l->u.i;
|
||||
ri = r->u.i;
|
||||
if (op == '+')
|
||||
@@ -259,7 +259,7 @@ static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
|
||||
if (op == '*')
|
||||
return li * ri;
|
||||
if (ri == 0)
|
||||
bb_simple_error_msg_and_die("division by zero");
|
||||
bb_error_msg_and_die("division by zero");
|
||||
if (op == '/')
|
||||
return li / ri;
|
||||
return li % ri;
|
||||
@@ -319,19 +319,19 @@ static VALUE *eval7(void)
|
||||
VALUE *v;
|
||||
|
||||
if (!*G.args)
|
||||
bb_simple_error_msg_and_die("syntax error");
|
||||
bb_error_msg_and_die("syntax error");
|
||||
|
||||
if (nextarg("(")) {
|
||||
G.args++;
|
||||
v = eval();
|
||||
if (!nextarg(")"))
|
||||
bb_simple_error_msg_and_die("syntax error");
|
||||
bb_error_msg_and_die("syntax error");
|
||||
G.args++;
|
||||
return v;
|
||||
}
|
||||
|
||||
if (nextarg(")"))
|
||||
bb_simple_error_msg_and_die("syntax error");
|
||||
bb_error_msg_and_die("syntax error");
|
||||
|
||||
return str_value(*G.args++);
|
||||
}
|
||||
@@ -353,7 +353,7 @@ static VALUE *eval6(void)
|
||||
G.args++; /* We have a valid token, so get the next argument. */
|
||||
if (key == 1) { /* quote */
|
||||
if (!*G.args)
|
||||
bb_simple_error_msg_and_die("syntax error");
|
||||
bb_error_msg_and_die("syntax error");
|
||||
return str_value(*G.args++);
|
||||
}
|
||||
if (key == 2) { /* length */
|
||||
@@ -546,11 +546,11 @@ int expr_main(int argc UNUSED_PARAM, char **argv)
|
||||
xfunc_error_retval = 2; /* coreutils compat */
|
||||
G.args = argv + 1;
|
||||
if (*G.args == NULL) {
|
||||
bb_simple_error_msg_and_die("too few arguments");
|
||||
bb_error_msg_and_die("too few arguments");
|
||||
}
|
||||
v = eval();
|
||||
if (*G.args)
|
||||
bb_simple_error_msg_and_die("syntax error");
|
||||
bb_error_msg_and_die("syntax error");
|
||||
if (v->type == INTEGER)
|
||||
printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
|
||||
else
|
||||
|
||||
+78
-133
@@ -4,7 +4,7 @@
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config FACTOR
|
||||
//config: bool "factor (2.7 kb)"
|
||||
//config: bool "factor (2.6 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: factor factorizes integers
|
||||
@@ -19,7 +19,6 @@
|
||||
//usage: "Print prime factors"
|
||||
|
||||
#include "libbb.h"
|
||||
#include "common_bufsiz.h"
|
||||
|
||||
#if 0
|
||||
# define dbg(...) bb_error_msg(__VA_ARGS__)
|
||||
@@ -43,116 +42,9 @@ typedef unsigned long half_t;
|
||||
#error Cant find an integer type which is half as wide as ullong
|
||||
#endif
|
||||
|
||||
/* The trial divisor increment wheel. Use it to skip over divisors that
|
||||
* are composites of 2, 3, 5, 7, or 11.
|
||||
* Larger wheels improve sieving only slightly, but quickly grow in size
|
||||
* (adding just one prime, 13, results in 5766 element sieve).
|
||||
*/
|
||||
#define R(a,b,c,d,e,f,g,h,i,j,A,B,C,D,E,F,G,H,I,J) \
|
||||
(((uint64_t)(a<<0) | (b<<3) | (c<<6) | (d<<9) | (e<<12) | (f<<15) | (g<<18) | (h<<21) | (i<<24) | (j<<27)) << 1) | \
|
||||
(((uint64_t)(A<<0) | (B<<3) | (C<<6) | (D<<9) | (E<<12) | (F<<15) | (G<<18) | (H<<21) | (I<<24) | (J<<27)) << 31)
|
||||
#define P(a,b,c,d,e,f,g,h,i,j,A,B,C,D,E,F,G,H,I,J) \
|
||||
R( (a/2),(b/2),(c/2),(d/2),(e/2),(f/2),(g/2),(h/2),(i/2),(j/2), \
|
||||
(A/2),(B/2),(C/2),(D/2),(E/2),(F/2),(G/2),(H/2),(I/2),(J/2) )
|
||||
static const uint64_t packed_wheel[] = {
|
||||
/*1, 2, 2, 4, 2,*/
|
||||
P( 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2, 6, 4, 6, 8, 4), //01
|
||||
P( 2, 4, 2, 4,14, 4, 6, 2,10, 2, 6, 6, 4, 2, 4, 6, 2,10, 2, 4), //02
|
||||
P( 2,12,10, 2, 4, 2, 4, 6, 2, 6, 4, 6, 6, 6, 2, 6, 4, 2, 6, 4), //03
|
||||
P( 6, 8, 4, 2, 4, 6, 8, 6,10, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2), //04
|
||||
P( 6, 4, 2, 6,10, 2,10, 2, 4, 2, 4, 6, 8, 4, 2, 4,12, 2, 6, 4), //05
|
||||
P( 2, 6, 4, 6,12, 2, 4, 2, 4, 8, 6, 4, 6, 2, 4, 6, 2, 6,10, 2), //06
|
||||
P( 4, 6, 2, 6, 4, 2, 4, 2,10, 2,10, 2, 4, 6, 6, 2, 6, 6, 4, 6), //07
|
||||
P( 6, 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 6, 4, 8, 6, 4, 6, 2, 4, 6), //08
|
||||
P( 8, 6, 4, 2,10, 2, 6, 4, 2, 4, 2,10, 2,10, 2, 4, 2, 4, 8, 6), //09
|
||||
P( 4, 2, 4, 6, 6, 2, 6, 4, 8, 4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4), //10
|
||||
P( 6, 6, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2,10, 2,10, 2), //11
|
||||
P( 6, 4, 6, 2, 6, 4, 2, 4, 6, 6, 8, 4, 2, 6,10, 8, 4, 2, 4, 2), //12
|
||||
P( 4, 8,10, 6, 2, 4, 8, 6, 6, 4, 2, 4, 6, 2, 6, 4, 6, 2,10, 2), //13
|
||||
P(10, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 6, 6, 4, 6, 8), //14
|
||||
P( 4, 2, 4, 2, 4, 8, 6, 4, 8, 4, 6, 2, 6, 6, 4, 2, 4, 6, 8, 4), //15
|
||||
P( 2, 4, 2,10, 2,10, 2, 4, 2, 4, 6, 2,10, 2, 4, 6, 8, 6, 4, 2), //16
|
||||
P( 6, 4, 6, 8, 4, 6, 2, 4, 8, 6, 4, 6, 2, 4, 6, 2, 6, 6, 4, 6), //17
|
||||
P( 6, 2, 6, 6, 4, 2,10, 2,10, 2, 4, 2, 4, 6, 2, 6, 4, 2,10, 6), //18
|
||||
P( 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 4, 2,12, 6, 4, 6, 2, 4, 6, 2), //19
|
||||
P(12, 4, 2, 4, 8, 6, 4, 2, 4, 2,10, 2,10, 6, 2, 4, 6, 2, 6, 4), //20
|
||||
P( 2, 4, 6, 6, 2, 6, 4, 2,10, 6, 8, 6, 4, 2, 4, 8, 6, 4, 6, 2), //21
|
||||
P( 4, 6, 2, 6, 6, 6, 4, 6, 2, 6, 4, 2, 4, 2,10,12, 2, 4, 2,10), //22
|
||||
P( 2, 6, 4, 2, 4, 6, 6, 2,10, 2, 6, 4,14, 4, 2, 4, 2, 4, 8, 6), //23
|
||||
P( 4, 6, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4,12, 2,12), //24
|
||||
};
|
||||
#undef P
|
||||
#undef R
|
||||
#define WHEEL_START 5
|
||||
#define WHEEL_SIZE (5 + 24 * 20)
|
||||
#define square_count (((uint8_t*)&bb_common_bufsiz1)[0])
|
||||
#define wheel_tab (((uint8_t*)&bb_common_bufsiz1) + 1)
|
||||
/*
|
||||
* Why, you ask?
|
||||
* plain byte array:
|
||||
* function old new delta
|
||||
* wheel_tab - 485 +485
|
||||
* 3-bit-packed insanity:
|
||||
* packed_wheel - 192 +192
|
||||
* factor_main 108 171 +63
|
||||
*/
|
||||
static void unpack_wheel(void)
|
||||
{
|
||||
int i;
|
||||
uint8_t *p;
|
||||
|
||||
setup_common_bufsiz();
|
||||
wheel_tab[0] = 1;
|
||||
wheel_tab[1] = 2;
|
||||
wheel_tab[2] = 2;
|
||||
wheel_tab[3] = 4;
|
||||
wheel_tab[4] = 2;
|
||||
p = &wheel_tab[5];
|
||||
for (i = 0; i < ARRAY_SIZE(packed_wheel); i++) {
|
||||
uint64_t v = packed_wheel[i];
|
||||
while ((v & 0xe) != 0) {
|
||||
*p = v & 0xe;
|
||||
//printf("%2u,", *p);
|
||||
p++;
|
||||
v >>= 3;
|
||||
}
|
||||
//printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* Prevent inlining, factorize() needs all help it can get with reducing register pressure */
|
||||
static NOINLINE void print_w(wide_t n)
|
||||
{
|
||||
unsigned rep = square_count;
|
||||
do
|
||||
printf(" %llu", n);
|
||||
while (--rep != 0);
|
||||
}
|
||||
static NOINLINE void print_h(half_t n)
|
||||
{
|
||||
print_w(n);
|
||||
}
|
||||
|
||||
static void factorize(wide_t N);
|
||||
|
||||
static half_t isqrt_odd(wide_t N)
|
||||
{
|
||||
half_t s = isqrt(N);
|
||||
/* s^2 is <= N, (s+1)^2 > N */
|
||||
|
||||
/* If s^2 in fact is EQUAL to N, it's very lucky.
|
||||
* Examples:
|
||||
* factor 18446743988964486098 = 2 * 3037000493 * 3037000493
|
||||
* factor 18446743902517389507 = 3 * 2479700513 * 2479700513
|
||||
*/
|
||||
if ((wide_t)s * s == N) {
|
||||
/* factorize sqrt(N), printing each factor twice */
|
||||
square_count *= 2;
|
||||
factorize(s);
|
||||
/* Let caller know we recursed */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Subtract 1 from even s, odd s won't change: */
|
||||
/* (doesnt work for zero, but we know that s != 0 here) */
|
||||
s = (s - 1) | 1;
|
||||
@@ -161,20 +53,43 @@ static half_t isqrt_odd(wide_t N)
|
||||
|
||||
static NOINLINE void factorize(wide_t N)
|
||||
{
|
||||
unsigned w;
|
||||
half_t factor;
|
||||
half_t max_factor;
|
||||
// unsigned count3;
|
||||
// unsigned count5;
|
||||
// unsigned count7;
|
||||
// ^^^^^^^^^^^^^^^ commented-out simple sieving code (easier to grasp).
|
||||
// Faster sieving, using one word for potentially up to 6 counters:
|
||||
// count upwards in each mask, counter "triggers" when it sets its mask to "100[0]..."
|
||||
// 10987654321098765432109876543210 - bits 31-0 in 32-bit word
|
||||
// 17777713333311111777775555333 - bit masks for counters for primes 3,5,7,11,13,17
|
||||
// 100000100001000010001001 - value for adding 1 to each mask
|
||||
// 10000010000010000100001000100 - value for checking that any mask reached msb
|
||||
enum {
|
||||
SHIFT_3 = 1 << 0,
|
||||
SHIFT_5 = 1 << 3,
|
||||
SHIFT_7 = 1 << 7,
|
||||
INCREMENT_EACH = SHIFT_3 | SHIFT_5 | SHIFT_7,
|
||||
MULTIPLE_OF_3 = 1 << 2,
|
||||
MULTIPLE_OF_5 = 1 << 6,
|
||||
MULTIPLE_OF_7 = 1 << 11,
|
||||
MULTIPLE_DETECTED = MULTIPLE_OF_3 | MULTIPLE_OF_5 | MULTIPLE_OF_7,
|
||||
};
|
||||
unsigned sieve_word;
|
||||
|
||||
if (N < 4)
|
||||
goto end;
|
||||
|
||||
while (!(N & 1)) {
|
||||
printf(" 2");
|
||||
N >>= 1;
|
||||
}
|
||||
|
||||
/* The code needs to be optimized for the case where
|
||||
* there are large prime factors. For example,
|
||||
* this is not hard:
|
||||
* 8262075252869367027 = 3 7 17 23 47 101 113 127 131 137 823
|
||||
* (the largest divisor to test for largest factor 823
|
||||
* is only ~sqrt(823) = 28, the entire factorization needs
|
||||
* only ~33 trial divisions)
|
||||
* (the largest factor to test is only ~sqrt(823) = 28)
|
||||
* but this is:
|
||||
* 18446744073709551601 = 53 348051774975651917
|
||||
* the last factor requires testing up to
|
||||
@@ -183,37 +98,70 @@ static NOINLINE void factorize(wide_t N)
|
||||
* factor 18446744073709551557 (0xffffffffffffffc5).
|
||||
*/
|
||||
max_factor = isqrt_odd(N);
|
||||
if (!max_factor)
|
||||
return; /* square was detected and recursively factored */
|
||||
factor = 2;
|
||||
w = 0;
|
||||
// count3 = 3;
|
||||
// count5 = 6;
|
||||
// count7 = 9;
|
||||
sieve_word = 0
|
||||
/* initial count for SHIFT_n is (n-1)/2*3: */
|
||||
+ (MULTIPLE_OF_3 - 3 * SHIFT_3)
|
||||
+ (MULTIPLE_OF_5 - 6 * SHIFT_5)
|
||||
+ (MULTIPLE_OF_7 - 9 * SHIFT_7)
|
||||
//+ (MULTIPLE_OF_11 - 15 * SHIFT_11)
|
||||
//+ (MULTIPLE_OF_13 - 18 * SHIFT_13)
|
||||
//+ (MULTIPLE_OF_17 - 24 * SHIFT_17)
|
||||
;
|
||||
factor = 3;
|
||||
for (;;) {
|
||||
half_t fw;
|
||||
|
||||
/* The division is the most costly part of the loop.
|
||||
* On 64bit CPUs, takes at best 12 cycles, often ~20.
|
||||
*/
|
||||
while ((N % factor) == 0) { /* not likely */
|
||||
N = N / factor;
|
||||
print_h(factor);
|
||||
printf(" %"HALF_FMT"u", factor);
|
||||
max_factor = isqrt_odd(N);
|
||||
if (!max_factor)
|
||||
return; /* square was detected */
|
||||
}
|
||||
next_factor:
|
||||
if (factor >= max_factor)
|
||||
break;
|
||||
fw = factor + wheel_tab[w];
|
||||
if (fw < factor)
|
||||
break; /* overflow */
|
||||
factor = fw;
|
||||
w++;
|
||||
if (w < WHEEL_SIZE)
|
||||
factor += 2;
|
||||
/* Rudimentary wheel sieving: skip multiples of 3, 5 and 7:
|
||||
* Every third odd number is divisible by three and thus isn't a prime:
|
||||
* 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47...
|
||||
* ^ ^ ^ ^ ^ ^ ^ _ ^ ^ _ ^ ^ ^ ^
|
||||
* (^ = primes, _ = would-be-primes-if-not-divisible-by-5)
|
||||
* The numbers with space under them are excluded by sieve 3.
|
||||
*/
|
||||
// count7--;
|
||||
// count5--;
|
||||
// count3--;
|
||||
// if (count3 && count5 && count7)
|
||||
// continue;
|
||||
sieve_word += INCREMENT_EACH;
|
||||
if (!(sieve_word & MULTIPLE_DETECTED))
|
||||
continue;
|
||||
w = WHEEL_START;
|
||||
/*
|
||||
* "factor" is multiple of 3 33% of the time (count3 reached 0),
|
||||
* else, multiple of 5 13% of the time,
|
||||
* else, multiple of 7 7.6% of the time.
|
||||
* Cumulatively, with 3,5,7 sieving we are here 54.3% of the time.
|
||||
*/
|
||||
// if (count3 == 0)
|
||||
// count3 = 3;
|
||||
if (sieve_word & MULTIPLE_OF_3)
|
||||
sieve_word -= SHIFT_3 * 3;
|
||||
// if (count5 == 0)
|
||||
// count5 = 5;
|
||||
if (sieve_word & MULTIPLE_OF_5)
|
||||
sieve_word -= SHIFT_5 * 5;
|
||||
// if (count7 == 0)
|
||||
// count7 = 7;
|
||||
if (sieve_word & MULTIPLE_OF_7)
|
||||
sieve_word -= SHIFT_7 * 7;
|
||||
goto next_factor;
|
||||
}
|
||||
end:
|
||||
if (N > 1)
|
||||
print_w(N);
|
||||
printf(" %llu", N);
|
||||
bb_putchar('\n');
|
||||
}
|
||||
|
||||
@@ -228,15 +176,12 @@ static void factorize_numstr(const char *numstr)
|
||||
if (errno)
|
||||
bb_show_usage();
|
||||
printf("%llu:", N);
|
||||
square_count = 1;
|
||||
factorize(N);
|
||||
}
|
||||
|
||||
int factor_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int factor_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
unpack_wheel();
|
||||
|
||||
//// coreutils has undocumented option ---debug (three dashes)
|
||||
//getopt32(argv, "");
|
||||
//argv += optind;
|
||||
|
||||
Loaded 100 of 1248 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user