File: file2.c

package info (click to toggle)
file-kanji 1.1-16
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 148 kB
  • ctags: 190
  • sloc: ansic: 1,529; makefile: 13
file content (356 lines) | stat: -rw-r--r-- 8,236 bytes parent folder | download | duplicates (3)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/***********************************************************************
**                                                                    **
**  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 UTF8      6
#define UNKNOWN   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, status;
    char  *f, work[256];
    struct stat stat_buff, stat_file;
    pid_t pid;

    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 UNKNOWN:
	    if ( stat("/usr/bin/file", &stat_file)==-1 || (stat_file.st_mode & S_IXOTH)==0 ) {
		f = "UNKNOWN"; break;
	    }
	    else{
		if ( (pid=fork()) < 0 ) {
		    perror("The fork failed!");
		    return(-1);
		}
		else if ( pid==0 ) {
		    execl("/usr/bin/file", "file", filename, NULL);
		    _exit(0);
		}
		else {
		wait(&status);
		return(0);
		}
	    }
        case UTF8:
	    f = "UTF-8 text"; 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_utf8_file(buff, size) ) return(UTF8);
    if ( is_euc_file(buff, size) ) return(EUC);
    if ( is_sjis_file(buff, size) ) return(SJIS);
    return(UNKNOWN);
}

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

    kanji_in_type = kanji_out_type = 0;

    for (i=0; i<size-1; 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_ATMARK;
	return(TRUE);
    }
    else if ( c1==0x1b /*ESC*/ && c2==0x24 /*$*/ && c3==0x42 /*B*/ ) {
	kanji_in_type = ESC_DALLER_B;
	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-1; 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-1; 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);
}

int is_utf8_file( buff, size)
uchar buff[];
int   size;
{
    int  i;
    if ( size<=1 ) return(FALSE);
    if ( size==2 && is_utf8_2_byte_char(buff[0], buff[1])) return(TRUE);
    if ( size==3 && ( is_utf8_2_byte_char(buff[0], buff[1]) || is_utf8_2_byte_char(buff[1], buff[2]) || is_utf8_3_byte_char(buff[0], buff[1], buff[2]) ) ) return(TRUE);
    for ( i=0; i<size-3; i++) {
	if ( is_ascii_char(buff[i]) ) continue;
	if ( is_utf8_2_byte_char(buff[i], buff[i+1]) ) {
	    i++;
	    continue;
	}
	if ( is_utf8_3_byte_char(buff[i], buff[i+1], buff[i+2]) ) {
	    i += 2;
	    continue;
	}
	if (is_utf8_4_byte_char(buff[i], buff[i+1], buff[i+2], buff[i+3]) ) {
	    i += 3;
	    continue;
	}
	return(FALSE);
    }
    return(TRUE);
}

int is_utf8_2_byte_char(c1, c2)
uchar c1,c2;
{
    if ( c1>=0xc0 && c1<=0xdf ) {
	if ( c2>=0x80 && c2<=0xbf ) return(TRUE);
    }
    return(FALSE);
}

int is_utf8_3_byte_char(c1, c2, c3)
uchar c1,c2,c3;
{
    if ( c1>=0xe0 && c1<=0xef ) {
    	if ( c2>=0x80 && c2<=0xbf ) {
	    if (c3>=0x80 && c3<=0xbf ) return(TRUE);
	}
    }
    return(FALSE);
}

int is_utf8_4_byte_char(c1, c2, c3, c4)
uchar c1,c2,c3,c4;
{
    if ( c1>=0xf0 && c1<=0xf7 ) {
	if ( c2>=0x80 && c2<=0xbf ) {
	    if (c3>=0x80 && c3<=0xbf ) {
		if (c4>=0x80 && c4<=0xbf ) return(TRUE);
	    }
	}
    }
    return(FALSE);
}