[go: up one dir, main page]

File: file2.c

package info (click to toggle)
file-kanji 1.1-15
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 88 kB
  • ctags: 30
  • sloc: sh: 237; ansic: 232; makefile: 56
file content (275 lines) | stat: -rw-r--r-- 6,413 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/***********************************************************************
**                                                                    **
**  file2 : եΥɥå                                  **
**                                                                    **
**    :                                                   **
**                                                                    **
**    :                                                           **
**    v1.0 ????/??/??                                                 **
**                                                                **
**    v1.1 1993/07/23                                                 **
**         DOS^ZASCIIʸ˴ޤˤ                         **
**                                                                    **
***********************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

#define	BUFF_SIZE 4096

#define TRUE      1
#define FALSE     0

#define ASCII     1
#define EUC       2
#define SJIS      3
#define JIS       4
#define EMPTY     5
#define UNKOWN    99

/* Υ */
#define	ESC_DALLER_B		1	/* ESC-$-B	*/
#define	ESC_DALLER_ATMARK	2	/* ESC-$-@	*/

/* ȤΥ */
#define	ESC_LEFT_PARA_B		1	/* ESC-(-B	*/
#define	ESC_LEFT_PARA_J		2	/* ESC-(-J	*/

typedef unsigned char uchar;

int	kanji_in_type;
int	kanji_out_type;

main(argc,argv)
int  argc;
char *argv[];
{
    int  i;

    if ( argc<2 ) {
        printf("Usage : %s file ...\n", argv[0]);
        exit(1);
    }
    for( i=1; i<argc; i++) {
        (void)check(argv[i]);
    }
}

int  check(filename)
char *filename;
{
    FILE  *fid;
    uchar buff[BUFF_SIZE];
    int   kind, n;
    char  *f, work[256];
    struct stat stat_buff;

    if ( stat(filename, &stat_buff)==-1 ) {
	perror(filename);
	return(1);
    }
    f = "";
    if      ( stat_buff.st_mode & S_IFIFO )  f = "pipe or FIFO";
    else if ( stat_buff.st_mode & S_IFCHR )  f = "character special";
    else if ( stat_buff.st_mode & S_IFDIR )  f = "directory";
    else if ( stat_buff.st_mode & S_IFBLK )  f = "block special";
    else if ( stat_buff.st_mode & S_IFREG ) {
        if ( (fid = fopen( filename, "r"))==NULL ) {
	    perror(filename);
	    return(1);
        }
        n = fread(buff, sizeof(char), BUFF_SIZE, fid);
        fclose(fid);
        kind = check_detail(buff, n);
        switch(kind) {
        case ASCII:
	    f = "ascii text"; break;
        case SJIS:
	    f = "SJIS text"; break;
        case EUC:
	    f = "EUC text"; break;
        case JIS:
	    strcpy(work, "JIS text");
	    if ( kanji_in_type==ESC_DALLER_B )
		strcat(work, " (KI=ESC-$-B");
	    else
	    	strcat(work, " (KI=ESC-$-@");
	    if ( kanji_out_type==ESC_LEFT_PARA_B )
		strcat(work, ",KO=ESC-(-B)");
	    else
		strcat(work, ",KO=ESC-(-J)");
	    f = work;
	    break;
        case EMPTY:
	    f = "empty"; break;
        case UNKOWN:
	    f = "UNKOWN"; break;
        }
    }
    else if ( stat_buff.st_mode & S_IFLNK )  f = "symbolic link";
    else if ( stat_buff.st_mode & S_IFSOCK ) f = "socket";
    printf("%s:%s\t%s\n", filename, (strlen(filename)+1)<=7?"\t":"", f);
    return(0);
}

int   check_detail(buff, size)
uchar buff[];
int   size;
{
    if ( size==0 ) return(EMPTY);
    if ( is_jis_file(buff, size) ) return(JIS);
    if ( is_ascii_file(buff, size) ) return(ASCII);
    if ( is_euc_file(buff, size) ) return(EUC);
    if ( is_sjis_file(buff, size) ) return(SJIS);
    return(UNKOWN);
}

int   is_jis_file(buff, size)
uchar buff[];
int   size;
{
    int  i;

    kanji_in_type = kanji_out_type = 0;

    for (i=0; i<size; i++ ) {
	if ( (i+2) < size ) {
	    if ( is_kanji_in_code(buff[i], buff[i+1], buff[i+2]) ) {
	        i += 2;
	        continue;
	    }
	    if ( is_kanji_out_code(buff[i], buff[i+1], buff[i+2]) ) {
	        i += 2;
	        continue;
	    }
	}
        if ( is_ascii_char(buff[i]) ) continue;
	return(FALSE);
    }
    if ( kanji_in_type || kanji_out_type )
        return(TRUE);
    return(FALSE);
}

int   is_ascii_file(buff, size)
uchar buff[];
int   size;
{
    int  i;

    for(i=0; i<size; i++) {
        if ( is_ascii_char(buff[i]) ) continue;
        return(FALSE);
    }
    return(TRUE);
}

int   is_kanji_in_code(c1, c2, c3)
uchar c1, c2, c3;
{
    if      ( c1==0x1b /*ESC*/ && c2==0x24 /*$*/ && c3==0x40 /*@*/ ) {
	kanji_in_type = ESC_DALLER_B;
	return(TRUE);
    }
    else if ( c1==0x1b /*ESC*/ && c2==0x24 /*$*/ && c3==0x42 /*B*/ ) {
	kanji_in_type = ESC_DALLER_ATMARK;
	return(TRUE);
    }
    return(FALSE);
}

int   is_kanji_out_code(c1, c2, c3)
uchar c1, c2, c3;
{
    if      ( c1==0x1b /*ESC*/ && c2==0x28 /*(*/ && c3==0x42 /*B*/ ) {
	kanji_out_type = ESC_LEFT_PARA_B;
	return(TRUE);
    }
    else if ( c1==0x1b /*ESC*/ && c2==0x28 /*(*/ && c3==0x4A /*J*/ ) {
	kanji_out_type = ESC_LEFT_PARA_J;
	return(TRUE);
    }
    return(FALSE);
}

int   is_ascii_char(c)
uchar c;
{
    return( c>=0x07 && c<=0x0d || c==0x1a || c==0x1b || c>=0x20 && c<=0x7f
		? TRUE : FALSE);
}

int  is_sjis_file( buff, size)
uchar buff[];
int   size;
{
    int  i;

    if ( size<=0 ) return(FALSE);
    for ( i=0; i<size; i++) {
	if ( is_ascii_char(buff[i]) ) continue;
        if ( is_katakana_char(buff[i]) ) continue;
        if ( is_sjis_char(buff[i],buff[i+1]) ) {
	    i++;
	    continue;
	}
	return(FALSE);
    }
    return(TRUE);
}

int   is_katakana_char(c)
uchar c;
{
    if ( c>=0x0a1 && c<=0xdf ) return(TRUE);
    return(FALSE);
}

int   is_sjis_char(c1, c2)
uchar c1,c2;
{
    if ( (c1>=0x081 && c1<=0x09f) || (c1>=0xe0 && c1<=0x0ee) ) {
	if ( c2>=0x40 && c2<=0x0fc ) return(TRUE);
    }
    return(FALSE);
}

int  is_euc_file( buff, size)
uchar buff[];
int   size;
{
    int  i;

    if ( size<=0 ) return(FALSE);
    for ( i=0; i<size; i++) {
	if ( is_ascii_char(buff[i]) ) continue;
	if ( is_euc_katakana_char(buff[i], buff[i+1]) ) {
	    i++;
	    continue;
	}
        if ( is_euc_char(buff[i],buff[i+1]) ) {
	    i++;
	    continue;
	}
	return(FALSE);
    }
    return(TRUE);
}

int   is_euc_char(c1, c2)
uchar c1,c2;
{
    if ( c1>=0x0a1 && c1<=0x0fe ) {
	if ( c2>=0x0a1 && c2<=0x0fe ) return(TRUE);
    }
    return(FALSE);
}

int   is_euc_katakana_char(c1, c2)
uchar c1,c2;
{
    if ( c1==0x08e ) {
	if ( is_katakana_char(c2) ) return(TRUE);
    }
    return(FALSE);
}