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