Annotation of src/usr.bin/file/internat.c, Revision 1.3
1.3 ! ian 1: /* $OpenBSD: internat.c,v 1.2 2002/02/17 19:42:30 millert Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) Ian F. Darwin 1986-1995.
! 5: * Software written by Ian F. Darwin and others;
! 6: * maintained 1995-present by Christos Zoulas and others.
! 7: *
! 8: * Redistribution and use in source and binary forms, with or without
! 9: * modification, are permitted provided that the following conditions
! 10: * are met:
! 11: * 1. Redistributions of source code must retain the above copyright
! 12: * notice immediately at the beginning of the file, without modification,
! 13: * this list of conditions, and the following disclaimer.
! 14: * 2. Redistributions in binary form must reproduce the above copyright
! 15: * notice, this list of conditions and the following disclaimer in the
! 16: * documentation and/or other materials provided with the distribution.
! 17: * 3. All advertising materials mentioning features or use of this software
! 18: * must display the following acknowledgement:
! 19: * This product includes software developed by Ian F. Darwin and others.
! 20: * 4. The name of the author may not be used to endorse or promote products
! 21: * derived from this software without specific prior written permission.
! 22: *
! 23: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 26: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
! 27: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 33: * SUCH DAMAGE.
! 34: */
1.1 millert 35:
1.2 millert 36: #include <stdio.h>
1.1 millert 37: #include <string.h>
38: #include <sys/types.h>
39:
40: #include "file.h"
41:
42: #define F 0
43: #define T 1
44:
45: /*
46: * List of characters that look "reasonable" in international
47: * language texts. That's almost all characters :), except a
48: * few in the control range of ASCII (all the known international
49: * charactersets share the bottom half with ASCII).
50: */
51: static char maybe_internat[256] = {
52: F, F, F, F, F, F, F, F, T, T, T, T, T, T, F, F, /* 0x0X */
53: F, F, F, F, F, F, F, F, F, F, F, T, F, F, F, F, /* 0x1X */
54: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x2X */
55: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x3X */
56: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x4X */
57: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x5X */
58: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x6X */
59: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F, /* 0x7X */
60: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x8X */
61: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0x9X */
62: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xaX */
63: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xbX */
64: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xcX */
65: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xdX */
66: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, /* 0xeX */
67: T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T /* 0xfX */
68: };
69:
70: /* Maximal length of a line we consider "reasonable". */
71: #define MAXLINELEN 300
72:
73: int
74: internatmagic(buf, nbytes)
75: unsigned char *buf;
76: int nbytes;
77: {
78: int i;
79: unsigned char *cp;
80:
81: nbytes--;
82:
83: /* First, look whether there are "unreasonable" characters. */
84: for (i = 0, cp = buf; i < nbytes; i++, cp++)
85: if (!maybe_internat[*cp])
86: return 0;
87:
88: /*
89: * Now, look whether the file consists of lines of
90: * "reasonable" length.
91: */
92:
93: for (i = 0; i < nbytes;) {
94: cp = memchr(buf, '\n', nbytes - i);
95: if (cp == NULL) {
96: /* Don't fail if we hit the end of buffer. */
97: if (i + MAXLINELEN >= nbytes)
98: break;
99: else
100: return 0;
101: }
102: if (cp - buf > MAXLINELEN)
103: return 0;
104: i += (cp - buf + 1);
105: buf = cp + 1;
106: }
107: ckfputs("International language text", stdout);
108: return 1;
109: }