2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: ascmagic.c,v 1.3 1997/02/09 23:58:18 millert Exp $ */
27 * ASCII magic -- file types that we know based on keywords
28 * that can appear anywhere in the file.
30 * Copyright (c) Ian F. Darwin, 1987.
31 * Written by Ian F. Darwin.
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.
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:
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.
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.
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.
51 * 4. This notice may not be removed or altered.
64 static char *moduleid
= "$OpenBSD: ascmagic.c,v 1.3 1997/02/09 23:58:18 millert Exp $";
68 /* an optimisation over plain strcmp() */
69 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
74 int nbytes
; /* size actually read */
76 int i
, has_escapes
= 0;
78 char nbuf
[HOWMANY
+1]; /* one extra for terminating '\0' */
80 register struct names
*p
;
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.
86 switch (is_tar(buf
, nbytes
)) {
88 ckfputs("tar archive", stdout
);
91 ckfputs("POSIX tar archive", stdout
);
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.
101 unsigned char *tp
= buf
+ 1;
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
);
111 if ((*buf
== 'c' || *buf
== 'C') &&
112 isascii(buf
[1]) && isspace(buf
[1])) {
113 ckfputs("fortran program text", stdout
);
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 */
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
);
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
);
135 ckfputs(" (with escape sequences)",
142 /* all else fails, but it is ASCII... */
143 ckfputs("ASCII text", stdout
);
145 ckfputs(" (with escape sequences)", stdout
);