#include "patchlevel.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: file.c,v 1.83 2003/10/08 17:09:26 christos Exp $")
+FILE_RCSID("@(#)$Id: file.c,v 1.84 2003/10/09 15:15:23 christos Exp $")
#endif /* lint */
int flags = 0;
char *mime, *home, *usermagic;
struct stat sb;
-#define OPTSTRING "bcCdf:F:ikLm:nNpsvz"
+#define OPTSTRING "bcCdf:F:ikLm:nNprsvz"
#ifdef HAVE_GETOPT_LONG
int longindex;
private struct option long_options[] =
{"preserve-date", 0, 0, 'p'},
#endif
{"uncompress", 0, 0, 'z'},
+ {"raw", 0, 0, 'r'},
{"no-buffer", 0, 0, 'n'},
{"no-pad", 0, 0, 'N'},
{"special-files", 0, 0, 's'},
flags |= MAGIC_PRESERVE_ATIME;
break;
#endif
+ case 'r':
+ flags |= MAGIC_RAW;
+ break;
case 's':
flags |= MAGIC_DEVICES;
break;
*/
/*
* file.h - definitions for file(1) program
- * @(#)$Id: file.h,v 1.57 2003/10/08 16:37:27 christos Exp $
+ * @(#)$Id: file.h,v 1.58 2003/10/09 15:15:23 christos Exp $
*/
#ifndef __file_h__
int32_t *off;
} c;
struct out {
+ /* Accumulation buffer */
char *buf;
char *ptr;
size_t len;
size_t size;
+ /* Printable buffer */
+ char *pbuf;
+ size_t psize;
} o;
int flags;
int haderr;
protected void file_magwarn(const char *, ...);
protected void file_mdump(struct magic *);
protected void file_showstr(FILE *, const char *, size_t);
+protected const char *file_getbuffer(struct magic_set *);
#ifndef HAVE_STRERROR
extern int sys_nerr;
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
+#include <ctype.h>
/*
* Like printf, only we print to a buffer and advance it.
ms->haderr = 0;
return 0;
}
+
+protected const char *
+file_getbuffer(struct magic_set *ms)
+{
+ char *nbuf, *op, *np;
+ size_t nsize;
+
+ if (ms->haderr)
+ return NULL;
+
+ if (ms->flags & MAGIC_RAW)
+ return ms->o.buf;
+
+ nsize = ms->o.len * 4 + 1;
+ if (ms->o.psize < nsize) {
+ if ((nbuf = realloc(ms->o.pbuf, nsize)) == NULL) {
+ file_oomem(ms);
+ return NULL;
+ }
+ ms->o.psize = nsize;
+ ms->o.pbuf = nbuf;
+ }
+
+ for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
+ if (isprint((unsigned char)*op)) {
+ *np++ = *op;
+ } else {
+ *np++ = '\\';
+ *np++ = ((*op >> 6) & 3) + '0';
+ *np++ = ((*op >> 3) & 7) + '0';
+ *np++ = ((*op >> 0) & 7) + '0';
+ }
+ }
+ *np = '\0';
+ return ms->o.pbuf;
+}
#include "patchlevel.h"
#ifndef lint
-FILE_RCSID("@(#)$Id: magic.c,v 1.11 2003/10/08 16:37:27 christos Exp $")
+FILE_RCSID("@(#)$Id: magic.c,v 1.12 2003/10/09 15:15:23 christos Exp $")
#endif /* lint */
#ifdef __EMX__
free(ms);
return NULL;
}
+ ms->o.pbuf = malloc(ms->o.psize = 1024);
+ if (ms->o.pbuf == NULL) {
+ free(ms->o.buf);
+ free(ms);
+ return NULL;
+ }
ms->c.off = malloc((ms->c.len = 10) * sizeof(*ms->c.off));
if (ms->c.off == NULL) {
+ free(ms->o.pbuf);
free(ms->o.buf);
free(ms);
return NULL;
case 0:
break;
default:
- return ms->o.buf;
+ return file_getbuffer(ms);
}
#ifndef STDIN_FILENO
if (sb.st_mode & 0111)
if (file_printf(ms, "executable, ") == -1)
return NULL;
- return ms->o.buf;
+ return file_getbuffer(ms);
}
/*
case 0:
break;
default:
- return ms->o.buf;
+ return file_getbuffer(ms);
}
#endif
if (file_buffer(ms, buf, (size_t)nbytes) == -1)
}
close_and_restore(ms, inname, fd, &sb);
- return ms->haderr ? NULL : ms->o.buf;
+ return file_getbuffer(ms);
done:
close_and_restore(ms, inname, fd, &sb);
return NULL;
if (file_buffer(ms, buf, nb) == -1) {
return NULL;
}
- return ms->haderr ? NULL : ms->o.buf;
+ return file_getbuffer(ms);
}
public const char *
#include <sys/types.h>
-#define MAGIC_NONE 0x00 /* No flags */
-#define MAGIC_DEBUG 0x01 /* Turn on debugging */
-#define MAGIC_SYMLINK 0x02 /* Follow symlinks */
-#define MAGIC_COMPRESS 0x04 /* Check inside compressed files */
-#define MAGIC_DEVICES 0x08 /* Look at the contents of devices */
-#define MAGIC_MIME 0x10 /* Return a mime string */
-#define MAGIC_CONTINUE 0x20 /* Return all matches */
-#define MAGIC_CHECK 0x40 /* Print warnings to stderr */
-#define MAGIC_PRESERVE_ATIME 0x80 /* Restore access time on exit */
-
+#define MAGIC_NONE 0x000 /* No flags */
+#define MAGIC_DEBUG 0x001 /* Turn on debugging */
+#define MAGIC_SYMLINK 0x002 /* Follow symlinks */
+#define MAGIC_COMPRESS 0x004 /* Check inside compressed files */
+#define MAGIC_DEVICES 0x008 /* Look at the contents of devices */
+#define MAGIC_MIME 0x010 /* Return a mime string */
+#define MAGIC_CONTINUE 0x020 /* Return all matches */
+#define MAGIC_CHECK 0x040 /* Print warnings to stderr */
+#define MAGIC_PRESERVE_ATIME 0x080 /* Restore access time on exit */
+#define MAGIC_RAW 0x100 /* Don't translate unprintable chars */
#ifdef __cplusplus
extern "C" {