xref: /minix3/bin/ls/util.c (revision b7ef8cfb526b29a8298718baea6f7a71e155ac1d)
1*b7ef8cfbSLionel Sambuc /*	$NetBSD: util.c,v 1.34 2011/08/29 14:44:21 joerg Exp $	*/
2*b7ef8cfbSLionel Sambuc 
3*b7ef8cfbSLionel Sambuc /*
4*b7ef8cfbSLionel Sambuc  * Copyright (c) 1989, 1993, 1994
5*b7ef8cfbSLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*b7ef8cfbSLionel Sambuc  *
7*b7ef8cfbSLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*b7ef8cfbSLionel Sambuc  * Michael Fischbein.
9*b7ef8cfbSLionel Sambuc  *
10*b7ef8cfbSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*b7ef8cfbSLionel Sambuc  * modification, are permitted provided that the following conditions
12*b7ef8cfbSLionel Sambuc  * are met:
13*b7ef8cfbSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*b7ef8cfbSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*b7ef8cfbSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*b7ef8cfbSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*b7ef8cfbSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*b7ef8cfbSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*b7ef8cfbSLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*b7ef8cfbSLionel Sambuc  *    without specific prior written permission.
21*b7ef8cfbSLionel Sambuc  *
22*b7ef8cfbSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*b7ef8cfbSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*b7ef8cfbSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*b7ef8cfbSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*b7ef8cfbSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*b7ef8cfbSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*b7ef8cfbSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*b7ef8cfbSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*b7ef8cfbSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*b7ef8cfbSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*b7ef8cfbSLionel Sambuc  * SUCH DAMAGE.
33*b7ef8cfbSLionel Sambuc  */
34*b7ef8cfbSLionel Sambuc 
35*b7ef8cfbSLionel Sambuc #include <sys/cdefs.h>
36*b7ef8cfbSLionel Sambuc #ifndef lint
37*b7ef8cfbSLionel Sambuc #if 0
38*b7ef8cfbSLionel Sambuc static char sccsid[] = "@(#)util.c	8.5 (Berkeley) 4/28/95";
39*b7ef8cfbSLionel Sambuc #else
40*b7ef8cfbSLionel Sambuc __RCSID("$NetBSD: util.c,v 1.34 2011/08/29 14:44:21 joerg Exp $");
41*b7ef8cfbSLionel Sambuc #endif
42*b7ef8cfbSLionel Sambuc #endif /* not lint */
43*b7ef8cfbSLionel Sambuc 
44*b7ef8cfbSLionel Sambuc #include <sys/types.h>
45*b7ef8cfbSLionel Sambuc #include <sys/stat.h>
46*b7ef8cfbSLionel Sambuc 
47*b7ef8cfbSLionel Sambuc #include <err.h>
48*b7ef8cfbSLionel Sambuc #include <fts.h>
49*b7ef8cfbSLionel Sambuc #include <limits.h>
50*b7ef8cfbSLionel Sambuc #include <stdio.h>
51*b7ef8cfbSLionel Sambuc #include <stdlib.h>
52*b7ef8cfbSLionel Sambuc #include <string.h>
53*b7ef8cfbSLionel Sambuc #include <vis.h>
54*b7ef8cfbSLionel Sambuc #include <wchar.h>
55*b7ef8cfbSLionel Sambuc #include <wctype.h>
56*b7ef8cfbSLionel Sambuc 
57*b7ef8cfbSLionel Sambuc #include "ls.h"
58*b7ef8cfbSLionel Sambuc #include "extern.h"
59*b7ef8cfbSLionel Sambuc 
60*b7ef8cfbSLionel Sambuc int
safe_print(const char * src)61*b7ef8cfbSLionel Sambuc safe_print(const char *src)
62*b7ef8cfbSLionel Sambuc {
63*b7ef8cfbSLionel Sambuc 	size_t len;
64*b7ef8cfbSLionel Sambuc 	char *name;
65*b7ef8cfbSLionel Sambuc 	int flags;
66*b7ef8cfbSLionel Sambuc 
67*b7ef8cfbSLionel Sambuc 	flags = VIS_NL | VIS_OCTAL | VIS_WHITE;
68*b7ef8cfbSLionel Sambuc 	if (f_octal_escape)
69*b7ef8cfbSLionel Sambuc 		flags |= VIS_CSTYLE;
70*b7ef8cfbSLionel Sambuc 
71*b7ef8cfbSLionel Sambuc 	len = strlen(src);
72*b7ef8cfbSLionel Sambuc 	if (len != 0 && SIZE_T_MAX/len <= 4) {
73*b7ef8cfbSLionel Sambuc 		errx(EXIT_FAILURE, "%s: name too long", src);
74*b7ef8cfbSLionel Sambuc 		/* NOTREACHED */
75*b7ef8cfbSLionel Sambuc 	}
76*b7ef8cfbSLionel Sambuc 
77*b7ef8cfbSLionel Sambuc 	name = (char *)malloc(4*len+1);
78*b7ef8cfbSLionel Sambuc 	if (name != NULL) {
79*b7ef8cfbSLionel Sambuc 		len = strvis(name, src, flags);
80*b7ef8cfbSLionel Sambuc 		(void)printf("%s", name);
81*b7ef8cfbSLionel Sambuc 		free(name);
82*b7ef8cfbSLionel Sambuc 		return len;
83*b7ef8cfbSLionel Sambuc 	} else
84*b7ef8cfbSLionel Sambuc 		errx(EXIT_FAILURE, "out of memory!");
85*b7ef8cfbSLionel Sambuc 		/* NOTREACHED */
86*b7ef8cfbSLionel Sambuc }
87*b7ef8cfbSLionel Sambuc 
88*b7ef8cfbSLionel Sambuc /*
89*b7ef8cfbSLionel Sambuc  * The reasons why we don't use putwchar(wc) here are:
90*b7ef8cfbSLionel Sambuc  * - If wc == L'\0', we need to restore the initial shift state, but
91*b7ef8cfbSLionel Sambuc  *   the C language standard doesn't say that putwchar(L'\0') does.
92*b7ef8cfbSLionel Sambuc  * - It isn't portable to mix a wide-oriented function (i.e. getwchar)
93*b7ef8cfbSLionel Sambuc  *   with byte-oriented functions (printf et al.) in same FILE.
94*b7ef8cfbSLionel Sambuc  */
95*b7ef8cfbSLionel Sambuc static int
printwc(wchar_t wc,mbstate_t * pst)96*b7ef8cfbSLionel Sambuc printwc(wchar_t wc, mbstate_t *pst)
97*b7ef8cfbSLionel Sambuc {
98*b7ef8cfbSLionel Sambuc 	size_t size;
99*b7ef8cfbSLionel Sambuc 	char buf[MB_LEN_MAX];
100*b7ef8cfbSLionel Sambuc 
101*b7ef8cfbSLionel Sambuc 	size = wcrtomb(buf, wc, pst);
102*b7ef8cfbSLionel Sambuc 	if (size == (size_t)-1) /* This shouldn't happen, but for sure */
103*b7ef8cfbSLionel Sambuc 		return 0;
104*b7ef8cfbSLionel Sambuc 	if (wc == L'\0') {
105*b7ef8cfbSLionel Sambuc 		/* The following condition must be always true, but for sure */
106*b7ef8cfbSLionel Sambuc 		if (size > 0 && buf[size - 1] == '\0')
107*b7ef8cfbSLionel Sambuc 			--size;
108*b7ef8cfbSLionel Sambuc 	}
109*b7ef8cfbSLionel Sambuc 	if (size > 0)
110*b7ef8cfbSLionel Sambuc 		fwrite(buf, 1, size, stdout);
111*b7ef8cfbSLionel Sambuc 	return wc == L'\0' ? 0 : wcwidth(wc);
112*b7ef8cfbSLionel Sambuc }
113*b7ef8cfbSLionel Sambuc 
114*b7ef8cfbSLionel Sambuc int
printescaped(const char * src)115*b7ef8cfbSLionel Sambuc printescaped(const char *src)
116*b7ef8cfbSLionel Sambuc {
117*b7ef8cfbSLionel Sambuc 	int n = 0;
118*b7ef8cfbSLionel Sambuc 	mbstate_t src_state, stdout_state;
119*b7ef8cfbSLionel Sambuc 	/* The following +1 is to pass '\0' at the end of src to mbrtowc(). */
120*b7ef8cfbSLionel Sambuc 	const char *endptr = src + strlen(src) + 1;
121*b7ef8cfbSLionel Sambuc 
122*b7ef8cfbSLionel Sambuc 	/*
123*b7ef8cfbSLionel Sambuc 	 * We have to reset src_state each time in this function, because
124*b7ef8cfbSLionel Sambuc 	 * the codeset of src pathname may not match with current locale.
125*b7ef8cfbSLionel Sambuc 	 * Note that if we pass NULL instead of src_state to mbrtowc(),
126*b7ef8cfbSLionel Sambuc 	 * there is no way to reset the state.
127*b7ef8cfbSLionel Sambuc 	 */
128*b7ef8cfbSLionel Sambuc 	memset(&src_state, 0, sizeof(src_state));
129*b7ef8cfbSLionel Sambuc 	memset(&stdout_state, 0, sizeof(stdout_state));
130*b7ef8cfbSLionel Sambuc 	while (src < endptr) {
131*b7ef8cfbSLionel Sambuc 		wchar_t wc;
132*b7ef8cfbSLionel Sambuc 		size_t rv, span = endptr - src;
133*b7ef8cfbSLionel Sambuc 
134*b7ef8cfbSLionel Sambuc #if 0
135*b7ef8cfbSLionel Sambuc 		/*
136*b7ef8cfbSLionel Sambuc 		 * XXX - we should fix libc instead.
137*b7ef8cfbSLionel Sambuc 		 * Theoretically this should work, but our current
138*b7ef8cfbSLionel Sambuc 		 * implementation of iso2022 module doesn't actually work
139*b7ef8cfbSLionel Sambuc 		 * as expected, if there are redundant escape sequences
140*b7ef8cfbSLionel Sambuc 		 * which exceed 32 bytes.
141*b7ef8cfbSLionel Sambuc 		 */
142*b7ef8cfbSLionel Sambuc 		if (span > MB_CUR_MAX)
143*b7ef8cfbSLionel Sambuc 			span = MB_CUR_MAX;
144*b7ef8cfbSLionel Sambuc #endif
145*b7ef8cfbSLionel Sambuc 		rv = mbrtowc(&wc, src, span, &src_state);
146*b7ef8cfbSLionel Sambuc 		if (rv == 0) { /* assert(wc == L'\0'); */
147*b7ef8cfbSLionel Sambuc 			/* The following may output a shift sequence. */
148*b7ef8cfbSLionel Sambuc 			n += printwc(wc, &stdout_state);
149*b7ef8cfbSLionel Sambuc 			break;
150*b7ef8cfbSLionel Sambuc 		}
151*b7ef8cfbSLionel Sambuc 		if (rv == (size_t)-1) { /* probably errno == EILSEQ */
152*b7ef8cfbSLionel Sambuc 			n += printwc(L'?', &stdout_state);
153*b7ef8cfbSLionel Sambuc 			/* try to skip 1byte, because there is no better way */
154*b7ef8cfbSLionel Sambuc 			src++;
155*b7ef8cfbSLionel Sambuc 			memset(&src_state, 0, sizeof(src_state));
156*b7ef8cfbSLionel Sambuc 		} else if (rv == (size_t)-2) {
157*b7ef8cfbSLionel Sambuc 			if (span < MB_CUR_MAX) { /* incomplete char */
158*b7ef8cfbSLionel Sambuc 				n += printwc(L'?', &stdout_state);
159*b7ef8cfbSLionel Sambuc 				break;
160*b7ef8cfbSLionel Sambuc 			}
161*b7ef8cfbSLionel Sambuc 			src += span; /* a redundant shift sequence? */
162*b7ef8cfbSLionel Sambuc 		} else {
163*b7ef8cfbSLionel Sambuc 			n += printwc(iswprint(wc) ? wc : L'?', &stdout_state);
164*b7ef8cfbSLionel Sambuc 			src += rv;
165*b7ef8cfbSLionel Sambuc 		}
166*b7ef8cfbSLionel Sambuc 	}
167*b7ef8cfbSLionel Sambuc 	return n;
168*b7ef8cfbSLionel Sambuc }
169