xref: /illumos-gate/usr/src/lib/iconv_modules/euro/utils/gentbl/debug.c (revision bde334a8dbd66dfa70ce4d7fc9dcad6e1ae45fe4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef DEBUG_C
27 #define DEBUG_C
28 
29 #ifdef DEBUG
30 #include <sys/fcntl.h>
31 #include <sys/stat.h>
32 int main(int argc, char ** argv) {
33 	char *inbuf, *outbuf, *in_tmp, *out_tmp;
34 	size_t inbytesleft, outbytesleft;
35 	int fd;
36 	int i;
37 	struct stat s;
38 	struct _icv_state *st;
39 	if (argc < 2) {
40 		fprintf(stderr, "Usage: %s input\n", argv[0]);
41 		exit(-1);
42 	}
43 	if ((fd = open(argv[1], O_RDONLY)) == -1) {
44 		perror("open");
45 		exit(-2);
46 	}
47 	if (fstat(fd, &s) == -1) {
48 		perror("stat");
49 		exit(-3);
50 	}
51 	inbytesleft = outbytesleft = s.st_size;
52 	in_tmp = inbuf = (char *)malloc(inbytesleft);
53 	out_tmp = outbuf = (char *)malloc(outbytesleft);
54 	if (!inbuf || !outbuf) {
55 		perror("malloc");
56 		exit(-1);
57 	}
58 	if (read(fd, inbuf, inbytesleft) != inbytesleft) {
59 		perror("read");
60 		exit(-4);
61 	}
62 	st = (struct _icv_state *)_icv_open();
63 	if (st == (struct _icv_state *) -1) {
64 		perror("_icv_open");
65 		exit(-1);
66 	}
67 	if (_icv_iconv(st, &inbuf, &inbytesleft, \
68 			&outbuf, &outbytesleft) == -1) {
69 		perror("icv_iconv");
70 		fprintf(stderr, "\ninbytesleft = %d\n", inbytesleft);
71 		exit(-2);
72 	}
73 	/* Output tbl[] contents. */
74 	for (i=0; i < s.st_size; i++)
75 		i == s.st_size-1 ?
76 			printf("/* 0x%02X */  {  0x%02X, %d  }\n", i, (unsigned char) *(out_tmp+i), sizeof(*(out_tmp+i))) :
77 			printf("/* 0x%02X */  {  0x%02X, %d  },\n", i, (unsigned char) *(out_tmp+i), sizeof(*(out_tmp+i)));
78 	free(in_tmp);
79 	free(out_tmp);
80 	close(fd);
81 	_icv_close(st);
82 }
83 #endif	/* DEBUG */
84 #endif	/* DEBUG_C */
85