From 2e4aff2fd31fb9d949d6c47da1e46b97ecc23d24 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 6 Mar 2016 13:10:19 -0600 Subject: 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. --- toys/pending/file.c | 57 ++++++++++++++++++++++++++--------------------------- 1 file 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; isizeof(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"; -- cgit v1.2.3