file_cmds-60.tar.gz
[apple/file_cmds.git] / file / internat.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: internat.c,v 1.1 1997/02/09 23:58:26 millert Exp $ */
25
26 #include <string.h>
27 #include <sys/types.h>
28
29 #include "file.h"
30
31 #define F 0
32 #define T 1
33
34 /*
35 * List of characters that look "reasonable" in international
36 * language texts. That's almost all characters :), except a
37 * few in the control range of ASCII (all the known international
38 * charactersets share the bottom half with ASCII).
39 */
40 static char maybe_internat[256] = {
41 F, F, F, F, F, F, F, F, T, T, T, T, T, T, F, F, /* 0x0X */
42 F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F, /* 0x1X */
43 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x2X */
44 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x3X */
45 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x4X */
46 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x5X */
47 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x6X */
48 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F, /* 0x7X */
49 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x8X */
50 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x9X */
51 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xaX */
52 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xbX */
53 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xcX */
54 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xdX */
55 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xeX */
56 T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T /* 0xfX */
57 };
58
59 /* Maximal length of a line we consider "reasonable". */
60 #define MAXLINELEN 300
61
62 int
63 internatmagic(buf, nbytes)
64 unsigned char *buf;
65 int nbytes;
66 {
67 int i;
68 unsigned char *cp;
69
70 nbytes--;
71
72 /* First, look whether there are "unreasonable" characters. */
73 for (i = 0, cp = buf; i < nbytes; i++, cp++)
74 if (!maybe_internat[*cp])
75 return 0;
76
77 /*
78 * Now, look whether the file consists of lines of
79 * "reasonable" length.
80 */
81
82 for (i = 0; i < nbytes;) {
83 cp = memchr(buf, '\n', nbytes - i);
84 if (cp == NULL) {
85 /* Don't fail if we hit the end of buffer. */
86 if (i + MAXLINELEN >= nbytes)
87 break;
88 else
89 return 0;
90 }
91 if (cp - buf > MAXLINELEN)
92 return 0;
93 i += (cp - buf + 1);
94 buf = cp + 1;
95 }
96 ckfputs("International language text", stdout);
97 return 1;
98 }