Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbd3b883a8 | ||
|
|
30ffa2bb15 | ||
|
|
d811a8cd01 | ||
|
|
71afe6c513 | ||
|
|
0f668aad7f | ||
|
|
06f7cf1487 | ||
|
|
58cbf385e0 | ||
|
|
e7629ffbf5 | ||
|
|
e4c7cf3f8c | ||
|
|
4ac4f60942 | ||
|
|
94f6d1ece0 |
No files matched your search
@@ -181,6 +181,3 @@ Jie Zhang <jie.zhang@analog.com>
|
||||
|
||||
Maxime Coste <mawww@kakoune.org>
|
||||
paste implementation
|
||||
|
||||
Roger Knecht <rknecht@pm.me>
|
||||
tree
|
||||
@@ -108,17 +108,6 @@ config LFS
|
||||
programs that can benefit from large file support include dd, gzip,
|
||||
cp, mount, tar.
|
||||
|
||||
config TIME64
|
||||
bool "Support 64bit wide time types"
|
||||
default y
|
||||
depends on LFS
|
||||
help
|
||||
Make times later than 2038 representable for several libc syscalls
|
||||
(stat, clk_gettime etc.). Note this switch is specific to glibc
|
||||
and has no effect on platforms that already use 64bit wide time types
|
||||
(i.e. all 64bit archs and some selected 32bit archs (currently riscv
|
||||
and x32)).
|
||||
|
||||
config PAM
|
||||
bool "Support PAM (Pluggable Authentication Modules)"
|
||||
default n
|
||||
@@ -204,13 +193,6 @@ config FEATURE_INSTALLER
|
||||
busybox at runtime to create hard links or symlinks for all the
|
||||
applets that are compiled into busybox.
|
||||
|
||||
config FEATURE_VERSION
|
||||
bool "Support --version"
|
||||
default y
|
||||
depends on BUSYBOX
|
||||
help
|
||||
Enable 'busybox --version' support.
|
||||
|
||||
config INSTALL_NO_USR
|
||||
bool "Don't use /usr"
|
||||
default n
|
||||
@@ -330,6 +312,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.
|
||||
@@ -375,6 +358,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
|
||||
@@ -547,14 +541,6 @@ config STACK_OPTIMIZATION_386
|
||||
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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
VERSION = 1
|
||||
PATCHLEVEL = 39
|
||||
SUBLEVEL = 0
|
||||
EXTRAVERSION = .git
|
||||
PATCHLEVEL = 32
|
||||
SUBLEVEL = 1
|
||||
EXTRAVERSION =
|
||||
NAME = Unnamed
|
||||
|
||||
# *DOCUMENTATION*
|
||||
@@ -476,7 +476,6 @@ libs-y := \
|
||||
findutils/ \
|
||||
init/ \
|
||||
libbb/ \
|
||||
libbb/yescrypt/ \
|
||||
libpwdgrp/ \
|
||||
loginutils/ \
|
||||
mailutils/ \
|
||||
@@ -613,12 +612,7 @@ quiet_cmd_busybox__ ?= LINK $@
|
||||
"$(core-y)" \
|
||||
"$(libs-y)" \
|
||||
"$(LDLIBS)" \
|
||||
$(CONFIG_EXTRA_LDLIBS) \
|
||||
&& $(srctree)/scripts/generate_BUFSIZ.sh --post include/common_bufsiz.h
|
||||
# ^^^ note: CONFIG_xyz strings already have double quotes: their value
|
||||
# is '"LIB LIB2"', therefore $(CONFIG_EXTRA_LDLIBS) above must NOT be written
|
||||
# as "$(CONFIG_EXTRA_LDLIBS)", it would be passed as ""LIB LIB2"",
|
||||
# and LIB2 would end up in $9, not $8 (and lost or misinterpreted).
|
||||
|
||||
# Generate System.map
|
||||
quiet_cmd_sysmap = SYSMAP
|
||||
@@ -972,7 +966,6 @@ endif # CONFIG_MODULES
|
||||
# Directories & files removed with 'make clean'
|
||||
CLEAN_DIRS += $(MODVERDIR) _install 0_lib
|
||||
CLEAN_FILES += busybox busybox_unstripped* busybox.links \
|
||||
busybox*.suid busybox*.nosuid \
|
||||
System.map .kernelrelease \
|
||||
.tmp_kallsyms* .tmp_version .tmp_busybox* .tmp_System.map
|
||||
|
||||
@@ -1307,6 +1300,14 @@ quiet_cmd_rmdirs = $(if $(wildcard $(rm-dirs)),CLEAN $(wildcard $(rm-dirs)))
|
||||
quiet_cmd_rmfiles = $(if $(wildcard $(rm-files)),CLEAN $(wildcard $(rm-files)))
|
||||
cmd_rmfiles = rm -f $(rm-files)
|
||||
|
||||
|
||||
a_flags = -Wp,-MD,$(depfile) $(AFLAGS) $(AFLAGS_KERNEL) \
|
||||
$(NOSTDINC_FLAGS) $(CPPFLAGS) \
|
||||
$(modkern_aflags) $(EXTRA_AFLAGS) $(AFLAGS_$(*F).o)
|
||||
|
||||
quiet_cmd_as_o_S = AS $@
|
||||
cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
|
||||
|
||||
# read all saved command lines
|
||||
|
||||
targets := $(wildcard $(sort $(targets)))
|
||||
|
||||
+2
-2
@@ -156,12 +156,12 @@ docs/busybox.pod: $(srctree)/docs/busybox_header.pod \
|
||||
docs/BusyBox.txt: docs/busybox.pod
|
||||
$(disp_doc)
|
||||
$(Q)-mkdir -p docs
|
||||
$(Q)-pod2text --quotes=none $< > $@
|
||||
$(Q)-pod2text $< > $@
|
||||
|
||||
docs/busybox.1: docs/busybox.pod
|
||||
$(disp_doc)
|
||||
$(Q)-mkdir -p docs
|
||||
$(Q)-pod2man --quotes=none --center=busybox --release="version $(KERNELVERSION)" $< > $@
|
||||
$(Q)-pod2man --center=busybox --release="version $(KERNELVERSION)" $< > $@
|
||||
|
||||
docs/BusyBox.html: docs/busybox.net/BusyBox.html
|
||||
$(disp_doc)
|
||||
|
||||
+16
-23
@@ -15,7 +15,6 @@ CPPFLAGS += \
|
||||
-include include/autoconf.h \
|
||||
-D_GNU_SOURCE -DNDEBUG \
|
||||
$(if $(CONFIG_LFS),-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64) \
|
||||
$(if $(CONFIG_TIME64),-D_TIME_BITS=64) \
|
||||
-DBB_VER=$(squote)$(quote)$(BB_VER)$(quote)$(squote)
|
||||
|
||||
CFLAGS += $(call cc-option,-Wall,)
|
||||
@@ -27,8 +26,6 @@ CFLAGS += $(call cc-option,-Wunused -Wunused-parameter,)
|
||||
CFLAGS += $(call cc-option,-Wunused-function -Wunused-value,)
|
||||
CFLAGS += $(call cc-option,-Wmissing-prototypes -Wmissing-declarations,)
|
||||
CFLAGS += $(call cc-option,-Wno-format-security,)
|
||||
# we use "" format in ioctl_or_perror_and_die(a, b, c, "")
|
||||
CFLAGS += $(call cc-option,-Wno-format-zero-length,)
|
||||
# warn about C99 declaration after statement
|
||||
CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
|
||||
# If you want to add more -Wsomething above, make sure that it is
|
||||
@@ -50,7 +47,7 @@ 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 ($(lastword $(subst -, ,$(CC))),clang)
|
||||
ifneq ($(CC),clang)
|
||||
# "clang-9: warning: optimization flag '-finline-limit=0' is not supported
|
||||
CFLAGS += $(call cc-option,-finline-limit=0,)
|
||||
endif
|
||||
@@ -61,14 +58,13 @@ CFLAGS += $(call cc-option,-fno-builtin-strlen -fomit-frame-pointer -ffunction-s
|
||||
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
|
||||
ifneq ($(CC),clang)
|
||||
# "clang-9: warning: argument unused during compilation: '-static-libgcc'"
|
||||
CFLAGS += $(call cc-option,-static-libgcc,)
|
||||
endif
|
||||
|
||||
CFLAGS += $(call cc-option,-falign-functions=1,)
|
||||
ifneq ($(lastword $(subst -, ,$(CC))),clang)
|
||||
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
|
||||
@@ -81,7 +77,7 @@ CFLAGS += $(call cc-option,-fno-asynchronous-unwind-tables,)
|
||||
CFLAGS += $(call cc-option,-fno-builtin-printf,)
|
||||
|
||||
# clang-9 does not like "str" + N and "if (CONFIG_ITEM && cond)" constructs
|
||||
ifeq ($(lastword $(subst -, ,$(CC))),clang)
|
||||
ifeq ($(CC),clang)
|
||||
CFLAGS += $(call cc-option,-Wno-string-plus-int -Wno-constant-logical-operand)
|
||||
endif
|
||||
|
||||
@@ -90,14 +86,14 @@ endif
|
||||
#CFLAGS += $(call cc-option,-Wconversion,)
|
||||
|
||||
ifneq ($(CONFIG_DEBUG),y)
|
||||
CFLAGS += $(call cc-option,-Oz,$(call cc-option,-Os,$(call cc-option,-O2,)))
|
||||
CFLAGS += $(call cc-option,-Os,$(call cc-option,-O2,))
|
||||
else
|
||||
CFLAGS += $(call cc-option,-g,)
|
||||
#CFLAGS += "-D_FORTIFY_SOURCE=2"
|
||||
ifeq ($(CONFIG_DEBUG_PESSIMIZE),y)
|
||||
CFLAGS += $(call cc-option,-O0,)
|
||||
else
|
||||
CFLAGS += $(call cc-option,-Oz,$(call cc-option,-Os,$(call cc-option,-O2,)))
|
||||
CFLAGS += $(call cc-option,-Os,$(call cc-option,-O2,))
|
||||
endif
|
||||
endif
|
||||
ifeq ($(CONFIG_DEBUG_SANITIZE),y)
|
||||
@@ -148,21 +144,18 @@ 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
|
||||
LDLIBS += m rt crypt
|
||||
else
|
||||
LDLIBS += m rt
|
||||
endif
|
||||
# libm may be needed for dc, awk, ntpd
|
||||
# librt may be needed for clock_gettime()
|
||||
ifeq ($(RT_AVAILABLE),y)
|
||||
LDLIBS += rt
|
||||
endif
|
||||
|
||||
# libpam may use libpthread, libdl and/or libaudit.
|
||||
# On some platforms that requires an explicit -lpthread, -ldl, -laudit.
|
||||
@@ -187,13 +180,8 @@ 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
|
||||
ifneq (,$(findstring gnu,$(shell $(CC) $(CFLAGS) -dumpmachine)))
|
||||
LDLIBS += resolv
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_EFENCE),y)
|
||||
LDLIBS += efence
|
||||
@@ -218,6 +206,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!)
|
||||
|
||||
@@ -336,7 +336,6 @@ setuidgid - noexec. spawner
|
||||
sha1sum - noexec. runner
|
||||
sha256sum - noexec. runner
|
||||
sha3sum - noexec. runner
|
||||
sha384sum - noexec. runner
|
||||
sha512sum - noexec. runner
|
||||
showkey - interactive, longterm
|
||||
shred - runner
|
||||
|
||||
@@ -222,6 +222,8 @@ Minor stuff:
|
||||
---
|
||||
unify progress_meter. wget, flash_eraseall, pipe_progress, fbsplash, setfiles.
|
||||
---
|
||||
support start-stop-daemon -d <chdir-path>
|
||||
---
|
||||
|
||||
(TODO list after discussion 11.05.2009)
|
||||
|
||||
|
||||
@@ -20,10 +20,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 +52,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++;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,6 @@ int main(int argc, char **argv)
|
||||
|
||||
void bb_show_usage(void)
|
||||
{
|
||||
fputs_stdout(APPLET_full_usage "\n");
|
||||
exit_FAILURE();
|
||||
fputs(APPLET_full_usage "\n", stdout);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
+5
-12
@@ -67,37 +67,30 @@ int main(void)
|
||||
}
|
||||
if (col == 0) {
|
||||
col = 6;
|
||||
printf("\t");
|
||||
} else {
|
||||
printf(", ");
|
||||
}
|
||||
if (usage_array[i].usage[0] != NOUSAGE_STR[0]) {
|
||||
/*
|
||||
* If the applet usage string will be included in the final document
|
||||
* optimistically link to its header (which is just the applet name).
|
||||
*/
|
||||
printf("L<C<%1$s>|/\"%1$s\">", usage_array[i].aname);
|
||||
} else {
|
||||
/* Without a usage string, just output the applet name with no link. */
|
||||
printf("C<%s>", usage_array[i].aname);
|
||||
}
|
||||
printf(usage_array[i].aname);
|
||||
col += len2;
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
printf("=head1 COMMAND DESCRIPTIONS\n\n");
|
||||
printf("=over 4\n\n");
|
||||
|
||||
for (i = 0; i < num_messages; i++) {
|
||||
if (usage_array[i].aname[0] >= 'a' && usage_array[i].aname[0] <= 'z'
|
||||
&& usage_array[i].usage[0] != NOUSAGE_STR[0]
|
||||
) {
|
||||
/* This is the heading that will be linked from the command list. */
|
||||
printf("=head2 %s\n\n", usage_array[i].aname);
|
||||
printf("=item B<%s>\n\n", usage_array[i].aname);
|
||||
if (usage_array[i].usage[0])
|
||||
printf("%s %s\n\n", usage_array[i].aname, usage_array[i].usage);
|
||||
else
|
||||
printf("%s\n\n", usage_array[i].aname);
|
||||
}
|
||||
}
|
||||
printf("=back\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -11,11 +11,3 @@ ifeq ($(CONFIG_STACK_OPTIMIZATION_386),y)
|
||||
# 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,)
|
||||
@@ -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,)
|
||||
@@ -35,15 +35,4 @@ config FEATURE_LZMA_FAST
|
||||
This option reduces decompression time by about 25% at the cost of
|
||||
a 1K bigger binary.
|
||||
|
||||
config FEATURE_PATH_TRAVERSAL_PROTECTION
|
||||
bool "Prevent extraction of filenames with /../ path component"
|
||||
default n
|
||||
help
|
||||
busybox tar and unzip remove "PREFIX/../" (if it exists)
|
||||
from extracted names.
|
||||
This option enables this behavior for all other unpacking applets,
|
||||
such as cpio, ar, rpm.
|
||||
GNU cpio 2.15 has NO such sanity check.
|
||||
# try other archivers and document their behavior?
|
||||
|
||||
endmenu
|
||||
+35
-40
@@ -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 */
|
||||
@@ -151,15 +161,15 @@ static int write_ar_header(archive_handle_t *handle)
|
||||
|
||||
static int write_ar_archive(archive_handle_t *handle)
|
||||
{
|
||||
off_t st_size;
|
||||
struct stat st;
|
||||
archive_handle_t *out_handle;
|
||||
|
||||
st_size = xfstat_size(handle->src_fd, handle->ar__name);
|
||||
xfstat(handle->src_fd, &st, handle->ar__name);
|
||||
|
||||
/* if archive exists, create a new handle for output.
|
||||
* we create it in place of the old one.
|
||||
*/
|
||||
if (st_size != 0) {
|
||||
if (st.st_size != 0) {
|
||||
out_handle = init_handle();
|
||||
xunlink(handle->ar__name);
|
||||
out_handle->src_fd = xopen(handle->ar__name, O_WRONLY | O_CREAT | O_TRUNC);
|
||||
@@ -174,7 +184,7 @@ static int write_ar_archive(archive_handle_t *handle)
|
||||
out_handle->offset += AR_MAGIC_LEN + 1;
|
||||
|
||||
/* skip to the end of the archive if we have to append stuff */
|
||||
if (st_size != 0) {
|
||||
if (st.st_size != 0) {
|
||||
handle->filter = filter_replaceable;
|
||||
handle->action_data = copy_data;
|
||||
unpack_ar_archive(handle);
|
||||
@@ -194,13 +204,11 @@ static int write_ar_archive(archive_handle_t *handle)
|
||||
}
|
||||
#endif /* FEATURE_AR_CREATE */
|
||||
|
||||
static void FAST_FUNC header_verbose_list_ar(archive_handle_t *archive_handle)
|
||||
static void FAST_FUNC header_verbose_list_ar(const file_header_t *file_header)
|
||||
{
|
||||
const file_header_t *file_header = archive_handle->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);
|
||||
@@ -212,36 +220,23 @@ static void FAST_FUNC header_verbose_list_ar(archive_handle_t *archive_handle)
|
||||
);
|
||||
}
|
||||
|
||||
//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();
|
||||
|
||||
@@ -261,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
|
||||
);
|
||||
@@ -292,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
|
||||
|
||||
|
||||
+15
-19
@@ -71,8 +71,8 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
goto err;
|
||||
} else {
|
||||
/* "clever zcat" with FILE */
|
||||
/* die_if_not_compressed because zcat refuses uncompressed input */
|
||||
int fd = open_zipped(filename, /*die_if_not_compressed:*/ 1);
|
||||
/* fail_if_not_compressed because zcat refuses uncompressed input */
|
||||
int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
|
||||
if (fd < 0)
|
||||
goto err_name;
|
||||
xmove_fd(fd, STDIN_FILENO);
|
||||
@@ -80,7 +80,7 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
} else
|
||||
if (option_mask32 & BBUNPK_SEAMLESS_MAGIC) {
|
||||
/* "clever zcat" on stdin */
|
||||
if (setup_unzip_on_fd(STDIN_FILENO, /*die_if_not_compressed*/ 1))
|
||||
if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -219,7 +219,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 +284,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"
|
||||
@@ -407,15 +407,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 (9.1 kb)"
|
||||
//config: bool "bunzip2 (8.7 kb)"
|
||||
//config: default y
|
||||
//config: select FEATURE_BZIP2_DECOMPRESS
|
||||
//config: help
|
||||
@@ -429,7 +427,7 @@ int gunzip_main(int argc UNUSED_PARAM, char **argv)
|
||||
//config: should probably say N here.
|
||||
//config:
|
||||
//config:config BZCAT
|
||||
//config: bool "bzcat (9 kb)"
|
||||
//config: bool "bzcat (8.7 kb)"
|
||||
//config: default y
|
||||
//config: select FEATURE_BZIP2_DECOMPRESS
|
||||
//config: help
|
||||
@@ -463,21 +461,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 +481,7 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
|
||||
//usage: "Decompress to stdout"
|
||||
|
||||
//config:config UNLZMA
|
||||
//config: bool "unlzma (7.8 kb)"
|
||||
//config: bool "unlzma (7.5 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: unlzma is a compression utility using the Lempel-Ziv-Markov chain
|
||||
@@ -494,7 +490,7 @@ int bunzip2_main(int argc UNUSED_PARAM, char **argv)
|
||||
//config: compressors.
|
||||
//config:
|
||||
//config:config LZCAT
|
||||
//config: bool "lzcat (7.8 kb)"
|
||||
//config: bool "lzcat (7.5 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Alias to "unlzma -c".
|
||||
@@ -536,21 +532,21 @@ 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: "\n -t Test file 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: "\n -t Test file integrity"
|
||||
//usage:
|
||||
//usage:#define xzcat_trivial_usage
|
||||
//usage: "[FILE]..."
|
||||
|
||||
+2
-4
@@ -50,19 +50,17 @@
|
||||
//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"
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
+6
-70
@@ -38,22 +38,8 @@
|
||||
//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_USR_BIN, BB_SUID_DROP))
|
||||
//applet:IF_CPIO(APPLET(cpio, BB_DIR_BIN, BB_SUID_DROP))
|
||||
|
||||
//kbuild:lib-$(CONFIG_CPIO) += cpio.o
|
||||
|
||||
@@ -62,7 +48,7 @@
|
||||
//usage: " [-ti"IF_FEATURE_CPIO_O("o")"]" IF_FEATURE_CPIO_P(" [-p DIR]")
|
||||
//usage: " [EXTR_FILE]..."
|
||||
//usage:#define cpio_full_usage "\n\n"
|
||||
//usage: "Extract (-i) or list (-t) files from a cpio archive on stdin"
|
||||
//usage: "Extract (-i) or list (-t) files from a cpio archive"
|
||||
//usage: IF_FEATURE_CPIO_O(", or"
|
||||
//usage: "\ntake file list from stdin and create an archive (-o)"
|
||||
//usage: IF_FEATURE_CPIO_P(" or copy files (-p)")
|
||||
@@ -82,19 +68,13 @@
|
||||
//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: )
|
||||
//usage: "\n -0 Input is separated by NULs"
|
||||
|
||||
/* GNU cpio 2.9 --help (abridged):
|
||||
|
||||
@@ -182,22 +162,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 +206,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 +260,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 +278,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 +285,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,17 +304,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
|
||||
|
||||
if (sizeof(st.st_size) > 4
|
||||
&& st.st_size > (off_t)0xffffffff
|
||||
) {
|
||||
bb_error_msg_and_die("error: file '%s' is larger than 4GB", name);
|
||||
}
|
||||
|
||||
bytes += printf("070701"
|
||||
"%08X%08X%08X%08X%08X%08X%08X"
|
||||
"%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
|
||||
@@ -427,17 +375,10 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
#endif
|
||||
#endif
|
||||
"owner\0" Required_argument "R"
|
||||
"file\0" Required_argument "F"
|
||||
"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
|
||||
|
||||
@@ -477,8 +418,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.
|
||||
@@ -488,11 +428,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;
|
||||
@@ -511,6 +446,7 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
goto dump;
|
||||
}
|
||||
/* parent */
|
||||
USE_FOR_NOMMU(argv[-optind][0] &= 0x7f); /* undo fork_or_rexec() damage */
|
||||
xchdir(*argv++);
|
||||
close(pp.wr);
|
||||
xmove_fd(pp.rd, STDIN_FILENO);
|
||||
|
||||
+141
-202
@@ -41,30 +41,30 @@
|
||||
//kbuild:lib-$(CONFIG_DPKG) += dpkg.o
|
||||
|
||||
//usage:#define dpkg_trivial_usage
|
||||
//usage: IF_LONG_OPTS("-i|P|r|--unpack|--configure [--force-OPT]")IF_NOT_LONG_OPTS("-i|P|r|u|C [-F OPT]")" PACKAGE | -l [PATTERN]"
|
||||
//usage: "[-ilCPru] [-F OPT] PACKAGE"
|
||||
//usage:#define dpkg_full_usage "\n\n"
|
||||
//usage: "Manage Debian packages\n"
|
||||
//usage: "Install, remove and manage Debian packages\n"
|
||||
//usage: IF_LONG_OPTS(
|
||||
//usage: "\n -i,--install Install PACKAGE.deb"
|
||||
//usage: "\n -P,--purge Purge all files of PACKAGE"
|
||||
//usage: "\n -r,--remove Remove all but configuration files of PACKAGE"
|
||||
//usage: "\n --unpack Unpack PACKAGE.deb relative to /, don't configure"
|
||||
//usage: "\n --configure Configure unpacked PACKAGE"
|
||||
//usage: "\n -i,--install Install the package"
|
||||
//usage: "\n -l,--list List of installed packages"
|
||||
//usage: "\n --configure Configure an unpackaged package"
|
||||
//usage: "\n -P,--purge Purge all files of a package"
|
||||
//usage: "\n -r,--remove Remove all but the configuration files for a package"
|
||||
//usage: "\n --unpack Unpack a package, but don't configure it"
|
||||
//usage: "\n --force-depends Ignore dependency problems"
|
||||
//usage: "\n --force-confnew Overwrite existing config files when installing"
|
||||
//usage: "\n --force-confold Keep old config files when installing"
|
||||
//usage: "\n -l,--list List installed packages"
|
||||
//usage: )
|
||||
//usage: IF_NOT_LONG_OPTS(
|
||||
//usage: "\n -i Install PACKAGE.deb"
|
||||
//usage: "\n -P Purge all files of PACKAGE"
|
||||
//usage: "\n -r Remove all but configuration files of PACKAGE"
|
||||
//usage: "\n -u Unpack PACKAGE.deb relative to /, don't configure"
|
||||
//usage: "\n -C Configure unpacked PACKAGE"
|
||||
//usage: "\n -i Install the package"
|
||||
//usage: "\n -l List of installed packages"
|
||||
//usage: "\n -C Configure an unpackaged package"
|
||||
//usage: "\n -P Purge all files of a package"
|
||||
//usage: "\n -r Remove all but the configuration files for a package"
|
||||
//usage: "\n -u Unpack a package, but don't configure it"
|
||||
//usage: "\n -F depends Ignore dependency problems"
|
||||
//usage: "\n -F confnew Overwrite existing config files when installing"
|
||||
//usage: "\n -F confold Keep old config files when installing"
|
||||
//usage: "\n -l List installed packages"
|
||||
//usage: )
|
||||
|
||||
#include "libbb.h"
|
||||
@@ -166,9 +166,8 @@ typedef struct deb_file_s {
|
||||
} deb_file_t;
|
||||
|
||||
|
||||
static unsigned make_hash(const char *key, unsigned *decrement, int hash_prime)
|
||||
static void make_hash(const char *key, unsigned *start, unsigned *decrement, const int hash_prime)
|
||||
{
|
||||
unsigned start;
|
||||
unsigned long hash_num = key[0];
|
||||
int len = strlen(key);
|
||||
int i;
|
||||
@@ -182,9 +181,8 @@ static unsigned make_hash(const char *key, unsigned *decrement, int hash_prime)
|
||||
* no effect */
|
||||
hash_num += (key[i] + key[i-1]) << ((key[i] * i) % 24);
|
||||
}
|
||||
start = (unsigned) hash_num % hash_prime;
|
||||
*start = (unsigned) hash_num % hash_prime;
|
||||
*decrement = (unsigned) 1 + (hash_num % (hash_prime - 1));
|
||||
return start;
|
||||
}
|
||||
|
||||
/* this adds the key to the hash table */
|
||||
@@ -193,7 +191,7 @@ static int search_name_hashtable(const char *key)
|
||||
unsigned probe_address;
|
||||
unsigned probe_decrement;
|
||||
|
||||
probe_address = make_hash(key, &probe_decrement, NAME_HASH_PRIME);
|
||||
make_hash(key, &probe_address, &probe_decrement, NAME_HASH_PRIME);
|
||||
while (name_hashtable[probe_address] != NULL) {
|
||||
if (strcmp(name_hashtable[probe_address], key) == 0) {
|
||||
return probe_address;
|
||||
@@ -215,7 +213,7 @@ static unsigned search_status_hashtable(const char *key)
|
||||
unsigned probe_address;
|
||||
unsigned probe_decrement;
|
||||
|
||||
probe_address = make_hash(key, &probe_decrement, STATUS_HASH_PRIME);
|
||||
make_hash(key, &probe_address, &probe_decrement, STATUS_HASH_PRIME);
|
||||
while (status_hashtable[probe_address] != NULL) {
|
||||
if (strcmp(key, name_hashtable[package_hashtable[status_hashtable[probe_address]->package]->name]) == 0) {
|
||||
break;
|
||||
@@ -282,7 +280,7 @@ static int version_compare_part(const char *val, const char *ref)
|
||||
* if ver1 = ver2 return 0,
|
||||
* if ver1 > ver2 return 1,
|
||||
*/
|
||||
static int version_compare(unsigned ver1, unsigned ver2)
|
||||
static int version_compare(const unsigned ver1, const unsigned ver2)
|
||||
{
|
||||
char *ch_ver1 = name_hashtable[ver1];
|
||||
char *ch_ver2 = name_hashtable[ver2];
|
||||
@@ -335,9 +333,9 @@ static int version_compare(unsigned ver1, unsigned ver2)
|
||||
return result;
|
||||
}
|
||||
|
||||
static int test_version(unsigned version1, unsigned version2, unsigned operator)
|
||||
static int test_version(const unsigned version1, const unsigned version2, const unsigned operator)
|
||||
{
|
||||
int version_result = version_compare(version1, version2);
|
||||
const int version_result = version_compare(version1, version2);
|
||||
switch (operator) {
|
||||
case VER_ANY:
|
||||
return TRUE;
|
||||
@@ -355,12 +353,12 @@ static int test_version(unsigned version1, unsigned version2, unsigned operator)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static int search_package_hashtable(unsigned name, unsigned version, unsigned operator)
|
||||
static int search_package_hashtable(const unsigned name, const unsigned version, const unsigned operator)
|
||||
{
|
||||
unsigned probe_address;
|
||||
unsigned probe_decrement;
|
||||
|
||||
probe_address = make_hash(name_hashtable[name], &probe_decrement, PACKAGE_HASH_PRIME);
|
||||
make_hash(name_hashtable[name], &probe_address, &probe_decrement, PACKAGE_HASH_PRIME);
|
||||
while (package_hashtable[probe_address] != NULL) {
|
||||
if (package_hashtable[probe_address]->name == name) {
|
||||
if (operator == VER_ANY) {
|
||||
@@ -533,98 +531,90 @@ static void free_package(common_node_t *node)
|
||||
}
|
||||
|
||||
/*
|
||||
* Gets the next package field from pkg_buf:
|
||||
* "NAME:<spaces|tabs>VALUE{\n|NUL}"
|
||||
* "NAME:<spaces|tabs>VALUE\n
|
||||
* " VALUE_LINE2{\n|NUL}"
|
||||
* separated into "NAME" and "VALUE", both strdup()ed.
|
||||
* (Debian does not allow whitespace before/after NAME).
|
||||
* Returns offset to the first character of the next field (or to NUL).
|
||||
* The pkg_buf parameter is NUL-terminated.
|
||||
*
|
||||
* https://manpages.debian.org/testing/dpkg-dev/deb-control.5.en.html
|
||||
* """
|
||||
* This file contains a number of fields. Each field begins with a tag,
|
||||
* such as Package or Version (case insensitive), followed by a colon,
|
||||
* and the body of the field (case sensitive unless stated otherwise).
|
||||
* Fields are delimited only by field tags. In other words, field text
|
||||
* may be multiple lines in length, but the installation tools will
|
||||
* generally join lines when processing the body of the field (except
|
||||
* in the case of the Description field, see below).
|
||||
* ...
|
||||
* Description: short-description (recommended)
|
||||
* long-description
|
||||
* The format for the package description is a short brief summary
|
||||
* on the first line (after the Description field). The following lines
|
||||
* should be used as a longer, more detailed description. Each line
|
||||
* of the long description must be preceded by a space, and blank
|
||||
* lines in the long description must contain a single '.' following
|
||||
* the preceding space."""
|
||||
*
|
||||
* Seen in libgtk-3-0t64_3.24.52: source package's control:
|
||||
* Recommends: ibus-gtk3,
|
||||
* libgtk-3-bin,
|
||||
* librsvg2-common
|
||||
* gets translated to this in binary package's control:
|
||||
* Recommends: ibus-gtk3, libgtk-3-bin, librsvg2-common
|
||||
* Gets the next package field from package_buffer, separated into the field name
|
||||
* and field value, it returns the int offset to the first character of the next field
|
||||
*/
|
||||
static int read_package_field(const char *pkg_buf, char **pname, char **pvalue)
|
||||
static int read_package_field(const char *package_buffer, char **field_name, char **field_value)
|
||||
{
|
||||
int name_start;
|
||||
int name_end;
|
||||
int value_start = value_start;
|
||||
int offset;
|
||||
int offset_name_start = 0;
|
||||
int offset_name_end = 0;
|
||||
int offset_value_start = 0;
|
||||
int offset_value_end = 0;
|
||||
int offset = 0;
|
||||
int next_offset;
|
||||
int name_length;
|
||||
int value_length;
|
||||
int exit_flag = FALSE;
|
||||
|
||||
*pname = NULL;
|
||||
*pvalue = NULL;
|
||||
|
||||
offset = 0;
|
||||
name_start = 0;
|
||||
name_end = 0; /* "we did not see ':' yet" */
|
||||
if (package_buffer == NULL) {
|
||||
*field_name = NULL;
|
||||
*field_value = NULL;
|
||||
return -1;
|
||||
}
|
||||
while (1) {
|
||||
char ch = pkg_buf[offset];
|
||||
switch (ch) {
|
||||
case ':':
|
||||
if (name_end == 0) {
|
||||
offset++;
|
||||
name_end = offset; /* points AFTER ':' - think of empty NAME case */
|
||||
while (isblank(pkg_buf[offset])) /* skip spaces/tabs (not newlines!) */
|
||||
offset++;
|
||||
value_start = offset;
|
||||
continue; /* check next char */
|
||||
}
|
||||
break;
|
||||
case '\n':
|
||||
if (pkg_buf[offset + 1] == ' ')
|
||||
next_offset = offset + 1;
|
||||
switch (package_buffer[offset]) {
|
||||
case '\0':
|
||||
exit_flag = TRUE;
|
||||
break;
|
||||
/* end of VALUE - fall through */
|
||||
case '\0':
|
||||
/* Did we see the ':'? */
|
||||
if (name_end != 0) {
|
||||
/* Yes. Check that NAME is not empty */
|
||||
int nlen = (name_end - 1) - name_start;
|
||||
if (nlen > 0) {
|
||||
//TODO: why do we disallow empty NAME? Imagine a ": VALUE\n" line
|
||||
*pname = xstrndup(&pkg_buf[name_start], nlen);
|
||||
*pvalue = xstrndup(&pkg_buf[value_start], offset - value_start); /* could be "" */
|
||||
if (ch)
|
||||
offset++; /* skip '\n' */
|
||||
return offset;
|
||||
case ':':
|
||||
if (offset_name_end == 0) {
|
||||
offset_name_end = offset;
|
||||
offset_value_start = next_offset;
|
||||
}
|
||||
/* TODO: Name might still have trailing spaces if ':' isn't
|
||||
* immediately after name */
|
||||
break;
|
||||
case '\n':
|
||||
/* TODO: The char next_offset may be out of bounds */
|
||||
if (package_buffer[next_offset] != ' ') {
|
||||
exit_flag = TRUE;
|
||||
break;
|
||||
}
|
||||
case '\t':
|
||||
case ' ':
|
||||
/* increment the value start point if its a just filler */
|
||||
if (offset_name_start == offset) {
|
||||
offset_name_start++;
|
||||
}
|
||||
if (offset_value_start == offset) {
|
||||
offset_value_start++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (exit_flag) {
|
||||
/* Check that the names are valid */
|
||||
offset_value_end = offset;
|
||||
name_length = offset_name_end - offset_name_start;
|
||||
value_length = offset_value_end - offset_value_start;
|
||||
if (name_length == 0) {
|
||||
break;
|
||||
}
|
||||
if ((name_length > 0) && (value_length > 0)) {
|
||||
break;
|
||||
}
|
||||
if (!ch)
|
||||
return offset; /* stop at NUL */
|
||||
|
||||
/* Not valid: skip '\n', start fresh with next field */
|
||||
name_start = offset + 1;
|
||||
name_end = 0; /* "we did not see ':' yet" */
|
||||
break;
|
||||
/* If not valid, start fresh with next field */
|
||||
exit_flag = FALSE;
|
||||
offset_name_start = offset + 1;
|
||||
offset_name_end = 0;
|
||||
offset_value_start = offset + 1;
|
||||
offset_value_end = offset + 1;
|
||||
offset++;
|
||||
}
|
||||
offset++;
|
||||
} /* while (1) */
|
||||
}
|
||||
*field_name = NULL;
|
||||
if (name_length) {
|
||||
*field_name = xstrndup(&package_buffer[offset_name_start], name_length);
|
||||
}
|
||||
*field_value = NULL;
|
||||
if (value_length > 0) {
|
||||
*field_value = xstrndup(&package_buffer[offset_value_start], value_length);
|
||||
}
|
||||
return next_offset;
|
||||
}
|
||||
|
||||
/* The parameter is NUL-terminated */
|
||||
static unsigned fill_package_struct(char *control_buffer)
|
||||
{
|
||||
static const char field_names[] ALIGN1 =
|
||||
@@ -633,13 +623,14 @@ static unsigned fill_package_struct(char *control_buffer)
|
||||
"Conflicts\0""Suggests\0""Recommends\0""Enhances\0";
|
||||
|
||||
common_node_t *new_node = xzalloc(sizeof(common_node_t));
|
||||
char *field_name;
|
||||
char *field_value;
|
||||
int field_start = 0;
|
||||
int num;
|
||||
int num = -1;
|
||||
int buffer_length = strlen(control_buffer);
|
||||
|
||||
new_node->version = search_name_hashtable("unknown");
|
||||
while (control_buffer[field_start]) {
|
||||
char *field_name;
|
||||
char *field_value;
|
||||
while (field_start < buffer_length) {
|
||||
unsigned field_num;
|
||||
|
||||
field_start += read_package_field(&control_buffer[field_start],
|
||||
@@ -698,7 +689,7 @@ static unsigned fill_package_struct(char *control_buffer)
|
||||
}
|
||||
|
||||
/* if num = 1, it returns the want status, 2 returns flag, 3 returns status */
|
||||
static unsigned get_status(unsigned status_node, int num)
|
||||
static unsigned get_status(const unsigned status_node, const int num)
|
||||
{
|
||||
char *status_string = name_hashtable[status_hashtable[status_node]->status];
|
||||
char *state_sub_string;
|
||||
@@ -720,9 +711,9 @@ static unsigned get_status(unsigned status_node, int num)
|
||||
return state_sub_num;
|
||||
}
|
||||
|
||||
static void set_status(unsigned status_node_num, const char *new_value, int position)
|
||||
static void set_status(const unsigned status_node_num, const char *new_value, const int position)
|
||||
{
|
||||
unsigned new_value_num = search_name_hashtable(new_value);
|
||||
const unsigned new_value_num = search_name_hashtable(new_value);
|
||||
unsigned want = get_status(status_node_num, 1);
|
||||
unsigned flag = get_status(status_node_num, 2);
|
||||
unsigned status = get_status(status_node_num, 3);
|
||||
@@ -780,10 +771,9 @@ static void index_status_file(const char *filename)
|
||||
status_node_t *status_node = NULL;
|
||||
unsigned status_num;
|
||||
|
||||
//FIXME: dpkg 1.22.22 recreates empty /var/lib/dpkg/status if it's missing - does not fail
|
||||
status_file = xfopen_for_read(filename);
|
||||
while ((control_buffer = xmalloc_fgetline_str(status_file, "\n\n")) != NULL) {
|
||||
unsigned package_num = fill_package_struct(control_buffer);
|
||||
const unsigned package_num = fill_package_struct(control_buffer);
|
||||
if (package_num != -1) {
|
||||
status_node = xmalloc(sizeof(status_node_t));
|
||||
/* fill_package_struct doesn't handle the status field */
|
||||
@@ -809,7 +799,7 @@ static void write_buffer_no_status(FILE *new_status_file, const char *control_bu
|
||||
char *name;
|
||||
char *value;
|
||||
int start = 0;
|
||||
while (control_buffer[start]) {
|
||||
while (1) {
|
||||
start += read_package_field(&control_buffer[start], &name, &value);
|
||||
if (name == NULL) {
|
||||
break;
|
||||
@@ -817,42 +807,42 @@ static void write_buffer_no_status(FILE *new_status_file, const char *control_bu
|
||||
if (strcmp(name, "Status") != 0) {
|
||||
fprintf(new_status_file, "%s: %s\n", name, value);
|
||||
}
|
||||
free(name);
|
||||
free(value);
|
||||
}
|
||||
}
|
||||
|
||||
/* This could do with a cleanup */
|
||||
static void write_status_file(deb_file_t **deb_file)
|
||||
{
|
||||
//FIXME: dpkg 1.22.22 recreates empty /var/lib/dpkg/status if it's missing - does not fail
|
||||
FILE *old_status_file = xfopen_for_read("/var/lib/dpkg/status");
|
||||
FILE *new_status_file = xfopen_for_write("/var/lib/dpkg/status.udeb");
|
||||
char *control_buffer;
|
||||
char *package_name;
|
||||
char *status_from_file;
|
||||
char *control_buffer = NULL;
|
||||
char *tmp_string;
|
||||
int status_num;
|
||||
int i;
|
||||
int field_start = 0;
|
||||
int write_flag;
|
||||
int i = 0;
|
||||
|
||||
/* Update previously known packages */
|
||||
while ((control_buffer = xmalloc_fgetline_str(old_status_file, "\n\n")) != NULL) {
|
||||
char *package_name;
|
||||
char *status_from_file;
|
||||
char *tmp_string;
|
||||
int write_flag;
|
||||
|
||||
tmp_string = strstr(control_buffer, "Package:");
|
||||
if (tmp_string == NULL) {
|
||||
free(control_buffer);
|
||||
continue;
|
||||
}
|
||||
tmp_string = skip_whitespace(tmp_string + 8);
|
||||
package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n"));
|
||||
|
||||
tmp_string += 8;
|
||||
tmp_string += strspn(tmp_string, " \n\t");
|
||||
package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n"));
|
||||
write_flag = FALSE;
|
||||
status_from_file = NULL;
|
||||
tmp_string = strstr(control_buffer, "Status:");
|
||||
if (tmp_string != NULL) {
|
||||
tmp_string = skip_whitespace(tmp_string + 7);
|
||||
/* Separate the status value from the control buffer */
|
||||
tmp_string += 7;
|
||||
tmp_string += strspn(tmp_string, " \n\t");
|
||||
status_from_file = xstrndup(tmp_string, strcspn(tmp_string, "\n"));
|
||||
} else {
|
||||
status_from_file = NULL;
|
||||
}
|
||||
|
||||
/* Find this package in the status hashtable */
|
||||
@@ -861,7 +851,7 @@ static void write_status_file(deb_file_t **deb_file)
|
||||
const char *status_from_hashtable = name_hashtable[status_hashtable[status_num]->status];
|
||||
if (strcmp(status_from_file, status_from_hashtable) != 0) {
|
||||
/* New status isn't exactly the same as old status */
|
||||
int state_status = get_status(status_num, 3);
|
||||
const int state_status = get_status(status_num, 3);
|
||||
if ((strcmp("installed", name_hashtable[state_status]) == 0)
|
||||
|| (strcmp("unpacked", name_hashtable[state_status]) == 0)
|
||||
) {
|
||||
@@ -880,7 +870,7 @@ static void write_status_file(deb_file_t **deb_file)
|
||||
}
|
||||
i++;
|
||||
}
|
||||
/* This is temporary, debugging only */
|
||||
/* This is temperary, debugging only */
|
||||
if (deb_file[i] == NULL) {
|
||||
bb_error_msg_and_die("ALERT: cannot find a control file, "
|
||||
"your status file may be broken, status may be "
|
||||
@@ -888,18 +878,14 @@ static void write_status_file(deb_file_t **deb_file)
|
||||
}
|
||||
}
|
||||
else if (strcmp("not-installed", name_hashtable[state_status]) == 0) {
|
||||
int field_start = 0;
|
||||
|
||||
/* Only write the Package, Status, Priority and Section lines */
|
||||
fprintf(new_status_file, "Package: %s\n", package_name);
|
||||
fprintf(new_status_file, "Status: %s\n", status_from_hashtable);
|
||||
|
||||
while (control_buffer[field_start]) {
|
||||
while (1) {
|
||||
char *field_name;
|
||||
char *field_value;
|
||||
field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value);
|
||||
//FIXME: the questionable ": VALUE" lines (empty NAME)
|
||||
//probably should not stop parsing of the entire file?
|
||||
if (field_name == NULL) {
|
||||
break;
|
||||
}
|
||||
@@ -908,17 +894,13 @@ static void write_status_file(deb_file_t **deb_file)
|
||||
) {
|
||||
fprintf(new_status_file, "%s: %s\n", field_name, field_value);
|
||||
}
|
||||
free(field_name);
|
||||
free(field_value);
|
||||
}
|
||||
write_flag = TRUE;
|
||||
fputs("\n", new_status_file);
|
||||
}
|
||||
else if (strcmp("config-files", name_hashtable[state_status]) == 0) {
|
||||
int field_start = 0;
|
||||
|
||||
/* only change the status line */
|
||||
while (control_buffer[field_start]) {
|
||||
while (1) {
|
||||
char *field_name;
|
||||
char *field_value;
|
||||
field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value);
|
||||
@@ -931,15 +913,13 @@ static void write_status_file(deb_file_t **deb_file)
|
||||
} else {
|
||||
fprintf(new_status_file, "%s: %s\n", field_name, field_value);
|
||||
}
|
||||
free(field_name);
|
||||
free(field_value);
|
||||
}
|
||||
write_flag = TRUE;
|
||||
fputs("\n", new_status_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* If the package from the status file wasn't handled above, do it now */
|
||||
/* If the package from the status file wasn't handle above, do it now*/
|
||||
if (!write_flag) {
|
||||
fprintf(new_status_file, "%s\n\n", control_buffer);
|
||||
}
|
||||
@@ -947,7 +927,7 @@ static void write_status_file(deb_file_t **deb_file)
|
||||
free(status_from_file);
|
||||
free(package_name);
|
||||
free(control_buffer);
|
||||
} /* while (control_buffer) */
|
||||
}
|
||||
|
||||
/* Write any new packages */
|
||||
for (i = 0; deb_file[i] != NULL; i++) {
|
||||
@@ -1014,14 +994,14 @@ static int check_deps(deb_file_t **deb_file, int deb_start /*, int dep_max_count
|
||||
/* Create array of package numbers to check against
|
||||
* installed package for conflicts*/
|
||||
while (deb_file[i] != NULL) {
|
||||
unsigned package_num = deb_file[i]->package;
|
||||
const unsigned package_num = deb_file[i]->package;
|
||||
conflicts = xrealloc_vector(conflicts, 2, conflicts_num);
|
||||
conflicts[conflicts_num] = package_num;
|
||||
conflicts_num++;
|
||||
/* add provides to conflicts list */
|
||||
for (j = 0; j < package_hashtable[package_num]->num_of_edges; j++) {
|
||||
if (package_hashtable[package_num]->edge[j]->type == EDGE_PROVIDES) {
|
||||
int conflicts_package_num = search_package_hashtable(
|
||||
const int conflicts_package_num = search_package_hashtable(
|
||||
package_hashtable[package_num]->edge[j]->name,
|
||||
package_hashtable[package_num]->edge[j]->version,
|
||||
package_hashtable[package_num]->edge[j]->operator);
|
||||
@@ -1056,7 +1036,7 @@ static int check_deps(deb_file_t **deb_file, int deb_start /*, int dep_max_count
|
||||
const edge_t *package_edge = package_node->edge[j];
|
||||
|
||||
if (package_edge->type == EDGE_CONFLICTS) {
|
||||
unsigned package_num =
|
||||
const unsigned package_num =
|
||||
search_package_hashtable(package_edge->name,
|
||||
package_edge->version,
|
||||
package_edge->operator);
|
||||
@@ -1085,7 +1065,7 @@ static int check_deps(deb_file_t **deb_file, int deb_start /*, int dep_max_count
|
||||
for (i = 0; i < PACKAGE_HASH_PRIME; i++) {
|
||||
int status_num = 0;
|
||||
int number_of_alternatives = 0;
|
||||
const edge_t *root_of_alternatives = NULL;
|
||||
const edge_t * root_of_alternatives = NULL;
|
||||
const common_node_t *package_node = package_hashtable[i];
|
||||
|
||||
/* If the package node does not exist then this
|
||||
@@ -1135,7 +1115,7 @@ static int check_deps(deb_file_t **deb_file, int deb_start /*, int dep_max_count
|
||||
if (package_edge->type == EDGE_PRE_DEPENDS
|
||||
|| package_edge->type == EDGE_DEPENDS
|
||||
) {
|
||||
int result = 1;
|
||||
int result=1;
|
||||
status_num = 0;
|
||||
|
||||
/* If we are inside an alternative then check
|
||||
@@ -1224,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)
|
||||
{
|
||||
@@ -1256,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) {
|
||||
@@ -1324,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"
|
||||
@@ -1401,12 +1358,12 @@ static void list_packages(const char *pattern)
|
||||
}
|
||||
}
|
||||
|
||||
static void remove_package(unsigned package_num, int noisy)
|
||||
static void remove_package(const unsigned package_num, int noisy)
|
||||
{
|
||||
const char *package_name = name_hashtable[package_hashtable[package_num]->name];
|
||||
const char *package_version = name_hashtable[package_hashtable[package_num]->version];
|
||||
unsigned status_num = search_status_hashtable(package_name);
|
||||
int package_name_length = strlen(package_name);
|
||||
const unsigned status_num = search_status_hashtable(package_name);
|
||||
const int package_name_length = strlen(package_name);
|
||||
char **remove_files;
|
||||
char **exclude_files;
|
||||
char list_name[package_name_length + 25];
|
||||
@@ -1452,11 +1409,11 @@ static void remove_package(unsigned package_num, int noisy)
|
||||
set_status(status_num, "config-files", 3);
|
||||
}
|
||||
|
||||
static void purge_package(unsigned package_num)
|
||||
static void purge_package(const unsigned package_num)
|
||||
{
|
||||
const char *package_name = name_hashtable[package_hashtable[package_num]->name];
|
||||
const char *package_version = name_hashtable[package_hashtable[package_num]->version];
|
||||
unsigned status_num = search_status_hashtable(package_name);
|
||||
const unsigned status_num = search_status_hashtable(package_name);
|
||||
char **remove_files;
|
||||
char **exclude_files;
|
||||
char list_name[strlen(package_name) + 25];
|
||||
@@ -1674,8 +1631,8 @@ enum {
|
||||
static void unpack_package(deb_file_t *deb_file)
|
||||
{
|
||||
const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
|
||||
unsigned status_num = search_status_hashtable(package_name);
|
||||
unsigned status_package_num = status_hashtable[status_num]->package;
|
||||
const unsigned status_num = search_status_hashtable(package_name);
|
||||
const unsigned status_package_num = status_hashtable[status_num]->package;
|
||||
char *info_prefix;
|
||||
char *list_filename;
|
||||
archive_handle_t *archive_handle;
|
||||
@@ -1709,7 +1666,7 @@ static void unpack_package(deb_file_t *deb_file)
|
||||
accept_list = NULL;
|
||||
i = 0;
|
||||
while (i < ARRAY_SIZE(all_control_files)) {
|
||||
char *c = concat_path_file(".", all_control_files[i]);
|
||||
char *c = xasprintf("./%s", all_control_files[i]);
|
||||
llist_add_to(&accept_list, c);
|
||||
i++;
|
||||
}
|
||||
@@ -1765,7 +1722,7 @@ static void configure_package(deb_file_t *deb_file)
|
||||
{
|
||||
const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
|
||||
const char *package_version = name_hashtable[package_hashtable[deb_file->package]->version];
|
||||
int status_num = search_status_hashtable(package_name);
|
||||
const int status_num = search_status_hashtable(package_name);
|
||||
|
||||
printf("Setting up %s (%s)...\n", package_name, package_version);
|
||||
|
||||
@@ -1806,24 +1763,6 @@ int dpkg_main(int argc UNUSED_PARAM, char **argv)
|
||||
"force-confold\0" No_argument "\xfd"
|
||||
;
|
||||
#endif
|
||||
//--admindir=DIR
|
||||
// Set the administrative directory to DIR. This directory
|
||||
// contains many files that give information about status of
|
||||
// installed or uninstalled packages, etc. Defaults to
|
||||
// /var/lib/dpkg if DPKG_ADMINDIR has not been set.
|
||||
//--instdir=DIR
|
||||
// Set the installation directory, which refers to the directory
|
||||
// where packages are to be installed. DIR is also the
|
||||
// directory passed to chroot(2) before running package's
|
||||
// installation scripts, which means that the scripts see DIR
|
||||
// as a root directory. Defaults to / if DPKG_ROOT has not
|
||||
// been set (since dpkg 1.21.10).
|
||||
//--root=DIR
|
||||
// Set the root directory to DIR, which sets the
|
||||
// installation directory to DIR and the administrative
|
||||
// directory to DIR/var/lib/dpkg if DPKG_ROOT
|
||||
// has not been set (since dpkg 1.21.10).
|
||||
//TODO: implement --root=DIR? necessary for testing
|
||||
|
||||
INIT_G();
|
||||
|
||||
|
||||
+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 (29 kb)"
|
||||
//config: bool "dpkg-deb (30 kb)"
|
||||
//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"
|
||||
|
||||
+1
-6
@@ -77,13 +77,11 @@ aa: 85.1% -- replaced with aa.gz
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_GZIP_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_GZIP_DECOMPRESS(
|
||||
//usage: "\n -t Test integrity"
|
||||
//usage: )
|
||||
//usage:
|
||||
//usage:#define gzip_example_usage
|
||||
//usage: "$ ls -la /tmp/busybox*\n"
|
||||
@@ -2215,9 +2213,6 @@ int gzip_main(int argc UNUSED_PARAM, char **argv)
|
||||
};
|
||||
#endif
|
||||
|
||||
// TODO: use less ugly "split-globals" trick via SET_OFFSET_PTR_TO_GLOBALS().
|
||||
// The problem is, the current method strategically places G2.heap[]
|
||||
// (~24 references) so that it has zero offset.
|
||||
SET_PTR_TO_GLOBALS((char *)xzalloc(sizeof(struct globals)+sizeof(struct globals2))
|
||||
+ sizeof(struct globals));
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ COMMON_FILES:= \
|
||||
data_extract_all.o \
|
||||
data_extract_to_stdout.o \
|
||||
\
|
||||
unsafe_prefix.o \
|
||||
unsafe_symlink_target.o \
|
||||
\
|
||||
filter_accept_all.o \
|
||||
@@ -33,6 +32,7 @@ COMMON_FILES:= \
|
||||
DPKG_FILES:= \
|
||||
unpack_ar_archive.o \
|
||||
filter_accept_list_reassign.o \
|
||||
unsafe_prefix.o \
|
||||
get_header_ar.o \
|
||||
get_header_tar.o \
|
||||
get_header_tar_gz.o \
|
||||
@@ -47,7 +47,7 @@ lib-$(CONFIG_DPKG_DEB) += $(DPKG_FILES)
|
||||
|
||||
lib-$(CONFIG_AR) += get_header_ar.o unpack_ar_archive.o
|
||||
lib-$(CONFIG_CPIO) += get_header_cpio.o
|
||||
lib-$(CONFIG_TAR) += get_header_tar.o
|
||||
lib-$(CONFIG_TAR) += get_header_tar.o unsafe_prefix.o
|
||||
lib-$(CONFIG_FEATURE_TAR_TO_COMMAND) += data_extract_to_command.o
|
||||
lib-$(CONFIG_LZOP) += lzo1x_1.o lzo1x_1o.o lzo1x_d.o
|
||||
lib-$(CONFIG_UNLZOP) += lzo1x_1.o lzo1x_1o.o lzo1x_d.o
|
||||
@@ -67,7 +67,7 @@ lib-$(CONFIG_FEATURE_UNZIP_XZ) += open_transformer.o decompress_unxz.o
|
||||
# 'gzip -d', gunzip or zcat selects FEATURE_GZIP_DECOMPRESS
|
||||
lib-$(CONFIG_FEATURE_GZIP_DECOMPRESS) += open_transformer.o decompress_gunzip.o
|
||||
lib-$(CONFIG_UNCOMPRESS) += open_transformer.o decompress_uncompress.o
|
||||
lib-$(CONFIG_UNZIP) += open_transformer.o decompress_gunzip.o
|
||||
lib-$(CONFIG_UNZIP) += open_transformer.o decompress_gunzip.o unsafe_prefix.o
|
||||
lib-$(CONFIG_RPM2CPIO) += open_transformer.o decompress_gunzip.o get_header_cpio.o
|
||||
lib-$(CONFIG_RPM) += open_transformer.o decompress_gunzip.o get_header_cpio.o
|
||||
lib-$(CONFIG_GZIP) += open_transformer.o
|
||||
|
||||
@@ -107,7 +107,7 @@ 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,
|
||||
@@ -459,7 +459,7 @@ int mainGtU(EState* state,
|
||||
* usually small, typically <= 20.
|
||||
*/
|
||||
static
|
||||
const uint32_t incs[14] ALIGN4 = {
|
||||
const uint32_t incs[14] = {
|
||||
1, 4, 13, 40, 121, 364, 1093, 3280,
|
||||
9841, 29524, 88573, 265720,
|
||||
797161, 2391484
|
||||
|
||||
@@ -99,8 +99,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;
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ typedef struct EState {
|
||||
/* for doing the block sorting */
|
||||
uint32_t *arr1;
|
||||
uint32_t *arr2;
|
||||
//uint32_t *ftab; //moved into this struct, see below
|
||||
uint32_t *ftab;
|
||||
|
||||
uint16_t *quadrant;
|
||||
int32_t budget;
|
||||
@@ -160,6 +160,9 @@ typedef struct EState {
|
||||
uint32_t bsBuff;
|
||||
int32_t bsLive;
|
||||
|
||||
/* guess what */
|
||||
uint32_t *crc32table;
|
||||
|
||||
/* block and combined CRCs */
|
||||
uint32_t blockCRC;
|
||||
uint32_t combinedCRC;
|
||||
@@ -182,12 +185,6 @@ 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];
|
||||
|
||||
@@ -65,18 +65,6 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
} while (--n != 0);
|
||||
}
|
||||
#endif
|
||||
#if ENABLE_FEATURE_PATH_TRAVERSAL_PROTECTION
|
||||
/* Skip leading "/" and past last ".." path component */
|
||||
# if ENABLE_FEATURE_TAR_LONG_OPTIONS
|
||||
dst_name = (char *)skip_unsafe_prefix(dst_name);
|
||||
# else /* dst_name is file_header->name, which is free()d by e.g. cpio - must copy, can't just advance the ptr */
|
||||
strip_unsafe_prefix(dst_name);
|
||||
# endif
|
||||
#endif
|
||||
// ^^^ This may be a problem if some applets do need to extract absolute names.
|
||||
// (Probably will need to invent ARCHIVE_ALLOW_UNSAFE_NAME flag).
|
||||
// You might think that rpm needs it, but in my tests rpm's internal cpio
|
||||
// archive has names like "./usr/bin/FOO", not "/usr/bin/FOO".
|
||||
|
||||
if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
|
||||
char *slash = strrchr(dst_name, '/');
|
||||
@@ -171,10 +159,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 */
|
||||
@@ -189,7 +173,8 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
|
||||
/* To avoid a directory traversal attack via symlinks,
|
||||
* do not restore symlinks with ".." components
|
||||
* or symlinks starting with "/"
|
||||
* or symlinks starting with "/", unless a magic
|
||||
* envvar is set.
|
||||
*
|
||||
* For example, consider a .tar created via:
|
||||
* $ tar cvf bug.tar anything.txt
|
||||
|
||||
@@ -20,7 +20,7 @@ enum {
|
||||
TAR_MAX,
|
||||
};
|
||||
|
||||
static const char *const tar_var[] ALIGN_PTR = {
|
||||
static const char *const tar_var[] = {
|
||||
// "FILETYPE",
|
||||
"MODE",
|
||||
"FILENAME",
|
||||
|
||||
@@ -463,12 +463,13 @@ static int get_next_block(bunzip_data *bd)
|
||||
symbols, but a run of length 0 doesn't mean anything in this
|
||||
context). Thus space is saved. */
|
||||
runCnt += (runPos << nextSym); /* +runPos if RUNA; +2*runPos if RUNB */
|
||||
/* Catch 32-bit overflow (we have an example .bz2 file) */
|
||||
if ((int)runCnt < 0) {
|
||||
dbg("runCnt:%u > dbufSize:%u RETVAL_DATA_ERROR",
|
||||
runCnt, bd->dbufSize);
|
||||
return RETVAL_DATA_ERROR;
|
||||
}
|
||||
//The 32-bit overflow of runCnt wasn't yet seen, but probably can happen.
|
||||
//This would be the fix (catches too large count way before it can overflow):
|
||||
// if (runCnt > bd->dbufSize) {
|
||||
// dbg("runCnt:%u > dbufSize:%u RETVAL_DATA_ERROR",
|
||||
// runCnt, bd->dbufSize);
|
||||
// return RETVAL_DATA_ERROR;
|
||||
// }
|
||||
if (runPos < bd->dbufSize) runPos <<= 1;
|
||||
goto end_of_huffman_loop;
|
||||
}
|
||||
@@ -653,7 +654,7 @@ static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
|
||||
/* Subtract the 1 copy we'd output anyway to get extras */
|
||||
--bd->writeCopies;
|
||||
}
|
||||
} /* for (;;) */
|
||||
} /* for(;;) */
|
||||
|
||||
/* Decompression of this input block completed successfully */
|
||||
bd->writeCRC = CRC = ~CRC;
|
||||
|
||||
@@ -194,14 +194,14 @@ struct cp_ext {
|
||||
};
|
||||
/* 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 = {
|
||||
static const struct cp_ext lit = {
|
||||
/*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 */
|
||||
};
|
||||
/* Copy offsets and extra bits for distance codes 0..29 */
|
||||
static const struct cp_ext dist ALIGN2 = {
|
||||
static const struct cp_ext dist = {
|
||||
/*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 }
|
||||
@@ -230,8 +230,9 @@ static void huft_free(huft_t *p)
|
||||
* 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);
|
||||
if (BAD_HUFT(p)) {
|
||||
p = (huft_t*)((uintptr_t)(p) ^ (uintptr_t)(ERR_RET));
|
||||
}
|
||||
|
||||
/* Go through linked list, freeing from the malloced (t[-1]) address. */
|
||||
while (p) {
|
||||
|
||||
@@ -290,11 +290,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;
|
||||
|
||||
@@ -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)
|
||||
@@ -92,12 +92,8 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
|
||||
/* 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));
|
||||
@@ -124,7 +120,7 @@ char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
|
||||
typed->name[strcspn(typed->name, " /")] = '\0';
|
||||
|
||||
if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
|
||||
archive_handle->action_header(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;
|
||||
|
||||
@@ -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;
|
||||
@@ -43,7 +43,6 @@ char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
|
||||
bb_simple_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"
|
||||
@@ -125,7 +124,7 @@ char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
|
||||
//cpio: etc/hosts not created: newer or same age file exists
|
||||
//etc/hosts <-- should NOT show it
|
||||
//2 blocks <-- should say "0 blocks"
|
||||
archive_handle->action_header(archive_handle);
|
||||
archive_handle->action_header(file_header);
|
||||
} else {
|
||||
data_skip(archive_handle);
|
||||
}
|
||||
|
||||
@@ -147,14 +147,6 @@ static void process_pax_hdr(archive_handle_t *archive_handle, unsigned sz, int g
|
||||
#endif
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_TAR_GNU_EXTENSIONS
|
||||
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");
|
||||
}
|
||||
#endif
|
||||
|
||||
char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
{
|
||||
file_header_t *file_header = archive_handle->file_header;
|
||||
@@ -339,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 */
|
||||
@@ -347,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.
|
||||
@@ -358,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
|
||||
@@ -396,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);
|
||||
@@ -407,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;
|
||||
@@ -454,22 +446,15 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
#endif
|
||||
|
||||
/* Everything up to and including last ".." component is stripped */
|
||||
strip_unsafe_prefix(file_header->name);
|
||||
if (file_header->link_target && !S_ISLNK(file_header->mode)) {
|
||||
/* GNU tar 1.34 examples:
|
||||
* tar: Removing leading '/' from hard link targets
|
||||
* tar: Removing leading '../' from hard link targets
|
||||
* tar: Removing leading 'etc/../' from hard link targets
|
||||
*/
|
||||
strip_unsafe_prefix(file_header->link_target);
|
||||
}
|
||||
overlapping_strcpy(file_header->name, strip_unsafe_prefix(file_header->name));
|
||||
//TODO: do the same for file_header->link_target?
|
||||
|
||||
/* Strip trailing '/' in directories */
|
||||
/* Must be done after mode is set as '/' is used to check if it's a directory */
|
||||
cp = last_char_is(file_header->name, '/');
|
||||
|
||||
if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
|
||||
archive_handle->action_header(archive_handle);
|
||||
archive_handle->action_header(/*archive_handle->*/ file_header);
|
||||
/* Note that we kill the '/' only after action_header() */
|
||||
/* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */
|
||||
if (cp)
|
||||
|
||||
@@ -5,13 +5,8 @@
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
void FAST_FUNC header_list(archive_handle_t *archive_handle)
|
||||
void FAST_FUNC header_list(const file_header_t *file_header)
|
||||
{
|
||||
//TODO: cpio -vp DIR should output "DIR/NAME", not just "NAME" */
|
||||
|
||||
const file_header_t *file_header = archive_handle->file_header;
|
||||
/* "tar xvOf TARFILE >OUTFILE" should use stderr for file list output: */
|
||||
FILE *fp = (archive_handle->action_data == data_extract_to_stdout) ? stderr : stdout;
|
||||
|
||||
fprintf(fp, "%s\n", printable_string(file_header->name));
|
||||
puts(file_header->name);
|
||||
}
|
||||
@@ -5,6 +5,6 @@
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
void FAST_FUNC header_skip(archive_handle_t *archive_handle UNUSED_PARAM)
|
||||
void FAST_FUNC header_skip(const file_header_t *file_header UNUSED_PARAM)
|
||||
{
|
||||
}
|
||||
@@ -5,14 +5,10 @@
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
void FAST_FUNC header_verbose_list(archive_handle_t *archive_handle)
|
||||
void FAST_FUNC header_verbose_list(const file_header_t *file_header)
|
||||
{
|
||||
const file_header_t *file_header = archive_handle->file_header;
|
||||
struct tm tm_time;
|
||||
struct tm *ptm = &tm_time; //localtime(&file_header->mtime);
|
||||
char modestr[12];
|
||||
/* "tar xvvOf TARFILE >OUTFILE" should use stderr for file list output: */
|
||||
FILE *fp = (archive_handle->action_data == data_extract_to_stdout) ? stderr : stdout;
|
||||
|
||||
#if ENABLE_FEATURE_TAR_UNAME_GNAME
|
||||
char uid[sizeof(int)*3 + 2];
|
||||
@@ -32,8 +28,8 @@ void FAST_FUNC header_verbose_list(archive_handle_t *archive_handle)
|
||||
/*sprintf(gid, "%u", (unsigned)file_header->gid);*/
|
||||
group = utoa(file_header->gid);
|
||||
}
|
||||
fprintf(fp, "%s %s/%s %9"OFF_FMT"u %4u-%02u-%02u %02u:%02u:%02u %s",
|
||||
bb_mode_string(modestr, file_header->mode),
|
||||
printf("%s %s/%s %9"OFF_FMT"u %4u-%02u-%02u %02u:%02u:%02u %s",
|
||||
bb_mode_string(file_header->mode),
|
||||
user,
|
||||
group,
|
||||
file_header->size,
|
||||
@@ -43,14 +39,14 @@ void FAST_FUNC header_verbose_list(archive_handle_t *archive_handle)
|
||||
ptm->tm_hour,
|
||||
ptm->tm_min,
|
||||
ptm->tm_sec,
|
||||
printable_string(file_header->name));
|
||||
file_header->name);
|
||||
|
||||
#else /* !FEATURE_TAR_UNAME_GNAME */
|
||||
|
||||
localtime_r(&file_header->mtime, ptm);
|
||||
|
||||
fprintf(fp, "%s %u/%u %9"OFF_FMT"u %4u-%02u-%02u %02u:%02u:%02u %s",
|
||||
bb_mode_string(modestr, file_header->mode),
|
||||
printf("%s %u/%u %9"OFF_FMT"u %4u-%02u-%02u %02u:%02u:%02u %s",
|
||||
bb_mode_string(file_header->mode),
|
||||
(unsigned)file_header->uid,
|
||||
(unsigned)file_header->gid,
|
||||
file_header->size,
|
||||
@@ -60,13 +56,13 @@ void FAST_FUNC header_verbose_list(archive_handle_t *archive_handle)
|
||||
ptm->tm_hour,
|
||||
ptm->tm_min,
|
||||
ptm->tm_sec,
|
||||
printable_string(file_header->name));
|
||||
file_header->name);
|
||||
|
||||
#endif /* FEATURE_TAR_UNAME_GNAME */
|
||||
|
||||
/* NB: GNU tar shows "->" for symlinks and "link to" for hardlinks */
|
||||
if (file_header->link_target) {
|
||||
fprintf(fp, " -> %s", printable_string(file_header->link_target));
|
||||
printf(" -> %s", file_header->link_target);
|
||||
}
|
||||
putc('\n', fp);
|
||||
bb_putchar('\n');
|
||||
}
|
||||
@@ -797,6 +797,7 @@ static int lzo1x_999_compress_internal(const uint8_t *in, unsigned in_len,
|
||||
if (swd->use_best_off)
|
||||
better_match(swd, &m_len, &m_off);
|
||||
#endif
|
||||
|
||||
/* shall we try a lazy match ? */
|
||||
ahead = 0;
|
||||
if (m_len >= max_lazy) {
|
||||
@@ -810,6 +811,7 @@ static int lzo1x_999_compress_internal(const uint8_t *in, unsigned in_len,
|
||||
max_ahead = LZO_MIN(2, (unsigned)l1 - 1);
|
||||
}
|
||||
|
||||
|
||||
while (ahead < max_ahead && c->look > m_len) {
|
||||
int lazy_match_min_gain;
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ void FAST_FUNC fork_transformer(int fd, const char *transform_prog)
|
||||
/* Used by e.g. rpm which gives us a fd without filename,
|
||||
* thus we can't guess the format from filename's extension.
|
||||
*/
|
||||
static transformer_state_t *setup_transformer_on_fd(int fd, int die_if_not_compressed)
|
||||
static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_compressed)
|
||||
{
|
||||
transformer_state_t *xstate;
|
||||
|
||||
@@ -204,7 +204,7 @@ static transformer_state_t *setup_transformer_on_fd(int fd, int die_if_not_compr
|
||||
}
|
||||
|
||||
/* No known magic seen */
|
||||
if (die_if_not_compressed)
|
||||
if (fail_if_not_compressed)
|
||||
bb_simple_error_msg_and_die("no gzip"
|
||||
IF_FEATURE_SEAMLESS_BZ2("/bzip2")
|
||||
IF_FEATURE_SEAMLESS_XZ("/xz")
|
||||
@@ -240,15 +240,13 @@ static void fork_transformer_and_free(transformer_state_t *xstate)
|
||||
/* Used by e.g. rpm which gives us a fd without filename,
|
||||
* thus we can't guess the format from filename's extension.
|
||||
*/
|
||||
int FAST_FUNC setup_unzip_on_fd(int fd, int die_if_not_compressed)
|
||||
int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
|
||||
{
|
||||
transformer_state_t *xstate = setup_transformer_on_fd(fd, die_if_not_compressed);
|
||||
transformer_state_t *xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
|
||||
|
||||
if (!xstate->xformer) {
|
||||
/* Not compressed */
|
||||
int retval = xstate->signature_skipped; /* never zero */
|
||||
free(xstate);
|
||||
return retval;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fork_transformer_and_free(xstate);
|
||||
@@ -266,7 +264,7 @@ void FAST_FUNC setup_lzma_on_fd(int fd)
|
||||
}
|
||||
#endif
|
||||
|
||||
static transformer_state_t *open_transformer(const char *fname, int die_if_not_compressed)
|
||||
static transformer_state_t *open_transformer(const char *fname, int fail_if_not_compressed)
|
||||
{
|
||||
transformer_state_t *xstate;
|
||||
int fd;
|
||||
@@ -286,18 +284,18 @@ static transformer_state_t *open_transformer(const char *fname, int die_if_not_c
|
||||
}
|
||||
}
|
||||
|
||||
xstate = setup_transformer_on_fd(fd, die_if_not_compressed);
|
||||
xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
|
||||
|
||||
return xstate;
|
||||
}
|
||||
|
||||
int FAST_FUNC open_zipped(const char *fname, int die_if_not_compressed)
|
||||
int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
|
||||
{
|
||||
int fd;
|
||||
transformer_state_t *xstate;
|
||||
|
||||
xstate = open_transformer(fname, die_if_not_compressed);
|
||||
if (!xstate) /* open error */
|
||||
xstate = open_transformer(fname, fail_if_not_compressed);
|
||||
if (!xstate)
|
||||
return -1;
|
||||
|
||||
fd = xstate->src_fd;
|
||||
@@ -328,7 +326,7 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
|
||||
transformer_state_t *xstate;
|
||||
char *image;
|
||||
|
||||
xstate = open_transformer(fname, /*die_if_not_compressed:*/ 0);
|
||||
xstate = open_transformer(fname, /*fail_if_not_compressed:*/ 0);
|
||||
if (!xstate) /* file open error */
|
||||
return NULL;
|
||||
|
||||
@@ -373,7 +371,7 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
|
||||
int fd;
|
||||
char *image;
|
||||
|
||||
fd = open_zipped(fname, /*die_if_not_compressed:*/ 0);
|
||||
fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -5,48 +5,23 @@
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
const char* FAST_FUNC skip_unsafe_prefix(const char *str)
|
||||
const char* FAST_FUNC strip_unsafe_prefix(const char *str)
|
||||
{
|
||||
const char *cp = str;
|
||||
|
||||
/* if "..": malicious, return "" */
|
||||
/* (causes harmless errors trying to create or hardlink a file named "") */
|
||||
if (cp[0] == '.' && cp[1] == '.' && cp[2] == '\0')
|
||||
return cp + 2;
|
||||
|
||||
while (1) {
|
||||
const char *cp2;
|
||||
char *cp2;
|
||||
if (*cp == '/') {
|
||||
cp++;
|
||||
continue;
|
||||
}
|
||||
/* We are called lots of times.
|
||||
* is_prefixed_with(cp, "../") is slower than open-coding it,
|
||||
* with minimal code growth (~few bytes).
|
||||
*/
|
||||
if (cp[0] == '.' && cp[1] == '.' && cp[2] == '/') {
|
||||
if (is_prefixed_with(cp, "/../"+1)) {
|
||||
cp += 3;
|
||||
continue;
|
||||
}
|
||||
cp2 = cp;
|
||||
find_dotdot:
|
||||
cp2 = strstr(cp2, "/..");
|
||||
cp2 = strstr(cp, "/../");
|
||||
if (!cp2)
|
||||
break; /* No (more) malicious components */
|
||||
|
||||
/* We found "/..something" */
|
||||
cp2 += 3;
|
||||
if (*cp2 != '/') {
|
||||
if (*cp2 == '\0') {
|
||||
/* Trailing "/..": malicious, return "" */
|
||||
/* (causes harmless errors trying to create or hardlink a file named "") */
|
||||
return cp2;
|
||||
}
|
||||
/* "/..name" is not malicious, look for next "/.." */
|
||||
goto find_dotdot;
|
||||
}
|
||||
/* Found "/../": malicious, advance past it */
|
||||
cp = cp2 + 1;
|
||||
break;
|
||||
cp = cp2 + 4;
|
||||
}
|
||||
if (cp != str) {
|
||||
static smallint warned = 0;
|
||||
@@ -58,8 +33,3 @@ const char* FAST_FUNC skip_unsafe_prefix(const char *str)
|
||||
}
|
||||
return cp;
|
||||
}
|
||||
|
||||
void FAST_FUNC strip_unsafe_prefix(char *str)
|
||||
{
|
||||
overlapping_strcpy(str, skip_unsafe_prefix(str));
|
||||
}
|
||||
@@ -17,65 +17,25 @@ void FAST_FUNC create_or_remember_link(llist_t **link_placeholders,
|
||||
return;
|
||||
}
|
||||
if (symlink(target, linkname) != 0) {
|
||||
// shared message
|
||||
/* shared message */
|
||||
bb_perror_msg_and_die("can't create %slink '%s' to '%s'",
|
||||
"sym", linkname, target
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static int all_dir_components_are_dirs(char *path)
|
||||
{
|
||||
struct stat sb;
|
||||
char *slash;
|
||||
|
||||
slash = path;
|
||||
while ((slash = strchr(slash, '/')) != NULL) {
|
||||
int ok;
|
||||
*slash = '\0';
|
||||
ok = (lstat(path, &sb) == 0 && S_ISDIR(sb.st_mode));
|
||||
*slash++ = '/';
|
||||
if (!ok)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void FAST_FUNC create_links_from_list(llist_t *list)
|
||||
{
|
||||
// This idea sounds better (fewer file ops):
|
||||
// Create hardlinks first, then symlinks.
|
||||
// ^^^ but it breaks the "hardlink to symlink" case:
|
||||
// mkdir dir
|
||||
// >dir/a
|
||||
// ln -s ../dir/a dir/b
|
||||
// ln dir/b dir/c
|
||||
// mkdir new; tar cf - dir/a dir/b dir/c | tar -C new -xvf -
|
||||
// On extract, 'c' will have no 'b' to be a hardlink to.
|
||||
|
||||
while (list) {
|
||||
char *target;
|
||||
char *linkname = list->data + 1;
|
||||
|
||||
target = linkname + strlen(linkname) + 1;
|
||||
|
||||
// Check whether the linkname has suspicious components:
|
||||
// SYMLINK_OUTSIDE_EXTRACT_DIR/etc/passwd sort of thing.
|
||||
// (Our delayed creation of symlinks is not defending against it,
|
||||
// we could create SYMLINK_OUTSIDE_EXTRACT_DIR ourself!)
|
||||
if (!all_dir_components_are_dirs(linkname)) {
|
||||
errno = ENOTDIR;
|
||||
goto err;
|
||||
}
|
||||
if ((list->data[0] ? link : symlink) (target, linkname)) {
|
||||
// shared message
|
||||
err:
|
||||
bb_perror_msg_and_die("can't create %slink '%s' to '%s'",
|
||||
list->data[0] ? "hard" : "sym",
|
||||
linkname, target
|
||||
target = list->data + 1 + strlen(list->data + 1) + 1;
|
||||
if ((*list->data ? link : symlink) (target, list->data + 1)) {
|
||||
/* shared message */
|
||||
bb_error_msg_and_die("can't create %slink '%s' to '%s'",
|
||||
*list->data ? "hard" : "sym",
|
||||
list->data + 1, target
|
||||
);
|
||||
// Note: GNU tar 1.34 errors out only _after_ all links are (attempted to be) created
|
||||
// We bail out at once
|
||||
}
|
||||
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;
|
||||
|
||||
|
||||
+8
-8
@@ -25,7 +25,7 @@
|
||||
"Minimalized" for busybox by Alain Knaff
|
||||
*/
|
||||
//config:config LZOP
|
||||
//config: bool "lzop (13 kb)"
|
||||
//config: bool "lzop (12 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Lzop compression/decompresion.
|
||||
@@ -86,7 +86,6 @@
|
||||
//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"
|
||||
|
||||
@@ -154,7 +153,7 @@ static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
unsigned lit = 0;
|
||||
unsigned next_lit = NO_LIT;
|
||||
unsigned nl;
|
||||
// unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
|
||||
unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
|
||||
|
||||
// LZO_UNUSED(wrkmem);
|
||||
|
||||
@@ -208,6 +207,7 @@ static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
lit = 0;
|
||||
goto match_done;
|
||||
|
||||
|
||||
/* handle matches */
|
||||
do {
|
||||
if (t < 16) { /* a M1 match */
|
||||
@@ -227,7 +227,7 @@ static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
*litp = (unsigned char)((*litp & ~3) | lit);
|
||||
/* copy over the 2 literals that replace the match */
|
||||
copy2(ip-2, m_pos, pd(op, m_pos));
|
||||
// o_m1_a++;
|
||||
o_m1_a++;
|
||||
}
|
||||
/* test if a literal run follows */
|
||||
else
|
||||
@@ -249,7 +249,7 @@ static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
lit += 2 + t + 3;
|
||||
*litp = (unsigned char)(lit - 3);
|
||||
|
||||
// o_m1_b++;
|
||||
o_m1_b++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
goto copy_literal_run;
|
||||
@@ -284,7 +284,7 @@ static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
/* set new length of previous literal run */
|
||||
lit += 3 + t + 3;
|
||||
*litp = (unsigned char)(lit - 3);
|
||||
// o_m2++;
|
||||
o_m2++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
@@ -330,7 +330,7 @@ static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
*litp = (unsigned char)((*litp & ~3) | lit);
|
||||
/* copy over the 3 literals that replace the match */
|
||||
copy3(ip-3, m_pos, pd(op, m_pos));
|
||||
// o_m3_a++;
|
||||
o_m3_a++;
|
||||
}
|
||||
/* test if a literal run follows */
|
||||
else if (t == 1 && lit <= 3 && nl == 0
|
||||
@@ -349,7 +349,7 @@ static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
lit += 3 + t + 3;
|
||||
*litp = (unsigned char)(lit - 3);
|
||||
|
||||
// o_m3_b++;
|
||||
o_m3_b++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
*op++ = *m_pos++;
|
||||
|
||||
+9
-22
@@ -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)
|
||||
@@ -116,13 +114,8 @@ static int rpm_gettags(const char *filename)
|
||||
xread(fd, &header, sizeof(header));
|
||||
if (header.magic_and_ver != htonl(RPM_HEADER_MAGICnVER))
|
||||
bb_error_msg_and_die("invalid RPM header magic in '%s'", filename);
|
||||
#if BB_LITTLE_ENDIAN
|
||||
header.size = ntohl(header.size);
|
||||
#endif
|
||||
cnt = ntohl(header.entries);
|
||||
/* Prevent multiplication overflow leading to undersized allocations */
|
||||
if (sizeof(cnt) <= 4 && cnt >= 0x00400000) /* ~4 million */
|
||||
bb_error_msg_and_die("bad RPM header.entries:%u", cnt);
|
||||
storepos += sizeof(header) + cnt * 16;
|
||||
|
||||
G.tagcount += cnt;
|
||||
@@ -130,11 +123,9 @@ static int rpm_gettags(const char *filename)
|
||||
xread(fd, &tags[idx], sizeof(tags[0]) * cnt);
|
||||
while (cnt--) {
|
||||
rpm_index *tag = &tags[idx];
|
||||
#if BB_LITTLE_ENDIAN
|
||||
tag->tag = ntohl(tag->tag);
|
||||
tag->type = ntohl(tag->type);
|
||||
tag->count = ntohl(tag->count);
|
||||
#endif
|
||||
tag->offset = storepos + ntohl(tag->offset);
|
||||
if (pass == 0)
|
||||
tag->tag -= 743;
|
||||
@@ -150,10 +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;
|
||||
G.map = mmap_read(fd, storepos);
|
||||
/* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */
|
||||
G.map = mmap(0, storepos, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (G.map == MAP_FAILED)
|
||||
bb_perror_msg_and_die("mmap '%s'", filename);
|
||||
|
||||
@@ -322,7 +314,7 @@ static void extract_cpio(int fd, const char *source_rpm)
|
||||
archive_handle->src_fd = fd;
|
||||
/*archive_handle->offset = 0; - init_handle() did it */
|
||||
|
||||
setup_unzip_on_fd(archive_handle->src_fd, /*die_if_not_compressed:*/ 1);
|
||||
setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 1);
|
||||
while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
|
||||
continue;
|
||||
}
|
||||
@@ -364,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) {
|
||||
@@ -531,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]);
|
||||
|
||||
@@ -539,20 +531,15 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
// /* We need to know whether child (gzip/bzip/etc) exits abnormally */
|
||||
// signal(SIGCHLD, check_errors_in_children);
|
||||
|
||||
str = rpm_getstr0(TAG_PAYLOADCOMPRESSOR);
|
||||
if (ENABLE_FEATURE_SEAMLESS_LZMA
|
||||
&& str != NULL
|
||||
&& (str = rpm_getstr0(TAG_PAYLOADCOMPRESSOR)) != NULL
|
||||
&& strcmp(str, "lzma") == 0
|
||||
) {
|
||||
// lzma compression can't be detected
|
||||
// set up decompressor without detection
|
||||
setup_lzma_on_fd(rpm_fd);
|
||||
} else {
|
||||
int signature_bytes = setup_unzip_on_fd(rpm_fd, /*die_if_not_compressed:*/ 0);
|
||||
if (signature_bytes != 0) {
|
||||
xlseek(rpm_fd, - signature_bytes, SEEK_CUR);
|
||||
bb_error_msg("warning, unknown compression '%s'", str);
|
||||
}
|
||||
setup_unzip_on_fd(rpm_fd, /*fail_if_not_compressed:*/ 1);
|
||||
}
|
||||
|
||||
if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
|
||||
|
||||
+60
-45
@@ -119,7 +119,6 @@
|
||||
#include "libbb.h"
|
||||
#include "common_bufsiz.h"
|
||||
#include "bb_archive.h"
|
||||
#include "unicode.h"
|
||||
/* FIXME: Stop using this non-standard feature */
|
||||
#ifndef FNM_LEADING_DIR
|
||||
# define FNM_LEADING_DIR 0
|
||||
@@ -255,6 +254,32 @@ 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)
|
||||
{
|
||||
@@ -272,8 +297,7 @@ static void writeLongname(int fd, int type, const char *name, int dir)
|
||||
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));
|
||||
memset(header.mode, '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';
|
||||
@@ -285,7 +309,7 @@ static void writeLongname(int fd, int type, const char *name, int dir)
|
||||
/* + dir: account for possible '/' */
|
||||
|
||||
PUT_OCTAL(header.size, size);
|
||||
chksum_and_xwrite_tar_header(fd, &header);
|
||||
chksum_and_xwrite(fd, &header);
|
||||
|
||||
/* Write filename[/] and pad the block. */
|
||||
/* dir=0: writes 'name<NUL>', pads */
|
||||
@@ -416,7 +440,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) {
|
||||
@@ -428,7 +453,7 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
|
||||
/* GNU "tar cvvf" prints "extended" listing a-la "ls -l" */
|
||||
/* We don't have such excesses here: for us "v" == "vv" */
|
||||
/* '/' is probably a GNUism */
|
||||
fprintf(vbFd, "%s%s\n", printable_string(header_name),
|
||||
fprintf(vbFd, "%s%s\n", header_name,
|
||||
S_ISDIR(statbuf->st_mode) ? "/" : "");
|
||||
}
|
||||
|
||||
@@ -465,25 +490,21 @@ 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;
|
||||
|
||||
DBG("writeFileToTarball('%s')", fileName);
|
||||
|
||||
/* Strip leading '/' and such (must be before memorizing hardlink's name) */
|
||||
header_name = skip_unsafe_prefix(fileName);
|
||||
header_name = strip_unsafe_prefix(fileName);
|
||||
|
||||
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);
|
||||
@@ -517,6 +538,9 @@ 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");
|
||||
@@ -529,13 +553,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 */
|
||||
@@ -622,7 +646,7 @@ static void NOINLINE vfork_compressor(int tar_fd, const char *gzip)
|
||||
execlp(gzip, gzip, "-f", (char *)0);
|
||||
|
||||
vfork_exec_errno = errno;
|
||||
_exit_FAILURE();
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* parent */
|
||||
@@ -675,7 +699,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;
|
||||
}
|
||||
@@ -749,7 +773,7 @@ static llist_t *append_file_list_to_list(llist_t *list)
|
||||
//usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m")
|
||||
//usage: "vokO] "
|
||||
//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, ")
|
||||
@@ -783,11 +807,6 @@ static llist_t *append_file_list_to_list(llist_t *list)
|
||||
//usage: IF_FEATURE_SEAMLESS_BZ2(
|
||||
//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: )
|
||||
//usage: )
|
||||
//usage: "\n -a (De)compress based on extension"
|
||||
//usage: IF_FEATURE_TAR_CREATE(
|
||||
//usage: "\n -h Follow symlinks"
|
||||
@@ -799,21 +818,21 @@ 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:
|
||||
// lzma
|
||||
// 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 ,)
|
||||
@@ -1106,15 +1125,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);
|
||||
@@ -1134,9 +1152,6 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (tar_handle->accept || tar_handle->reject)
|
||||
tar_handle->filter = filter_accept_reject_list;
|
||||
|
||||
/* For "tar xvf/tvf" output and Unicode in names */
|
||||
init_unicode();
|
||||
|
||||
/* Open the tar file */
|
||||
{
|
||||
int tar_fd = STDIN_FILENO;
|
||||
@@ -1168,7 +1183,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
* on e.g. tarball with 1st file named "BZh5".
|
||||
*/
|
||||
) {
|
||||
tar_handle->src_fd = open_zipped(tar_filename, /*die_if_not_compressed:*/ 0);
|
||||
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 {
|
||||
|
||||
+9
-51
@@ -56,17 +56,15 @@
|
||||
//kbuild:lib-$(CONFIG_UNZIP) += unzip.o
|
||||
|
||||
//usage:#define unzip_trivial_usage
|
||||
//usage: "[-lnojpqK] 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 -K Do not clear SUID bit"
|
||||
//usage: "\n -x FILE Exclude FILEs"
|
||||
//usage: "\n -d DIR Extract into DIR"
|
||||
|
||||
@@ -84,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
|
||||
};
|
||||
@@ -119,8 +115,6 @@ typedef union {
|
||||
#define FIX_ENDIANNESS_ZIP(zip) \
|
||||
do { if (BB_BIG_ENDIAN) { \
|
||||
(zip).fmt.method = SWAP_LE16((zip).fmt.method ); \
|
||||
(zip).fmt.modtime = SWAP_LE16((zip).fmt.modtime ); \
|
||||
(zip).fmt.moddate = SWAP_LE16((zip).fmt.moddate ); \
|
||||
(zip).fmt.crc32 = SWAP_LE32((zip).fmt.crc32 ); \
|
||||
(zip).fmt.cmpsize = SWAP_LE32((zip).fmt.cmpsize ); \
|
||||
(zip).fmt.ucmpsize = SWAP_LE32((zip).fmt.ucmpsize ); \
|
||||
@@ -266,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
|
||||
@@ -314,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
|
||||
@@ -456,9 +436,7 @@ 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");
|
||||
@@ -495,7 +473,6 @@ int unzip_main(int argc, char **argv)
|
||||
OPT_l = (1 << 0),
|
||||
OPT_x = (1 << 1),
|
||||
OPT_j = (1 << 2),
|
||||
OPT_K = (1 << 3),
|
||||
};
|
||||
unsigned opts;
|
||||
smallint quiet = 0;
|
||||
@@ -559,14 +536,9 @@ int unzip_main(int argc, char **argv)
|
||||
* 204372 1 file
|
||||
*/
|
||||
|
||||
//TODO: accept and ignore these?
|
||||
// -a convert to text files with 't' label, -aa: all files
|
||||
// -b do not convert to text - bbox: we don't convert anything
|
||||
// -D skip restoration of timestamps for extracted items - bbox: we don't restore these (yet?)
|
||||
// -X restore user:group ownership
|
||||
opts = 0;
|
||||
/* '-' makes getopt return 1 for non-options */
|
||||
while ((i = getopt(argc, argv, "-d:lnotpqxjvK")) != -1) {
|
||||
while ((i = getopt(argc, argv, "-d:lnopqxjv")) != -1) {
|
||||
switch (i) {
|
||||
case 'd': /* Extract to base directory */
|
||||
base_dir = optarg;
|
||||
@@ -584,14 +556,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*/
|
||||
|
||||
// NB: -c extract files to stdout/screen (unlike -p, also prints .zip and file names to stdout)
|
||||
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++;
|
||||
@@ -610,10 +576,6 @@ int unzip_main(int argc, char **argv)
|
||||
opts |= OPT_j;
|
||||
break;
|
||||
|
||||
case 'K':
|
||||
opts |= OPT_K;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
if (!src_fn) {
|
||||
/* The zip file */
|
||||
@@ -831,10 +793,7 @@ int unzip_main(int argc, char **argv)
|
||||
# endif
|
||||
if ((cdf.fmt.version_made_by >> 8) == 3) {
|
||||
/* This archive is created on Unix */
|
||||
file_mode = (cdf.fmt.external_attributes >> 16);
|
||||
if (!(opts & OPT_K))
|
||||
file_mode &= ~(mode_t)(S_ISUID | S_ISGID);
|
||||
dir_mode = file_mode;
|
||||
dir_mode = file_mode = (cdf.fmt.external_attributes >> 16);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -859,8 +818,7 @@ int unzip_main(int argc, char **argv)
|
||||
unzip_skip(zip.fmt.extra_len);
|
||||
|
||||
/* Guard against "/abspath", "/../" and similar attacks */
|
||||
// NB: UnZip 6.00 has option -: to disable this
|
||||
strip_unsafe_prefix(dst_fn);
|
||||
overlapping_strcpy(dst_fn, strip_unsafe_prefix(dst_fn));
|
||||
|
||||
/* Filter zip entries */
|
||||
if (find_list_entry(zreject, dst_fn)
|
||||
@@ -1008,6 +966,7 @@ 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"
|
||||
@@ -1015,7 +974,6 @@ int unzip_main(int argc, char **argv)
|
||||
printable_string(dst_fn)
|
||||
);
|
||||
}
|
||||
do_extract:
|
||||
#if ENABLE_FEATURE_UNZIP_CDF
|
||||
if (S_ISLNK(file_mode)) {
|
||||
if (dst_fd != STDOUT_FILENO) /* not -p? */
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
/* Add SORT_BY_ALIGNMENT to linker script (found in busybox_unstripped.out):
|
||||
## .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
|
||||
## .data : { *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*) }
|
||||
## .bss : { *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*) }
|
||||
## This will eliminate most of the padding (~3kb).
|
||||
## Hmm, "ld --sort-section alignment" should do it too.
|
||||
##
|
||||
## There is a ld hack which is meant to decrease disk usage
|
||||
## at the cost of more RAM usage (??!!) in standard ld script:
|
||||
## . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
|
||||
## Replace it with:
|
||||
## . = ALIGN (0x1000); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
|
||||
## to unconditionally align .data to the next page boundary,
|
||||
## instead of "next page, plus current offset in this page"
|
||||
*/
|
||||
|
||||
/* To reduce the number of VMAs each bbox process has,
|
||||
## move *(.bss SORT_BY_ALIGNMENT(.bss.*) ...)
|
||||
## part from .bss : {...} block to .data : { ... } block.
|
||||
## (This usually increases .data section by only one page).
|
||||
## Result:
|
||||
##
|
||||
## text data bss dec hex filename
|
||||
## 1050792 560 7580 1058932 102874 busybox.bss
|
||||
## 1050792 8149 0 1058941 10287d busybox.nobss
|
||||
##
|
||||
## $ exec busybox.bss pmap $$
|
||||
## 0000000008048000 1028K r-xp /path/to/busybox.bss
|
||||
## 0000000008149000 8K rw-p /path/to/busybox.bss
|
||||
## 000000000814b000 4K rw-p [ anon ] <---- this VMA is eliminated
|
||||
## 00000000085f5000 4K ---p [heap]
|
||||
## 00000000085f6000 4K rw-p [heap]
|
||||
## 00000000f7778000 8K rw-p [ anon ]
|
||||
## 00000000f777a000 12K r--p [vvar]
|
||||
## 00000000f777d000 8K r-xp [vdso]
|
||||
## 00000000ff7e9000 132K rw-p [stack]
|
||||
##
|
||||
## $ exec busybox.nobss pmap $$
|
||||
## 0000000008048000 1028K r-xp /path/to/busybox.nobss
|
||||
## 0000000008149000 12K rw-p /path/to/busybox.nobss
|
||||
## 00000000086f0000 4K ---p [heap]
|
||||
## 00000000086f1000 4K rw-p [heap]
|
||||
## 00000000f7783000 8K rw-p [ anon ]
|
||||
## 00000000f7785000 12K r--p [vvar]
|
||||
## 00000000f7788000 8K r-xp [vdso]
|
||||
## 00000000ffac0000 132K rw-p [stack]
|
||||
*/
|
||||
@@ -703,7 +703,6 @@ CONFIG_FEATURE_INETD_RPC=y
|
||||
CONFIG_IP=y
|
||||
CONFIG_FEATURE_IP_ADDRESS=y
|
||||
CONFIG_FEATURE_IP_LINK=y
|
||||
CONFIG_FEATURE_IP_LINK_CAN=y
|
||||
CONFIG_FEATURE_IP_ROUTE=y
|
||||
CONFIG_FEATURE_IP_TUNNEL=y
|
||||
CONFIG_FEATURE_IP_RULE=y
|
||||
|
||||
@@ -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
|
||||
|
||||
+367
-604
File diff suppressed because it is too large.
Load diff
@@ -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
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CHVT
|
||||
//config: bool "chvt (2.2 kb)"
|
||||
//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)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CLEAR
|
||||
//config: bool "clear (371 bytes)"
|
||||
//config: bool "clear (tiny)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: This program clears the terminal screen.
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config DEALLOCVT
|
||||
//config: bool "deallocvt (2.2 kb)"
|
||||
//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.9 kb)"
|
||||
//config: bool "dumpkmap (1.6 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.8 kb)"
|
||||
//config: bool "fgconsole (1.5 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.3 kb)"
|
||||
//config: bool "kbd_mode (4.1 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program reports and sets keyboard mode.
|
||||
|
||||
|
||||
@@ -10,14 +10,16 @@
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config LOADFONT
|
||||
//config: bool "loadfont (5.4 kb)"
|
||||
//config: bool "loadfont (5.2 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: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: Allows to load console screen map. Useful for i18n.
|
||||
//config:
|
||||
@@ -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"
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config LOADKMAP
|
||||
//config: bool "loadkmap (2.1 kb)"
|
||||
//config: bool "loadkmap (1.8 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: This program loads a keyboard translation table from
|
||||
//config: standard input.
|
||||
|
||||
@@ -8,8 +8,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config OPENVT
|
||||
//config: bool "openvt (7.4 kb)"
|
||||
//config: bool "openvt (7.2 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.
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config RESET
|
||||
//config: bool "reset (676 bytes)"
|
||||
//config: bool "reset (345 bytes)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: This program is used to reset the terminal screen, if it
|
||||
@@ -21,7 +21,7 @@
|
||||
//usage:#define reset_trivial_usage
|
||||
//usage: ""
|
||||
//usage:#define reset_full_usage "\n\n"
|
||||
//usage: "Reset terminal (ESC codes) and termios (signals, buffering, echo)"
|
||||
//usage: "Reset the screen"
|
||||
|
||||
/* "Standard" version of this tool is in ncurses package */
|
||||
|
||||
@@ -36,7 +36,7 @@ int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int reset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int reset_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
||||
{
|
||||
static const char *const args[] ALIGN_PTR = {
|
||||
static const char *const args[] = {
|
||||
"stty", "sane", NULL
|
||||
};
|
||||
|
||||
|
||||
+3
-15
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config RESIZE
|
||||
//config: bool "resize (1.2 kb)"
|
||||
//config: bool "resize (903 bytes)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: This program is used to (re)set the width and height of your current
|
||||
@@ -45,7 +45,7 @@ static void
|
||||
onintr(int sig UNUSED_PARAM)
|
||||
{
|
||||
tcsetattr(STDERR_FILENO, TCSANOW, old_termios_p);
|
||||
_exit_FAILURE();
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int resize_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
@@ -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.8 kb)"
|
||||
//config: bool "setconsole (3.6 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.4 kb)"
|
||||
//config: bool "setkeycodes (2.1 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.
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config SETLOGCONS
|
||||
//config: bool "setlogcons (2 kb)"
|
||||
//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.
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config SHOWKEY
|
||||
//config: bool "showkey (4.9 kb)"
|
||||
//config: bool "showkey (4.7 kb)"
|
||||
//config: default y
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: Shows keys pressed.
|
||||
|
||||
@@ -70,8 +71,8 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
INIT_G();
|
||||
|
||||
// a,k,s are all mutually exclusive
|
||||
getopt32(argv, "^" "aks" "\0" "a--ks:k--as:s--ak");
|
||||
// FIXME: aks are all mutually exclusive
|
||||
getopt32(argv, "aks");
|
||||
|
||||
// prepare for raw mode
|
||||
xget1(&tio, &tio0);
|
||||
@@ -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");
|
||||
|
||||
+4
-15
@@ -5,6 +5,10 @@
|
||||
|
||||
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 (3.7 kb)"
|
||||
//config: bool "basename (438 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;
|
||||
}
|
||||
+3
-3
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CAT
|
||||
//config: bool "cat (5.8 kb)"
|
||||
//config: bool "cat (5.6 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: cat is used to concatenate files and print them to the standard
|
||||
@@ -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)
|
||||
@@ -201,7 +201,7 @@ int cat_main(int argc UNUSED_PARAM, char **argv)
|
||||
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;
|
||||
|
||||
+2
-6
@@ -23,17 +23,13 @@
|
||||
//usage:#define chgrp_trivial_usage
|
||||
//usage: "[-Rh"IF_DESKTOP("LHPcvf")"]... 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"
|
||||
//usage: "\n -f Hide errors"
|
||||
|
||||
+11
-15
@@ -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;
|
||||
}
|
||||
|
||||
+8
-11
@@ -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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CHROOT
|
||||
//config: bool "chroot (4 kb)"
|
||||
//config: bool "chroot (3.7 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: chroot is used to change the root directory and run a command.
|
||||
|
||||
+18
-40
@@ -7,42 +7,34 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CKSUM
|
||||
//config: bool "cksum (4.3 kb)"
|
||||
//config: default y
|
||||
//config:
|
||||
//config:config CRC32
|
||||
//config: bool "crc32 (4.2 kb)"
|
||||
//config: bool "cksum (4.1 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);
|
||||
exitcode_t exit_code = EXIT_SUCCESS;
|
||||
uint32_t *crc32_table = crc32_filltable(NULL, 1);
|
||||
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++;
|
||||
@@ -51,55 +43,41 @@ 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);
|
||||
off_t filesize;
|
||||
int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
|
||||
|
||||
if (fd < 0) {
|
||||
exit_code = EXIT_FAILURE;
|
||||
continue;
|
||||
}
|
||||
|
||||
crc = IS_CKSUM ? 0 : 0xffffffff;
|
||||
IF_CKSUM(filesize = 0;)
|
||||
crc = 0;
|
||||
filesize = 0;
|
||||
#define read_buf bb_common_bufsiz1
|
||||
for (;;) {
|
||||
uoff_t t;
|
||||
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;)
|
||||
filesize += bytes_read;
|
||||
} else {
|
||||
IF_CKSUM(uoff_t t;)
|
||||
|
||||
/* Checksum filesize bytes, LSB first, and exit */
|
||||
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 */
|
||||
bytes_read = 0;
|
||||
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)
|
||||
crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
|
||||
if (fd < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
crc = ~crc;
|
||||
#if ENABLE_CKSUM
|
||||
if (IS_CKSUM)
|
||||
printf((*argv ? "%u %"OFF_FMT"u %s\n" : "%u %"OFF_FMT"u\n"),
|
||||
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);
|
||||
} 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.4 kb)"
|
||||
//config: bool "comm (4.2 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: comm is used to compare two files line by line and return
|
||||
|
||||
+98
-140
@@ -29,11 +29,6 @@
|
||||
//config: bool "Enable --reflink[=auto]"
|
||||
//config: default y
|
||||
//config: depends on FEATURE_CP_LONG_OPTIONS
|
||||
//config:
|
||||
//config:config FEATURE_CP_SPARSE
|
||||
//config: bool "Enable --sparse=WHEN"
|
||||
//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) */
|
||||
@@ -42,57 +37,10 @@
|
||||
|
||||
/* 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"
|
||||
@@ -104,17 +52,13 @@
|
||||
//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"
|
||||
#include "libcoreutils/coreutils.h"
|
||||
|
||||
#define DBG_OPTION_PARSING 0
|
||||
|
||||
/* This is a NOEXEC applet. Be very careful! */
|
||||
|
||||
int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
@@ -128,60 +72,47 @@ 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),
|
||||
OPT_reflink = 1 << (FILEUTILS_CP_OPTNUM+2),
|
||||
#endif
|
||||
};
|
||||
|
||||
#if ENABLE_FEATURE_CP_LONG_OPTIONS
|
||||
# if ENABLE_FEATURE_CP_REFLINK
|
||||
char *reflink = NULL;
|
||||
# endif
|
||||
# if ENABLE_FEATURE_CP_SPARSE
|
||||
char *sparse = NULL;
|
||||
# endif
|
||||
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
|
||||
# if ENABLE_FEATURE_CP_SPARSE
|
||||
"sparse\0" Required_argument "\xfc"
|
||||
# endif
|
||||
, &last
|
||||
# if ENABLE_FEATURE_CP_REFLINK
|
||||
, &reflink
|
||||
# endif
|
||||
# if ENABLE_FEATURE_CP_SPARSE
|
||||
// NOP for now
|
||||
, &sparse
|
||||
# endif
|
||||
);
|
||||
|
||||
# if ENABLE_FEATURE_CP_REFLINK
|
||||
BUILD_BUG_ON((int)OPT_reflink != (int)FILEUTILS_REFLINK);
|
||||
if (flags & FILEUTILS_REFLINK) {
|
||||
@@ -193,24 +124,59 @@ int cp_main(int argc, char **argv)
|
||||
bb_show_usage();
|
||||
}
|
||||
# endif
|
||||
# if DBG_OPTION_PARSING
|
||||
bb_error_msg("flags: 0x%08x", flags);
|
||||
# define showopt(o) bb_error_msg("flags & %s(%x):\t%x", #o, o, flags & o);
|
||||
showopt(FILEUTILS_RMDEST );
|
||||
showopt(OPT_parents );
|
||||
showopt(OPT_reflink );
|
||||
showopt(FILEUTILS_REFLINK_ALWAYS);
|
||||
return 0;
|
||||
# undef showopt
|
||||
# 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: */
|
||||
@@ -229,57 +195,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_stat2(last, &dest_stat,
|
||||
(flags & FILEUTILS_NO_TARGET_DIR) ? lstat : 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_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 */
|
||||
}
|
||||
} else if (flags & FILEUTILS_NO_TARGET_DIR) {
|
||||
bb_simple_error_msg_and_die("too many arguments");
|
||||
}
|
||||
/* else: last is DIR from "-t DIR" */
|
||||
|
||||
while (1) {
|
||||
#if ENABLE_FEATURE_CP_LONG_OPTIONS
|
||||
@@ -301,7 +259,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;
|
||||
}
|
||||
|
||||
+184
-302
@@ -9,52 +9,26 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config CUT
|
||||
//config: bool "cut (6.7 kb)"
|
||||
//config: bool "cut (5.8 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))
|
||||
|
||||
//kbuild:lib-$(CONFIG_CUT) += cut.o
|
||||
|
||||
//usage:#define cut_trivial_usage
|
||||
//usage: "{-b|c LIST | -f"IF_FEATURE_CUT_REGEX("|F")" LIST [-d SEP] [-s]} [-D] [-O SEP] [FILE]..."
|
||||
// --output-delimiter SEP is too long to fit into 80 char-wide help ----------------^^^^^^^^
|
||||
//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: IF_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -d SEP Input field delimiter (default -f TAB, -F run of whitespace)"
|
||||
//usage: ) IF_NOT_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -d SEP Input field delimiter (default TAB)"
|
||||
//usage: )
|
||||
//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 Drop lines with no delimiter (else print them in full)"
|
||||
//usage: "\n -D Don't sort ranges; line without delimiters has one field"
|
||||
//usage: IF_LONG_OPTS(
|
||||
//usage: "\n --output-delimiter SEP Output field delimeter"
|
||||
//usage: ) IF_NOT_LONG_OPTS(
|
||||
//usage: IF_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -O SEP Output field delimeter (default = -d for -f, one space for -F)"
|
||||
//usage: ) IF_NOT_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -O SEP Output field delimeter (default = -d)"
|
||||
//usage: )
|
||||
//usage: )
|
||||
//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"
|
||||
@@ -64,358 +38,266 @@
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
#include "xregex.h"
|
||||
#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_BYTE (1 << 0)
|
||||
#define OPT_CHAR (1 << 1)
|
||||
#define OPT_FIELDS (1 << 2)
|
||||
#define OPT_DELIM (1 << 3)
|
||||
#define OPT_ODELIM (1 << 4)
|
||||
#define OPT_SUPPRESS (1 << 5)
|
||||
#define OPT_NOSORT (1 << 6)
|
||||
#define OPT_REGEX ((1 << 7) * ENABLE_FEATURE_CUT_REGEX)
|
||||
#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_SUPPRESS_FLGS (1 << 4)
|
||||
|
||||
#define opt_REGEX (option_mask32 & OPT_REGEX)
|
||||
struct cut_list {
|
||||
int startpos;
|
||||
int endpos;
|
||||
};
|
||||
|
||||
struct cut_range {
|
||||
unsigned startpos;
|
||||
unsigned endpos;
|
||||
enum {
|
||||
BOL = 0,
|
||||
EOL = INT_MAX,
|
||||
NON_RANGE = -1
|
||||
};
|
||||
|
||||
static int cmpfunc(const void *a, const void *b)
|
||||
{
|
||||
const struct cut_range *aa = a;
|
||||
const struct cut_range *bb = b;
|
||||
return aa->startpos - bb->startpos;
|
||||
return (((struct cut_list *) a)->startpos -
|
||||
((struct cut_list *) b)->startpos);
|
||||
}
|
||||
|
||||
#define END_OF_LIST(list_elem) ((list_elem).startpos == UINT_MAX)
|
||||
#define NOT_END_OF_LIST(list_elem) ((list_elem).startpos != UINT_MAX)
|
||||
|
||||
static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||
const struct cut_range *cut_list)
|
||||
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 */
|
||||
int first_print = 1;
|
||||
|
||||
/* go through every line in the file */
|
||||
while ((line = xmalloc_fgetline(file)) != NULL) {
|
||||
|
||||
/* set up a list so we can keep track of what's been printed */
|
||||
unsigned linelen = strlen(line);
|
||||
int linelen = strlen(line);
|
||||
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 & (OPT_CHAR | OPT_BYTE)) {
|
||||
char *printed = xzalloc(linelen + 1);
|
||||
int need_odelim = 0;
|
||||
|
||||
/* 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 (; NOT_END_OF_LIST(cut_list[cl_pos]); cl_pos++) {
|
||||
unsigned spos = cut_list[cl_pos].startpos;
|
||||
for (; cl_pos < nlists; cl_pos++) {
|
||||
spos = cut_lists[cl_pos].startpos;
|
||||
while (spos < linelen) {
|
||||
if (!printed[spos]) {
|
||||
printed[spos] = 'X';
|
||||
if (need_odelim && spos != 0 && !printed[spos-1]) {
|
||||
need_odelim = 0;
|
||||
fputs_stdout(odelim);
|
||||
}
|
||||
putchar(line[spos]);
|
||||
}
|
||||
spos++;
|
||||
if (spos > cut_list[cl_pos].endpos) {
|
||||
/* will print OSEP (if not empty) */
|
||||
need_odelim = (odelim && odelim[0]);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
free(printed);
|
||||
/* Cut by lines */
|
||||
} else if (!opt_REGEX && *delim == '\n') {
|
||||
unsigned spos = cut_list[cl_pos].startpos;
|
||||
} else if (delim == '\n') { /* cut by lines */
|
||||
spos = cut_lists[cl_pos].startpos;
|
||||
|
||||
linenum++;
|
||||
/* get out if we have no more ranges to process or if the lines
|
||||
/* get out if we have no more lists to process or if the lines
|
||||
* are lower than what we're interested in */
|
||||
if (linenum <= spos || END_OF_LIST(cut_list[cl_pos]))
|
||||
if (((int)linenum < spos) || (cl_pos >= nlists))
|
||||
goto next_line;
|
||||
|
||||
/* if the line we're looking for is lower than the one we were
|
||||
* passed, it means we displayed it already, so move on */
|
||||
while (++spos < linenum) {
|
||||
while (spos < (int)linenum) {
|
||||
spos++;
|
||||
/* go to the next list if we're at the end of this one */
|
||||
if (spos > cut_list[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 ranges to process */
|
||||
if (END_OF_LIST(cut_list[cl_pos]))
|
||||
/* get out if there's no more lists to process */
|
||||
if (cl_pos >= nlists)
|
||||
goto next_line;
|
||||
spos = cut_list[cl_pos].startpos;
|
||||
spos = cut_lists[cl_pos].startpos;
|
||||
/* get out if the current line is lower than the one
|
||||
* we just became interested in */
|
||||
if (linenum <= spos)
|
||||
if ((int)linenum < spos)
|
||||
goto next_line;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we made it here, it means we've found the line we're
|
||||
* looking for, so print it */
|
||||
if (first_print) {
|
||||
first_print = 0;
|
||||
fputs_stdout(line);
|
||||
} else
|
||||
printf("%s%s", odelim, line);
|
||||
puts(line);
|
||||
goto next_line;
|
||||
/* Cut by fields */
|
||||
} else {
|
||||
unsigned next = 0, start = 0, end = 0;
|
||||
unsigned dcount = 0; /* we saw Nth delimiter (0 - didn't see any yet) */
|
||||
} else { /* cut by fields */
|
||||
int ndelim = -1; /* zero-based / one-based problem */
|
||||
int nfields_printed = 0;
|
||||
char *field = NULL;
|
||||
char delimiter[2];
|
||||
|
||||
/* Blank line? Check -s (later check for -s does not catch empty lines) */
|
||||
if (linelen == 0) {
|
||||
if (option_mask32 & OPT_SUPPRESS)
|
||||
goto next_line;
|
||||
delimiter[0] = delim;
|
||||
delimiter[1] = 0;
|
||||
|
||||
/* does this line contain any delimiters? */
|
||||
if (strchr(line, delim) == NULL) {
|
||||
if (!(option_mask32 & CUT_OPT_SUPPRESS_FLGS))
|
||||
puts(line);
|
||||
goto next_line;
|
||||
}
|
||||
|
||||
if (!odelim)
|
||||
odelim = "\t";
|
||||
first_print = 1;
|
||||
/* 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++;
|
||||
}
|
||||
|
||||
/* Loop through bytes, finding next delimiter */
|
||||
for (;;) {
|
||||
/* End of current range? */
|
||||
if (end == linelen || dcount > cut_list[cl_pos].endpos) {
|
||||
end_of_range:
|
||||
cl_pos++;
|
||||
if (END_OF_LIST(cut_list[cl_pos]))
|
||||
break;
|
||||
if (option_mask32 & OPT_NOSORT)
|
||||
start = dcount = next = 0;
|
||||
end = 0; /* (why?) */
|
||||
//bb_error_msg("End of current range");
|
||||
}
|
||||
/* End of current line? */
|
||||
if (next == linelen) {
|
||||
end = linelen; /* print up to end */
|
||||
/* If we've seen no delimiters, and no -D, check -s */
|
||||
if (!(option_mask32 & OPT_NOSORT) && cl_pos == 0 && dcount == 0) {
|
||||
if (option_mask32 & OPT_SUPPRESS)
|
||||
goto next_line;
|
||||
/* else: will print entire line */
|
||||
} else if (dcount < cut_list[cl_pos].startpos) {
|
||||
/* echo 1.2 | cut -d. -f1,3: prints "1", not "1." */
|
||||
//break;
|
||||
/* ^^^ this fails a case with -D:
|
||||
* echo 1 2 | cut -DF 1,3,2:
|
||||
* do not end line processing when didn't find field#3
|
||||
*/
|
||||
//if (option_mask32 & OPT_NOSORT) - no, just do it always
|
||||
goto end_of_range;
|
||||
/* 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.. */
|
||||
}
|
||||
//bb_error_msg("End of current line: s:%d e:%d", start, end);
|
||||
} else {
|
||||
/* Find next delimiter */
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
if (opt_REGEX) {
|
||||
regmatch_t rr;
|
||||
regex_t *reg = (void*) delim;
|
||||
|
||||
if (regexec(reg, line + next, 1, &rr, REG_NOTBOL|REG_NOTEOL) != 0) {
|
||||
/* not found, go to "end of line" logic */
|
||||
next = linelen;
|
||||
continue;
|
||||
}
|
||||
end = next + rr.rm_so;
|
||||
next += (rr.rm_eo ? rr.rm_eo : 1);
|
||||
/* ^^^ advancing by at least 1 prevents infinite loops */
|
||||
/* testcase: echo "no at sign" | cut -d'@*' -F 1- */
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
end = next++;
|
||||
if (line[end] != *delim)
|
||||
continue;
|
||||
}
|
||||
/* Got delimiter */
|
||||
dcount++;
|
||||
if (dcount <= cut_list[cl_pos].startpos) {
|
||||
/* Not yet within range - loop */
|
||||
start = next;
|
||||
continue;
|
||||
}
|
||||
/* -F N-M preserves intermediate delimiters: */
|
||||
//printf "1 2 3 4 5 6 7\n" | toybox cut -O: -F2,4-6,7
|
||||
//2:4 5 6:7
|
||||
if (opt_REGEX && dcount <= cut_list[cl_pos].endpos)
|
||||
continue;
|
||||
// NB: toybox does the above for -f too, but it's a compatibility bug:
|
||||
//printf "1 2 3 4 5 6 7 8\n" | toybox cut -d' ' -O: -f2,4-6,7
|
||||
//2:4 5 6:7 // WRONG!
|
||||
//printf "1 2 3 4 5 6 7 8\n" | cut -d' ' --output-delimiter=: -f2,4-6,7
|
||||
//2:4:5:6:7 // GNU coreutils 9.1
|
||||
}
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
if (end != start || !opt_REGEX)
|
||||
#endif
|
||||
{
|
||||
if (first_print) {
|
||||
first_print = 0;
|
||||
printf("%.*s", end - start, line + start);
|
||||
} else
|
||||
printf("%s%.*s", odelim, end - start, line + start);
|
||||
}
|
||||
start = next;
|
||||
//if (dcount == 0)
|
||||
// break; - why?
|
||||
} /* byte loop */
|
||||
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:
|
||||
free(line);
|
||||
} /* while (got line) */
|
||||
|
||||
/* For -d$'\n' --output-delimiter=^, the overall output is still terminated with \n, not ^ */
|
||||
if (!opt_REGEX && *delim == '\n' && !first_print)
|
||||
putchar('\n');
|
||||
linenum++;
|
||||
free(printed);
|
||||
free(orig_line);
|
||||
}
|
||||
}
|
||||
|
||||
int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
/* growable array holding a series of ranges */
|
||||
struct cut_range *cut_list = NULL;
|
||||
unsigned nranges = 0; /* number of elements in above list */
|
||||
char *LIST, *ltok;
|
||||
const char *delim = NULL;
|
||||
const char *odelim = NULL;
|
||||
/* 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;
|
||||
unsigned opt;
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
regex_t reg;
|
||||
#endif
|
||||
#if ENABLE_LONG_OPTS
|
||||
static const char cut_longopts[] ALIGN1 =
|
||||
"output-delimiter\0" Required_argument "O"
|
||||
;
|
||||
#endif
|
||||
|
||||
#define ARG "bcf"IF_FEATURE_CUT_REGEX("F")
|
||||
#if ENABLE_LONG_OPTS
|
||||
opt = getopt32long
|
||||
#else
|
||||
opt = getopt32
|
||||
#endif
|
||||
(argv, "^"
|
||||
OPT_STR // = "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
|
||||
"\0" "b:c:f:" IF_FEATURE_CUT_REGEX("F:") /* one of -bcfF is required */
|
||||
"b--"ARG":c--"ARG":f--"ARG IF_FEATURE_CUT_REGEX(":F--"ARG), /* they are mutually exclusive */
|
||||
IF_LONG_OPTS(cut_longopts,)
|
||||
&LIST, &LIST, &LIST, &delim, &odelim IF_FEATURE_CUT_REGEX(, &LIST)
|
||||
);
|
||||
if (!odelim)
|
||||
odelim = (opt & OPT_REGEX) ? " " : delim;
|
||||
if (!delim)
|
||||
delim = (opt & OPT_REGEX) ? "[[:space:]]+" : "\t";
|
||||
|
||||
opt = getopt32(argv, "^"
|
||||
OPT_STR
|
||||
"\0" "b--bcf:c--bcf:f--bcf",
|
||||
&sopt, &sopt, &sopt, <ok
|
||||
);
|
||||
// argc -= optind;
|
||||
argv += optind;
|
||||
//if (!(opt & (OPT_BYTE | OPT_CHAR | OPT_FIELDS | OPT_REGEX)))
|
||||
// bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
|
||||
//^^^ handled by getopt32
|
||||
if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS)))
|
||||
bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
|
||||
|
||||
/* non-field (char or byte) cutting has some special handling */
|
||||
if (!(opt & (OPT_FIELDS|OPT_REGEX))) {
|
||||
static const char requires_f[] ALIGN1 = " requires -f"
|
||||
IF_FEATURE_CUT_REGEX(" or -F");
|
||||
if (opt & OPT_SUPPRESS)
|
||||
bb_error_msg_and_die("-s%s", requires_f);
|
||||
if (opt & OPT_DELIM)
|
||||
bb_error_msg_and_die("-d DELIM%s", requires_f);
|
||||
if (opt & CUT_OPT_DELIM_FLGS) {
|
||||
if (ltok[0] && ltok[1]) { /* more than 1 char? */
|
||||
bb_simple_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)) {
|
||||
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);
|
||||
}
|
||||
if (delim != '\t') {
|
||||
bb_error_msg_and_die
|
||||
("a delimiter may be specified%s", _op_on_field);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* parse list and put values into startpos and endpos.
|
||||
* valid range formats: N, N-, N-M, -M
|
||||
* more than one range can be separated by commas
|
||||
* valid list formats: N, N-, N-M, -M
|
||||
* more than one list can be separated by commas
|
||||
*/
|
||||
/* take apart the ranges, one by one (separated with commas) */
|
||||
while ((ltok = strsep(&LIST, ",")) != NULL) {
|
||||
{
|
||||
char *ntok;
|
||||
int s, e;
|
||||
int s = 0, e = 0;
|
||||
|
||||
/* it's actually legal to pass an empty list */
|
||||
//if (!ltok[0])
|
||||
// continue;
|
||||
//^^^ testcase?
|
||||
/* take apart the lists, one by one (they are separated with commas) */
|
||||
while ((ltok = strsep(&sopt, ",")) != NULL) {
|
||||
|
||||
/* get the start pos */
|
||||
ntok = strsep(<ok, "-");
|
||||
if (!ntok[0]) {
|
||||
if (!ltok) /* testcase: -f '' */
|
||||
bb_show_usage();
|
||||
if (!ltok[0]) /* testcase: -f - */
|
||||
bb_show_usage();
|
||||
s = 0; /* "-M" means "1-M" */
|
||||
} else {
|
||||
/* "N" or "N-[M]" */
|
||||
/* arrays are zero based, while the user expects
|
||||
* the first field/char on the line to be char #1 */
|
||||
s = xatoi_positive(ntok) - 1;
|
||||
/* it's actually legal to pass an empty list */
|
||||
if (!ltok[0])
|
||||
continue;
|
||||
|
||||
/* get the start pos */
|
||||
ntok = strsep(<ok, "-");
|
||||
if (!ntok[0]) {
|
||||
s = BOL;
|
||||
} else {
|
||||
s = xatoi_positive(ntok);
|
||||
/* account for the fact that arrays are zero based, while
|
||||
* the user expects the first char on the line to be char #1 */
|
||||
if (s != 0)
|
||||
s--;
|
||||
}
|
||||
|
||||
/* get the end pos */
|
||||
if (ltok == NULL) {
|
||||
e = NON_RANGE;
|
||||
} else if (!ltok[0]) {
|
||||
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 (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,
|
||||
* while endpos may be = NON_RANGE (-1) */
|
||||
cut_lists[nlists].startpos = s;
|
||||
cut_lists[nlists].endpos = e;
|
||||
nlists++;
|
||||
}
|
||||
|
||||
/* get the end pos */
|
||||
if (!ltok) {
|
||||
e = s; /* "N" means "N-N" */
|
||||
} else if (!ltok[0]) {
|
||||
/* "N-" means "until the end of the line" */
|
||||
e = INT_MAX;
|
||||
} else {
|
||||
/* again, arrays are zero based, fields are 1 based */
|
||||
e = xatoi_positive(ltok) - 1;
|
||||
}
|
||||
/* make sure we got some cut positions out of all that */
|
||||
if (nlists == 0)
|
||||
bb_simple_error_msg_and_die("missing list of positions");
|
||||
|
||||
if (s < 0 || e < s)
|
||||
bb_error_msg_and_die("invalid range %s-%s", ntok, ltok ?: ntok);
|
||||
|
||||
/* add the new range */
|
||||
cut_list = xrealloc_vector(cut_list, 4, nranges);
|
||||
/* NB: s is always >= 0 */
|
||||
cut_list[nranges].startpos = s;
|
||||
cut_list[nranges].endpos = e;
|
||||
nranges++;
|
||||
/* 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
|
||||
*/
|
||||
qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc);
|
||||
}
|
||||
cut_list[nranges].startpos = UINT_MAX; /* end indicator */
|
||||
|
||||
/* make sure we got some cut positions out of all that */
|
||||
//if (nranges == 0)
|
||||
// bb_simple_error_msg_and_die("missing list of positions");
|
||||
//^^^ this is impossible since one of -bcfF is required,
|
||||
// they populate LIST with non-NULL string and when it is parsed,
|
||||
// cut_list[] gets at least one element.
|
||||
|
||||
/* 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 & OPT_NOSORT))
|
||||
qsort(cut_list, nranges, sizeof(cut_list[0]), cmpfunc);
|
||||
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
if (opt & OPT_REGEX) {
|
||||
xregcomp(®, delim, REG_EXTENDED);
|
||||
delim = (void*) ®
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
exitcode_t retval = EXIT_SUCCESS;
|
||||
int retval = EXIT_SUCCESS;
|
||||
|
||||
if (!*argv)
|
||||
*--argv = (char *)"-";
|
||||
@@ -426,12 +308,12 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
retval = EXIT_FAILURE;
|
||||
continue;
|
||||
}
|
||||
cut_file(file, delim, odelim, cut_list);
|
||||
cut_file(file, delim, cut_lists, nlists);
|
||||
fclose_if_not_stdin(file);
|
||||
} while (*++argv);
|
||||
|
||||
if (ENABLE_FEATURE_CLEAN_UP)
|
||||
free(cut_list);
|
||||
free(cut_lists);
|
||||
fflush_stdout_and_exit(retval);
|
||||
}
|
||||
}
|
||||
+60
-50
@@ -19,7 +19,7 @@
|
||||
much as possible, missed out a lot of bounds checking */
|
||||
|
||||
//config:config DATE
|
||||
//config: bool "date (7.2 kb)"
|
||||
//config: bool "date (7 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: date is used to set the system date or display the
|
||||
@@ -37,6 +37,7 @@
|
||||
//config: bool "Support %[num]N nanosecond format specifier"
|
||||
//config: default n # stat's nanosecond field is a bit non-portable
|
||||
//config: depends on DATE
|
||||
//config: select PLATFORM_LINUX
|
||||
//config: help
|
||||
//config: Support %[num]N format specifier. Adds ~250 bytes of code.
|
||||
//config:
|
||||
@@ -93,25 +94,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 (strptime format) 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 +148,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 +171,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 +184,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 +203,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 +221,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) {
|
||||
@@ -266,30 +283,29 @@ int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
/* 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_and_die("can't set date");
|
||||
bb_simple_perror_msg("can't set date");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,34 +315,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 +384,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
-123
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config DD
|
||||
//config: bool "dd (8.3 kb)"
|
||||
//config: bool "dd (7.5 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: dd copies a file (from standard input to standard output,
|
||||
@@ -56,10 +56,10 @@
|
||||
//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: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]\n"
|
||||
//usage: IF_FEATURE_DD_IBS_OBS(
|
||||
//usage: " [conv=notrunc|noerror|sync|fsync]\n"
|
||||
//usage: " [iflag=skip_bytes|count_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]"
|
||||
//usage: " [iflag=skip_bytes|fullblock] [oflag=seek_bytes|append]"
|
||||
//usage: )
|
||||
//usage:#define dd_full_usage "\n\n"
|
||||
//usage: "Copy a file with converting and formatting\n"
|
||||
@@ -82,12 +82,9 @@
|
||||
//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: "\n oflag=seek_bytes seek=N is in bytes"
|
||||
//usage: "\n oflag=append Open output file in append mode"
|
||||
//usage: )
|
||||
//usage: IF_FEATURE_DD_STATUS(
|
||||
//usage: "\n status=noxfer Suppress rate output"
|
||||
@@ -137,22 +134,19 @@ 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,
|
||||
FLAG_FULLBLOCK = (1 << 6) * 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,
|
||||
FLAG_OFLAG_SHIFT = 7,
|
||||
FLAG_SEEK_BYTES = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_APPEND = (1 << 8) * 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 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
|
||||
FLAG_COUNT = 1 << 10,
|
||||
FLAG_STATUS_NONE = 1 << 11,
|
||||
FLAG_STATUS_NOXFER = 1 << 12,
|
||||
};
|
||||
|
||||
static void dd_output_status(int UNUSED_PARAM cur_signal)
|
||||
@@ -177,9 +171,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,56 +192,12 @@ static void dd_output_status(int UNUSED_PARAM cur_signal)
|
||||
#endif
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
# ifdef O_DIRECT
|
||||
static int clear_O_DIRECT(int fd)
|
||||
{
|
||||
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
|
||||
#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
|
||||
# ifdef O_DIRECT
|
||||
if (n < 0 && (G.flags & FLAG_IDIRECT) && clear_O_DIRECT(ifd))
|
||||
goto read_again;
|
||||
# endif
|
||||
#endif
|
||||
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
|
||||
# ifdef O_DIRECT
|
||||
if (n < 0 && (G.flags & FLAG_ODIRECT) && clear_O_DIRECT(ofd))
|
||||
goto write_again;
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
||||
if (n > 0)
|
||||
G.total_bytes += n;
|
||||
@@ -272,6 +221,12 @@ static bool write_and_stats(const void *buf, size_t len, size_t obs,
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if ENABLE_LFS
|
||||
# define XATOU_SFX xatoull_sfx
|
||||
#else
|
||||
# define XATOU_SFX xatoul_sfx
|
||||
#endif
|
||||
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
static int parse_comma_flags(char *val, const char *words, const char *error_in)
|
||||
{
|
||||
@@ -299,14 +254,6 @@ 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)
|
||||
{
|
||||
@@ -320,9 +267,9 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
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";
|
||||
"skip_bytes\0""fullblock\0";
|
||||
static const char oflag_words[] ALIGN1 =
|
||||
"seek_bytes\0append\0""direct\0";
|
||||
"seek_bytes\0append\0";
|
||||
#endif
|
||||
#if ENABLE_FEATURE_DD_STATUS
|
||||
static const char status_words[] ALIGN1 =
|
||||
@@ -362,14 +309,11 @@ 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
|
||||
};
|
||||
exitcode_t exitcode = EXIT_FAILURE;
|
||||
smallint exitcode = EXIT_FAILURE;
|
||||
int i;
|
||||
size_t ibs = 512;
|
||||
char *ibuf;
|
||||
@@ -451,15 +395,15 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* These can be large: */
|
||||
if (what == OP_count) {
|
||||
G.flags |= FLAG_COUNT;
|
||||
count = XATOOFF_SFX(val, cwbkMG_suffixes);
|
||||
count = XATOU_SFX(val, cwbkMG_suffixes);
|
||||
/*continue;*/
|
||||
}
|
||||
if (what == OP_seek) {
|
||||
seek = XATOOFF_SFX(val, cwbkMG_suffixes);
|
||||
seek = XATOU_SFX(val, cwbkMG_suffixes);
|
||||
/*continue;*/
|
||||
}
|
||||
if (what == OP_skip) {
|
||||
skip = XATOOFF_SFX(val, cwbkMG_suffixes);
|
||||
skip = XATOU_SFX(val, cwbkMG_suffixes);
|
||||
/*continue;*/
|
||||
}
|
||||
if (what == OP_if) {
|
||||
@@ -482,12 +426,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,17 +444,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) {
|
||||
# ifdef O_DIRECT
|
||||
iflag |= O_DIRECT;
|
||||
# else
|
||||
bb_error_msg_and_die("O_DIRECT not supported on this platform");
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
xmove_fd(xopen(infile, iflag), ifd);
|
||||
xmove_fd(xopen(infile, O_RDONLY), ifd);
|
||||
} else {
|
||||
infile = bb_msg_standard_input;
|
||||
}
|
||||
@@ -520,15 +455,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
oflag |= O_TRUNC;
|
||||
if (G.flags & FLAG_APPEND)
|
||||
oflag |= O_APPEND;
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (G.flags & FLAG_ODIRECT) {
|
||||
# ifdef O_DIRECT
|
||||
oflag |= O_DIRECT;
|
||||
# else
|
||||
bb_error_msg_and_die("O_DIRECT not supported on this platform");
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
|
||||
xmove_fd(xopen(outfile, oflag), ofd);
|
||||
|
||||
if (seek && !(G.flags & FLAG_NOTRUNC)) {
|
||||
@@ -551,7 +478,13 @@ 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;
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (G.flags & FLAG_FULLBLOCK)
|
||||
n = full_read(ifd, ibuf, blocksz);
|
||||
else
|
||||
#endif
|
||||
n = safe_read(ifd, ibuf, blocksz);
|
||||
if (n < 0)
|
||||
goto die_infile;
|
||||
if (n == 0)
|
||||
@@ -565,17 +498,14 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
goto die_outfile;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
ssize_t n = ibs;
|
||||
|
||||
if (G.flags & FLAG_COUNT) {
|
||||
if (count == 0)
|
||||
break;
|
||||
if ((G.flags & FLAG_COUNT_BYTES) && count < ibs)
|
||||
n = count;
|
||||
}
|
||||
|
||||
n = dd_read(ibuf, n);
|
||||
while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
|
||||
ssize_t n;
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
if (G.flags & FLAG_FULLBLOCK)
|
||||
n = full_read(ifd, ibuf, ibs);
|
||||
else
|
||||
#endif
|
||||
n = safe_read(ifd, ibuf, ibs);
|
||||
if (n == 0)
|
||||
break;
|
||||
if (n < 0) {
|
||||
@@ -610,7 +540,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 {
|
||||
@@ -671,13 +600,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;
|
||||
}
|
||||
+25
-59
@@ -18,7 +18,7 @@
|
||||
* Implement -P and -B; better coreutils compat; cleanup
|
||||
*/
|
||||
//config:config DF
|
||||
//config: bool "df (7.1 kb)"
|
||||
//config: bool "df (6.8 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"
|
||||
@@ -113,51 +92,39 @@ int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
unsigned long df_disp_hr = 1024;
|
||||
exitcode_t status = EXIT_SUCCESS;
|
||||
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)
|
||||
);
|
||||
/* -k overrides $POSIXLY_CORRECT: */
|
||||
if (opt & OPT_KILO)
|
||||
df_disp_hr = 1024;
|
||||
if (opt & OPT_MEGA)
|
||||
df_disp_hr = 1024*1024;
|
||||
|
||||
@@ -175,11 +142,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)
|
||||
@@ -188,8 +159,8 @@ int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (disp_units_hdr == NULL) {
|
||||
#if ENABLE_FEATURE_HUMAN_READABLE
|
||||
disp_units_hdr = xasprintf("%s-blocks",
|
||||
/* print df_disp_hr; show no fractionals;
|
||||
* if -P, unit=1 (print it in full, no KMG suffixes) */
|
||||
/* print df_disp_hr, show no fractionals,
|
||||
* use suffixes if OPT_POSIX is set in opt */
|
||||
make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
|
||||
);
|
||||
#else
|
||||
@@ -243,11 +214,6 @@ int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
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;
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config DIRNAME
|
||||
//config: bool "dirname (611 bytes)"
|
||||
//config: bool "dirname (329 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.5 kb)"
|
||||
//config: bool "dos2unix (5.2 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.5 kb)"
|
||||
//config: bool "unix2dos (5.2 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: unix2dos is used to convert a text file from UNIX format to
|
||||
|
||||
+14
-23
@@ -19,7 +19,7 @@
|
||||
* 4) Fixed busybox bug #1284 involving long overflow with human_readable.
|
||||
*/
|
||||
//config:config DU
|
||||
//config: bool "du (6.5 kb)"
|
||||
//config: bool "du (6.3 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: du is used to report the amount of disk space used
|
||||
@@ -40,9 +40,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 +84,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 +109,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 +124,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 +155,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 +164,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 +241,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 +257,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;
|
||||
|
||||
+10
-12
@@ -19,7 +19,7 @@
|
||||
* The previous version did not allow 4-digit octals.
|
||||
*/
|
||||
//config:config ECHO
|
||||
//config: bool "echo (2 kb)"
|
||||
//config: bool "echo (1.8 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: echo prints a specified string to stdout.
|
||||
@@ -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_simple_perror_msg(bb_msg_write_error);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -321,8 +321,6 @@ int echo_main(int argc, char **argv)
|
||||
if (*arg == '0' && (unsigned char)(arg[1] - '0') < 8) {
|
||||
arg++;
|
||||
}
|
||||
//FIXME? we also accept non-0 starting sequences (see echo-prints-slash_41 test)
|
||||
// echo -ne '-\41-' prints "-!-". bash 5.0.17 does not (prints "-\41-").
|
||||
/* bb_process_escape_sequence can handle nul correctly */
|
||||
c = bb_process_escape_sequence( (void*) &arg);
|
||||
}
|
||||
|
||||
+9
-14
@@ -24,7 +24,7 @@
|
||||
* - use xfunc_error_retval
|
||||
*/
|
||||
//config:config ENV
|
||||
//config: bool "env (4.3 kb)"
|
||||
//config: bool "env (4 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
|
||||
);
|
||||
@@ -94,13 +90,12 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
fflush_stdout_and_exit_SUCCESS();
|
||||
fflush_stdout_and_exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
+4
-4
@@ -21,13 +21,13 @@
|
||||
* Caveat: this versions of expand and unexpand don't accept tab lists.
|
||||
*/
|
||||
//config:config EXPAND
|
||||
//config: bool "expand (5.3 kb)"
|
||||
//config: bool "expand (5.1 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: By default, convert all tabs to spaces.
|
||||
//config:
|
||||
//config:config UNEXPAND
|
||||
//config: bool "unexpand (5.5 kb)"
|
||||
//config: bool "unexpand (5.3 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: By default, convert only leading sequences of blanks to tabs.
|
||||
@@ -123,7 +123,7 @@ static void expand(FILE *file, unsigned tab_size, unsigned opt)
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
fputs_stdout(ptr_strbeg);
|
||||
fputs(ptr_strbeg, stdout);
|
||||
free(line);
|
||||
}
|
||||
}
|
||||
@@ -192,7 +192,7 @@ int expand_main(int argc UNUSED_PARAM, char **argv)
|
||||
FILE *file;
|
||||
unsigned tab_size;
|
||||
unsigned opt;
|
||||
exitcode_t exit_status = EXIT_SUCCESS;
|
||||
int exit_status = EXIT_SUCCESS;
|
||||
|
||||
init_unicode();
|
||||
|
||||
|
||||
+3
-3
@@ -23,7 +23,7 @@
|
||||
* provided they all associate ((x op x) op x).
|
||||
*/
|
||||
//config:config EXPR
|
||||
//config: bool "expr (6.8 kb)"
|
||||
//config: bool "expr (6.6 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"
|
||||
|
||||
+79
-133
@@ -4,7 +4,7 @@
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config FACTOR
|
||||
//config: bool "factor (3.2 kb)"
|
||||
//config: bool "factor (2.7 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,115 +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,x) \
|
||||
(((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) | \
|
||||
((uint64_t)x << 61)
|
||||
#define P(a,b,c,d,e,f,g,h,i,j,A,B,C,D,E,F,G,H,I,J,x) \
|
||||
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), \
|
||||
(x/2) \
|
||||
)
|
||||
static const uint64_t packed_wheel[] = {
|
||||
/* 1, 2, */
|
||||
P( 2, 4, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2, 6, 4, 6),
|
||||
P( 8, 4, 2, 4, 2, 4,14, 4, 6, 2,10, 2, 6, 6, 4, 2, 4, 6, 2,10, 2),
|
||||
P( 4, 2,12,10, 2, 4, 2, 4, 6, 2, 6, 4, 6, 6, 6, 2, 6, 4, 2, 6, 4),
|
||||
P( 6, 8, 4, 2, 4, 6, 8, 6,10, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6),
|
||||
P( 4, 2, 6,10, 2,10, 2, 4, 2, 4, 6, 8, 4, 2, 4,12, 2, 6, 4, 2, 6),
|
||||
P( 4, 6,12, 2, 4, 2, 4, 8, 6, 4, 6, 2, 4, 6, 2, 6,10, 2, 4, 6, 2),
|
||||
P( 6, 4, 2, 4, 2,10, 2,10, 2, 4, 6, 6, 2, 6, 6, 4, 6, 6, 2, 6, 4),
|
||||
P( 2, 6, 4, 6, 8, 4, 2, 6, 4, 8, 6, 4, 6, 2, 4, 6, 8, 6, 4, 2,10),
|
||||
P( 2, 6, 4, 2, 4, 2,10, 2,10, 2, 4, 2, 4, 8, 6, 4, 2, 4, 6, 6, 2),
|
||||
P( 6, 4, 8, 4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4, 6, 6, 6, 2, 6, 6, 4),
|
||||
P( 2, 4, 6, 2, 6, 4, 2, 4, 2,10, 2,10, 2, 6, 4, 6, 2, 6, 4, 2, 4),
|
||||
P( 6, 6, 8, 4, 2, 6,10, 8, 4, 2, 4, 2, 4, 8,10, 6, 2, 4, 8, 6, 6),
|
||||
P( 4, 2, 4, 6, 2, 6, 4, 6, 2,10, 2,10, 2, 4, 2, 4, 6, 2, 6, 4, 2),
|
||||
P( 4, 6, 6, 2, 6, 6, 6, 4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4, 8, 4, 6),
|
||||
P( 2, 6, 6, 4, 2, 4, 6, 8, 4, 2, 4, 2,10, 2,10, 2, 4, 2, 4, 6, 2),
|
||||
P(10, 2, 4, 6, 8, 6, 4, 2, 6, 4, 6, 8, 4, 6, 2, 4, 8, 6, 4, 6, 2),
|
||||
P( 4, 6, 2, 6, 6, 4, 6, 6, 2, 6, 6, 4, 2,10, 2,10, 2, 4, 2, 4, 6),
|
||||
P( 2, 6, 4, 2,10, 6, 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 4, 2,12, 6, 4),
|
||||
P( 6, 2, 4, 6, 2,12, 4, 2, 4, 8, 6, 4, 2, 4, 2,10, 2,10, 6, 2, 4),
|
||||
P( 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2,10, 6, 8, 6, 4, 2, 4, 8, 6),
|
||||
P( 4, 6, 2, 4, 6, 2, 6, 6, 6, 4, 6, 2, 6, 4, 2, 4, 2,10,12, 2, 4),
|
||||
P( 2,10, 2, 6, 4, 2, 4, 6, 6, 2,10, 2, 6, 4,14, 4, 2, 4, 2, 4, 8),
|
||||
P( 6, 4, 6, 2, 4, 6, 2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4,12, 2,12),
|
||||
};
|
||||
#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 - 184 +184
|
||||
* factor_main 108 163 +55
|
||||
*/
|
||||
static void unpack_wheel(void)
|
||||
{
|
||||
int i;
|
||||
uint8_t *p;
|
||||
|
||||
setup_common_bufsiz();
|
||||
wheel_tab[0] = 1;
|
||||
wheel_tab[1] = 2;
|
||||
p = &wheel_tab[2];
|
||||
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_ull(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;
|
||||
}
|
||||
|
||||
half_t s = isqrt(N);
|
||||
/* 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;
|
||||
@@ -160,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
|
||||
@@ -182,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');
|
||||
}
|
||||
|
||||
@@ -227,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;
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config FALSE
|
||||
//config: bool "false (314 bytes)"
|
||||
//config: bool "false (tiny)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: false returns an exit code of FALSE (1).
|
||||
|
||||
+3
-3
@@ -11,7 +11,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config FOLD
|
||||
//config: bool "fold (4.8 kb)"
|
||||
//config: bool "fold (4.6 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Wrap text to fit a specific width.
|
||||
@@ -23,7 +23,7 @@
|
||||
//usage:#define fold_trivial_usage
|
||||
//usage: "[-bs] [-w WIDTH] [FILE]..."
|
||||
//usage:#define fold_full_usage "\n\n"
|
||||
//usage: "Wrap input lines in FILEs (or stdin), writing to stdout\n"
|
||||
//usage: "Wrap input lines in each FILE (or stdin), writing to stdout\n"
|
||||
//usage: "\n -b Count bytes rather than columns"
|
||||
//usage: "\n -s Break at spaces"
|
||||
//usage: "\n -w Use WIDTH columns instead of 80"
|
||||
@@ -77,7 +77,7 @@ int fold_main(int argc UNUSED_PARAM, char **argv)
|
||||
char *line_out = NULL;
|
||||
const char *w_opt = "80";
|
||||
unsigned width;
|
||||
exitcode_t exitcode = EXIT_SUCCESS;
|
||||
smallint exitcode = EXIT_SUCCESS;
|
||||
|
||||
init_unicode();
|
||||
|
||||
|
||||
+11
-12
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config HEAD
|
||||
//config: bool "head (4 kb)"
|
||||
//config: bool "head (3.8 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: head is used to print the first specified number of lines
|
||||
@@ -29,18 +29,17 @@
|
||||
//usage:#define head_trivial_usage
|
||||
//usage: "[OPTIONS] [FILE]..."
|
||||
//usage:#define head_full_usage "\n\n"
|
||||
//usage: "Print first 10 lines of FILEs (or stdin).\n"
|
||||
//usage: "Print first 10 lines of each FILE (or stdin) to stdout.\n"
|
||||
//usage: "With more than one FILE, precede each with a filename header.\n"
|
||||
//usage: "\n -n N[bkm] Print first N lines"
|
||||
//usage: IF_FEATURE_FANCY_HEAD(
|
||||
//usage: "\n -n -N[bkm] Print all except N last lines"
|
||||
//usage: "\n -c [-]N[bkm] Print first N bytes"
|
||||
//usage: )
|
||||
//usage: "\n (b:*512 k:*1024 m:*1024^2)"
|
||||
//usage: "\n -n N[kbm] Print first N lines"
|
||||
//usage: IF_FEATURE_FANCY_HEAD(
|
||||
//usage: "\n -n -N[kbm] Print all except N last lines"
|
||||
//usage: "\n -c [-]N[kbm] Print first N bytes"
|
||||
//usage: "\n -q Never print headers"
|
||||
//usage: "\n -v Always print headers"
|
||||
//usage: )
|
||||
//usage: "\n"
|
||||
//usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)."
|
||||
//usage:
|
||||
//usage:#define head_example_usage
|
||||
//usage: "$ head -n 2 /etc/passwd\n"
|
||||
@@ -76,7 +75,7 @@ print_except_N_last_bytes(FILE *fp, unsigned count)
|
||||
{
|
||||
unsigned char *circle = xmalloc(++count);
|
||||
unsigned head = 0;
|
||||
for (;;) {
|
||||
for(;;) {
|
||||
int c;
|
||||
c = getc(fp);
|
||||
if (c == EOF)
|
||||
@@ -105,7 +104,7 @@ print_except_N_last_lines(FILE *fp, unsigned count)
|
||||
{
|
||||
char **circle = xzalloc((++count) * sizeof(circle[0]));
|
||||
unsigned head = 0;
|
||||
for (;;) {
|
||||
for(;;) {
|
||||
char *c;
|
||||
c = xmalloc_fgets(fp);
|
||||
if (!c)
|
||||
@@ -118,7 +117,7 @@ print_except_N_last_lines(FILE *fp, unsigned count)
|
||||
char *c;
|
||||
if (head == count)
|
||||
head = 0;
|
||||
fputs_stdout(circle[head]);
|
||||
fputs(circle[head], stdout);
|
||||
c = xmalloc_fgets(fp);
|
||||
if (!c)
|
||||
goto ret;
|
||||
@@ -127,7 +126,7 @@ print_except_N_last_lines(FILE *fp, unsigned count)
|
||||
}
|
||||
ret:
|
||||
head = 0;
|
||||
for (;;) {
|
||||
for(;;) {
|
||||
free(circle[head++]);
|
||||
if (head == count)
|
||||
break;
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config HOSTID
|
||||
//config: bool "hostid (566 bytes)"
|
||||
//config: bool "hostid (286 bytes)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: hostid prints the numeric identifier (in hexadecimal) for
|
||||
|
||||
+6
-7
@@ -13,13 +13,13 @@
|
||||
* Added -G option Tito Ragusa (C) 2008 for SUSv3.
|
||||
*/
|
||||
//config:config ID
|
||||
//config: bool "id (7.1 kb)"
|
||||
//config: bool "id (7 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: id displays the current user and group ID names.
|
||||
//config:
|
||||
//config:config GROUPS
|
||||
//config: bool "groups (6.8 kb)"
|
||||
//config: bool "groups (6.7 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Print the group names associated with current user id.
|
||||
@@ -33,7 +33,7 @@
|
||||
/* BB_AUDIT SUSv3 compliant. */
|
||||
|
||||
//usage:#define id_trivial_usage
|
||||
//usage: "[-ugGnr"IF_SELINUX("Z")"] [USER]"
|
||||
//usage: "[OPTIONS] [USER]"
|
||||
//usage:#define id_full_usage "\n\n"
|
||||
//usage: "Print information about USER or the current user\n"
|
||||
//usage: IF_SELINUX(
|
||||
@@ -52,7 +52,7 @@
|
||||
//usage:#define groups_trivial_usage
|
||||
//usage: "[USER]"
|
||||
//usage:#define groups_full_usage "\n\n"
|
||||
//usage: "Print the groups USER is in"
|
||||
//usage: "Print the group memberships of USER or for the current process"
|
||||
//usage:
|
||||
//usage:#define groups_example_usage
|
||||
//usage: "$ groups\n"
|
||||
@@ -156,7 +156,7 @@ int id_main(int argc UNUSED_PARAM, char **argv)
|
||||
const char *prefix;
|
||||
const char *username;
|
||||
#if ENABLE_SELINUX
|
||||
char *scontext = NULL;
|
||||
security_context_t scontext = NULL;
|
||||
#endif
|
||||
|
||||
if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) {
|
||||
@@ -230,7 +230,6 @@ int id_main(int argc UNUSED_PARAM, char **argv)
|
||||
prefix = ",";
|
||||
}
|
||||
} else if (n < 0) { /* error in get_groups() */
|
||||
bb_putchar('\n');
|
||||
if (ENABLE_DESKTOP)
|
||||
bb_simple_error_msg_and_die("can't get groups");
|
||||
return EXIT_FAILURE;
|
||||
@@ -259,7 +258,7 @@ int id_main(int argc UNUSED_PARAM, char **argv)
|
||||
bb_error_msg_and_die("can't get process context%s",
|
||||
username ? " for a different user" : "");
|
||||
}
|
||||
fputs_stdout(scontext);
|
||||
fputs(scontext, stdout);
|
||||
}
|
||||
/* freecon(NULL) seems to be harmless */
|
||||
if (ENABLE_FEATURE_CLEAN_UP)
|
||||
|
||||
+11
-11
@@ -50,11 +50,11 @@ if test $FLAG_USER_EXISTS = "yes"; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
$BUSYBOX adduser -s /bin/true -g "" -H -D "$TEST_USER" || exit 1
|
||||
adduser -s /bin/true -g "" -H -D "$TEST_USER" || exit 1
|
||||
|
||||
chown $TEST_USER:$TEST_USER $BUSYBOX
|
||||
chown $TEST_USER.$TEST_USER $BUSYBOX
|
||||
chmod u+s $BUSYBOX 2>&1 /dev/null
|
||||
chown $TEST_USER:$TEST_USER $ID
|
||||
chown $TEST_USER.$TEST_USER $ID
|
||||
chmod u+s $ID 2>&1 /dev/null
|
||||
|
||||
echo "test 3 setuid, existing user: id [options] no username"
|
||||
@@ -90,9 +90,9 @@ do
|
||||
done
|
||||
done
|
||||
|
||||
chown $TEST_USER:$TEST_USER $BUSYBOX
|
||||
chown $TEST_USER.$TEST_USER $BUSYBOX
|
||||
chmod g+s $BUSYBOX 2>&1 /dev/null
|
||||
chown $TEST_USER:$TEST_USER $ID
|
||||
chown $TEST_USER.$TEST_USER $ID
|
||||
chmod g+s $ID 2>&1 /dev/null
|
||||
|
||||
echo "test 5 setgid, existing user: id [options] no username"
|
||||
@@ -128,9 +128,9 @@ do
|
||||
done
|
||||
done
|
||||
|
||||
chown $TEST_USER:$TEST_USER $BUSYBOX
|
||||
chown $TEST_USER.$TEST_USER $BUSYBOX
|
||||
chmod u+s,g+s $BUSYBOX 2>&1 /dev/null
|
||||
chown $TEST_USER:$TEST_USER $ID
|
||||
chown $TEST_USER.$TEST_USER $ID
|
||||
chmod u+s,g+s $ID 2>&1 /dev/null
|
||||
|
||||
echo "test 7 setuid, setgid, existing user: id [options] no username"
|
||||
@@ -200,8 +200,8 @@ do
|
||||
done
|
||||
done
|
||||
|
||||
chown :root $BUSYBOX 2>&1 /dev/null
|
||||
chown :root $ID 2>&1 /dev/null
|
||||
chown .root $BUSYBOX 2>&1 /dev/null
|
||||
chown .root $ID 2>&1 /dev/null
|
||||
chmod g+s $BUSYBOX 2>&1 /dev/null
|
||||
chmod g+s $ID 2>&1 /dev/null
|
||||
|
||||
@@ -238,7 +238,7 @@ do
|
||||
done
|
||||
done
|
||||
|
||||
chown root:root $BUSYBOX 2>&1 /dev/null
|
||||
chown root:root $ID 2>&1 /dev/null
|
||||
chown root.root $BUSYBOX 2>&1 /dev/null
|
||||
chown root.root $ID 2>&1 /dev/null
|
||||
rm -f $ID
|
||||
rm -f foo bar
|
||||
+9
-11
@@ -73,7 +73,7 @@ static const char install_longopts[] ALIGN1 =
|
||||
static void setdefaultfilecon(const char *path)
|
||||
{
|
||||
struct stat s;
|
||||
char *scontext = NULL;
|
||||
security_context_t scontext = NULL;
|
||||
|
||||
if (!is_selinux_enabled()) {
|
||||
return;
|
||||
@@ -119,7 +119,7 @@ int install_main(int argc, char **argv)
|
||||
int ret = EXIT_SUCCESS;
|
||||
int isdir;
|
||||
#if ENABLE_SELINUX
|
||||
char *scontext;
|
||||
security_context_t scontext;
|
||||
bool use_default_selinux_context = 1;
|
||||
#endif
|
||||
enum {
|
||||
@@ -244,15 +244,6 @@ int install_main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
/* Set the user and group id */
|
||||
/* (must be before chmod, or else chown may clear suid/gid bits) */
|
||||
if ((opts & (OPT_OWNER|OPT_GROUP))
|
||||
&& lchown(dest, uid, gid) == -1
|
||||
) {
|
||||
bb_perror_msg("can't change %s of %s", "ownership", dest);
|
||||
ret = EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* Set the file mode (always, not only with -m).
|
||||
* GNU coreutils 6.10 is not affected by umask. */
|
||||
if (chmod(dest, mode) == -1) {
|
||||
@@ -263,6 +254,13 @@ int install_main(int argc, char **argv)
|
||||
if (use_default_selinux_context)
|
||||
setdefaultfilecon(dest);
|
||||
#endif
|
||||
/* Set the user and group id */
|
||||
if ((opts & (OPT_OWNER|OPT_GROUP))
|
||||
&& lchown(dest, uid, gid) == -1
|
||||
) {
|
||||
bb_perror_msg("can't change %s of %s", "ownership", dest);
|
||||
ret = EXIT_FAILURE;
|
||||
}
|
||||
next:
|
||||
if (ENABLE_FEATURE_CLEAN_UP && isdir)
|
||||
free(dest);
|
||||
|
||||
@@ -1,546 +0,0 @@
|
||||
/* vi: set sw=4 ts=4: */
|
||||
/*
|
||||
* join -- equality join on two files
|
||||
*
|
||||
* Written by Morgan Bartlett
|
||||
* Copyright (C) 2025 Morgan Bartlett
|
||||
*
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//config:config JOIN
|
||||
//config: bool "join (6.6 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Equality join on two files
|
||||
|
||||
//applet:IF_JOIN(APPLET_NOEXEC(join, join, BB_DIR_USR_BIN, BB_SUID_DROP, join))
|
||||
|
||||
//kbuild:lib-$(CONFIG_JOIN) += join.o
|
||||
|
||||
//usage:#define join_trivial_usage
|
||||
//usage: "[-a 1|2 | -v 1|2] [-e STR] [-o LIST] [-t SEP_CHAR] [-1 NUM] [-2 NUM] FILE1 FILE2"
|
||||
//usage:#define join_full_usage "\n\n"
|
||||
//usage: "Join FILE1 and FILE2, writing to stdout\n"
|
||||
//usage: "\n -a 1|2 Also write unpaired lines from FILE1/2"
|
||||
//usage: "\n -v 1|2 Only write unpaired lines from FILE1/2"
|
||||
//usage: "\n -e STR Replace empty fields with STR"
|
||||
//usage: "\n -o LIST Write the fields in LIST"
|
||||
//usage: "\n -t CHAR Use a different field separator"
|
||||
//usage: "\n -1 NUM Join on field NUM from FILE1"
|
||||
//usage: "\n -2 NUM Join on field NUM from FILE2"
|
||||
//usage: "\n"
|
||||
//usage: "\nLIST is a space or comma separated list of 1/2.FIELD or 0 (the join field)"
|
||||
|
||||
#include "libbb.h"
|
||||
#include "unicode.h"
|
||||
#include "common_bufsiz.h"
|
||||
|
||||
/* This is a NOEXEC applet. Be very careful! */
|
||||
|
||||
typedef struct {
|
||||
char *line;
|
||||
char **fields;
|
||||
int fieldcount;
|
||||
} LINE;
|
||||
|
||||
typedef struct {
|
||||
FILE *fp;
|
||||
int idx; /* index of join field in fields array */
|
||||
const char *field; /* pointer to lines[0].fields[idx] or "" */
|
||||
LINE *lines;
|
||||
LINE pushback;
|
||||
int linecount; /* number of lines currently cached */
|
||||
int linecap; /* current capacity of lines array */
|
||||
} FDAT;
|
||||
|
||||
/* "globals" can be made local to main(), but then passing them
|
||||
down the callchains as parameters requires ~90 bytes of code (on x86).
|
||||
*/
|
||||
struct globals {
|
||||
int *format; // = NULL;
|
||||
const char *empty_str;
|
||||
/* \0 can't be separator, 0 means: "whitespace+" pattern */
|
||||
char sep; // = 0;
|
||||
} FIX_ALIASING;
|
||||
#define G (*(struct globals*)bb_common_bufsiz1)
|
||||
#define INIT_G() do { \
|
||||
setup_common_bufsiz(); \
|
||||
/* we have to zero it out because of NOEXEC */ \
|
||||
memset(&G, 0, sizeof(G)); \
|
||||
} while (0)
|
||||
|
||||
/* split s by sep, and put the results into *curr */
|
||||
static void field_split(char *s, LINE *curr)
|
||||
{
|
||||
/* compare awk_split from editors/awk.c */
|
||||
int n;
|
||||
char *ps = s;
|
||||
char *s1 = s;
|
||||
|
||||
char **sl;
|
||||
size_t sn;
|
||||
|
||||
/* in worst case, every character is a separator */
|
||||
curr->fields = sl = xzalloc(sizeof(char*) * (strlen(s) + 2));
|
||||
curr->line = s;
|
||||
|
||||
n = 0;
|
||||
if (G.sep != '\0') { /* single-character split */
|
||||
while ((s1 = strchr(ps, G.sep)) != NULL) {
|
||||
sl[n] = ps;
|
||||
*s1 = '\0';
|
||||
ps = s1 + 1;
|
||||
n++;
|
||||
}
|
||||
/* Add the last field */
|
||||
sl[n] = ps;
|
||||
n++;
|
||||
curr->fieldcount = n;
|
||||
return;
|
||||
}
|
||||
/* default split: skip the initial whitespace and then any run
|
||||
of non-whitespace characters is a field */
|
||||
while (*s) {
|
||||
/* blanks are space and tab in the POSIX locale */
|
||||
while (*s == ' ' || *s == '\t')
|
||||
s++;
|
||||
if (!*s)
|
||||
break;
|
||||
|
||||
s1 = strpbrk(s, " \t");
|
||||
if (s1 == NULL) {
|
||||
/* last field */
|
||||
sn = strlen(s);
|
||||
} else {
|
||||
sn = s1 - s;
|
||||
*s1 = '\0';
|
||||
}
|
||||
|
||||
sl[n] = s;
|
||||
n++;
|
||||
s += sn + 1;
|
||||
}
|
||||
curr->fieldcount = n;
|
||||
}
|
||||
|
||||
static inline void freefields(LINE *lp)
|
||||
{
|
||||
free(lp->line);
|
||||
free(lp->fields);
|
||||
lp->line = NULL;
|
||||
lp->fields = NULL;
|
||||
}
|
||||
|
||||
static void freelines(FDAT *f)
|
||||
{
|
||||
for (int n = 0; n < f->linecount; n++) {
|
||||
freefields(f->lines + n);
|
||||
}
|
||||
f->linecount = 0;
|
||||
f->field = "";
|
||||
}
|
||||
|
||||
static void readfields(FDAT *f)
|
||||
{
|
||||
LINE curr = { .line = NULL, .fields = NULL, .fieldcount = 0 };
|
||||
char *line;
|
||||
const char *field2;
|
||||
bool first = TRUE;
|
||||
|
||||
freelines(f);
|
||||
|
||||
while (true) {
|
||||
if (first && f->pushback.fields) {
|
||||
/* first check for a pushed back line */
|
||||
curr = f->pushback;
|
||||
f->pushback = (LINE) { .line = NULL, .fields = NULL, .fieldcount = 0 };
|
||||
} else {
|
||||
/* read a line and split */
|
||||
line = xmalloc_fgetline(f->fp);
|
||||
if (line == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
field_split(line, &curr);
|
||||
}
|
||||
|
||||
if (f->idx >= curr.fieldcount)
|
||||
field2 = "";
|
||||
else
|
||||
field2 = curr.fields[f->idx];
|
||||
|
||||
/* Ensure strcmp() matches on first pass through loop */
|
||||
if (first)
|
||||
f->field = field2;
|
||||
|
||||
if (strcmp(f->field, field2) == 0) {
|
||||
/* add to the stack */
|
||||
if (f->linecount == f->linecap) {
|
||||
f->linecap = f->linecap ? f->linecap * 2 : 8;
|
||||
f->lines = xrealloc(f->lines, sizeof(LINE) * f->linecap);
|
||||
}
|
||||
f->lines[f->linecount++] = curr;
|
||||
} else {
|
||||
f->pushback = curr;
|
||||
return;
|
||||
}
|
||||
first = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
static inline const char *fieldorempty(const char *field)
|
||||
{
|
||||
if (*field == '\0')
|
||||
return G.empty_str;
|
||||
return field;
|
||||
}
|
||||
|
||||
static void printfields(FDAT *f1, FDAT *f2)
|
||||
{
|
||||
const char *field = (f1 == NULL) ? f2->field : f1->field;
|
||||
|
||||
int linef1;
|
||||
int linef2;
|
||||
int fn;
|
||||
int format_fl;
|
||||
int format_idx;
|
||||
int *formatcurr;
|
||||
bool first;
|
||||
char sep;
|
||||
|
||||
LINE *l1;
|
||||
LINE *l2;
|
||||
|
||||
sep = (G.sep == '\0') ? ' ' : G.sep;
|
||||
|
||||
for (linef1 = 0; linef1 < (f1 ? f1->linecount : 1); linef1++) {
|
||||
l1 = f1 ? &f1->lines[linef1] : NULL;
|
||||
for (linef2 = 0; linef2 < (f2 ? f2->linecount : 1); linef2++) {
|
||||
l2 = f2 ? &f2->lines[linef2] : NULL;
|
||||
|
||||
if (G.format) {
|
||||
/*
|
||||
Format is a sort of null-terminated array:
|
||||
They are indexed by [n] for file number and [n + 1] for field number.
|
||||
If file number is 0 then that is the null-terminator.
|
||||
If file number is neither 1 nor 2 then we have the join field.
|
||||
*/
|
||||
first = true;
|
||||
formatcurr = G.format;
|
||||
while (formatcurr[0]) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
bb_putchar(sep);
|
||||
|
||||
format_fl = formatcurr[0];
|
||||
format_idx = formatcurr[1];
|
||||
|
||||
if (format_fl == 1) {
|
||||
if (l1 != NULL && l1->fieldcount > format_idx)
|
||||
fputs_stdout(fieldorempty(l1->fields[format_idx]));
|
||||
else
|
||||
fputs_stdout(G.empty_str);
|
||||
} else if (format_fl == 2) {
|
||||
if (l2 != NULL && l2->fieldcount > format_idx)
|
||||
fputs_stdout(fieldorempty(l2->fields[format_idx]));
|
||||
else
|
||||
fputs_stdout(G.empty_str);
|
||||
} else
|
||||
fputs_stdout(fieldorempty(field));
|
||||
|
||||
formatcurr += 2;
|
||||
}
|
||||
} else {
|
||||
fputs_stdout(fieldorempty(field));
|
||||
|
||||
fn = 0;
|
||||
if (l1 != NULL)
|
||||
while (l1->fields[fn]) {
|
||||
if (fn != f1->idx) {
|
||||
bb_putchar(sep);
|
||||
fputs_stdout(fieldorempty(l1->fields[fn]));
|
||||
}
|
||||
fn++;
|
||||
}
|
||||
|
||||
fn = 0;
|
||||
if (l2 != NULL)
|
||||
while (l2->fields[fn]) {
|
||||
if (fn != f2->idx) {
|
||||
bb_putchar(sep);
|
||||
fputs_stdout(fieldorempty(l2->fields[fn]));
|
||||
}
|
||||
fn++;
|
||||
}
|
||||
}
|
||||
bb_putchar('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void parsejformat(const char *format_str)
|
||||
{
|
||||
int *format;
|
||||
int field_idx;
|
||||
|
||||
char scache[20] = { 0 };
|
||||
|
||||
int n = 0;
|
||||
const char *s1 = format_str;
|
||||
|
||||
size_t sn;
|
||||
|
||||
/* The most formats we can have is if the format_str is noted as 0,0,0,0
|
||||
which ends up being (strlen + 1) / 2
|
||||
and then we need to have two for each entry plus one for the terminator.
|
||||
( (strlen+1) / 2 * 2 is optimized out in the alloc) */
|
||||
G.format = format = xzalloc(sizeof(format[0]) * ((strlen(format_str) + 1) + 1));
|
||||
|
||||
/* default split: skip the initial whitespace and then any run
|
||||
of non-whitespace characters is a field */
|
||||
while (*format_str) {
|
||||
/* blanks are space and tab in the POSIX locale */
|
||||
while (*format_str == ' ' || *format_str == '\t' || *format_str == ',')
|
||||
format_str++;
|
||||
if (!*format_str)
|
||||
break;
|
||||
|
||||
s1 = strpbrk(format_str, " \t,");
|
||||
if (s1 == NULL)
|
||||
/* last field */
|
||||
sn = strlen(format_str);
|
||||
else
|
||||
sn = s1 - format_str;
|
||||
|
||||
if (sn == 1 && *format_str == '0') {
|
||||
format[n * 2] = 3;
|
||||
} else if (sn >= 3 && (*format_str == '1' || *format_str == '2') && format_str[1] == '.') {
|
||||
if (sn > 21) /* res_str > 19 */
|
||||
bb_simple_error_msg_and_die("field specifier too large");
|
||||
memcpy(scache, format_str + 2, sn - 2);
|
||||
scache[sn - 2] = '\0';
|
||||
field_idx = xatoi_positive(scache);
|
||||
if (--field_idx < 0)
|
||||
bb_simple_error_msg_and_die("field number can't be 0");
|
||||
format[n * 2] = *format_str - '0';
|
||||
format[n * 2 + 1] = field_idx;
|
||||
} else {
|
||||
bb_simple_error_msg_and_die("field specifier must be 0, 1.x or 2.x");
|
||||
}
|
||||
|
||||
n++;
|
||||
format_str += sn;
|
||||
}
|
||||
}
|
||||
|
||||
int join_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int join_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
struct {
|
||||
llist_t *unpaired_list; // = NULL;
|
||||
const char *format_str; // = NULL;
|
||||
|
||||
bool print1unpaired; // = false;
|
||||
bool print2unpaired; // = false;
|
||||
bool printpaired;
|
||||
|
||||
FDAT f1; // = { };
|
||||
FDAT f2; // = { };
|
||||
} L;
|
||||
#define unpaired_list L.unpaired_list
|
||||
#define format_str L.format_str
|
||||
#define print1unpaired L.print1unpaired
|
||||
#define print2unpaired L.print2unpaired
|
||||
#define printpaired L.printpaired
|
||||
#define f1 L.f1
|
||||
#define f2 L.f2
|
||||
|
||||
char *separator;
|
||||
uint32_t opts;
|
||||
/* Must match getopt32 call */
|
||||
enum {
|
||||
FLAG_UNPAIRED_ADD = (1 << 0),
|
||||
FLAG_UNPAIRED_ONLY = (1 << 1),
|
||||
FLAG_EMPTY_STRING = (1 << 2),
|
||||
FLAG_LIST_OUTPUT = (1 << 3),
|
||||
FLAG_SEP_USED = (1 << 4),
|
||||
FLAG_FIELD_1 = (1 << 5),
|
||||
FLAG_FIELD_2 = (1 << 6),
|
||||
};
|
||||
|
||||
INIT_G();
|
||||
G.empty_str = "";
|
||||
memset(&L, 0, sizeof(L));
|
||||
init_unicode();
|
||||
|
||||
opts = getopt32(argv,
|
||||
"^a:*v:*"
|
||||
"e:"
|
||||
"o:"
|
||||
"t:"
|
||||
"1:+2:+"
|
||||
"\0""=2:a--v:v--a",
|
||||
&unpaired_list,
|
||||
&unpaired_list,
|
||||
&G.empty_str,
|
||||
&format_str,
|
||||
&separator,
|
||||
&f1.idx,
|
||||
&f2.idx);
|
||||
argv += optind;
|
||||
|
||||
printpaired = !(opts & FLAG_UNPAIRED_ONLY);
|
||||
if (opts & (FLAG_UNPAIRED_ADD|FLAG_UNPAIRED_ONLY)) {
|
||||
char *unpaired_str;
|
||||
while ((unpaired_str = llist_pop(&unpaired_list)) != NULL) {
|
||||
if (*unpaired_str == '1')
|
||||
print1unpaired = true;
|
||||
else if (*unpaired_str == '2')
|
||||
print2unpaired = true;
|
||||
else
|
||||
bb_show_usage();
|
||||
if (unpaired_str[1])
|
||||
bb_show_usage();
|
||||
}
|
||||
}
|
||||
|
||||
if (((opts & FLAG_FIELD_1) && --f1.idx < 0)
|
||||
|| ((opts & FLAG_FIELD_2) && --f2.idx < 0)
|
||||
) {
|
||||
bb_simple_error_msg_and_die("field 0 doesn't exist");
|
||||
}
|
||||
|
||||
if (opts & FLAG_SEP_USED) {
|
||||
if (separator[0] && separator[1])
|
||||
bb_simple_error_msg_and_die("separators are single characters");
|
||||
|
||||
G.sep = *separator;
|
||||
}
|
||||
|
||||
if (opts & FLAG_LIST_OUTPUT)
|
||||
parsejformat(format_str);
|
||||
|
||||
f1.fp = xfopen_stdin(argv[0]);
|
||||
f2.fp = xfopen_stdin(argv[1]);
|
||||
if (f1.fp == f2.fp)
|
||||
bb_simple_error_msg_and_die("can't combine stdin with itself");
|
||||
|
||||
/*
|
||||
|
||||
Outline of the program:
|
||||
|
||||
- Read a line set from each file into cache
|
||||
|
||||
- While there are cached current lines from both files:
|
||||
- If the two indexing fields are the same:
|
||||
- If no unpaired_only options:
|
||||
- Print joined line set formatted with fields
|
||||
- Read line sets from both files
|
||||
- Elseif indexing1 < indexing2 then
|
||||
- If print1unpaired:
|
||||
- Print line set from file 1
|
||||
- Read another line set from file 1
|
||||
- Elseif indexing1 > indexing2 then
|
||||
- If print2unpaired:
|
||||
- Print line set from file 2
|
||||
- Read another line set from file 2
|
||||
|
||||
- If there are cached lines from file 1 AND print1unpaired:
|
||||
- Print the rest of the lines from file 1
|
||||
- If there are cached lines from file 2 AND print2unpaired:
|
||||
- Print the rest of the lines from file 2
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
Notes for POSIX:
|
||||
|
||||
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/join.html
|
||||
|
||||
Multiple instances of the same key are meant to give combinatorial results.
|
||||
|
||||
E.g:
|
||||
file1:
|
||||
a 1
|
||||
a 2
|
||||
|
||||
file2:
|
||||
a A
|
||||
a B
|
||||
a C
|
||||
|
||||
result:
|
||||
a 1 A
|
||||
a 1 B
|
||||
a 1 C
|
||||
a 2 A
|
||||
a 2 B
|
||||
a 2 C
|
||||
|
||||
This is done in this implementation by reading all the lines that have the same key
|
||||
and then iterating through the combinations when printing them.
|
||||
|
||||
(test with `./busybox join <(printf 'a 1\na 2\n';) <(printf 'a A\na B\na C\n')`)
|
||||
|
||||
*/
|
||||
|
||||
readfields(&f1);
|
||||
readfields(&f2);
|
||||
|
||||
while (f1.linecount != 0 && f2.linecount != 0) {
|
||||
int res = strcmp(f1.field, f2.field);
|
||||
|
||||
if (res == 0) {
|
||||
if (printpaired)
|
||||
printfields(&f1, &f2);
|
||||
|
||||
readfields(&f1);
|
||||
readfields(&f2);
|
||||
} else if (res < 0) {
|
||||
if (print1unpaired)
|
||||
printfields(&f1, NULL);
|
||||
|
||||
readfields(&f1);
|
||||
} else {
|
||||
if (print2unpaired)
|
||||
printfields(NULL, &f2);
|
||||
|
||||
readfields(&f2);
|
||||
}
|
||||
}
|
||||
|
||||
if (print1unpaired) {
|
||||
while (f1.linecount != 0) {
|
||||
printfields(&f1, NULL);
|
||||
readfields(&f1);
|
||||
}
|
||||
}
|
||||
|
||||
if (print2unpaired) {
|
||||
while (f2.linecount != 0) {
|
||||
printfields(NULL, &f2);
|
||||
readfields(&f2);
|
||||
}
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_CLEAN_UP
|
||||
if (f1.linecap) {
|
||||
freelines(&f1);
|
||||
free(f1.lines);
|
||||
}
|
||||
|
||||
if (f2.linecap) {
|
||||
freelines(&f2);
|
||||
free(f2.lines);
|
||||
}
|
||||
|
||||
free(G.format);
|
||||
|
||||
fclose_if_not_stdin(f1.fp);
|
||||
fclose_if_not_stdin(f2.fp);
|
||||
#endif
|
||||
|
||||
fflush_stdout_and_exit_SUCCESS();
|
||||
}
|
||||
@@ -26,7 +26,7 @@ mode_t FAST_FUNC getopt_mk_fifo_nod(char **argv)
|
||||
mode_t mode = 0666;
|
||||
char *smode = NULL;
|
||||
#if ENABLE_SELINUX
|
||||
char *scontext;
|
||||
security_context_t scontext;
|
||||
#endif
|
||||
int opt;
|
||||
opt = getopt32(argv, "m:" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
|
||||
|
||||
Loaded 100 of 1025 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user