определить тип файла (determine file type)
Обоснование (Rationale)
The -f
option was omitted because the same effect can (and
should) be obtained using the xargs utility.
Historical versions of the file utility attempt to identify the
following types of files: symbolic link, directory, character
special, block special, socket, tar archive, cpio archive, SCCS
archive, archive library, empty, compress output, pack output,
binary data, C source, FORTRAN source, assembler source,
nroff/troff/eqn/tbl source troff output, shell script, C shell
script, English text, ASCII text, various executables, APL
workspace, compiled terminfo entries, and CURSES screen images.
Only those types that are reasonably well specified in POSIX or
are directly related to POSIX utilities are listed in the table.
Historical systems have used a ``magic file'' named /etc/magic
to
help identify file types. Because it is generally useful for
users and scripts to be able to identify special file types, the
-m
flag and a portable format for user-created magic files has
been specified. No requirement is made that an implementation of
file use this method of identifying files, only that users be
permitted to add their own classifying tests.
In addition, three options have been added to historical
practice. The -d
flag has been added to permit users to cause
their tests to follow any default system tests. The -i
flag has
been added to permit users to test portably for regular files in
shell scripts. The -M
flag has been added to permit users to
ignore any default system tests.
The POSIX.1‐2008 description of default system tests and the
interaction between the -d
, -M
, and -m
options did not clearly
indicate that there were two types of ``default system tests''.
The ``position-sensitive tests'' determine file types by looking
for certain string or binary values at specific offsets in the
file being examined. These position-sensitive tests were
implemented in historical systems using the magic file described
above. Some of these tests are now built into the file utility
itself on some implementations so the output can provide more
detail than can be provided by magic files. For example, a magic
file can easily identify a core
file on most implementations, but
cannot name the program file that dropped the core. A magic file
could produce output such as:
/home/dwc/core: ELF 32-bit MSB core file SPARC Version 1
but by building the test into the file utility, you could get
output such as:
/home/dwc/core: ELF 32-bit MSB core file SPARC Version 1, from 'testprog'
These extended built-in tests are still to be treated as
position-sensitive default system tests even if they are not
listed in /etc/magic
or any other magic file.
The context-sensitive default system tests were always built into
the file utility. These tests looked for language constructs in
text files trying to identify shell scripts, C, FORTRAN, and
other computer language source files, and even plain text files.
With the addition of the -m
and -M
options the distinction
between position-sensitive and context-sensitive default system
tests became important because the order of testing is important.
The context-sensitive system default tests should never be
applied before any position-sensitive tests even if the -d
option
is specified before a -m
option or -M
option due to the high
probability that the context-sensitive system default tests will
incorrectly identify arbitrary text files as text files before
position-sensitive tests specified by the -m
or -M
option would
be applied to give a more accurate identification.
Leaving the meaning of -M -
and -m -
unspecified allows an
existing prototype of these options to continue to work in a
backwards-compatible manner. (In that implementation, -M -
was
roughly equivalent to -d
in POSIX.1‐2008.)
The historical -c
option was omitted as not particularly useful
to users or portable shell scripts. In addition, a reasonable
implementation of the file utility would report any errors found
each time the magic file is read.
The historical format of the magic file was the same as that
specified by the Rationale in the ISO POSIX‐2:1993 standard for
the offset, value, and message fields; however, it used less
precise type fields than the format specified by the current
normative text. The new type field values are a superset of the
historical ones.
The following is an example magic file:
0 short 070707 cpio archive
0 short 0143561 Byte-swapped cpio archive
0 string 070707 ASCII cpio archive
0 long 0177555 Very old archive
0 short 0177545 Old archive
0 short 017437 Old packed data
0 string \037\036 Packed data
0 string \377\037 Compacted data
0 string \037\235 Compressed data
>2 byte&0x80 >0 Block compressed
>2 byte&0x1f x %d bits
0 string \032\001 Compiled Terminfo Entry
0 short 0433 Curses screen image
0 short 0434 Curses screen image
0 string <ar> System V Release 1 archive
0 string !<arch>\n__.SYMDEF Archive random library
0 string !<arch> Archive
0 string ARF_BEGARF PHIGS clear text archive
0 long 0x137A2950 Scalable OpenFont binary
0 long 0x137A2951 Encrypted scalable OpenFont binary
The use of a basic integer data type is intended to allow the
implementation to choose a word size commonly used by
applications on that architecture.
Earlier versions of this standard allowed for implementations
with bytes other than eight bits, but this has been modified in
this version.