[BACK]Return to internat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / file

Annotation of src/usr.bin/file/internat.c, Revision 1.2

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