diff options
author | Rob Landley <rob@landley.net> | 2016-03-06 13:10:19 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2016-03-06 13:10:19 -0600 |
commit | 2e4aff2fd31fb9d949d6c47da1e46b97ecc23d24 (patch) | |
tree | ef35aa2eb4e4e159090ddb203e185c87922e89f5 | |
parent | b3e9c9281d188d6b82c94f300eb7237368e6a8d8 (diff) | |
download | toybox-2e4aff2fd31fb9d949d6c47da1e46b97ecc23d24.tar.gz |
Another pass at file.c: move posix-required "executable" to front of output,
next few fields in the order the other implementation outputs, fewer commas,
fix a big where big endian executable type wasn't detected right,
and fix the filehandle leak.
-rw-r--r-- | toys/pending/file.c | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/toys/pending/file.c b/toys/pending/file.c index fbff5867..18941227 100644 --- a/toys/pending/file.c +++ b/toys/pending/file.c @@ -46,39 +46,40 @@ static void do_elf_file(int fd) {0xabc7, "xtensa-old"} }; - xprintf("ELF "); + printf("ELF "); + + // executable (ELF says this is short but reality says byte, not MSB swapped) + i = toybuf[16]; + if (i == 1) printf("relocatable"); + else if (i == 2) printf("executable"); + else if (i == 3) printf("shared object"); + else if (i == 4) printf("core dump"); + else printf("(bad type %d)", i); + printf(", "); // "64-bit" - if (bits == 1) xprintf("32-bit "); - else if (bits == 2) xprintf("64-bit "); + if (bits == 1) printf("32-bit "); + else if (bits == 2) printf("64-bit "); else { - xprintf("(bad class %d) ", bits); + printf("(bad class %d) ", bits); bits = 0; } - // e_machine, ala "x86", from big table above - j = elf_int(toybuf+18, 2); - for (i = 0; i<ARRAY_LEN(type); i++) if (j==type[i].val) break; - if (i<ARRAY_LEN(type)) xprintf("%s ", type[i].name); - else xprintf("(unknown arch %d) ", j); - // "LSB" - if (endian == 1) xprintf("LSB "); + if (endian == 1) printf("LSB "); else if (endian == 2) { - xprintf("MSB "); + printf("MSB "); elf_int = peek_be; } else { - xprintf("(bad endian %d)\n", endian); + printf("(bad endian %d) \n", endian); endian = 0; } - // ", executable" - i = elf_int(toybuf+16, 2); - if (i == 1) xprintf("relocatable"); - else if (i == 2) xprintf("executable"); - else if (i == 3) xprintf("shared object"); - else if (i == 4) xprintf("core dump"); - else xprintf("(bad type %d)", i); + // e_machine, ala "x86", from big table above + j = elf_int(toybuf+18, 2); + for (i = 0; i<ARRAY_LEN(type); i++) if (j==type[i].val) break; + if (i<ARRAY_LEN(type)) printf("%s", type[i].name); + else printf("(unknown arch %d)", j); bits--; // If we know our bits and endianness and phentsize agrees show dynamic linker @@ -94,7 +95,7 @@ static void do_elf_file(int fd) // map e_phentsize*e_phnum bytes at e_phoff map = mmap(0, phsize*phnum, PROT_READ, MAP_SHARED, fd, mapoff); if (map) { - // Find PT_INTERP entry. (Not: fields got reordered for 64 bit) + // Find PT_INTERP entry. (Note: fields got reordered for 64 bit) for (i = 0; i<phnum; i++) { long long dlpos, dllen; @@ -110,10 +111,10 @@ static void do_elf_file(int fd) if (dllen<0 || dllen>sizeof(toybuf)-128 || dlpos!=lseek(fd, dlpos, SEEK_SET) || dllen!=readall(fd, toybuf+128, dllen)) break; - printf(", dynamic (%.*s)", (int)dllen, toybuf+128); + printf(", dynamic (%.*s", (int)dllen, toybuf+128); } if (!lib) printf(", static"); - else printf(", needs %d lib%s", lib, lib>1 ? "s" : ""); + else printf(" loads %d lib%s)", lib, lib>1 ? "s" : ""); munmap(map, phsize*phnum); } } @@ -250,13 +251,11 @@ void file_main(void) int fd = !strcmp(name, "-") ? 0 : open(name, O_RDONLY); if (fd!=-1) { - if (sb.st_size == 0) what = "empty"; - else { - do_regular_file(fd, name); - continue; - } + if (!sb.st_size) what = "empty"; + else do_regular_file(fd, name); + if (fd) close(fd); + if (sb.st_size) continue; } - if (fd>0) close(fd); } else if (S_ISBLK(sb.st_mode)) what = "block special"; else if (S_ISCHR(sb.st_mode)) what = "character special"; else if (S_ISDIR(sb.st_mode)) what = "directory"; |