1MMagic(3) User Contributed Perl Documentation MMagic(3)
2
3
4
6 File::MMagic - Guess file type
7
9 use File::MMagic;
10 use FileHandle;
11
12 $mm = new File::MMagic; # use internal magic file
13 # $mm = File::MMagic->new('/etc/magic'); # use external magic file
14 # $mm = File::MMagic->new('/usr/share/etc/magic'); # if you use Debian
15 $res = $mm->checktype_filename("/somewhere/unknown/file");
16
17 $fh = new FileHandle "< /somewhere/unknown/file2";
18 $res = $mm->checktype_filehandle($fh);
19
20 $fh->read($data, 0x8564);
21 $res = $mm->checktype_contents($data);
22
24 This perl library uses perl5 objects to guess file type from filename
25 and/or filehandle.
26
28 checktype_filename(), checktype_filehandle() and checktype_contents
29 returns string contains file type with MIME mediatype format.
30
32 File::MMagic->new()
33 File::MMagic->new( $filename )
34 Initializes the module. If no filename is given, the magic numbers
35 stored in File::MMagic are used.
36
37 $mm->addSpecials
38 If a filetype cannot be determined by magic numbers, extra checks
39 are done based on extra regular expressions which can be defined
40 here. The first argument should be the filetype, the remaining
41 arguments should be one or more regular expressions.
42
43 By default, checks are done for message/news, message/rfc822,
44 text/html, text/x-roff.
45
46 $mm->removeSpecials
47 Removes special regular expressions. Specify one or more filetypes.
48 If no filetypes are specified, all special regexps are removed.
49
50 Returns a hash containing the removed entries.
51
52 $mm->addFileExts
53 If a filetype cannot be determined by magic numbers, extra checks
54 can be done based on the file extension (actually, a regexp). Two
55 arguments should be geiven: the filename pattern and the
56 corresponding filetype.
57
58 By default, checks are done for application/x-compress,
59 application/x-bzip2, application/x-gzip, text/html, text/plain
60
61 $mm->removeFileExts
62 Remove filename pattern checks. Specify one or more patterns. If no
63 pattern is specified, all are removed.
64
65 Returns a hash containing the removed entries.
66
67 $mm->addMagicEntry
68 Add a new magic entry in the object. The format is same as magic(5)
69 file.
70
71 Ex.
72 # Add a entry
73 $mm->addMagicEntry("0\tstring\tabc\ttext/abc");
74 # Add a entry with a sub entry
75 $mm->addMagicEntry("0\tstring\tdef\t");
76 $mm->addMagicEntry(">10\tstring\tghi\ttext/ghi");
77
78 $mm->readMagicHandle
79 $mm->checktype_filename
80 $mm->checktype_magic
81 $mm->checktype_contents
82
84 This program is originated from file.kulp that is a production of The
85 Unix Reconstruction Projct.
86 <http://language.perl.com/ppt/index.html> Copyright (c) 1999 NOKUBI
87 Takatsugu <knok@daionet.gr.jp>.
88
89 There is no warranty for the program.
90
91 This product includes software developed by the Apache Group for use in
92 the Apache HTTP server project (http://www.apache.org/).
93
94 License for the program is followed the original software. The license
95 is below.
96
97 This program is free and open software. You may use, copy, modify,
98 distribute and sell this program (and any modified variants) in any way
99 you wish, provided you do not restrict others to do the same, except
100 for the following consideration.
101
102 I read some of Ian F. Darwin's BSD C implementation, to try to
103 determine how some of this was done since the specification is a little
104 vague. I don't believe that this perl version could be construed as an
105 "altered version", but I did grab the tokens for identifying the hard-
106 coded file types in names.h and copied some of the man page.
107
108 Here's his notice:
109
110 * Copyright (c) Ian F. Darwin, 1987.
111 * Written by Ian F. Darwin.
112 *
113 * This software is not subject to any license of the American Telephone
114 * and Telegraph Company or of the Regents of the University of California.
115 *
116 * Permission is granted to anyone to use this software for any purpose on
117 * any computer system, and to alter it and redistribute it freely, subject
118 * to the following restrictions:
119 *
120 * 1. The author is not responsible for the consequences of use of this
121 * software, no matter how awful, even if they arise from flaws in it.
122 *
123 * 2. The origin of this software must not be misrepresented, either by
124 * explicit claim or by omission. Since few users ever read sources,
125 * credits must appear in the documentation.
126 *
127 * 3. Altered versions must be plainly marked as such, and must not be
128 * misrepresented as being the original software. Since few users
129 * ever read sources, credits must appear in the documentation.
130 *
131 * 4. This notice may not be removed or altered.
132
133 The following is the Apache License. This program contains the magic
134 file that derived from the Apache HTTP Server.
135
136 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
137 *
138 * Redistribution and use in source and binary forms, with or without
139 * modification, are permitted provided that the following conditions
140 * are met:
141 *
142 * 1. Redistributions of source code must retain the above copyright
143 * notice, this list of conditions and the following disclaimer.
144 *
145 * 2. Redistributions in binary form must reproduce the above copyright
146 * notice, this list of conditions and the following disclaimer in
147 * the documentation and/or other materials provided with the
148 * distribution.
149 *
150 * 3. All advertising materials mentioning features or use of this
151 * software must display the following acknowledgment:
152 * "This product includes software developed by the Apache Group
153 * for use in the Apache HTTP server project (http://www.apache.org/)."
154 *
155 * 4. The names "Apache Server" and "Apache Group" must not be used to
156 * endorse or promote products derived from this software without
157 * prior written permission. For written permission, please contact
158 * apache@apache.org.
159 *
160 * 5. Products derived from this software may not be called "Apache"
161 * nor may "Apache" appear in their names without prior written
162 * permission of the Apache Group.
163 *
164 * 6. Redistributions of any form whatsoever must retain the following
165 * acknowledgment:
166 * "This product includes software developed by the Apache Group
167 * for use in the Apache HTTP server project (http://www.apache.org/)."
168 *
169 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
170 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
172 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
173 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
174 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
175 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
176 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
177 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
178 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
179 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
180 * OF THE POSSIBILITY OF SUCH DAMAGE.
181
182
183
184perl v5.32.0 2020-07-28 MMagic(3)