]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/file/ascmagic.c
This commit was generated by cvs2svn to compensate for changes in r65285,
[FreeBSD/FreeBSD.git] / usr.bin / file / ascmagic.c
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 #ifndef lint
30 static const char rcsid[] =
31   "$FreeBSD$";
32 #endif /* not lint */
33
34 #include <ctype.h>
35 #include <string.h>
36 #include "file.h"
37 #include "names.h"
38
39                         /* an optimisation over plain strcmp() */
40 #define STREQ(a, b)     (*(a) == *(b) && strcmp((a), (b)) == 0)
41
42 int
43 ascmagic(buf, nbytes)
44 unsigned char *buf;
45 int nbytes;     /* size actually read */
46 {
47         int i, has_escapes = 0;
48         char *s;
49         char nbuf[HOWMANY+1];   /* one extra for terminating '\0' */
50         char *token;
51         register struct names *p;
52
53         /*
54          * Do the tar test first, because if the first file in the tar
55          * archive starts with a dot, we can confuse it with an nroff file.
56          */
57         switch (is_tar(buf, nbytes)) {
58         case 1:
59                 ckfputs("tar archive", stdout);
60                 return 1;
61         case 2:
62                 ckfputs("POSIX tar archive", stdout);
63                 return 1;
64         }
65
66         /*
67          * for troff, look for . + letter + letter or .\";
68          * this must be done to disambiguate tar archives' ./file
69          * and other trash from real troff input.
70          */
71         if (*buf == '.') {
72                 unsigned char *tp = buf + 1;
73
74                 while (isascii(*tp) && isspace(*tp))
75                         ++tp;   /* skip leading whitespace */
76                 if ((isascii(*tp) && (isalnum(*tp) || *tp=='\\') &&
77                     isascii(tp[1]) && (isalnum(tp[1]) || tp[1] == '"'))) {
78                         ckfputs("troff or preprocessor input text", stdout);
79                         return 1;
80                 }
81         }
82         if ((*buf == 'c' || *buf == 'C') && 
83             isascii(buf[1]) && isspace(buf[1])) {
84                 ckfputs("fortran program text", stdout);
85                 return 1;
86         }
87
88
89         /* Make sure we are dealing with ascii text before looking for tokens */
90         for (i = 0; i < nbytes - 1; i++) {
91                 if (!isascii(buf[i]) ||
92                     (iscntrl(buf[i]) && !isspace(buf[i]) &&
93                      buf[i] != '\b' && buf[i] != '\032' && buf[i] != '\033'
94                     )
95                    )
96                         return 0;       /* not all ASCII */
97         }
98
99         /* look for tokens from names.h - this is expensive! */
100         /* make a copy of the buffer here because strtok() will destroy it */
101         s = strcpy(nbuf, buf);
102         has_escapes = (strchr(s, '\033') != NULL);
103         while ((token = strtok(s, " \t\n\r\f")) != NULL) {
104                 s = NULL;       /* make strtok() keep on tokin' */
105                 for (p = names; p < names + NNAMES; p++) {
106                         if (STREQ(p->name, token)) {
107                                 ckfputs(types[p->type], stdout);
108                                 if (has_escapes)
109                                         ckfputs(" (with escape sequences)",
110                                                 stdout);
111                                 return 1;
112                         }
113                 }
114         }
115
116         /* all else fails, but it is ASCII... */
117         ckfputs("ASCII text", stdout);
118         if (has_escapes) {
119                 ckfputs(" (with escape sequences)", stdout);
120         }
121         return 1;
122 }