Deleted Added
full compact
magic.c (159764) magic.c (169942)
1/*
2 * Copyright (c) Christos Zoulas 2003.
3 * All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 24 unchanged lines hidden (view full) ---

33#include <unistd.h>
34#include <string.h>
35#include <sys/types.h>
36#include <sys/param.h> /* for MAXPATHLEN */
37#include <sys/stat.h>
38#ifdef QUICK
39#include <sys/mman.h>
40#endif
1/*
2 * Copyright (c) Christos Zoulas 2003.
3 * All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 24 unchanged lines hidden (view full) ---

33#include <unistd.h>
34#include <string.h>
35#include <sys/types.h>
36#include <sys/param.h> /* for MAXPATHLEN */
37#include <sys/stat.h>
38#ifdef QUICK
39#include <sys/mman.h>
40#endif
41#include <limits.h> /* for PIPE_BUF */
41
42#if defined(HAVE_UTIMES)
43# include <sys/time.h>
44#elif defined(HAVE_UTIME)
45# if defined(HAVE_SYS_UTIME_H)
46# include <sys/utime.h>
47# elif defined(HAVE_UTIME_H)
48# include <utime.h>

--- 8 unchanged lines hidden (view full) ---

57#include <locale.h>
58#endif
59
60#include <netinet/in.h> /* for byte swapping */
61
62#include "patchlevel.h"
63
64#ifndef lint
42
43#if defined(HAVE_UTIMES)
44# include <sys/time.h>
45#elif defined(HAVE_UTIME)
46# if defined(HAVE_SYS_UTIME_H)
47# include <sys/utime.h>
48# elif defined(HAVE_UTIME_H)
49# include <utime.h>

--- 8 unchanged lines hidden (view full) ---

58#include <locale.h>
59#endif
60
61#include <netinet/in.h> /* for byte swapping */
62
63#include "patchlevel.h"
64
65#ifndef lint
65FILE_RCSID("@(#)$Id: magic.c,v 1.32 2005/10/17 15:31:10 christos Exp $")
66FILE_RCSID("@(#)$Id: magic.c,v 1.35 2006/10/31 19:37:17 christos Exp $")
66#endif /* lint */
67
68#ifdef __EMX__
69private char *apptypeName = NULL;
70protected int file_os2_apptype(struct magic_set *ms, const char *fn,
71 const void *buf, size_t nb);
72#endif /* __EMX__ */
73
74private void free_mlist(struct mlist *);
75private void close_and_restore(const struct magic_set *, const char *, int,
76 const struct stat *);
67#endif /* lint */
68
69#ifdef __EMX__
70private char *apptypeName = NULL;
71protected int file_os2_apptype(struct magic_set *ms, const char *fn,
72 const void *buf, size_t nb);
73#endif /* __EMX__ */
74
75private void free_mlist(struct mlist *);
76private void close_and_restore(const struct magic_set *, const char *, int,
77 const struct stat *);
78private int info_from_stat(struct magic_set *, mode_t);
77
78#ifndef STDIN_FILENO
79#define STDIN_FILENO 0
80#endif
81
82public struct magic_set *
83magic_open(int flags)
84{

--- 46 unchanged lines hidden (view full) ---

131 struct magic *mg = ml->magic;
132 file_delmagic(mg, ml->mapped, ml->nmagic);
133 free(ml);
134 ml = next;
135 }
136 free(ml);
137}
138
79
80#ifndef STDIN_FILENO
81#define STDIN_FILENO 0
82#endif
83
84public struct magic_set *
85magic_open(int flags)
86{

--- 46 unchanged lines hidden (view full) ---

133 struct magic *mg = ml->magic;
134 file_delmagic(mg, ml->mapped, ml->nmagic);
135 free(ml);
136 ml = next;
137 }
138 free(ml);
139}
140
141private int
142info_from_stat(struct magic_set *ms, mode_t md)
143{
144 /* We cannot open it, but we were able to stat it. */
145 if (md & 0222)
146 if (file_printf(ms, "writable, ") == -1)
147 return -1;
148 if (md & 0111)
149 if (file_printf(ms, "executable, ") == -1)
150 return -1;
151 if (S_ISREG(md))
152 if (file_printf(ms, "regular file, ") == -1)
153 return -1;
154 if (file_printf(ms, "no read permission") == -1)
155 return -1;
156 return 0;
157}
158
139public void
159public void
140magic_close(ms)
141 struct magic_set *ms;
160magic_close(struct magic_set *ms)
142{
143 free_mlist(ms->mlist);
144 free(ms->o.pbuf);
145 free(ms->o.buf);
146 free(ms->c.off);
147 free(ms);
148}
149

--- 66 unchanged lines hidden (view full) ---

216public const char *
217magic_file(struct magic_set *ms, const char *inname)
218{
219 int fd = 0;
220 int rv = -1;
221 unsigned char *buf;
222 struct stat sb;
223 ssize_t nbytes = 0; /* number of bytes read from a datafile */
161{
162 free_mlist(ms->mlist);
163 free(ms->o.pbuf);
164 free(ms->o.buf);
165 free(ms->c.off);
166 free(ms);
167}
168

--- 66 unchanged lines hidden (view full) ---

235public const char *
236magic_file(struct magic_set *ms, const char *inname)
237{
238 int fd = 0;
239 int rv = -1;
240 unsigned char *buf;
241 struct stat sb;
242 ssize_t nbytes = 0; /* number of bytes read from a datafile */
243 int ispipe = 0;
224
225 /*
226 * one extra for terminating '\0', and
227 * some overlapping space for matches near EOF
228 */
229#define SLOP (1 + sizeof(union VALUETYPE))
230 if ((buf = malloc(HOWMANY + SLOP)) == NULL)
231 return NULL;
232
233 if (file_reset(ms) == -1)
234 goto done;
235
236 switch (file_fsmagic(ms, inname, &sb)) {
244
245 /*
246 * one extra for terminating '\0', and
247 * some overlapping space for matches near EOF
248 */
249#define SLOP (1 + sizeof(union VALUETYPE))
250 if ((buf = malloc(HOWMANY + SLOP)) == NULL)
251 return NULL;
252
253 if (file_reset(ms) == -1)
254 goto done;
255
256 switch (file_fsmagic(ms, inname, &sb)) {
237 case -1:
257 case -1: /* error */
238 goto done;
258 goto done;
239 case 0:
259 case 0: /* nothing found */
240 break;
260 break;
241 default:
261 default: /* matched it and printed type */
242 rv = 0;
243 goto done;
244 }
245
262 rv = 0;
263 goto done;
264 }
265
246 if (inname == NULL)
266 if (inname == NULL) {
247 fd = STDIN_FILENO;
267 fd = STDIN_FILENO;
248 else if ((fd = open(inname, O_RDONLY|O_BINARY)) < 0) {
268 if (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode))
269 ispipe = 1;
270 } else {
271 int flags = O_RDONLY|O_BINARY;
272
273 if (stat(inname, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
274 flags |= O_NONBLOCK;
275 ispipe = 1;
276 }
277
278 errno = 0;
279 if ((fd = open(inname, flags)) < 0) {
249#ifdef __CYGWIN__
280#ifdef __CYGWIN__
250 char *tmp = alloca(strlen(inname) + 5);
251 (void)strcat(strcpy(tmp, inname), ".exe");
252 if ((fd = open(tmp, O_RDONLY|O_BINARY)) < 0) {
281 char *tmp = alloca(strlen(inname) + 5);
282 (void)strcat(strcpy(tmp, inname), ".exe");
283 if ((fd = open(tmp, flags)) < 0) {
253#endif
284#endif
254 /* We cannot open it, but we were able to stat it. */
255 if (sb.st_mode & 0222)
256 if (file_printf(ms, "writable, ") == -1)
257 goto done;
258 if (sb.st_mode & 0111)
259 if (file_printf(ms, "executable, ") == -1)
260 goto done;
261 if (S_ISREG(sb.st_mode))
262 if (file_printf(ms, "regular file, ") == -1)
263 goto done;
264 if (file_printf(ms, "no read permission") == -1)
285 if (info_from_stat(ms, sb.st_mode) == -1)
286 goto done;
287 rv = 0;
265 goto done;
288 goto done;
266 rv = 0;
267 goto done;
268#ifdef __CYGWIN__
289#ifdef __CYGWIN__
269 }
290 }
270#endif
291#endif
292 }
293#ifdef O_NONBLOCK
294 if ((flags = fcntl(fd, F_GETFL)) != -1) {
295 flags &= ~O_NONBLOCK;
296 (void)fcntl(fd, F_SETFL, flags);
297 }
298#endif
271 }
272
273 /*
274 * try looking at the first HOWMANY bytes
275 */
299 }
300
301 /*
302 * try looking at the first HOWMANY bytes
303 */
276 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
277 file_error(ms, errno, "cannot read `%s'", inname);
278 goto done;
304 if (ispipe) {
305 ssize_t r = 0;
306
307 while ((r = sread(fd, (void *)&buf[nbytes],
308 (size_t)(HOWMANY - nbytes))) > 0) {
309 nbytes += r;
310 if (r < PIPE_BUF) break;
311 }
312
313 if (nbytes == 0) {
314 /* We can not read it, but we were able to stat it. */
315 if (info_from_stat(ms, sb.st_mode) == -1)
316 goto done;
317 rv = 0;
318 goto done;
319 }
320
321 } else {
322 if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) {
323 file_error(ms, errno, "cannot read `%s'", inname);
324 goto done;
325 }
279 }
280
281 if (nbytes == 0) {
282 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
283 "application/x-empty" : "empty") == -1)
284 goto done;
285 } else if (nbytes == 1) {
286 if (file_printf(ms, "very short file (no magic)") == -1)

--- 76 unchanged lines hidden ---
326 }
327
328 if (nbytes == 0) {
329 if (file_printf(ms, (ms->flags & MAGIC_MIME) ?
330 "application/x-empty" : "empty") == -1)
331 goto done;
332 } else if (nbytes == 1) {
333 if (file_printf(ms, "very short file (no magic)") == -1)

--- 76 unchanged lines hidden ---