Debian Codegraph @

main.c

raw symbols SCIP
1 /*
2 * main.c
3 * Main entry point and global utility functions.
4 *
5 * Copyright (c) 2003 Christoph Pfisterer
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "global.h"
29
30 #ifdef USE_MACOS_TYPE
31 #include <CoreServices/CoreServices.h>
32 #endif
33
34 /*
35 * local functions
36 */
37
38 static void analyze_file(const char *filename);
39 static void print_kind(int filekind, u8 size, int size_known);
40
41 #ifdef USE_MACOS_TYPE
42 static void show_macos_type(const char *filename);
43 #endif
44
45 /*
46 * entry point
47 */
48
49 int main(int argc, char *argv[])
50 {
51 int i;
52
53 /* argument check */
54 if (argc < 2) {
55 fprintf(stderr, "Usage: %s <device/file>...\n", PROGNAME);
56 return 1;
57 }
58
59 /* loop over filenames */
60 print_line(0, "");
61 for (i = 1; i < argc; i++) {
62 analyze_file(argv[i]);
63 print_line(0, "");
64 }
65
66 return 0;
67 }
68
69 /*
70 * Analyze one file
71 */
72
73 static void analyze_file(const char *filename)
74 {
75 int fd, filekind;
76 u8 filesize;
77 struct stat sb;
78 char *reason;
79 SOURCE *s;
80
81 print_line(0, "--- %s", filename);
82
83 /* stat check */
84 if (stat(filename, &sb) < 0) {
85 errore("Can't stat %.300s", filename);
86 return;
87 }
88
89 filekind = 0;
90 filesize = 0;
91 reason = NULL;
92 if (S_ISREG(sb.st_mode)) {
93 filesize = sb.st_size;
94 print_kind(filekind, filesize, 1);
95 } else if (S_ISBLK(sb.st_mode))
96 filekind = 1;
97 else if (S_ISCHR(sb.st_mode))
98 filekind = 2;
99 else if (S_ISDIR(sb.st_mode))
100 reason = "Is a directory";
101 else if (S_ISFIFO(sb.st_mode))
102 reason = "Is a FIFO";
103 #ifdef S_ISSOCK
104 else if (S_ISSOCK(sb.st_mode))
105 reason = "Is a socket";
106 #endif
107 else
108 reason = "Is an unknown kind of special file";
109
110 if (reason != NULL) {
111 error("%.300s: %s", filename, reason);
112 return;
113 }
114
115 /* Mac OS type & creator code (if running on Mac OS X) */
116 #ifdef USE_MACOS_TYPE
117 if (filekind == 0)
118 show_macos_type(filename);
119 #endif
120
121 /* empty regular files need no further analysis */
122 if (filekind == 0 && filesize == 0)
123 return;
124
125 /* open for reading */
126 fd = open(filename, O_RDONLY);
127 if (fd < 0) {
128 errore("Can't open %.300s", filename);
129 return;
130 }
131
132 /* (try to) guard against TTY character devices */
133 if (filekind == 2) {
134 if (isatty(fd)) {
135 error("%.300s: Is a TTY device", filename);
136 return;
137 }
138 }
139
140 /* create a source */
141 s = init_file_source(fd, filekind);
142
143 /* tell the user what it is */
144 if (filekind != 0)
145 print_kind(filekind, s->size, s->size_known);
146
147 /* now analyze it */
148 analyze_source(s, 0);
149
150 /* finish it up */
151 close_source(s);
152 }
153
154 static void print_kind(int filekind, u8 size, int size_known)
155 {
156 char buf[256], *kindname;
157
158 if (filekind == 0)
159 kindname = "Regular file";
160 else if (filekind == 1)
161 kindname = "Block device";
162 else if (filekind == 2)
163 kindname = "Character device";
164 else
165 kindname = "Unknown kind";
166
167 if (size_known) {
168 format_size_verbose(buf, size);
169 print_line(0, "%s, size %s", kindname, buf);
170 } else {
171 print_line(0, "%s, unknown size", kindname);
172 }
173 }
174
175 /*
176 * Mac OS type & creator code
177 */
178
179 #ifdef USE_MACOS_TYPE
180
181 static void show_macos_type(const char *filename)
182 {
183 int err;
184 FSRef ref;
185 FSCatalogInfo info;
186 FInfo *finfo;
187
188 err = FSPathMakeRef(filename, &ref, NULL);
189 if (err == 0) {
190 err = FSGetCatalogInfo(&ref, kFSCatInfoFinderInfo,
191 &info, NULL, NULL, NULL);
192 }
193
194 if (err == 0) {
195 finfo = (FInfo *)(info.finderInfo);
196 if (finfo->fdType != 0 || finfo->fdCreator != 0) {
197 char typecode[5], creatorcode[5], s1[256], s2[256];
198
199 memcpy(typecode, &finfo->fdType, 4);
200 typecode[4] = 0;
201 format_ascii(typecode, s1);
202
203 memcpy(creatorcode, &finfo->fdCreator, 4);
204 creatorcode[4] = 0;
205 format_ascii(creatorcode, s2);
206
207 print_line(0, "Type code \"%s\", creator code \"%s\"",
208 s1, s2);
209 } else {
210 print_line(0, "No type and creator code");
211 }
212 }
213 if (err) {
214 print_line(0, "Type and creator code unknown (error %d)", err);
215 }
216 }
217
218 #endif
219
220 /* EOF */