Commit | Line | Data |
---|---|---|
66a734b3 WJ |
1 | /* |
2 | * Ascii magic -- file types that we know based on keywords | |
3 | * that can appear anywhere in the file. | |
4 | * | |
5 | * Copyright (c) Ian F. Darwin, 1987. | |
6 | * Written by Ian F. Darwin. | |
7 | * | |
8 | * This software is not subject to any license of the American Telephone | |
9 | * and Telegraph Company or of the Regents of the University of California. | |
10 | * | |
11 | * Permission is granted to anyone to use this software for any purpose on | |
12 | * any computer system, and to alter it and redistribute it freely, subject | |
13 | * to the following restrictions: | |
14 | * | |
15 | * 1. The author is not responsible for the consequences of use of this | |
16 | * software, no matter how awful, even if they arise from flaws in it. | |
17 | * | |
18 | * 2. The origin of this software must not be misrepresented, either by | |
19 | * explicit claim or by omission. Since few users ever read sources, | |
20 | * credits must appear in the documentation. | |
21 | * | |
22 | * 3. Altered versions must be plainly marked as such, and must not be | |
23 | * misrepresented as being the original software. Since few users | |
24 | * ever read sources, credits must appear in the documentation. | |
25 | * | |
26 | * 4. This notice may not be removed or altered. | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <ctype.h> | |
31 | #include "file.h" | |
32 | #include "names.h" | |
33 | ||
34 | #ifndef lint | |
35 | static char *moduleid = | |
36 | "@(#)$Header: ascmagic.c,v 1.5 87/09/16 14:44:45 ian Exp $"; | |
37 | #endif /* lint */ | |
38 | ||
39 | char *ckfmsg = "write error on output"; | |
40 | ||
41 | /* an optimisation over plain strcmp() */ | |
42 | #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) | |
43 | ||
44 | ascmagic(buf) | |
45 | register char *buf; | |
46 | { | |
47 | register int i; | |
48 | char *s, *strtok(), *token; | |
49 | register struct names *p; | |
50 | extern int nbytes; | |
51 | short has_escapes = 0; | |
52 | ||
53 | /* these are easy, do them first */ | |
54 | ||
55 | /* | |
56 | * for troff, look for . + letter + letter; | |
57 | * this must be done to disambiguate tar archives' ./file | |
58 | * and other trash from real troff input. | |
59 | */ | |
60 | if (*buf == '.' && | |
61 | isascii(*(buf+1)) && isalnum(*(buf+1)) && | |
62 | isascii(*(buf+2)) && isalnum(*(buf+2))){ | |
63 | ckfputs("troff or preprocessor input text", stdout); | |
64 | return 1; | |
65 | } | |
66 | if ((*buf == 'c' || *buf == 'C') && | |
67 | isascii(*(buf + 1)) && isspace(*(buf + 1))) { | |
68 | ckfputs("fortran program text", stdout); | |
69 | return 1; | |
70 | } | |
71 | ||
72 | /* look for tokens from names.h - this is expensive! */ | |
73 | s = buf; | |
74 | while ((token = strtok(s, " \t\n\r\f")) != NULL) { | |
75 | s = NULL; /* make strtok() keep on tokin' */ | |
76 | for (p = names; p < names + NNAMES; p++) { | |
77 | if (STREQ(p->name, token)) { | |
78 | ckfputs(types[p->type], stdout); | |
79 | return 1; | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | switch (is_tar(buf)) { | |
85 | case 1: | |
86 | ckfputs("tar archive", stdout); | |
87 | return 1; | |
88 | case 2: | |
89 | ckfputs("POSIX tar archive", stdout); | |
90 | return 1; | |
91 | } | |
92 | ||
93 | for (i = 0; i < nbytes; i++) { | |
94 | if (!isascii(*(buf+i))) | |
95 | return 0; /* not all ascii */ | |
96 | if (*(buf+i) == '\033') /* ascii ESCAPE */ | |
97 | has_escapes ++; | |
98 | } | |
99 | ||
100 | /* all else fails, but it is ascii... */ | |
101 | if (has_escapes){ | |
102 | ckfputs("ascii text (with escape sequences)", stdout); | |
103 | } | |
104 | else { | |
105 | ckfputs("ascii text", stdout); | |
106 | } | |
107 | return 1; | |
108 | } | |
109 | ||
110 |