file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* file.c - describe file type
  2. *
  3. * Copyright 2016 The Android Open Source Project
  4. *
  5. * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/file.html
  6. USE_FILE(NEWTOY(file, "<1bhLs[!hL]", TOYFLAG_USR|TOYFLAG_BIN))
  7. config FILE
  8. bool "file"
  9. default y
  10. help
  11. usage: file [-bhLs] [FILE...]
  12. Examine the given files and describe their content types.
  13. -b Brief (no filename)
  14. -h Don't follow symlinks (default)
  15. -L Follow symlinks
  16. -s Show block/char device contents
  17. */
  18. #define FOR_file
  19. #include "toys.h"
  20. GLOBALS(
  21. int max_name_len;
  22. off_t len;
  23. )
  24. // We don't trust elf.h to be there, and two codepaths for 32/64 is awkward
  25. // anyway, so calculate struct offsets manually. (It's a fixed ABI.)
  26. static void do_elf_file(int fd)
  27. {
  28. unsigned endian = toybuf[5], bits = toybuf[4]-1, i, j, dynamic = 0,
  29. stripped = 1, phentsize, phnum, shsize, shnum, bail = 0;
  30. int64_t (*elf_int)(void *ptr, unsigned size) = (endian==2)?peek_be:peek_le;
  31. char *map = MAP_FAILED;
  32. unsigned long phoff, shoff;
  33. printf("ELF ");
  34. // executable type
  35. i = elf_int(toybuf+16, 2);
  36. if (i == 1) printf("relocatable");
  37. else if (i == 2) printf("executable");
  38. else if (i == 3) printf("shared object");
  39. else if (i == 4) printf("core dump");
  40. else {
  41. printf("(bad type %d)", i);
  42. bail++;
  43. }
  44. if (elf_int(toybuf+36+12*!!bits, 4) & 0x8000) printf(" (fdpic)");
  45. printf(", ");
  46. // "64-bit"
  47. if (bits&~1) {
  48. printf("(bad class %d) ", bits);
  49. bail++;
  50. } else printf("%d-bit ", 32<<bits);
  51. // "LSB"
  52. if (endian == 1) printf("LSB ");
  53. else if (endian == 2) printf("MSB ");
  54. else {
  55. printf("(bad endian %d) ", endian);
  56. bail++;
  57. }
  58. // "x86".
  59. printf("%s", elf_arch_name(elf_int(toybuf+18, 2)));
  60. // If what we've seen so far doesn't seem consistent, bail.
  61. if (bail) goto bad;
  62. // Stash what we need from the header; it's okay to reuse toybuf after this.
  63. phentsize = elf_int(toybuf+42+12*bits, 2);
  64. phnum = elf_int(toybuf+44+12*bits, 2);
  65. phoff = elf_int(toybuf+28+4*bits, 4+4*bits);
  66. shsize = elf_int(toybuf+46+12*bits, 2);
  67. shnum = elf_int(toybuf+48+12*bits, 2);
  68. shoff = elf_int(toybuf+32+8*bits, 4+4*bits);
  69. // With binutils, phentsize seems to only be non-zero if phnum is non-zero.
  70. // Such ELF files are rare, but do exist. (Android's crtbegin files, say.)
  71. if (phnum && (phentsize != 32+24*bits)) {
  72. printf(", bad phentsize %d?", phentsize);
  73. goto bad;
  74. }
  75. if (phoff>TT.len || phnum*phentsize>TT.len-phoff) {
  76. printf(", bad phoff %lu?", phoff);
  77. goto bad;
  78. }
  79. if (shoff>TT.len || shnum*shsize>TT.len-shoff) {
  80. printf(", bad shoff %lu?", phoff);
  81. goto bad;
  82. }
  83. // Parsing ELF means following tables that may point to data earlier in
  84. // the file, so sequential reading involves buffering unknown amounts of
  85. // data. Just skip it if we can't mmap.
  86. if (MAP_FAILED == (map = mmap(0, TT.len, PROT_READ, MAP_SHARED, fd, 0))) {
  87. perror_msg("mmap");
  88. goto bad;
  89. }
  90. // Read the phdrs for dynamic vs static. (Note: fields reordered on 64 bit)
  91. for (i = 0; i<phnum; i++) {
  92. char *phdr = map+phoff+i*phentsize;
  93. unsigned p_type = elf_int(phdr, 4);
  94. unsigned long long p_offset, p_filesz;
  95. // TODO: what does PT_DYNAMIC without PT_INTERP mean?
  96. if (p_type-2>2) continue; // 2 = PT_DYNAMIC, 3 = PT_INTERP, 4 = PT_NOTE
  97. dynamic |= p_type==2;
  98. p_offset = elf_int(phdr+(4<<bits), 4<<bits);
  99. p_filesz = elf_int(phdr+(16<<bits), 4<<bits);
  100. if (p_type==3) {
  101. if (p_filesz>TT.len || p_offset>TT.len-p_filesz) {
  102. printf(", bad phdr %d?", i);
  103. goto bad;
  104. }
  105. // TODO: if (int)<0 prints endlessly, could go off end of map?
  106. printf(", dynamic (%.*s)", (int)p_filesz, map+p_offset);
  107. }
  108. }
  109. if (!dynamic) printf(", static");
  110. // We need to read the shdrs for stripped/unstripped and any notes.
  111. // Notes are in program headers *and* section headers, but some files don't
  112. // contain program headers, so check here. (Note: fields reordered on 64 bit)
  113. for (i = 0; i<shnum; i++) {
  114. char *shdr = map+shoff+i*shsize;
  115. unsigned long sh_offset;
  116. int sh_type, sh_size;
  117. if (shdr>map+TT.len-(8+(4<<bits))) {
  118. printf(", bad shdr %d?", i);
  119. goto bad;
  120. }
  121. sh_type = elf_int(shdr+4, 4);
  122. sh_offset = elf_int(shdr+8+(8<<bits), 4<<bits);
  123. sh_size = elf_int(shdr+8+(12<<bits), 4);
  124. if (sh_type == 8 /*SHT_NOBITS*/) sh_size = 0;
  125. if (sh_offset>TT.len || sh_size>TT.len-sh_offset) {
  126. printf(", bad shdr %d?", i);
  127. goto bad;
  128. }
  129. if (sh_type == 2 /*SHT_SYMTAB*/) {
  130. stripped = 0;
  131. break;
  132. } else if (sh_type == 7 /*SHT_NOTE*/) {
  133. char *note = map+sh_offset;
  134. // An ELF note is a sequence of entries, each consisting of an
  135. // ndhr followed by n_namesz+n_descsz bytes of data (each of those
  136. // rounded up to the next 4 bytes, without this being reflected in
  137. // the header byte counts themselves).
  138. while (sh_size >= 3*4) { // Don't try to read a truncated entry.
  139. unsigned n_namesz, n_descsz, n_type, notesz;
  140. if (note>map+TT.len-3*4) {
  141. printf(", bad note %d?", i);
  142. goto bad;
  143. }
  144. n_namesz = elf_int(note, 4);
  145. n_descsz = elf_int(note+4, 4);
  146. n_type = elf_int(note+8, 4);
  147. notesz = 3*4 + ((n_namesz+3)&~3) + ((n_descsz+3)&~3);
  148. // Are the name/desc sizes consistent, and does the claimed size of
  149. // the note actually fit in the section?
  150. if (notesz<n_namesz || notesz<n_descsz || notesz>sh_size) {
  151. printf(", bad note %d size?", i);
  152. goto bad;
  153. }
  154. if (n_namesz==4 && !memcmp(note+12, "GNU", 4) && n_type==3) {
  155. printf(", BuildID=");
  156. for (j = 0; j<n_descsz; j++) printf("%02x", note[16+j]);
  157. } else if (n_namesz==8 && !memcmp(note+12, "Android", 8)) {
  158. if (n_type==1 /*.android.note.ident*/ && n_descsz >= 4) {
  159. printf(", for Android %d", (int)elf_int(note+20, 4));
  160. // NDK r14 and later also include NDK version info. OS binaries
  161. // and binaries built by older NDKs don't have this.
  162. if (n_descsz >= 4+64+64)
  163. printf(", built by NDK %.64s (%.64s)", note+24, note+24+64);
  164. }
  165. }
  166. note += notesz;
  167. sh_size -= notesz;
  168. }
  169. }
  170. }
  171. printf(", %sstripped", stripped ? "" : "not ");
  172. bad:
  173. xputc('\n');
  174. if (map != MAP_FAILED) munmap(map, TT.len);
  175. }
  176. static void do_regular_file(int fd, char *name)
  177. {
  178. char *s = toybuf;
  179. unsigned len, magic;
  180. // zero through elf shnum, just in case
  181. memset(s, 0, 80);
  182. if ((len = readall(fd, s, sizeof(toybuf)-8))<0) perror_msg("%s", name);
  183. if (!len) xputs("empty");
  184. // 45 bytes: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
  185. else if (len>=45 && strstart(&s, "\177ELF")) do_elf_file(fd);
  186. else if (strstart(&s, "!<arch>\n")) xputs("ar archive");
  187. else if (len>28 && strstart(&s, "\x89PNG\x0d\x0a\x1a\x0a")) {
  188. // PNG is big-endian: https://www.w3.org/TR/PNG/#7Integers-and-byte-order
  189. int chunk_length = peek_be(s, 4);
  190. xprintf("PNG image data");
  191. // The IHDR chunk comes first: https://www.w3.org/TR/PNG/#11IHDR
  192. s += 4;
  193. if (chunk_length == 13 && strstart(&s, "IHDR")) {
  194. // https://www.w3.org/TR/PNG/#6Colour-values
  195. char *c = 0, *colors[] = {"grayscale", 0, "color RGB", "indexed color",
  196. "grayscale with alpha", 0, "color RGBA"};
  197. if (s[9]<ARRAY_LEN(colors)) c = colors[s[9]];
  198. xprintf(", %d x %d, %d-bit/%s, %sinterlaced", (int)peek_be(s, 4),
  199. (int)peek_be(s+4, 4), s[8], c ? : "unknown", s[12] ? "" : "non-");
  200. }
  201. xputc('\n');
  202. // https://www.w3.org/Graphics/GIF/spec-gif89a.txt
  203. } else if (len>16 && (strstart(&s, "GIF87a") || strstart(&s, "GIF89a")))
  204. xprintf("GIF image data, version %3.3s, %d x %d\n",
  205. s-3, (int)peek_le(s, 2), (int)peek_le(s+2, 2));
  206. // TODO: parsing JPEG for width/height is harder than GIF or PNG.
  207. else if (len>32 && !memcmp(s, "\xff\xd8", 2)) xputs("JPEG image data");
  208. else if (len>8 && strstart(&s, "\xca\xfe\xba\xbe")) {
  209. unsigned count = peek_be(s, 4), i, arch;
  210. // 0xcafebabe can be a Java class file or a Mach-O universal binary.
  211. // Java major version numbers start with 0x2d for JDK 1.1, and realistically
  212. // you're never going to see more than 2 architectures in a binary anyway...
  213. if (count<0x2d && len>=(count*20)) {
  214. // https://eclecticlight.co/2020/07/28/universal-binaries-inside-fat-headers/
  215. xprintf("Mach-O universal binary with %u architecture%s:",
  216. count, count == 1 ? "" : "s");
  217. for (i = 0, s += 4; i < count; i++, s += 20) {
  218. arch = peek_be(s, 4);
  219. if (arch == 0x00000007) name = "i386";
  220. else if (arch == 0x01000007) name = "x86_64";
  221. else if (arch == 0x0000000c) name = "arm";
  222. else if (arch == 0x0100000c) name = "arm64";
  223. else name = "unknown";
  224. xprintf(" [%s]", name);
  225. }
  226. xprintf("\n");
  227. } else {
  228. // https://en.wikipedia.org/wiki/Java_class_file#General_layout
  229. xprintf("Java class file, version %d.%d (Java 1.%d)\n",
  230. (int)peek_be(s+2, 2), (int)peek_be(s, 2), (int)peek_be(s+2, 2)-44);
  231. }
  232. // https://source.android.com/devices/tech/dalvik/dex-format#dex-file-magic
  233. } else if (len>8 && strstart(&s, "dex\n") && !s[3])
  234. xprintf("Android dex file, version %s\n", s);
  235. // https://people.freebsd.org/~kientzle/libarchive/man/cpio.5.txt
  236. // the lengths for cpio are size of header + 9 bytes, since any valid
  237. // cpio archive ends with a record for "TARGET!!!"
  238. else if (len>6 && strstart(&s, "07070")) {
  239. char *cpioformat = "unknown type";
  240. if (*s == '7') cpioformat = "pre-SVR4 or odc";
  241. else if (*s == '1') cpioformat = "SVR4 with no CRC";
  242. else if (*s == '2') cpioformat = "SVR4 with CRC";
  243. xprintf("ASCII cpio archive (%s)\n", cpioformat);
  244. } else if (len>33 && ((magic=peek(&s,2))==0143561 || magic==070707)) {
  245. if (magic == 0143561) printf("byte-swapped ");
  246. xputs("cpio archive");
  247. // tar archive (old, ustar/pax, or gnu)
  248. } else if (len>500 && is_tar_header(s))
  249. xprintf("%s tar archive%s\n", s[257] ? "POSIX" : "old",
  250. (s[262]!=' ' || s[263]!=' ')?"":" (GNU)");
  251. // zip/jar/apk archive, ODF/OOXML document, or such
  252. else if (len>5 && strstart(&s, "PK\03\04")) {
  253. xprintf("Zip archive data");
  254. if (*s) xprintf(", requires at least v%d.%d to extract", *s/10, *s%10);
  255. xputc('\n');
  256. } else if (len>9 && strstart(&s, "7z\xbc\xaf\x27\x1c")) {
  257. xprintf("7-zip archive data");
  258. if (*s || s[1]) xprintf(", version %d.%d", *s, s[1]);
  259. xputc('\n');
  260. } else if (len>4 && strstart(&s, "BZh") && isdigit(*s))
  261. xprintf("bzip2 compressed data, block size = %c00k\n", *s);
  262. else if (len>31 && peek_be(s, 7) == 0xfd377a585a0000UL)
  263. xputs("xz compressed data");
  264. else if (len>10 && strstart(&s, "\x1f\x8b")) xputs("gzip compressed data");
  265. else if (len>32 && !memcmp(s+1, "\xfa\xed\xfe", 3)) {
  266. int bit = (*s==0xce) ? 32 : 64;
  267. char *what = 0;
  268. xprintf("Mach-O %d-bit ", bit);
  269. if (s[4] == 7) what = (bit==32)?"x86":"x86-";
  270. else if (s[4] == 12) what = "arm";
  271. else if (s[4] == 18) what = "ppc";
  272. if (what) xprintf("%s%s ", what, (bit==32)?"":"64");
  273. else xprintf("(bad arch %d) ", s[4]);
  274. if (s[12] == 1) what = "object";
  275. else if (s[12] == 2) what = "executable";
  276. else if (s[12] == 6) what = "shared library";
  277. else what = NULL;
  278. if (what) xprintf("%s\n", what);
  279. else xprintf("(bad type %d)\n", s[9]);
  280. } else if (len>36 && !memcmp(s, "OggS\x00\x02", 6)) {
  281. xprintf("Ogg data");
  282. // https://wiki.xiph.org/MIMETypesCodecs
  283. if (!memcmp(s+28, "CELT ", 8)) xprintf(", celt audio");
  284. else if (!memcmp(s+28, "CMML ", 8)) xprintf(", cmml text");
  285. else if (!memcmp(s+28, "BBCD\0", 5)) xprintf(", dirac video");
  286. else if (!memcmp(s+28, "\177FLAC", 5)) xprintf(", flac audio");
  287. else if (!memcmp(s+28, "\x8bJNG\r\n\x1a\n", 8)) xprintf(", jng video");
  288. else if (!memcmp(s+28, "\x80kate\0\0\0", 8)) xprintf(", kate text");
  289. else if (!memcmp(s+28, "OggMIDI\0", 8)) xprintf(", midi text");
  290. else if (!memcmp(s+28, "\x8aMNG\r\n\x1a\n", 8)) xprintf(", mng video");
  291. else if (!memcmp(s+28, "OpusHead", 8)) xprintf(", opus audio");
  292. else if (!memcmp(s+28, "PCM ", 8)) xprintf(", pcm audio");
  293. else if (!memcmp(s+28, "\x89PNG\r\n\x1a\n", 8)) xprintf(", png video");
  294. else if (!memcmp(s+28, "Speex ", 8)) xprintf(", speex audio");
  295. else if (!memcmp(s+28, "\x80theora", 7)) xprintf(", theora video");
  296. else if (!memcmp(s+28, "\x01vorbis", 7)) xprintf(", vorbis audio");
  297. else if (!memcmp(s+28, "YUV4MPEG", 8)) xprintf(", yuv4mpeg video");
  298. xputc('\n');
  299. } else if (len>32 && !memcmp(s, "RIF", 3) && !memcmp(s+8, "WAVEfmt ", 8)) {
  300. // https://en.wikipedia.org/wiki/WAV
  301. int le = (s[3] == 'F');
  302. int format = le ? peek_le(s+20, 2) : peek_be(s+20, 2);
  303. int channels = le ? peek_le(s+22, 2) : peek_be(s+22, 2);
  304. int hz = le ? peek_le(s+24, 4) : peek_be(s+24, 4);
  305. int bits = le ? peek_le(s+34, 2) : peek_be(s+34, 2);
  306. xprintf("WAV audio, %s, ", le ? "LE" : "BE");
  307. if (bits) xprintf("%d-bit, ", bits);
  308. if (channels==1||channels==2) xprintf("%s, ",(channels==1)?"mono":"stereo");
  309. else xprintf("%d-channel, ", channels);
  310. xprintf("%d Hz, ", hz);
  311. // See https://tools.ietf.org/html/rfc2361, though there appear to be bugs
  312. // in the RFC. This assumes wikipedia's example files are more correct.
  313. if (format == 0x01) xprintf("PCM");
  314. else if (format == 0x03) xprintf("IEEE float");
  315. else if (format == 0x06) xprintf("A-law");
  316. else if (format == 0x07) xprintf("µ-law");
  317. else if (format == 0x11) xprintf("ADPCM");
  318. else if (format == 0x22) xprintf("Truespeech");
  319. else if (format == 0x31) xprintf("GSM");
  320. else if (format == 0x55) xprintf("MP3");
  321. else if (format == 0x70) xprintf("CELP");
  322. else if (format == 0xfffe) xprintf("extensible");
  323. else xprintf("unknown format %d", format);
  324. xputc('\n');
  325. } else if (len>12 && peek_be(s, 4)==0x10000) xputs("TrueType font");
  326. else if (len>12 && !memcmp(s, "ttcf\x00", 5)) {
  327. xprintf("TrueType font collection, version %d, %d fonts\n",
  328. (int)peek_be(s+4, 2), (int)peek_be(s+8, 4));
  329. // https://docs.microsoft.com/en-us/typography/opentype/spec/otff
  330. } else if (len>12 && strstart(&s, "OTTO")) xputs("OpenType font");
  331. else if (strstart(&s, "BC\xc0\xde")) xputs("LLVM IR bitcode");
  332. else if (strstart(&s,"-----BEGIN CERTIFICATE-----")) xputs("PEM certificate");
  333. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx
  334. else if (len>0x70 && !memcmp(s, "MZ", 2) &&
  335. (magic=peek_le(s+0x3c,4))<len-4 && !memcmp(s+magic, "\x50\x45\0", 4)) {
  336. xprintf("MS PE32%s executable %s", (peek_le(s+magic+24, 2)==0x20b)?"+":"",
  337. (peek_le(s+magic+22, 2)&0x2000)?"(DLL) ":"");
  338. if (peek_le(s+magic+20, 2)>70) {
  339. char *types[] = {0, "native", "GUI", "console", "OS/2", "driver", "CE",
  340. "EFI", "EFI boot", "EFI runtime", "EFI ROM", "XBOX", 0, "boot"}, *nn;
  341. unsigned type = peek_le(s+magic+92, 2);
  342. nn = (type<ARRAY_LEN(types)) ? types[type] : 0;
  343. xprintf("(%s) ", nn ? : "unknown");
  344. }
  345. xprintf("x86%s\n", (peek_le(s+magic+4, 2)==0x14c) ? "" : "-64");
  346. // https://en.wikipedia.org/wiki/BMP_file_format
  347. } else if (len>0x32 && !memcmp(s, "BM", 2) && !peek_be(s+6, 4)) {
  348. xprintf("BMP image, %d x %d, %d bpp\n", (int)peek_le(s+18, 4),
  349. (int)peek_le(s+22,4), (int)peek_le(s+28, 2));
  350. // https://github.com/torvalds/linux/blob/master/tools/perf/Documentation/perf.data-file-format.txt
  351. } else if (len>=104 && strstart(&s, "PERFILE2")) xputs("Linux perf data");
  352. // https://android.googlesource.com/platform/system/core/+/master/libsparse/sparse_format.h
  353. else if (len>28 && peek_le(s, 4) == 0xed26ff3a) {
  354. xprintf("Android sparse image v%d.%d, %d %d-byte blocks (%d chunks)\n",
  355. (int)peek_le(s+4, 2), (int)peek_le(s+6, 2), (int)peek_le(s+16, 4),
  356. (int)peek_le(s+12, 4), (int)peek_le(s+20, 4));
  357. // https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/master/include/bootimg/bootimg.h
  358. } else if (len>1632 && !memcmp(s, "ANDROID!", 8)) {
  359. xprintf("Android boot image v%d\n", (int)peek_le(s+40, 4));
  360. // https://source.android.com/devices/architecture/dto/partitions
  361. } else if (len>32 && peek_be(s, 4) == 0xd7b7ab1e) {
  362. xprintf("Android DTB/DTBO v%d, %d entries\n", (int)peek_be(s+28, 4),
  363. (int)peek_be(s+16, 4));
  364. // frameworks/base/core/java/com/android/internal/util/BinaryXmlSerializer.java
  365. } else if (len>4 && !memcmp(s, "ABX", 3)) {
  366. xprintf("Android Binary XML v%d\n", s[3]);
  367. // Text files, including shell scripts.
  368. } else {
  369. char *what = 0;
  370. int i, bytes;
  371. // If shell script, report which interpreter
  372. if (len>3 && strstart(&s, "#!")) {
  373. // Whitespace is allowed between the #! and the interpreter
  374. while (isspace(*s)) s++;
  375. if (strstart(&s, "/usr/bin/env")) while (isspace(*s)) s++;
  376. for (what = s; *s && !isspace(*s); s++);
  377. strcpy(s, " script");
  378. // Distinguish ASCII text, UTF-8 text, or data
  379. } else for (i = 0; i<len; ++i) {
  380. if (!(isprint(s[i]) || isspace(s[i]))) {
  381. unsigned wc;
  382. if ((bytes = utf8towc(&wc, s+i, len-i))>0 && wcwidth(wc)>=0) {
  383. i += bytes-1;
  384. if (!what) what = "UTF-8 text";
  385. } else {
  386. what = "data";
  387. break;
  388. }
  389. }
  390. }
  391. xputs(what ? what : "ASCII text");
  392. }
  393. }
  394. void file_main(void)
  395. {
  396. char **arg;
  397. for (arg = toys.optargs; *arg; ++arg)
  398. TT.max_name_len = maxof(strlen(*arg), TT.max_name_len);
  399. // Can't use loopfiles here because it doesn't call function when can't open
  400. for (arg = toys.optargs; *arg; arg++) {
  401. char *name = *arg, *what = "unknown";
  402. struct stat sb;
  403. int fd = !strcmp(name, "-");
  404. if (!FLAG(b))
  405. xprintf("%s: %*s", name, (int)(TT.max_name_len - strlen(name)), "");
  406. sb.st_size = 0;
  407. if (!fd && (FLAG(L) ? stat : lstat)(name, &sb)) {
  408. xprintf("cannot open: %s\n", strerror(errno));
  409. continue;
  410. }
  411. if (!fd && !FLAG(s) && (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode))) {
  412. sprintf(what = toybuf, "%s special (%u/%u)",
  413. S_ISBLK(sb.st_mode) ? "block" : "character",
  414. dev_major(sb.st_rdev), dev_minor(sb.st_rdev));
  415. } else if (fd || S_ISREG(sb.st_mode)) {
  416. TT.len = sb.st_size;
  417. // This test identifies an empty file we don't have permission to read
  418. if (!fd && !sb.st_size) what = "empty";
  419. else if ((fd = openro(name, O_RDONLY)) != -1) {
  420. do_regular_file(fd, name);
  421. if (fd) close(fd);
  422. continue;
  423. }
  424. } else if (S_ISFIFO(sb.st_mode)) what = "fifo";
  425. else if (S_ISDIR(sb.st_mode)) what = "directory";
  426. else if (S_ISSOCK(sb.st_mode)) what = "socket";
  427. else if (S_ISLNK(sb.st_mode)) {
  428. char *lnk = xreadlink(name);
  429. sprintf(what = toybuf, "%ssymbolic link to %s",
  430. stat(name, &sb) ? "broken " : "", lnk);
  431. free(lnk);
  432. }
  433. xputs(what);
  434. }
  435. }