aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toys.h2
-rw-r--r--toys/posix/sed.c19
-rw-r--r--toys/posix/sort.c17
-rw-r--r--toys/posix/strings.c5
-rw-r--r--toys/posix/tail.c11
-rw-r--r--toys/posix/tee.c4
-rw-r--r--toys/posix/time.c2
-rw-r--r--toys/posix/touch.c13
-rw-r--r--toys/posix/ulimit.c14
-rw-r--r--toys/posix/uniq.c13
-rw-r--r--toys/posix/uuencode.c2
-rw-r--r--toys/posix/wc.c4
-rw-r--r--toys/posix/who.c2
-rw-r--r--toys/posix/xargs.c2
14 files changed, 53 insertions, 57 deletions
diff --git a/toys.h b/toys.h
index 5e71e3fe..4083725a 100644
--- a/toys.h
+++ b/toys.h
@@ -119,6 +119,8 @@ extern char toybuf[4096], libbuf[4096];
extern char **environ;
+#define FLAG(x) (toys.optflags&FLAG_##x)
+
#define GLOBALS(...)
#define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))
#define TAGGED_ARRAY(X, ...) {__VA_ARGS__}
diff --git a/toys/posix/sed.c b/toys/posix/sed.c
index eadac885..03179ca2 100644
--- a/toys/posix/sed.c
+++ b/toys/posix/sed.c
@@ -260,7 +260,7 @@ static void sed_line(char **pline, long plen)
int eol = 0, tea = 0;
// Ignore EOF for all files before last unless -i
- if (!pline && !(toys.optflags&FLAG_i)) return;
+ if (!pline && !FLAG(i)) return;
// Grab next line for deferred processing (EOF detection: we get a NULL
// pline at EOF to flush last line). Note that only end of _last_ input
@@ -600,7 +600,7 @@ writenow:
command = command->next;
}
- if (line && !(toys.optflags & FLAG_n)) emit(line, len, eol);
+ if (line && !FLAG(n)) emit(line, len, eol);
done:
if (dlist_terminate(append)) while (append) {
@@ -627,10 +627,9 @@ done:
// Callback called on each input file
static void do_sed_file(int fd, char *name)
{
- int i = toys.optflags & FLAG_i;
char *tmp;
- if (i) {
+ if (FLAG(i)) {
struct sedcmd *command;
if (!fd) return error_msg("-i on stdin");
@@ -640,7 +639,7 @@ static void do_sed_file(int fd, char *name)
command->hit = 0;
}
do_lines(fd, sed_line);
- if (i) {
+ if (FLAG(i)) {
replace_tempfile(-1, TT.fdout, &tmp);
TT.fdout = 1;
TT.nextline = 0;
@@ -780,7 +779,7 @@ static void parse_pattern(char **pline, long len)
if (!(s = unescape_delimited_string(&line, 0))) goto error;
if (!*s) command->rmatch[i] = 0;
else {
- xregcomp((void *)reg, s, (toys.optflags & FLAG_r)*REG_EXTENDED);
+ xregcomp((void *)reg, s, FLAG(r)*REG_EXTENDED);
command->rmatch[i] = reg-toybuf;
reg += sizeof(regex_t);
}
@@ -874,7 +873,7 @@ resume_s:
// allocating the space was done by extend_string() above
if (!*TT.remember) command->arg1 = 0;
else xregcomp((void *)(command->arg1 + (char *)command), TT.remember,
- ((toys.optflags & FLAG_r)*REG_EXTENDED)|((command->sflags&1)*REG_ICASE));
+ (FLAG(r)*REG_EXTENDED)|((command->sflags&1)*REG_ICASE));
free(TT.remember);
TT.remember = 0;
if (*line == 'w') {
@@ -995,13 +994,13 @@ void sed_main(void)
// Lie to autoconf when it asks stupid questions, so configure regexes
// that look for "GNU sed version %f" greater than some old buggy number
// don't fail us for not matching their narrow expectations.
- if (toys.optflags & FLAG_version) {
+ if (FLAG(version)) {
xprintf("This is not GNU sed version 9.0\n");
return;
}
// Handling our own --version means we handle our own --help too.
- if (toys.optflags&FLAG_help) help_exit(0);
+ if (FLAG(help)) help_exit(0);
// Parse pattern into commands.
@@ -1027,7 +1026,7 @@ void sed_main(void)
loopfiles_rw(args, O_RDONLY|WARN_ONLY, 0, do_sed_file);
// Provide EOF flush at end of cumulative input for non-i mode.
- if (!(toys.optflags & FLAG_i)) {
+ if (!FLAG(i)) {
toys.optflags |= FLAG_i;
sed_line(0, 0);
}
diff --git a/toys/posix/sort.c b/toys/posix/sort.c
index c33b8ff0..2f4d49a0 100644
--- a/toys/posix/sort.c
+++ b/toys/posix/sort.c
@@ -250,7 +250,7 @@ static int compare_keys(const void *xarg, const void *yarg)
// Perform fallback sort if necessary (always case insensitive, no -f,
// the point is to get a stable order even for -f sorts)
- if (!retval && !(toys.optflags&FLAG_s)) {
+ if (!retval && !FLAG(s)) {
flags = toys.optflags;
retval = strcmp(xx, yy);
}
@@ -264,14 +264,13 @@ static void sort_read(int fd, char *name)
// Read each line from file, appending to a big array.
for (;;) {
- char * line = (toys.optflags&FLAG_z)
- ? get_rawline(fd, NULL, 0) : get_line(fd);
+ char * line = FLAG(z) ? get_rawline(fd, NULL, 0) : get_line(fd);
if (!line) break;
// handle -c here so we don't allocate more memory than necessary.
- if (toys.optflags&FLAG_c) {
- int j = (toys.optflags&FLAG_u) ? -1 : 0;
+ if (FLAG(c)) {
+ int j = FLAG(u) ? -1 : 0;
if (TT.lines && compare_keys((void *)&TT.lines, &line)>j)
error_exit("%s: Check line %d\n", name, TT.linecount);
@@ -343,7 +342,7 @@ void sort_main(void)
}
// global b flag strips both leading and trailing spaces
- if (toys.optflags&FLAG_b) toys.optflags |= FLAG_bb;
+ if (FLAG(b)) toys.optflags |= FLAG_bb;
// If no keys, perform alphabetic sort over the whole line.
if (!TT.key_list) add_key()->range[0] = 1;
@@ -353,13 +352,13 @@ void sort_main(void)
// The compare (-c) logic was handled in sort_read(),
// so if we got here, we're done.
- if (toys.optflags&FLAG_c) goto exit_now;
+ if (FLAG(c)) goto exit_now;
// Perform the actual sort
qsort(TT.lines, TT.linecount, sizeof(char *), compare_keys);
// handle unique (-u)
- if (toys.optflags&FLAG_u) {
+ if (FLAG(u)) {
int jdx;
for (jdx=0, idx=1; idx<TT.linecount; idx++) {
@@ -375,7 +374,7 @@ void sort_main(void)
char *s = TT.lines[idx];
unsigned i = strlen(s);
- if (!(toys.optflags&FLAG_z)) s[i] = '\n';
+ if (!FLAG(z)) s[i] = '\n';
xwrite(fd, s, i+1);
if (CFG_TOYBOX_FREE) free(s);
}
diff --git a/toys/posix/strings.c b/toys/posix/strings.c
index e33ae960..816280b0 100644
--- a/toys/posix/strings.c
+++ b/toys/posix/strings.c
@@ -66,9 +66,8 @@ static void do_strings(int fd, char *filename)
else {
string[count++] = toybuf[i];
if (count == wlen) {
- if (toys.optflags & FLAG_f) printf("%s: ", filename);
- if (toys.optflags & (FLAG_o|FLAG_t))
- printf(pattern, (long long)(offset - wlen));
+ if (FLAG(f)) printf("%s: ", filename);
+ if (FLAG(o) || FLAG(t)) printf(pattern, (long long)(offset - wlen));
printf("%s", string);
}
}
diff --git a/toys/posix/tail.c b/toys/posix/tail.c
index d01329c4..8c6548d3 100644
--- a/toys/posix/tail.c
+++ b/toys/posix/tail.c
@@ -136,7 +136,7 @@ static void do_tail(int fd, char *name)
long bytes = TT.c, lines = TT.n;
int linepop = 1;
- if (toys.optflags & FLAG_f) {
+ if (FLAG(f)) {
int f = TT.file_no*2;
char *s = name;
@@ -223,7 +223,7 @@ void tail_main(void)
{
char **args = toys.optargs;
- if (!(toys.optflags&(FLAG_n|FLAG_c))) {
+ if (!FLAG(n) && !FLAG(c)) {
char *arg = *args;
// handle old "-42" style arguments
@@ -237,14 +237,13 @@ void tail_main(void)
}
// Allocate 2 ints per optarg for -f
- if (toys.optflags&FLAG_f) {
+ if (FLAG(f)) {
if ((TT.ffd = inotify_init()) < 0) perror_exit("inotify_init");
TT.files = xmalloc(toys.optc*8);
}
- loopfiles_rw(args, O_RDONLY|WARN_ONLY|(O_CLOEXEC*!(toys.optflags&FLAG_f)),
- 0, do_tail);
+ loopfiles_rw(args, O_RDONLY|WARN_ONLY|(O_CLOEXEC*!FLAG(f)), 0, do_tail);
- if ((toys.optflags & FLAG_f) && TT.file_no) {
+ if (FLAG(f) && TT.file_no) {
int len, last_fd = TT.files[(TT.file_no-1)*2], i, fd;
struct inotify_event ev;
diff --git a/toys/posix/tee.c b/toys/posix/tee.c
index 90098ce7..78139a8d 100644
--- a/toys/posix/tee.c
+++ b/toys/posix/tee.c
@@ -45,11 +45,11 @@ static void do_tee_open(int fd, char *name)
void tee_main(void)
{
- if (toys.optflags & FLAG_i) xsignal(SIGINT, SIG_IGN);
+ if (FLAG(i)) xsignal(SIGINT, SIG_IGN);
// Open output files
loopfiles_rw(toys.optargs,
- O_RDWR|O_CREAT|WARN_ONLY|((toys.optflags & FLAG_a)?O_APPEND:O_TRUNC),
+ O_RDWR|O_CREAT|WARN_ONLY|(FLAG(a)?O_APPEND:O_TRUNC),
0666, do_tee_open);
for (;;) {
diff --git a/toys/posix/time.c b/toys/posix/time.c
index 0cf63dee..79a98676 100644
--- a/toys/posix/time.c
+++ b/toys/posix/time.c
@@ -45,7 +45,7 @@ void time_main(void)
r = (tv2.tv_sec-tv.tv_sec)+((tv2.tv_usec-tv.tv_usec)/1000000.0);
u = ru.ru_utime.tv_sec+(ru.ru_utime.tv_usec/1000000.0);
s = ru.ru_stime.tv_sec+(ru.ru_stime.tv_usec/1000000.0);
- if (toys.optflags&FLAG_v) {
+ if (FLAG(v)) {
fprintf(stderr, "Real time (s): %f\n"
"System time (s): %f\n"
"User time (s): %f\n"
diff --git a/toys/posix/touch.c b/toys/posix/touch.c
index 17a0a3ed..3775c8bc 100644
--- a/toys/posix/touch.c
+++ b/toys/posix/touch.c
@@ -41,7 +41,7 @@ void touch_main(void)
// use current time if no -t or -d
ts[0].tv_nsec = UTIME_NOW;
- if (toys.optflags & (FLAG_t|FLAG_d)) {
+ if (FLAG(t) || FLAG(d)) {
char *s, *date, **format;
struct tm tm;
int len = 0;
@@ -51,7 +51,7 @@ void touch_main(void)
ts->tv_nsec = 0;
// List of search types
- if (toys.optflags & FLAG_d) {
+ if (FLAG(d)) {
format = (char *[]){"%Y-%m-%dT%T", "%Y-%m-%d %T", 0};
date = TT.d;
} else {
@@ -67,7 +67,7 @@ void touch_main(void)
}
while (*format) {
- if (toys.optflags&FLAG_t) {
+ if (FLAG(t)) {
s = strchr(date, '.');
if ((s ? s-date : strlen(date)) != strlen(*format)) {
format++;
@@ -83,7 +83,7 @@ void touch_main(void)
// parse nanoseconds
if (s && *s=='.' && isdigit(s[1])) {
s++;
- if (toys.optflags&FLAG_t)
+ if (FLAG(t))
if (1 == sscanf(s, "%2u%n", &(tm.tm_sec), &len)) s += len;
if (1 == sscanf(s, "%lu%n", &ts->tv_nsec, &len)) {
s += len;
@@ -122,9 +122,8 @@ void touch_main(void)
if (!futimens(1, ts)) continue;
} else {
// cheat: FLAG_h is rightmost flag, so its value is 1
- if (!utimensat(AT_FDCWD, s, ts,
- (toys.optflags & FLAG_h)*AT_SYMLINK_NOFOLLOW)) continue;
- if (toys.optflags & FLAG_c) continue;
+ if (!utimensat(AT_FDCWD, s, ts, FLAG(h)*AT_SYMLINK_NOFOLLOW)) continue;
+ if (FLAG(c)) continue;
if (access(s, F_OK) && (-1!=(fd = open(s, O_CREAT, 0666)))) {
close(fd);
if (toys.optflags) ss--;
diff --git a/toys/posix/ulimit.c b/toys/posix/ulimit.c
index 4fa01618..da7f2ead 100644
--- a/toys/posix/ulimit.c
+++ b/toys/posix/ulimit.c
@@ -68,10 +68,10 @@ void ulimit_main(void)
RLIMIT_CPU, RLIMIT_NPROC, RLIMIT_AS};
if (!(toys.optflags&(FLAG_H-1))) toys.optflags |= FLAG_f;
- if ((toys.optflags&(FLAG_a|FLAG_p)) && toys.optc) error_exit("can't set -ap");
+ if ((FLAG(a)||FLAG(p)) && toys.optc) error_exit("can't set -ap");
// Fetch data
- if (!(toys.optflags&FLAG_P)) TT.P = getppid();
+ if (!FLAG(P)) TT.P = getppid();
for (i=0; i<sizeof(map); i++) {
char *flags="cdefilmnpqRrstuv";
@@ -80,10 +80,10 @@ void ulimit_main(void)
if (get && prlimit(TT.P, map[i], 0, &rr)) perror_exit("-%c", flags[i]);
if (!toys.optc) {
- if (toys.optflags&FLAG_a) printf("-%c: ", flags[i]);
+ if (FLAG(a)) printf("-%c: ", flags[i]);
if (get) {
if ((1<<i)&FLAG_p) {
- if (toys.optflags&FLAG_H)
+ if (FLAG(H))
xreadfile("/proc/sys/fs/pipe-max-size", toybuf, sizeof(toybuf));
else {
int pp[2];
@@ -93,7 +93,7 @@ void ulimit_main(void)
}
printf("%s", toybuf);
} else {
- rlim_t rl = (toys.optflags&FLAG_H) ? rr.rlim_max : rr.rlim_cur;
+ rlim_t rl = FLAG(H) ? rr.rlim_max : rr.rlim_cur;
if (rl == RLIM_INFINITY) printf("unlimited\n");
else printf("%ld\n", (long)rl);
@@ -103,7 +103,7 @@ void ulimit_main(void)
if (toys.optflags&(1<<i)) break;
}
- if (toys.optflags&(FLAG_a|FLAG_p)) return;
+ if (FLAG(a)||FLAG(p)) return;
if (toys.optc) {
rlim_t val;
@@ -111,7 +111,7 @@ void ulimit_main(void)
if (tolower(**toys.optargs) == 'u') val = RLIM_INFINITY;
else val = atolx_range(*toys.optargs, 0, LONG_MAX);
- if (toys.optflags&FLAG_H) rr.rlim_max = val;
+ if (FLAG(H)) rr.rlim_max = val;
else rr.rlim_cur = val;
if (prlimit(TT.P, map[i], &rr, 0)) perror_exit(0);
}
diff --git a/toys/posix/uniq.c b/toys/posix/uniq.c
index 6c89c630..0ca01783 100644
--- a/toys/posix/uniq.c
+++ b/toys/posix/uniq.c
@@ -50,10 +50,10 @@ static char *skip(char *str)
static void print_line(FILE *f, char *line)
{
- if (toys.optflags & (TT.repeats ? FLAG_u : FLAG_d)) return;
- if (toys.optflags & FLAG_c) fprintf(f, "%7lu ", TT.repeats + 1);
+ if (TT.repeats ? FLAG(u) : FLAG(d)) return;
+ if (FLAG(c)) fprintf(f, "%7lu ", TT.repeats + 1);
fputs(line, f);
- if (toys.optflags & FLAG_z) fputc(0, f);
+ if (FLAG(z)) fputc(0, f);
}
void uniq_main(void)
@@ -65,7 +65,7 @@ void uniq_main(void)
if (toys.optc >= 1) infile = xfopen(toys.optargs[0], "r");
if (toys.optc >= 2) outfile = xfopen(toys.optargs[1], "w");
- if (toys.optflags & FLAG_z) eol = 0;
+ if (FLAG(z)) eol = 0;
// If first line can't be read
if (getdelim(&prevline, &prevsize, eol, infile) < 0) return;
@@ -84,9 +84,8 @@ void uniq_main(void)
}
if (!TT.w)
- diff = !(toys.optflags & FLAG_i) ? strcmp(t1, t2) : strcasecmp(t1, t2);
- else diff = !(toys.optflags & FLAG_i) ? strncmp(t1, t2, TT.w)
- : strncasecmp(t1, t2, TT.w);
+ diff = !FLAG(i) ? strcmp(t1, t2) : strcasecmp(t1, t2);
+ else diff = !FLAG(i) ? strncmp(t1, t2, TT.w) : strncasecmp(t1, t2, TT.w);
if (!diff) TT.repeats++;
else {
diff --git a/toys/posix/uuencode.c b/toys/posix/uuencode.c
index f0e5be12..44e38bcb 100644
--- a/toys/posix/uuencode.c
+++ b/toys/posix/uuencode.c
@@ -24,7 +24,7 @@ void uuencode_main(void)
{
char *name = toys.optargs[toys.optc-1], buf[(76/4)*3];
- int i, m = toys.optflags & FLAG_m, fd = 0;
+ int i, m = FLAG(m), fd = 0;
if (toys.optc > 1) fd = xopenro(toys.optargs[0]);
diff --git a/toys/posix/wc.c b/toys/posix/wc.c
index 5d362e7d..910e4690 100644
--- a/toys/posix/wc.c
+++ b/toys/posix/wc.c
@@ -42,7 +42,7 @@ static void show_lengths(unsigned long *lengths, char *name)
// And, yes, folks have test scripts that rely on all this nonsense :-(
// Note: sufficiently modern versions of coreutils wc will use the smallest
// column width necessary to have all columns be equal width rather than 0.
- if (!(toys.optc==0 && (toys.optflags & (toys.optflags-1))==0) && toys.optc!=1)
+ if (!(!toys.optc && !(toys.optflags & (toys.optflags-1))) && toys.optc!=1)
space = 7;
for (i = 0; i<4; i++) {
@@ -82,7 +82,7 @@ static void do_wc(int fd, char *name)
for (pos = 0; pos<len; pos++) {
if (toybuf[pos]=='\n') lengths[0]++;
lengths[2]++;
- if (toys.optflags&FLAG_m) {
+ if (FLAG(m)) {
// If we've consumed next wide char
if (--clen<1) {
wchar_t wchar;
diff --git a/toys/posix/who.c b/toys/posix/who.c
index 414cdfc0..53252e87 100644
--- a/toys/posix/who.c
+++ b/toys/posix/who.c
@@ -31,7 +31,7 @@ void who_main(void)
setutxent();
while ((entry = getutxent())) {
- if ((toys.optflags & FLAG_a) || entry->ut_type == USER_PROCESS) {
+ if (FLAG(a) || entry->ut_type == USER_PROCESS) {
time_t time;
int time_size;
char *times;
diff --git a/toys/posix/xargs.c b/toys/posix/xargs.c
index aaa0ac79..d25a2e3f 100644
--- a/toys/posix/xargs.c
+++ b/toys/posix/xargs.c
@@ -120,7 +120,7 @@ void xargs_main(void)
posix_max_bytes = sysconf(_SC_ARG_MAX) - environ_bytes() - 2048;
if (!TT.s || TT.s > posix_max_bytes) TT.s = posix_max_bytes;
- if (!(toys.optflags & FLAG_0)) TT.delim = '\n';
+ if (!FLAG(0)) TT.delim = '\n';
// If no optargs, call echo.
if (!toys.optc) {