]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/file/internat.c
This commit was generated by cvs2svn to compensate for changes in r65285,
[FreeBSD/FreeBSD.git] / usr.bin / file / internat.c
1
2 #ifndef lint
3 static const char rcsid[] =
4   "$FreeBSD$";
5 #endif /* not lint */
6
7 #include "file.h"
8
9 #include <string.h>
10
11 #define F 0
12 #define T 1
13
14 /*
15  * List of characters that look "reasonable" in international
16  * language texts.  That's almost all characters :), except a
17  * few in the control range of ASCII (all the known international
18  * charactersets share the bottom half with ASCII).
19  */
20 static char maybe_internat[256] = {
21         F, F, F, F, F, F, F, F, T, T, T, T, T, T, F, F,  /* 0x0X */
22         F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F,  /* 0x1X */
23         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x2X */
24         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x3X */
25         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x4X */
26         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x5X */
27         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x6X */
28         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F,  /* 0x7X */
29         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x8X */
30         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0x9X */
31         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0xaX */
32         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0xbX */
33         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0xcX */
34         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0xdX */
35         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T,  /* 0xeX */
36         T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T   /* 0xfX */
37 };
38
39 /* Maximal length of a line we consider "reasonable". */
40 #define MAXLINELEN 300
41
42 int
43 internatmagic(buf, nbytes)
44         unsigned char *buf;
45         int nbytes;
46 {
47         int i;
48         unsigned char *cp;
49
50         nbytes--;
51
52         /* First, look whether there are "unreasonable" characters. */
53         for (i = 0, cp = buf; i < nbytes; i++, cp++)
54                 if (!maybe_internat[*cp])
55                         return 0;
56
57         /*
58          * Now, look whether the file consists of lines of
59          * "reasonable" length.
60          */
61
62         for (i = 0; i < nbytes;) {
63                 cp = memchr(buf, '\n', nbytes - i);
64                 if (cp == NULL) {
65                         /* Don't fail if we hit the end of buffer. */
66                         if (i + MAXLINELEN >= nbytes)
67                                 break;
68                         else
69                                 return 0;
70                 }
71                 if (cp - buf > MAXLINELEN)
72                         return 0;
73                 i += (cp - buf + 1);
74                 buf = cp + 1;
75         }
76         ckfputs("International language text", stdout);
77         return 1;
78 }