xref: /freebsd/tools/test/iconv/const-gnuism.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
1*bcf205e1SGabor Kovesdan /*-
2*bcf205e1SGabor Kovesdan  * Copyright (C) 2010 Gabor Kovesdan <gabor@FreeBSD.org>
3*bcf205e1SGabor Kovesdan  * All rights reserved.
4*bcf205e1SGabor Kovesdan  *
5*bcf205e1SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
6*bcf205e1SGabor Kovesdan  * modification, are permitted provided that the following conditions
7*bcf205e1SGabor Kovesdan  * are met:
8*bcf205e1SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
9*bcf205e1SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
10*bcf205e1SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
11*bcf205e1SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
12*bcf205e1SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
13*bcf205e1SGabor Kovesdan  *
14*bcf205e1SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*bcf205e1SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*bcf205e1SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*bcf205e1SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*bcf205e1SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*bcf205e1SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*bcf205e1SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*bcf205e1SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*bcf205e1SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*bcf205e1SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*bcf205e1SGabor Kovesdan  * SUCH DAMAGE.
25*bcf205e1SGabor Kovesdan  */
26*bcf205e1SGabor Kovesdan 
27*bcf205e1SGabor Kovesdan #include <sys/types.h>
28*bcf205e1SGabor Kovesdan 
29*bcf205e1SGabor Kovesdan #include <err.h>
30*bcf205e1SGabor Kovesdan #include <iconv.h>
31*bcf205e1SGabor Kovesdan #include <stdlib.h>
32*bcf205e1SGabor Kovesdan #include <string.h>
33*bcf205e1SGabor Kovesdan 
34*bcf205e1SGabor Kovesdan int
main(void)35*bcf205e1SGabor Kovesdan main(void)
36*bcf205e1SGabor Kovesdan {
37*bcf205e1SGabor Kovesdan 	iconv_t cd;
38*bcf205e1SGabor Kovesdan 	size_t inbytes, outbytes;
39*bcf205e1SGabor Kovesdan 	char *str1 = "FOOBAR";
40*bcf205e1SGabor Kovesdan 	const char *str2 = "FOOBAR";
41*bcf205e1SGabor Kovesdan 	char ** in1;
42*bcf205e1SGabor Kovesdan 	const char ** in2 = &str2;
43*bcf205e1SGabor Kovesdan 	char *out1, *out2;
44*bcf205e1SGabor Kovesdan 
45*bcf205e1SGabor Kovesdan 	inbytes = outbytes = strlen("FOOBAR");
46*bcf205e1SGabor Kovesdan 
47*bcf205e1SGabor Kovesdan 	if ((cd = iconv_open("UTF-8", "ASCII")) == (iconv_t)-1)
48*bcf205e1SGabor Kovesdan 		err(1, NULL);
49*bcf205e1SGabor Kovesdan 
50*bcf205e1SGabor Kovesdan 	if ((out2 = malloc(inbytes)) == NULL)
51*bcf205e1SGabor Kovesdan 		err(1, NULL);
52*bcf205e1SGabor Kovesdan 
53*bcf205e1SGabor Kovesdan 	if (iconv(cd, in2, &inbytes, &out2, &outbytes) == -1)
54*bcf205e1SGabor Kovesdan 		err(1, NULL);
55*bcf205e1SGabor Kovesdan 
56*bcf205e1SGabor Kovesdan 	in1 = &str1;
57*bcf205e1SGabor Kovesdan 	inbytes = outbytes = strlen("FOOBAR");
58*bcf205e1SGabor Kovesdan 
59*bcf205e1SGabor Kovesdan 	if ((out1 = malloc(inbytes)) == NULL)
60*bcf205e1SGabor Kovesdan 		err(1, NULL);
61*bcf205e1SGabor Kovesdan 
62*bcf205e1SGabor Kovesdan 	if (iconv(cd, in1, &inbytes, &out1, &outbytes) == -1)
63*bcf205e1SGabor Kovesdan 		err(1, NULL);
64*bcf205e1SGabor Kovesdan 
65*bcf205e1SGabor Kovesdan 	return (EXIT_SUCCESS);
66*bcf205e1SGabor Kovesdan 
67*bcf205e1SGabor Kovesdan }
68