file_cmds-60.tar.gz
[apple/file_cmds.git] / file / ascmagic.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* $OpenBSD: ascmagic.c,v 1.3 1997/02/09 23:58:18 millert Exp $ */
25
26 /*
27 * ASCII magic -- file types that we know based on keywords
28 * that can appear anywhere in the file.
29 *
30 * Copyright (c) Ian F. Darwin, 1987.
31 * Written by Ian F. Darwin.
32 *
33 * This software is not subject to any license of the American Telephone
34 * and Telegraph Company or of the Regents of the University of California.
35 *
36 * Permission is granted to anyone to use this software for any purpose on
37 * any computer system, and to alter it and redistribute it freely, subject
38 * to the following restrictions:
39 *
40 * 1. The author is not responsible for the consequences of use of this
41 * software, no matter how awful, even if they arise from flaws in it.
42 *
43 * 2. The origin of this software must not be misrepresented, either by
44 * explicit claim or by omission. Since few users ever read sources,
45 * credits must appear in the documentation.
46 *
47 * 3. Altered versions must be plainly marked as such, and must not be
48 * misrepresented as being the original software. Since few users
49 * ever read sources, credits must appear in the documentation.
50 *
51 * 4. This notice may not be removed or altered.
52 */
53
54 #include <stdio.h>
55 #include <string.h>
56 #include <ctype.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include "file.h"
60 #include "names.h"
61
62 #ifndef lint
63 #if 0
64 static char *moduleid = "$OpenBSD: ascmagic.c,v 1.3 1997/02/09 23:58:18 millert Exp $";
65 #endif
66 #endif /* lint */
67
68 /* an optimisation over plain strcmp() */
69 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
70
71 int
72 ascmagic(buf, nbytes)
73 unsigned char *buf;
74 int nbytes; /* size actually read */
75 {
76 int i, has_escapes = 0;
77 unsigned char *s;
78 char nbuf[HOWMANY+1]; /* one extra for terminating '\0' */
79 char *token;
80 register struct names *p;
81
82 /*
83 * Do the tar test first, because if the first file in the tar
84 * archive starts with a dot, we can confuse it with an nroff file.
85 */
86 switch (is_tar(buf, nbytes)) {
87 case 1:
88 ckfputs("tar archive", stdout);
89 return 1;
90 case 2:
91 ckfputs("POSIX tar archive", stdout);
92 return 1;
93 }
94
95 /*
96 * for troff, look for . + letter + letter or .\";
97 * this must be done to disambiguate tar archives' ./file
98 * and other trash from real troff input.
99 */
100 if (*buf == '.') {
101 unsigned char *tp = buf + 1;
102
103 while (isascii(*tp) && isspace(*tp))
104 ++tp; /* skip leading whitespace */
105 if ((isascii(*tp) && (isalnum(*tp) || *tp=='\\') &&
106 isascii(tp[1]) && (isalnum(tp[1]) || tp[1] == '"'))) {
107 ckfputs("troff or preprocessor input text", stdout);
108 return 1;
109 }
110 }
111 if ((*buf == 'c' || *buf == 'C') &&
112 isascii(buf[1]) && isspace(buf[1])) {
113 ckfputs("fortran program text", stdout);
114 return 1;
115 }
116
117
118 /* Make sure we are dealing with ascii text before looking for tokens */
119 for (i = 0; i < nbytes; i++) {
120 if (!isascii(buf[i]))
121 return 0; /* not all ASCII */
122 }
123
124 /* look for tokens from names.h - this is expensive! */
125 /* make a copy of the buffer here because strtok() will destroy it */
126 s = (unsigned char*) memcpy(nbuf, buf, nbytes);
127 s[nbytes] = '\0';
128 has_escapes = (memchr(s, '\033', nbytes) != NULL);
129 while ((token = strtok((char *) s, " \t\n\r\f")) != NULL) {
130 s = NULL; /* make strtok() keep on tokin' */
131 for (p = names; p < names + NNAMES; p++) {
132 if (STREQ(p->name, token)) {
133 ckfputs(types[p->type], stdout);
134 if (has_escapes)
135 ckfputs(" (with escape sequences)",
136 stdout);
137 return 1;
138 }
139 }
140 }
141
142 /* all else fails, but it is ASCII... */
143 ckfputs("ASCII text", stdout);
144 if (has_escapes) {
145 ckfputs(" (with escape sequences)", stdout);
146 }
147 return 1;
148 }
149
150