diff options
author | Rob Landley <rob@landley.net> | 2023-01-06 19:02:17 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2023-01-06 19:05:51 -0600 |
commit | e0ea4d471216103fcd85719e9539c47220b335e1 (patch) | |
tree | dd86f5a5a4c0e493642cf706dc3671e299813006 | |
parent | 2012ee53ec9dfb0ac6db88c80f18eb349d918e40 (diff) | |
download | toybox-e0ea4d471216103fcd85719e9539c47220b335e1.tar.gz |
Rename/move xmemcmp() (which doesn't exit) to smemcmp(), and add x prefix
to notstdio() while there.
-rw-r--r-- | lib/env.c | 2 | ||||
-rw-r--r-- | lib/lib.c | 16 | ||||
-rw-r--r-- | lib/lib.h | 4 | ||||
-rw-r--r-- | lib/tty.c | 2 | ||||
-rw-r--r-- | lib/xwrap.c | 19 | ||||
-rw-r--r-- | toys/other/hexedit.c | 2 | ||||
-rw-r--r-- | toys/other/nbd_client.c | 2 | ||||
-rw-r--r-- | toys/other/readelf.c | 2 | ||||
-rw-r--r-- | toys/other/taskset.c | 2 | ||||
-rw-r--r-- | toys/pending/sh.c | 16 | ||||
-rw-r--r-- | toys/posix/cpio.c | 2 | ||||
-rw-r--r-- | toys/posix/file.c | 58 | ||||
-rw-r--r-- | toys/posix/od.c | 2 | ||||
-rw-r--r-- | toys/posix/ps.c | 2 | ||||
-rw-r--r-- | toys/posix/tar.c | 2 |
15 files changed, 66 insertions, 67 deletions
@@ -61,7 +61,7 @@ char *xsetenv(char *name, char *val) for (i = 0; environ[i]; i++) { // Drop old entry, freeing as appropriate. Assumes no duplicates. - if (!xmemcmp(name, environ[i], len) && environ[i][len]=='=') { + if (!smemcmp(name, environ[i], len) && environ[i][len]=='=') { if (i<toys.envc-1) toys.envc--; else free(environ[i]); j++; @@ -738,7 +738,7 @@ void loopfiles_rw(char **argv, int flags, int permissions, // Inability to open a file prints a warning, but doesn't exit. if (!strcmp(*argv, "-")) fd = 0; - else if (0>(fd = notstdio(open(*argv, flags, permissions))) && !failok) { + else if (0>(fd = xnotstdio(open(*argv, flags, permissions))) && !failok) { perror_msg_raw(*argv); if (!anyway) continue; } @@ -1492,7 +1492,7 @@ int is_tar_header(void *pkt) char *p = pkt; int i = 0; - if (p[257] && xmemcmp("ustar", p+257, 5)) return 0; + if (p[257] && smemcmp("ustar", p+257, 5)) return 0; if (p[148] != '0' && p[148] != ' ') return 0; sscanf(p+148, "%8o", &i); @@ -1550,3 +1550,15 @@ void octal_deslash(char *s) *o = 0; } + +// ASAN flips out about memcmp("a", "abc", 4) but the result is well-defined. +// This one's guaranteed to stop at len _or_ the first difference. +int smemcmp(char *one, char *two, unsigned long len) +{ + int ii = 0; + + while (len--) if ((ii = *one++ - *two++)) break; + + return ii; +} + @@ -103,7 +103,6 @@ struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); #define ABS_LAST 8 // don't resolve symlink in last path component // xwrap.c -int xmemcmp(char *one, char *two, unsigned long len); void xstrncpy(char *dest, char *src, size_t size); void xstrncat(char *dest, char *src, size_t size); _Noreturn void _xexit(void); @@ -146,7 +145,7 @@ int xopenro(char *path); void xpipe(int *pp); void xclose(int fd); int xdup(int fd); -int notstdio(int fd); +int xnotstdio(int fd); FILE *xfdopen(int fd, char *mode); FILE *xfopen(char *path, char *mode); size_t xread(int fd, void *buf, size_t len); @@ -273,6 +272,7 @@ unsigned tar_cksum(void *data); int is_tar_header(void *pkt); char *elf_arch_name(int type); void octal_deslash(char *s); +int smemcmp(char *one, char *two, unsigned long len); #define HR_SPACE 1 // Space between number and units #define HR_B 2 // Use "B" for single byte units @@ -22,7 +22,7 @@ int tty_fd(void) for (i = 0; i<3; i++) if (isatty(j = (i+1)%3)) return j; - return notstdio(open("/dev/tty", O_RDWR)); + return xnotstdio(open("/dev/tty", O_RDWR)); } // Query size of terminal (without ANSI probe fallback). diff --git a/lib/xwrap.c b/lib/xwrap.c index e1d242ff..5ea47d4c 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -9,17 +9,6 @@ #include "toys.h" -// ASAN flips out about memcmp("a", "abc", 4) but the result is well-defined. -// This one's guaranteed to stop at len _or_ the first difference. -int xmemcmp(char *one, char *two, unsigned long len) -{ - int ii = 0; - - while (len--) if ((ii = *one++ - *two++)) break; - - return ii; -} - // strcpy and strncat with size checking. Size is the total space in "dest", // including null terminator. Exit if there's not enough space for the string // (including space for the null terminator), because silently truncating is @@ -468,9 +457,7 @@ int xdup(int fd) return fd; } -// Move file descriptor above stdin/stdout/stderr, using /dev/null to consume -// old one. (We should never be called with stdin/stdout/stderr closed, but...) -int notstdio(int fd) +int xnotstdio(int fd) { if (fd<0) return fd; @@ -503,13 +490,13 @@ int xtempfile(char *name, char **tempname) // Create a file but don't return stdin/stdout/stderr int xcreate(char *path, int flags, int mode) { - return notstdio(xcreate_stdio(path, flags, mode)); + return xnotstdio(xcreate_stdio(path, flags, mode)); } // Open a file descriptor NOT in stdin/stdout/stderr int xopen(char *path, int flags) { - return notstdio(xopen_stdio(path, flags)); + return xnotstdio(xopen_stdio(path, flags)); } // Open read only, treating "-" as a synonym for stdin, defaulting to warn only diff --git a/toys/other/hexedit.c b/toys/other/hexedit.c index 2b8f632a..4550dc34 100644 --- a/toys/other/hexedit.c +++ b/toys/other/hexedit.c @@ -181,7 +181,7 @@ static void find_prev(int pos) size_t len = strlen(TT.search); for (; pos >= 0; pos--) { - if (!xmemcmp(TT.data+pos, TT.search, len)) { + if (!smemcmp(TT.data+pos, TT.search, len)) { TT.pos = pos; return; } diff --git a/toys/other/nbd_client.c b/toys/other/nbd_client.c index 8a46993e..caa7da61 100644 --- a/toys/other/nbd_client.c +++ b/toys/other/nbd_client.c @@ -76,7 +76,7 @@ void nbd_client_main(void) // Read login data xreadall(sock, toybuf, 152); - if (xmemcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16)) + if (smemcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16)) error_exit("bad login %s:%s", host, port); devsize = SWAP_BE64(*(unsigned long long *)(toybuf+16)); flags = SWAP_BE32(*(int *)(toybuf+24)); diff --git a/toys/other/readelf.c b/toys/other/readelf.c index 6d01190b..e93e6eb0 100644 --- a/toys/other/readelf.c +++ b/toys/other/readelf.c @@ -355,7 +355,7 @@ static void scan_elf() char *hdr = TT.elf; int type, machine, version, flags, entry, ehsize, phnum, shstrndx, i, j, w; - if (TT.size < 45 || xmemcmp(hdr, "\177ELF", 4)) + if (TT.size < 45 || smemcmp(hdr, "\177ELF", 4)) return error_msg("%s: not ELF", TT.f); TT.bits = hdr[4] - 1; diff --git a/toys/other/taskset.c b/toys/other/taskset.c index 7ca26490..a3d0ffb3 100644 --- a/toys/other/taskset.c +++ b/toys/other/taskset.c @@ -127,7 +127,7 @@ void nproc_main(void) char *ss; while ((de = readdir(dd))) { - if (xmemcmp(de->d_name, "cpu", 3)) continue; + if (smemcmp(de->d_name, "cpu", 3)) continue; for (ss = de->d_name+3; isdigit(*ss); ss++); if (!*ss) nproc++; } diff --git a/toys/pending/sh.c b/toys/pending/sh.c index d9abb61a..0f9532e2 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -637,7 +637,7 @@ static int recalculate(long long *dd, char **ss, int lvl) // Assignment operators: = *= /= %= += -= <<= >>= &= ^= |= } else if (lvl<=2 && (*ss)[ii = (-1 != stridx("*/%+-", **ss)) - +2*!xmemcmp(*ss, "<<", 2)+2*!xmemcmp(*ss, ">>", 2)]=='=') + +2*!smemcmp(*ss, "<<", 2)+2*!smemcmp(*ss, ">>", 2)]=='=') { // TODO: assignments are lower priority BUT must go after variable, // come up with precedence checking tests? @@ -2748,7 +2748,7 @@ static struct sh_process *run_command(void) // Skip [[ ]] and (( )) contents for now if ((s = arg->v[envlen])) { - if (!xmemcmp(s, "((", 2)) skiplen = 1; + if (!smemcmp(s, "((", 2)) skiplen = 1; else if (!strcmp(s, "[[")) while (strcmp(arg->v[envlen+skiplen++], "]]")); } pp = expand_redir(arg, envlen+skiplen, 0); @@ -2812,7 +2812,7 @@ static struct sh_process *run_command(void) // Several NOFORK can just NOP in a pipeline? Except ${a?b} still errors // ((math)) - else if (!xmemcmp(s = *pp->arg.v, "((", 2)) { + else if (!smemcmp(s = *pp->arg.v, "((", 2)) { char *ss = s+2; long long ll; @@ -3081,7 +3081,7 @@ static int parse_line(char *line, struct sh_pipeline **ppl, } // "for" on its own line is an error. - if (arg->c == 1 && ex && !xmemcmp(ex, "do\0A", 4)) { + if (arg->c == 1 && ex && !smemcmp(ex, "do\0A", 4)) { s = "newline"; goto flush; } @@ -3175,7 +3175,7 @@ static int parse_line(char *line, struct sh_pipeline **ppl, free(s); s = 0; // TODO can't have ; between "for i" and in or do. (Newline yes, ; no. Why?) - if (!arg->c && ex && !xmemcmp(ex, "do\0C", 4)) continue; + if (!arg->c && ex && !smemcmp(ex, "do\0C", 4)) continue; // ;; and friends only allowed in case statements } else if (*s == ';') goto flush; @@ -3187,7 +3187,7 @@ static int parse_line(char *line, struct sh_pipeline **ppl, continue; // a for/select must have at least one additional argument on same line - } else if (ex && !xmemcmp(ex, "do\0A", 4)) { + } else if (ex && !smemcmp(ex, "do\0A", 4)) { // Sanity check and break the segment if (strncmp(s, "((", 2) && *varend(s)) goto flush; @@ -3206,7 +3206,7 @@ static int parse_line(char *line, struct sh_pipeline **ppl, // The "test" part of for/select loops can have (at most) one "in" line, // for {((;;))|name [in...]} do - if (ex && !xmemcmp(ex, "do\0C", 4)) { + if (ex && !smemcmp(ex, "do\0C", 4)) { if (strcmp(s, "do")) { // can only have one "in" line between for/do, but not with for(()) if (pl->prev->type == 's') goto flush; @@ -3238,7 +3238,7 @@ static int parse_line(char *line, struct sh_pipeline **ppl, // Expecting NULL means any statement (don't care which). if (!ex && *expect) { - if (pl->prev->type == 'f' && !end && xmemcmp(s, "((", 2)) goto flush; + if (pl->prev->type == 'f' && !end && smemcmp(s, "((", 2)) goto flush; free(dlist_lpop(expect)); } diff --git a/toys/posix/cpio.c b/toys/posix/cpio.c index b5542000..8faf899b 100644 --- a/toys/posix/cpio.c +++ b/toys/posix/cpio.c @@ -132,7 +132,7 @@ void cpio_main(void) if (empty) error_exit("empty archive"); else break; } - if (size != 110 || xmemcmp(toybuf, "070701", 6)) error_exit("bad header"); + if (size != 110 || smemcmp(toybuf, "070701", 6)) error_exit("bad header"); tofree = name = strpad(afd, x8u(toybuf+94), 110); if (!strcmp("TRAILER!!!", name)) { free(tofree); diff --git a/toys/posix/file.c b/toys/posix/file.c index acfd28da..e48a5472 100644 --- a/toys/posix/file.c +++ b/toys/posix/file.c @@ -177,10 +177,10 @@ static void do_elf_file(int fd) goto bad; } - if (n_namesz==4 && !xmemcmp(note+12, "GNU", 4) && n_type==3) { + if (n_namesz==4 && !smemcmp(note+12, "GNU", 4) && n_type==3) { printf(", BuildID="); for (j = 0; j<n_descsz; j++) printf("%02x", note[16+j]); - } else if (n_namesz==8 && !xmemcmp(note+12, "Android", 8)) { + } else if (n_namesz==8 && !smemcmp(note+12, "Android", 8)) { if (n_type==1 /*.android.note.ident*/ && n_descsz >= 4) { printf(", for Android %d", (int)elf_int(note+20, 4)); // NDK r14 and later also include NDK version info. OS binaries @@ -244,7 +244,7 @@ static void do_regular_file(int fd, char *name) s-3, (int)peek_le(s, 2), (int)peek_le(s+2, 2)); // TODO: parsing JPEG for width/height is harder than GIF or PNG. - else if (len>32 && !xmemcmp(s, "\xff\xd8", 2)) xputs("JPEG image data"); + else if (len>32 && !smemcmp(s, "\xff\xd8", 2)) xputs("JPEG image data"); else if (len>8 && strstart(&s, "\xca\xfe\xba\xbe")) { unsigned count = peek_be(s, 4), i, arch; @@ -307,7 +307,7 @@ static void do_regular_file(int fd, char *name) else if (len>31 && peek_be(s, 7) == 0xfd377a585a0000UL) xputs("xz compressed data"); else if (len>10 && strstart(&s, "\x1f\x8b")) xputs("gzip compressed data"); - else if (len>32 && !xmemcmp(s+1, "\xfa\xed\xfe", 3)) { + else if (len>32 && !smemcmp(s+1, "\xfa\xed\xfe", 3)) { int bit = (*s==0xce) ? 32 : 64; char *what = 0; @@ -325,26 +325,26 @@ static void do_regular_file(int fd, char *name) else what = NULL; if (what) xprintf("%s\n", what); else xprintf("(bad type %d)\n", s[9]); - } else if (len>36 && !xmemcmp(s, "OggS\x00\x02", 6)) { + } else if (len>36 && !smemcmp(s, "OggS\x00\x02", 6)) { xprintf("Ogg data"); // https://wiki.xiph.org/MIMETypesCodecs - if (!xmemcmp(s+28, "CELT ", 8)) xprintf(", celt audio"); - else if (!xmemcmp(s+28, "CMML ", 8)) xprintf(", cmml text"); - else if (!xmemcmp(s+28, "BBCD\0", 5)) xprintf(", dirac video"); - else if (!xmemcmp(s+28, "\177FLAC", 5)) xprintf(", flac audio"); - else if (!xmemcmp(s+28, "\x8bJNG\r\n\x1a\n", 8)) xprintf(", jng video"); - else if (!xmemcmp(s+28, "\x80kate\0\0\0", 8)) xprintf(", kate text"); - else if (!xmemcmp(s+28, "OggMIDI\0", 8)) xprintf(", midi text"); - else if (!xmemcmp(s+28, "\x8aMNG\r\n\x1a\n", 8)) xprintf(", mng video"); - else if (!xmemcmp(s+28, "OpusHead", 8)) xprintf(", opus audio"); - else if (!xmemcmp(s+28, "PCM ", 8)) xprintf(", pcm audio"); - else if (!xmemcmp(s+28, "\x89PNG\r\n\x1a\n", 8)) xprintf(", png video"); - else if (!xmemcmp(s+28, "Speex ", 8)) xprintf(", speex audio"); - else if (!xmemcmp(s+28, "\x80theora", 7)) xprintf(", theora video"); - else if (!xmemcmp(s+28, "\x01vorbis", 7)) xprintf(", vorbis audio"); - else if (!xmemcmp(s+28, "YUV4MPEG", 8)) xprintf(", yuv4mpeg video"); + if (!smemcmp(s+28, "CELT ", 8)) xprintf(", celt audio"); + else if (!smemcmp(s+28, "CMML ", 8)) xprintf(", cmml text"); + else if (!smemcmp(s+28, "BBCD", 5)) xprintf(", dirac video"); + else if (!smemcmp(s+28, "\177FLAC", 5)) xprintf(", flac audio"); + else if (!smemcmp(s+28, "\x8bJNG\r\n\x1a\n", 8)) xprintf(", jng video"); + else if (!smemcmp(s+28, "\x80kate\0\0", 8)) xprintf(", kate text"); + else if (!smemcmp(s+28, "OggMIDI", 8)) xprintf(", midi text"); + else if (!smemcmp(s+28, "\x8aMNG\r\n\x1a\n", 8)) xprintf(", mng video"); + else if (!smemcmp(s+28, "OpusHead", 8)) xprintf(", opus audio"); + else if (!smemcmp(s+28, "PCM ", 8)) xprintf(", pcm audio"); + else if (!smemcmp(s+28, "\x89PNG\r\n\x1a\n", 8)) xprintf(", png video"); + else if (!smemcmp(s+28, "Speex ", 8)) xprintf(", speex audio"); + else if (!smemcmp(s+28, "\x80theora", 7)) xprintf(", theora video"); + else if (!smemcmp(s+28, "\x01vorbis", 7)) xprintf(", vorbis audio"); + else if (!smemcmp(s+28, "YUV4MPEG", 8)) xprintf(", yuv4mpeg video"); xputc('\n'); - } else if (len>32 && !xmemcmp(s, "RIF", 3) && !xmemcmp(s+8, "WAVEfmt ", 8)) { + } else if (len>32 && !smemcmp(s, "RIF", 3) && !smemcmp(s+8, "WAVEfmt ", 8)) { // https://en.wikipedia.org/wiki/WAV int le = (s[3] == 'F'); int format = le ? peek_le(s+20, 2) : peek_be(s+20, 2); @@ -372,7 +372,7 @@ static void do_regular_file(int fd, char *name) else xprintf("unknown format %d", format); xputc('\n'); } else if (len>12 && peek_be(s, 4)==0x10000) xputs("TrueType font"); - else if (len>12 && !xmemcmp(s, "ttcf\x00", 5)) { + else if (len>12 && !smemcmp(s, "ttcf", 5)) { xprintf("TrueType font collection, version %d, %d fonts\n", (int)peek_be(s+4, 2), (int)peek_be(s+8, 4)); @@ -382,14 +382,14 @@ static void do_regular_file(int fd, char *name) else if (strstart(&s,"-----BEGIN CERTIFICATE-----")) xputs("PEM certificate"); // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx - else if (len>0x70 && !xmemcmp(s, "MZ", 2) && - (magic=peek_le(s+0x3c,4))<len-4 && !xmemcmp(s+magic, "\x50\x45\0", 4)) { + else if (len>0x70 && !smemcmp(s, "MZ", 2) && + (magic=peek_le(s+0x3c,4))<len-4 && !smemcmp(s+magic, "\x50\x45\0", 4)) { // Linux kernel images look like PE files. // https://www.kernel.org/doc/Documentation/arm64/booting.txt // I've only ever seen LE, 4KiB pages, so ignore flags for now. - if (!xmemcmp(s+0x38, "ARMd", 4)) return xputs("Linux arm64 kernel image"); - else if (!xmemcmp(s+0x202, "HdrS", 4)) { + if (!smemcmp(s+0x38, "ARMd", 4)) return xputs("Linux arm64 kernel image"); + else if (!smemcmp(s+0x202, "HdrS", 4)) { // https://www.kernel.org/doc/Documentation/x86/boot.txt unsigned ver_off = peek_le(s+0x20e, 2); @@ -418,7 +418,7 @@ static void do_regular_file(int fd, char *name) xprintf("x86%s\n", (peek_le(s+magic+4, 2)==0x14c) ? "" : "-64"); // https://en.wikipedia.org/wiki/BMP_file_format - } else if (len>0x32 && !xmemcmp(s, "BM", 2) && !peek_be(s+6, 4)) { + } else if (len>0x32 && !smemcmp(s, "BM", 2) && !peek_be(s+6, 4)) { xprintf("BMP image, %d x %d, %d bpp\n", (int)peek_le(s+18, 4), (int)peek_le(s+22,4), (int)peek_le(s+28, 2)); @@ -432,7 +432,7 @@ static void do_regular_file(int fd, char *name) (int)peek_le(s+12, 4), (int)peek_le(s+20, 4)); // https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/master/include/bootimg/bootimg.h - } else if (len>1632 && !xmemcmp(s, "ANDROID!", 8)) { + } else if (len>1632 && !smemcmp(s, "ANDROID!", 8)) { xprintf("Android boot image v%d\n", (int)peek_le(s+40, 4)); // https://source.android.com/devices/architecture/dto/partitions @@ -441,7 +441,7 @@ static void do_regular_file(int fd, char *name) (int)peek_be(s+16, 4)); // frameworks/base/core/java/com/android/internal/util/BinaryXmlSerializer.java - } else if (len>4 && !xmemcmp(s, "ABX", 3)) { + } else if (len>4 && !smemcmp(s, "ABX", 3)) { xprintf("Android Binary XML v%d\n", s[3]); // Text files, including shell scripts. diff --git a/toys/posix/od.c b/toys/posix/od.c index a37186cb..c12055e6 100644 --- a/toys/posix/od.c +++ b/toys/posix/od.c @@ -133,7 +133,7 @@ static void od_outline(void) // Handle duplciate lines as * if (!FLAG(v) && TT.j != TT.pos && TT.leftover - && !xmemcmp(TT.bufs[0], TT.bufs[1], TT.w)) + && !smemcmp(TT.bufs[0], TT.bufs[1], TT.w)) { if (!TT.star) { xputs("*"); diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 9033cdbd..1fdfc17c 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -845,7 +845,7 @@ static int get_ps(struct dirtree *new) off_t temp = 6; sprintf(buf, "%lld/exe", slot[SLOT_tid]); - if (readfileat(fd, buf, buf, &temp) && !xmemcmp(buf, "\177ELF", 4)) { + if (readfileat(fd, buf, buf, &temp) && !smemcmp(buf, "\177ELF", 4)) { if (buf[4] == 1) slot[SLOT_bits] = 32; else if (buf[4] == 2) slot[SLOT_bits] = 64; } diff --git a/toys/posix/tar.c b/toys/posix/tar.c index aef1397b..8a8601d1 100644 --- a/toys/posix/tar.c +++ b/toys/posix/tar.c @@ -990,7 +990,7 @@ void tar_main(void) if (len!=512 || !is_tar_header(hdr)) { // detect gzip and bzip signatures if (SWAP_BE16(*(short *)hdr)==0x1f8b) toys.optflags |= FLAG_z; - else if (!xmemcmp(hdr, "BZh", 3)) toys.optflags |= FLAG_j; + else if (!smemcmp(hdr, "BZh", 3)) toys.optflags |= FLAG_j; else if (peek_be(hdr, 7) == 0xfd377a585a0000UL) toys.optflags |= FLAG_J; else error_exit("Not tar"); |