Skip to content

Commit

Permalink
Merge branch 'busybox' into merge
Browse files Browse the repository at this point in the history
  • Loading branch information
rmyorston committed Jan 5, 2024
2 parents f723129 + 5dc9ece commit 49edb26
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 48 deletions.
33 changes: 17 additions & 16 deletions editors/awk.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,9 @@ struct globals {
//we are reusing ahash as fdhash, via define (see later)
const char *g_progname;
int g_lineno;
int nfields;
unsigned maxfields;
int num_fields; /* number of existing $N's */
unsigned num_alloc_fields; /* current size of Fields[] */
/* NB: Fields[0] corresponds to $1, not to $0 */
var *Fields;
char *g_pos;
char g_saved_ch;
Expand Down Expand Up @@ -631,8 +632,8 @@ struct globals2 {
// for fdhash in execution stage.
#define g_progname (G1.g_progname )
#define g_lineno (G1.g_lineno )
#define nfields (G1.nfields )
#define maxfields (G1.maxfields )
#define num_fields (G1.num_fields )
#define num_alloc_fields (G1.num_alloc_fields)
#define Fields (G1.Fields )
#define g_pos (G1.g_pos )
#define g_saved_ch (G1.g_saved_ch )
Expand Down Expand Up @@ -1966,30 +1967,30 @@ static void fsrealloc(int size)
{
int i, newsize;

if ((unsigned)size >= maxfields) {
if ((unsigned)size >= num_alloc_fields) {
/* Sanity cap, easier than catering for over/underflows */
if ((unsigned)size > 0xffffff)
bb_die_memory_exhausted();

i = maxfields;
maxfields = size + 16;
i = num_alloc_fields;
num_alloc_fields = size + 16;

newsize = maxfields * sizeof(Fields[0]);
newsize = num_alloc_fields * sizeof(Fields[0]);
debug_printf_eval("fsrealloc: xrealloc(%p, %u)\n", Fields, newsize);
Fields = xrealloc(Fields, newsize);
debug_printf_eval("fsrealloc: Fields=%p..%p\n", Fields, (char*)Fields + newsize - 1);
/* ^^^ did Fields[] move? debug aid for L.v getting "upstaged" by R.v in evaluate() */

for (; i < maxfields; i++) {
Fields[i].type = VF_SPECIAL;
for (; i < num_alloc_fields; i++) {
Fields[i].type = VF_SPECIAL | VF_DIRTY;
Fields[i].string = NULL;
}
}
/* if size < nfields, clear extra field variables */
for (i = size; i < nfields; i++) {
/* if size < num_fields, clear extra field variables */
for (i = size; i < num_fields; i++) {
clrvar(Fields + i);
}
nfields = size;
num_fields = size;
}

static int regexec1_nonempty(const regex_t *preg, const char *s, regmatch_t pmatch[])
Expand Down Expand Up @@ -2126,7 +2127,7 @@ static void split_f0(void)
/* set NF manually to avoid side effects */
clrvar(intvar[NF]);
intvar[NF]->type = VF_NUMBER | VF_SPECIAL;
intvar[NF]->number = nfields;
intvar[NF]->number = num_fields;
#undef fstrings
}

Expand Down Expand Up @@ -2999,7 +3000,7 @@ static var *evaluate(node *op, var *res)
syntax_error(EMSG_TOO_FEW_ARGS);
L.v = evaluate(op1, TMPVAR0);
/* Does L.v point to $n variable? */
if ((size_t)(L.v - Fields) < maxfields) {
if ((size_t)(L.v - Fields) < num_alloc_fields) {
/* yes, remember where Fields[] is */
old_Fields_ptr = Fields;
}
Expand Down Expand Up @@ -3553,7 +3554,7 @@ static var *evaluate(node *op, var *res)
res = intvar[F0];
} else {
split_f0();
if (i > nfields)
if (i > num_fields)
fsrealloc(i);
res = &Fields[i - 1];
}
Expand Down
5 changes: 5 additions & 0 deletions editors/sed.c
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,11 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
fchown(nonstdoutfd, statbuf.st_uid, statbuf.st_gid);

process_files();
fflush(G.nonstdout);
if (ferror(G.nonstdout)) {
xfunc_error_retval = 4; /* It's what gnu sed exits with... */
bb_simple_error_msg_and_die(bb_msg_write_error);
}
fclose(G.nonstdout);
G.nonstdout = stdout;

Expand Down
73 changes: 42 additions & 31 deletions miscutils/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ static void printargv(char *const *argv)
} while (*++argv);
}

#ifdef UNUSED
/* Return the number of kilobytes corresponding to a number of pages PAGES.
(Actually, we use it to convert pages*ticks into kilobytes*ticks.)
Expand All @@ -152,7 +153,8 @@ static unsigned long ptok(const unsigned pagesize, const unsigned long pages)
return tmp / 1024; /* then smaller. */
}
#undef pagesize
#endif
#endif /* UNUSED */
#endif /* !ENABLE_PLATFORM_MINGW32 */

/* summarize: Report on the system use of a command.
Expand Down Expand Up @@ -249,36 +251,31 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
}

switch (*fmt) {
#ifdef NOT_NEEDED
/* Handle literal char */
/* Usually we optimize for size, but there is a limit
* for everything. With this we do a lot of 1-byte writes */
default:
bb_putchar(*fmt);
break;
#endif

case '%':
switch (*++fmt) {
#ifdef NOT_NEEDED_YET
/* Our format strings do not have these */
/* and we do not take format str from user */
#if !ENABLE_PLATFORM_MINGW32
default:
bb_putchar('%');
/* Unknown %<char> is printed as "?<char>" */
bb_putchar('?');
if (!*fmt) {
/* Trailing -f '...%' prints "...?" but NOT newline */
goto ret;
}
/*FALLTHROUGH*/
case '%':
if (!*fmt) goto ret;
bb_putchar(*fmt);
break;
#endif
#if !ENABLE_PLATFORM_MINGW32
case 'C': /* The command that got timed. */
printargv(command);
break;
case 'D': /* Average unshared data size. */
/* (linux kernel sets ru_idrss/isrss/ixrss to 0,
* docs say the value is in kbytes, so ptok() is wrong) */
printf("%lu",
(ptok(pagesize, (UL) resp->ru.ru_idrss) +
ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks);
(/*ptok(pagesize,*/ (UL) resp->ru.ru_idrss +
(UL) resp->ru.ru_isrss
) / cpu_ticks
);
break;
#endif
case 'E': { /* Elapsed real (wall clock) time. */
Expand All @@ -303,13 +300,17 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
printf("%lu", resp->ru.ru_inblock);
break;
case 'K': /* Average mem usage == data+stack+text. */
/* (linux kernel sets ru_idrss/isrss/ixrss to 0,
* docs say the value is in kbytes, so ptok() is wrong) */
printf("%lu",
(ptok(pagesize, (UL) resp->ru.ru_idrss) +
ptok(pagesize, (UL) resp->ru.ru_isrss) +
ptok(pagesize, (UL) resp->ru.ru_ixrss)) / cpu_ticks);
(/*ptok(pagesize,*/ (UL) resp->ru.ru_idrss +
(UL) resp->ru.ru_isrss +
(UL) resp->ru.ru_ixrss
) / cpu_ticks
);
break;
case 'M': /* Maximum resident set size. */
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss));
printf("%lu", (UL) resp->ru.ru_maxrss);
break;
case 'O': /* Outputs. */
printf("%lu", resp->ru.ru_oublock);
Expand Down Expand Up @@ -364,7 +365,7 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
printf("%lu", resp->ru.ru_nswap);
break;
case 'X': /* Average shared text size. */
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks);
printf("%lu", /*ptok(pagesize,*/ (UL) resp->ru.ru_ixrss / cpu_ticks);
break;
case 'Z': /* Page size. */
printf("%u", pagesize);
Expand All @@ -383,7 +384,7 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
printf("%lu", resp->ru.ru_nsignals);
break;
case 'p': /* Average stack segment. */
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks);
printf("%lu", /*ptok(pagesize,*/ (UL) resp->ru.ru_isrss / cpu_ticks);
break;
case 'r': /* Incoming socket messages received. */
printf("%lu", resp->ru.ru_msgrcv);
Expand All @@ -392,7 +393,7 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
printf("%lu", resp->ru.ru_msgsnd);
break;
case 't': /* Average resident set size. */
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks);
printf("%lu", /*ptok(pagesize,*/ (UL) resp->ru.ru_idrss / cpu_ticks);
break;
case 'w': /* Voluntary context switches. */
printf("%lu", resp->ru.ru_nvcsw);
Expand All @@ -404,14 +405,22 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
}
break;

#ifdef NOT_NEEDED_YET
case '\\': /* Format escape. */
#if !ENABLE_PLATFORM_MINGW32
default: /* *fmt is '\': format escape */
switch (*++fmt) {
default:
/* Unknown \<char> is printed as "?\<char>" */
bb_putchar('?');
bb_putchar('\\');
if (!*fmt) {
/* Trailing -f '...\': GNU time 1.9 prints
* "...?\COMMAND" (it's probably a bug).
*/
puts(command[0]);
goto ret;
}
/*FALLTHROUGH*/
case '\\':
if (!*fmt) goto ret;
bb_putchar(*fmt);
break;
case 't':
Expand All @@ -426,8 +435,10 @@ static void summarize(const char *fmt, char **command, resource_t *resp)
}
++fmt;
}
/* ret: */
bb_putchar('\n');
#if !ENABLE_PLATFORM_MINGW32
ret: ;
#endif
}

/* Run command CMD and return statistics on it.
Expand Down Expand Up @@ -469,7 +480,7 @@ int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int time_main(int argc UNUSED_PARAM, char **argv)
{
resource_t res;
/* $TIME has lowest prio (-v,-p,-f FMT overrride it) */
/* $TIME has lowest prio (-v,-p,-f FMT override it) */
const char *output_format = getenv("TIME") ? : default_format;
char *output_filename;
int output_fd;
Expand Down
7 changes: 7 additions & 0 deletions testsuite/awk.tests
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,13 @@ testing 'awk gensub backslashes \\0' \
\\0|\\0
' '' ''

# References to empty (not provided in the input) fields in first versus subsequent lines
testing 'awk references to empty fields' \
'awk '$sq'$2 != 0'$sq \
'a
b
' '' 'a\nb\n'

# The "b" in "abc" should not match <b* pattern.
# Currently we use REG_STARTEND ("This flag is a BSD extension, not present in POSIX")
# to implement the code to handle this correctly, but if your libc has no REG_STARTEND,
Expand Down
37 changes: 37 additions & 0 deletions testsuite/time.tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh

# Copyright 2024 by Denys Vlasenko <[email protected]>
# Licensed under GPLv2, see file LICENSE in this source tree.

. ./testing.sh

# testing "description" "arguments" "result" "infile" "stdin"

testing "time -f trailing backslash" \
"time -f 'abc\' sleep 0 2>&1" \
'abc?\sleep\n' '' ''
# ^^^^^^^^^^^^^^ this is what GNU time version 1.9 prints

testing "time -f trailing percent" \
"time -f 'abc%' sleep 0 2>&1" \
'abc?' '' ''

testing "time -f undefined backslash" \
"time -f 'abc\^def' sleep 0 2>&1" \
'abc?\^def\n' '' ''

testing "time -f undefined percent" \
"time -f 'abc%^def' sleep 0 2>&1" \
'abc?^def\n' '' ''

testing "time -f backslash tab and newline" \
"time -f 'abc\ndef\txyz' sleep 0 2>&1" \
'abc
def xyz
' '' ''

testing "time -f percent percent" \
"time -f 'abc%%def' sleep 0 2>&1" \
'abc%def\n' '' ''

exit $FAILCOUNT
28 changes: 27 additions & 1 deletion util-linux/lsusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@

#include "libbb.h"

static char * FAST_FUNC add_sysfs_prop(const char *dir, const char *suffix,
char *buf, size_t size)
{
char *filename;
ssize_t len;

filename = concat_path_file(dir, suffix);
len = open_read_close(filename, buf, size - 1);
free(filename);

if (len < 0)
len = 0;

buf[len] = '\0';

return trim(buf);
}

static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
const char *fileName,
struct stat *statbuf UNUSED_PARAM)
Expand Down Expand Up @@ -61,7 +79,15 @@ static int FAST_FUNC fileAction(struct recursive_state *state UNUSED_PARAM,
config_close(parser);

if (busnum) {
printf("Bus %s Device %s: ID %04x:%04x\n", busnum, devnum, product_vid, product_did);
char name[256], *p;

p = add_sysfs_prop(fileName, "/manufacturer", name, sizeof(name) - 1);
if (p != name)
p = stpcpy(p, " ");
add_sysfs_prop(fileName, "/product", p, name + sizeof(name) - p);

printf("Bus %s Device %s: ID %04x:%04x %s\n", busnum, devnum,
product_vid, product_did, name);
free(busnum);
free(devnum);
}
Expand Down

0 comments on commit 49edb26

Please sign in to comment.