1 /*
2 * file - find type of a file or files - main program.
3 *
4 * Copyright (c) Ian F. Darwin, 1987.
5 * Written by Ian F. Darwin.
6 *
7 * This software is not subject to any license of the American Telephone
8 * and Telegraph Company or of the Regents of the University of California.
9 *
10 * Permission is granted to anyone to use this software for any purpose on
11 * any computer system, and to alter it and redistribute it freely, subject
12 * to the following restrictions:
13 *
14 * 1. The author is not responsible for the consequences of use of this
15 * software, no matter how awful, even if they arise from flaws in it.
16 *
17 * 2. The origin of this software must not be misrepresented, either by
18 * explicit claim or by omission. Since few users ever read sources,
19 * credits must appear in the documentation.
20 *
21 * 3. Altered versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software. Since few users
23 * ever read sources, credits must appear in the documentation.
24 *
25 * 4. This notice may not be removed or altered.
26 */
27 #if 0
28 static char *moduleid =
29 "@(#)$Id: s.file.c 1.7 03/05/09 01:42:48-07:00 orc@downbelow.pell.portland.or.us $";
30 #endif /* lint */
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <sys/param.h> /* for MAXPATHLEN */
36 #include <stdlib.h>
37
38 extern int getopt();
39 extern int optind;
40 extern char *optarg;
41
42 #ifndef __P
43 # if __STDC__ || __cplusplus
44 # define __P(a) a
45 # else
46 # define __P(a) ()
47 # define const
48 # endif
49 #endif
50
51 #include "magic.h"
52
53 #ifdef S_IFLNK
54 # define USAGE "Usage: %s [-czL] [-f namefile] [-m magicfile] file...\n"
55 #else
56 # define USAGE "Usage: %s [-cz] [-f namefile] [-m magicfile] file...\n"
57 #endif
58
59 #ifndef MAGIC
60 # define MAGIC "/etc/magic"
61 #endif
62
63 #ifndef MAXPATHLEN
64 #define MAXPATHLEN 512
65 #endif
66
67 static void unwrap __P((char *fn));
68
69 static magic_t poof = 0;
70
71 /*
72 * main - parse arguments and handle options
73 */
74 int
main(argc,argv)75 main(argc, argv)
76 int argc;
77 char *argv[];
78 {
79 int c;
80 int didsomefiles = 0, errflg = 0, ret = 0;
81 int flags = 0;
82 char *magicfile = 0;
83
84 if ((progname = strrchr(argv[0], '/')) != NULL)
85 progname++;
86 else
87 progname = argv[0];
88
89 while ((c = getopt(argc, argv, "cEdf:Lm:z")) != EOF)
90 switch (c) {
91 case 'E':
92 magic_emit_struct(1);
93 /* fall into 'c' */
94 case 'c':
95 flags |= MAGIC_CHECK;
96 break;
97 case 'd':
98 flags |= MAGIC_DEBUG;
99 break;
100 case 'f':
101 unwrap(optarg);
102 ++didsomefiles;
103 break;
104 #ifdef S_IFLNK
105 case 'L':
106 flags |= MAGIC_SYMLINK;
107 break;
108 #endif
109 case 'm':
110 magicfile = optarg;
111 break;
112 case 'z':
113 flags |= MAGIC_COMPRESS;
114 break;
115 case '?':
116 default:
117 errflg++;
118 break;
119 }
120 if (errflg) {
121 (void) fprintf(stderr, USAGE, progname);
122 exit(2);
123 }
124
125
126 if ( (poof = magic_open(flags)) == 0)
127 exit(1);
128 ret = magic_load(poof, magicfile);
129 if (flags & MAGIC_CHECK)
130 exit(ret);
131
132 if (optind == argc && !didsomefiles) {
133 fprintf(stdout, "%s\n", magic_file(poof, 0));
134 }
135 else {
136 int i, wid, nw;
137 for (wid = 0, i = optind; i < argc; i++) {
138 nw = strlen(argv[i]);
139 if (nw > wid)
140 wid = nw;
141 }
142 for (; optind < argc; optind++)
143 fprintf(stdout, "%.*s: %s\n", wid, argv[optind], magic_file(poof, argv[optind]));
144 }
145
146 return 0;
147 }
148
149
150 /*
151 * unwrap -- read a file of filenames, do each one.
152 */
153 static void
unwrap(fn)154 unwrap(fn)
155 char *fn;
156 {
157 char buf[MAXPATHLEN];
158 FILE *f;
159 int wid = 0, cwid;
160
161 if ((f = fopen(fn, "r")) == NULL) {
162 fprintf(stderr, "Cannot open `%s' (%s).\n", fn, strerror(errno));
163 exit(1);
164 /*NOTREACHED*/
165 }
166
167 while (fgets(buf, MAXPATHLEN, f) != NULL) {
168 cwid = strlen(buf) - 1;
169 if (cwid > wid)
170 wid = cwid;
171 }
172
173 rewind(f);
174
175 while (fgets(buf, MAXPATHLEN, f) != NULL) {
176 buf[strlen(buf)-1] = '\0';
177 fprintf(stdout, "%.*s: %s\n", wid, buf, magic_file(poof,buf));
178 }
179
180 (void) fclose(f);
181 }
182