Annotation of src/usr.bin/file/ascmagic.c, Revision 1.5
1.5 ! mpech 1: /* $OpenBSD: ascmagic.c,v 1.4 1998/07/10 15:05:15 mickey Exp $ */
1.3 millert 2:
1.1 deraadt 3: /*
4: * ASCII magic -- file types that we know based on keywords
5: * that can appear anywhere in the file.
6: *
7: * Copyright (c) Ian F. Darwin, 1987.
8: * Written by Ian F. Darwin.
9: *
10: * This software is not subject to any license of the American Telephone
11: * and Telegraph Company or of the Regents of the University of California.
12: *
13: * Permission is granted to anyone to use this software for any purpose on
14: * any computer system, and to alter it and redistribute it freely, subject
15: * to the following restrictions:
16: *
17: * 1. The author is not responsible for the consequences of use of this
18: * software, no matter how awful, even if they arise from flaws in it.
19: *
20: * 2. The origin of this software must not be misrepresented, either by
21: * explicit claim or by omission. Since few users ever read sources,
22: * credits must appear in the documentation.
23: *
24: * 3. Altered versions must be plainly marked as such, and must not be
25: * misrepresented as being the original software. Since few users
26: * ever read sources, credits must appear in the documentation.
27: *
28: * 4. This notice may not be removed or altered.
29: */
30:
31: #include <stdio.h>
32: #include <string.h>
33: #include <ctype.h>
34: #include <stdlib.h>
35: #include <unistd.h>
1.4 mickey 36: #include <err.h>
1.1 deraadt 37: #include "file.h"
38: #include "names.h"
39:
40: #ifndef lint
1.5 ! mpech 41: static char *moduleid = "$OpenBSD: ascmagic.c,v 1.4 1998/07/10 15:05:15 mickey Exp $";
1.1 deraadt 42: #endif /* lint */
43:
44: /* an optimisation over plain strcmp() */
45: #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
46:
47: int
48: ascmagic(buf, nbytes)
49: unsigned char *buf;
50: int nbytes; /* size actually read */
51: {
52: int i, has_escapes = 0;
53: unsigned char *s;
54: char nbuf[HOWMANY+1]; /* one extra for terminating '\0' */
55: char *token;
1.5 ! mpech 56: struct names *p;
1.1 deraadt 57:
58: /*
59: * Do the tar test first, because if the first file in the tar
60: * archive starts with a dot, we can confuse it with an nroff file.
61: */
62: switch (is_tar(buf, nbytes)) {
63: case 1:
64: ckfputs("tar archive", stdout);
65: return 1;
66: case 2:
67: ckfputs("POSIX tar archive", stdout);
68: return 1;
69: }
70:
71: /*
72: * for troff, look for . + letter + letter or .\";
73: * this must be done to disambiguate tar archives' ./file
74: * and other trash from real troff input.
75: */
76: if (*buf == '.') {
77: unsigned char *tp = buf + 1;
78:
79: while (isascii(*tp) && isspace(*tp))
80: ++tp; /* skip leading whitespace */
81: if ((isascii(*tp) && (isalnum(*tp) || *tp=='\\') &&
82: isascii(tp[1]) && (isalnum(tp[1]) || tp[1] == '"'))) {
83: ckfputs("troff or preprocessor input text", stdout);
84: return 1;
85: }
86: }
87: if ((*buf == 'c' || *buf == 'C') &&
88: isascii(buf[1]) && isspace(buf[1])) {
89: ckfputs("fortran program text", stdout);
90: return 1;
91: }
92:
1.3 millert 93:
94: /* Make sure we are dealing with ascii text before looking for tokens */
95: for (i = 0; i < nbytes; i++) {
96: if (!isascii(buf[i]))
97: return 0; /* not all ASCII */
98: }
99:
1.1 deraadt 100: /* look for tokens from names.h - this is expensive! */
101: /* make a copy of the buffer here because strtok() will destroy it */
102: s = (unsigned char*) memcpy(nbuf, buf, nbytes);
103: s[nbytes] = '\0';
104: has_escapes = (memchr(s, '\033', nbytes) != NULL);
105: while ((token = strtok((char *) s, " \t\n\r\f")) != NULL) {
106: s = NULL; /* make strtok() keep on tokin' */
107: for (p = names; p < names + NNAMES; p++) {
108: if (STREQ(p->name, token)) {
109: ckfputs(types[p->type], stdout);
110: if (has_escapes)
111: ckfputs(" (with escape sequences)",
112: stdout);
113: return 1;
114: }
115: }
116: }
117:
118: /* all else fails, but it is ASCII... */
119: ckfputs("ASCII text", stdout);
120: if (has_escapes) {
121: ckfputs(" (with escape sequences)", stdout);
122: }
123: return 1;
124: }
125:
126: