Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
F

file

> 编程语言
Open source

Read-only mirror of file CVS repository, updated every half hour. NOTE: do not make pull requests here, nor comment any commits, submit them usual way to bug tr

1.6K stars0 likes1 views
WebsiteGitHub

About

Read-only mirror of file CVS repository, updated every half hour. NOTE: do not make pull requests here, nor comment any commits, submit them usual way to bug tr

README for file(1) Command and the libmagic(3) library

@(#) $File: README.md,v 1.8 2026/08/28 15:26:23 christos Exp $
  • Bug Tracker: https://bugs.astron.com/
  • Build Status: https://travis-ci.org/file/file
  • Download link: ftp://ftp.astron.com/pub/file/
  • E-mail: [email protected]
  • Fuzzing link: https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:file
  • Home page: https://www.darwinsys.com/file/
  • Mailing List archives: https://mailman.astron.com/pipermail/file/
  • Mailing List: [email protected]
  • Public repo: https://github.com/file/file
  • Test framework: https://github.com/file/file-tests

Phone: Do not even think of telephoning me about this program. Send cash first!

This is Release 5.x of Ian Darwin's (copyright but distributable) file(1) command, an implementation of the Unix File(1) command. It knows the 'magic number' of several thousands of file types. This version is the standard "file" command for Linux, *BSD, and other systems. (See "patchlevel.h" for the exact release number).

The major changes for 5.x are CDF file parsing, indirect magic, name/use (recursion) and overhaul in mime and ascii encoding handling.

The major feature of 4.x is the refactoring of the code into a library, and the re-write of the file command in terms of that library. The library itself, libmagic can be used by 3rd party programs that wish to identify file types without having to fork() and exec() file. The prime contributor for 4.0 was Mans Rullgard.

UNIX is a trademark of UNIX System Laboratories.

The prime contributor to Release 3.8 was Guy Harris, who put in megachanges including byte-order independence.

The prime contributor to Release 3.0 was Christos Zoulas, who put in hundreds of lines of source code changes, including his own ANSIfication of the code (I liked my own ANSIfication better, but his (__P()) is the "Berkeley standard" way of doing it, and I wanted UCB to include the code...), his HP-like "indirection" (a feature of the HP file command, I think), and his mods that finally got the uncompress (-z) mode finished and working.

This release has compiled in numerous environments; see PORTING for a list and problems.

This fine freeware file(1) follows the USG (System V) model of the file command, rather than the Research (V7) version or the V7-derived 4.[23] Berkeley one. That is, the file /etc/magic contains much of the ritual information that is the source of this program's power. My version knows a little more magic (including tar archives) than System V; the /etc/magic parsing seems to be compatible with the (poorly documented) System V /etc/magic format (with one exception; see the man page).

In addition, the /etc/magic file is built from a subdirectory for easier(?) maintenance. I will act as a clearinghouse for magic numbers assigned to all sorts of data files that are in reasonable circulation. Send your magic numbers, in magic(5) format please, to the maintainer, Christos Zoulas.

COPYING - read this first.

  • ChangeLog - log of important changes
  • README.md - read this second (you are currently reading this file).
  • INSTALL - read on how to install
  • src/apprentice.c - parses /etc/magic to learn magic
  • src/apptype.c - used for OS/2 specific application type magic
  • src/ascmagic.c - third & last set of tests, based on hardwired assumptions.
  • src/asctime_r.c - replacement for OS's that don't have it.
  • src/asprintf.c - replacement for OS's that don't have it.
  • src/buffer.c - buffer handling functions.
  • src/cdf.[ch] - parser for Microsoft Compound Document Files
  • src/cdf_time.c - time converter for CDF.
  • src/compress.c - handles decompressing files to look inside.
  • src/ctime_r.c - replacement for OS's that don't have it.
  • src/der.[ch] - parser for Distinguished Encoding Rules
  • src/dprintf.c - replacement for OS's that don't have it.
  • src/elfclass.h - common code for elf 32/64.
  • src/encoding.c - handles unicode encodings
  • src/file.c - the main program
  • src/file.h - header file
  • src/file_opts.h - list of options
  • src/fmtcheck.c - replacement for OS's that don't have it.
  • src/fsmagic.c - first set of tests the program runs, based on filesystem info
  • src/funcs.c - utilility functions
  • src/getline.c - replacement for OS's that don't have it.
  • src/getopt_long.c - replacement for OS's that don't have it.
  • src/gmtime_r.c - replacement for OS's that don't have it.
  • src/is_csv.c - knows about Comma Separated Value file format (RFC 4180).
  • src/is_json.c - knows about JavaScript Object Notation format (RFC 8259).
  • src/is_simh.c - knows about SIMH tape file format.
  • src/is_tar.c, tar.h - knows about Tape ARchive format (courtesy John Gilmore).
  • src/landlock.c - linux landlock protection
  • src/localtime_r.c - replacement for OS's that don't have it.
  • src/magic.h.in - source file for magic.h
  • src/mygetopt.h - replacement for OS's that don't have it.
  • src/magic.c - the libmagic api
  • src/names.h - header file for ascmagic.c
  • src/pread.c - replacement for OS's that don't have it.
  • src/print.c - print results, errors, warnings.
  • src/readcdf.c - CDF wrapper.
  • src/readelf.[ch] - Stand-alone elf parsing code.
  • src/softmagic.c - 2nd set of tests, based on /etc/magic
  • src/swap.h - byte swapping
  • src/swap.c - byte swapping
  • src/mygetopt.h - replacement for OS's that don't have it.
  • src/seccomp.c - linux seccomp protection
  • src/strcasestr.c - replacement for OS's that don't have it.
  • src/strlcat.c - replacement for OS's that don't have it.
  • src/strlcpy.c - replacement for OS's that don't have it.
  • src/strndup.c - replacement for OS's that don't have it.
  • src/tar.h - tar file definitions
  • src/vasprintf.c - for systems that don't have it.
  • doc/file.man - man page for the command
  • doc/magic.man - man page for the magic file, courtesy Guy Harris. Install as magic.4 on USG and magic.5 on V7 or Berkeley; cf Makefile.

Magdir - directory of /etc/magic pieces

If you submit a new magic entry please make sure you read the following guidelines:

  • Initial match is preferably at least 32 bits long, and is a unique match

  • If this is not feasible, use additional check

  • Match of <= 16 bits are not accepted

  • Delay printing string as much as possible, don't print output too early

  • Avoid printf arbitrary byte as string, which can be a source of crash and buffer overflow

  • Provide complete information with entry:

    • One line short summary
    • Optional long description
    • File extension, if applicable
    • Full name and contact method (for discussion when entry has problem)
    • Further reference, such as documentation of format

gpg for dummies:

$ gpg --verify file-X.YY.tar.gz.asc file-X.YY.tar.gz
gpg: assuming signed data in `file-X.YY.tar.gz'
gpg: Signature made WWW MMM DD HH:MM:SS YYYY ZZZ using DSA key ID KKKKKKKK

To download the key:

$ gpg --keyserver hkp://keys.gnupg.net --recv-keys KKKKKKKK

Parts of this software were developed at SoftQuad Inc., developers of SGML/HTML/XML publishing software, in Toronto, Canada. SoftQuad was swallowed up by Corel in 2002 and does not exist any longer.

Building for Amiga systems

libmagic and file cross-compile for AmigaOS 3 (m68k), AmigaOS 4 (PPC) and MorphOS (PPC) using the sacredbanana/amiga-compiler toolchain images:

system image tag host triplet
AmigaOS 3 sacredbanana/amiga-compiler:m68k-amigaos m68k-unknown-amigaos
AmigaOS 4 sacredbanana/amiga-compiler:ppc-amigaos ppc-amigaos
MorphOS sacredbanana/amiga-compiler:ppc-morphos ppc-morphos

Scripts

Three scripts at the repository root drive this; run autoreconf -i once first.

  • amiga-variants.sh -- the shared table of every variant that is built, one entry per tree|slot|configure args, plus the configure flags common to all of them. Both scripts below source it, so they can never disagree.
  • build-amiga-sdk.sh [OS ...] -- builds every variant and installs libmagic.a plus magic.h into an SDK tree. The location comes from $AMIGA_SDK (default /opt/amiga); either a <target>/ or an AmigaSDK-gcc-style amigaos3/sdk/<target>/ layout is accepted, and an OS whose subdirectory is absent is skipped rather than failing.
  • package-amiga.sh [OS ...] -- builds every variant plus one file binary per OS and produces the two distribution archives in dist/.

Both default to all three systems. The archives are laid out as:

magic.readme
magic/C/magic.mgc                       shared -- it is not machine code
magic/AmigaOS3/{libnix,clib2}/{include,lib/...}
magic/AmigaOS4/{include,lib/clib4}
magic/MorphOS/{include,lib,lib/libb32,lib/libnix,lib/libb32/libnix}

file.readme
file/{AmigaOS3,AmigaOS4,MorphOS}/C/{file,magic.mgc}

package-amiga.sh needs a native magic.mgc to ship, so build the host copy into build-native/ first; it is refused otherwise.

Note that m68k and PPC lay their C runtimes out differently, and the variant table follows each. On m68k a runtime is a separate tree (libnix/, clib2/), each with its own include/ and lib/. On PPC a runtime is a multilib slot beneath a single shared lib/ -- -mcrt=clib4 searches <target>/lib/clib4/ and takes headers from <target>/include/. This was read off ppc-amigaos-gcc -mcrt=clib4 -print-search-dirs rather than assumed, because installing to the wrong one produces a library the compiler silently never finds.

Continuous integration

.github/workflows/amiga-packages.yml runs the whole packaging path on every push to master (and on demand via Run workflow), so the commit gets a tick when both archives build and verify and a cross when anything fails. It uploads magic.lha and file.lha as run artifacts, named magic-lha and file-lha; GitHub always wraps an artifact in a zip, so the download is a zip containing the untouched .lha.

The runner builds LHa for UNIX (jca02266/lha, pinned to a release tag) from source, because Debian and Ubuntu only ship lhasa, which extracts .lha but cannot create one. After packaging, the workflow lists both archives and checks the counts inside them -- one libmagic.a per variant, one magic.h per per-OS tree, one file and one magic.mgc per OS, one copy of each readme -- with the expected numbers derived from amiga-variants.sh so the check cannot go stale when a slot is added. That matters because a build failure now makes package-amiga.sh exit non-zero, but a missing copy would otherwise leave a perfectly valid, quietly incomplete archive.

Building by hand

bash
docker run --rm -v ${PWD}:/work -e USER=$( id -u ) -e GROUP=$( id -g ) \
    -it sacredbanana/amiga-compiler:m68k-amigaos bash
mkdir build-amiga && cd build-amiga
../configure --host=m68k-unknown-amigaos --disable-shared --enable-static \
    --disable-zlib --disable-bzlib --disable-xzlib --disable-zstdlib \
    --disable-lzlib --disable-lrziplib --disable-lz4lib \
    --disable-libseccomp --disable-landlock CFLAGS="-O2"
make -C src magic.h && make -C src libmagic.la

libmagic.a is left in build-amiga/src/.libs/, and the public header magic.h is generated into build-amiga/src/.

ABI options

These change the ABI, so they are applied to the compiler driver before any of configure's feature tests run -- which matters because the C runtime choice selects a different set of target headers, and a probe run with the wrong ones produces a wrong config.h. configure classifies the target from --host and only honours the options for that system.

AmigaOS 3 (m68k)

  • `--with-m68k-crt=

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言