xref: /netbsd/bin/ls/util.c (revision abbeacd9)
1*abbeacd9Sjoerg /*	$NetBSD: util.c,v 1.34 2011/08/29 14:44:21 joerg Exp $	*/
249f0ad86Scgd 
361f28255Scgd /*
483ede345Smycroft  * Copyright (c) 1989, 1993, 1994
583ede345Smycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Michael Fischbein.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
35b22592e8Schristos #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
3749f0ad86Scgd #if 0
38d943cdadSjtc static char sccsid[] = "@(#)util.c	8.5 (Berkeley) 4/28/95";
3949f0ad86Scgd #else
40*abbeacd9Sjoerg __RCSID("$NetBSD: util.c,v 1.34 2011/08/29 14:44:21 joerg Exp $");
4149f0ad86Scgd #endif
4261f28255Scgd #endif /* not lint */
4361f28255Scgd 
4461f28255Scgd #include <sys/types.h>
45203e4227Smycroft #include <sys/stat.h>
4683ede345Smycroft 
472ccef82cSjschauma #include <err.h>
48203e4227Smycroft #include <fts.h>
492ccef82cSjschauma #include <limits.h>
5083ede345Smycroft #include <stdio.h>
5183ede345Smycroft #include <stdlib.h>
52203e4227Smycroft #include <string.h>
532ccef82cSjschauma #include <vis.h>
548c64f063Swiz #include <wchar.h>
558c64f063Swiz #include <wctype.h>
5683ede345Smycroft 
57203e4227Smycroft #include "ls.h"
58203e4227Smycroft #include "extern.h"
5961f28255Scgd 
60b23df5beSassar int
safe_print(const char * src)612ccef82cSjschauma safe_print(const char *src)
622ccef82cSjschauma {
632ccef82cSjschauma 	size_t len;
642ccef82cSjschauma 	char *name;
6521ab6335Sjschauma 	int flags;
6621ab6335Sjschauma 
67b3389957Sjschauma 	flags = VIS_NL | VIS_OCTAL | VIS_WHITE;
6821ab6335Sjschauma 	if (f_octal_escape)
6921ab6335Sjschauma 		flags |= VIS_CSTYLE;
702ccef82cSjschauma 
712ccef82cSjschauma 	len = strlen(src);
722ccef82cSjschauma 	if (len != 0 && SIZE_T_MAX/len <= 4) {
732ccef82cSjschauma 		errx(EXIT_FAILURE, "%s: name too long", src);
742ccef82cSjschauma 		/* NOTREACHED */
752ccef82cSjschauma 	}
762ccef82cSjschauma 
772ccef82cSjschauma 	name = (char *)malloc(4*len+1);
782ccef82cSjschauma 	if (name != NULL) {
7921ab6335Sjschauma 		len = strvis(name, src, flags);
8078509e7fSchristos 		(void)printf("%s", name);
812ccef82cSjschauma 		free(name);
822ccef82cSjschauma 		return len;
832ccef82cSjschauma 	} else
842ccef82cSjschauma 		errx(EXIT_FAILURE, "out of memory!");
852ccef82cSjschauma 		/* NOTREACHED */
862ccef82cSjschauma }
872ccef82cSjschauma 
888c64f063Swiz /*
898c64f063Swiz  * The reasons why we don't use putwchar(wc) here are:
908c64f063Swiz  * - If wc == L'\0', we need to restore the initial shift state, but
918c64f063Swiz  *   the C language standard doesn't say that putwchar(L'\0') does.
928c64f063Swiz  * - It isn't portable to mix a wide-oriented function (i.e. getwchar)
938c64f063Swiz  *   with byte-oriented functions (printf et al.) in same FILE.
948c64f063Swiz  */
958c64f063Swiz static int
printwc(wchar_t wc,mbstate_t * pst)968c64f063Swiz printwc(wchar_t wc, mbstate_t *pst)
978c64f063Swiz {
988c64f063Swiz 	size_t size;
998c64f063Swiz 	char buf[MB_LEN_MAX];
1008c64f063Swiz 
1018c64f063Swiz 	size = wcrtomb(buf, wc, pst);
1028c64f063Swiz 	if (size == (size_t)-1) /* This shouldn't happen, but for sure */
1038c64f063Swiz 		return 0;
1048c64f063Swiz 	if (wc == L'\0') {
1058c64f063Swiz 		/* The following condition must be always true, but for sure */
1068c64f063Swiz 		if (size > 0 && buf[size - 1] == '\0')
1078c64f063Swiz 			--size;
1088c64f063Swiz 	}
1098c64f063Swiz 	if (size > 0)
1108c64f063Swiz 		fwrite(buf, 1, size, stdout);
1118c64f063Swiz 	return wc == L'\0' ? 0 : wcwidth(wc);
1128c64f063Swiz }
1138c64f063Swiz 
1142ccef82cSjschauma int
printescaped(const char * src)115ba2e04dcSlukem printescaped(const char *src)
11661f28255Scgd {
1178c64f063Swiz 	int n = 0;
1188c64f063Swiz 	mbstate_t src_state, stdout_state;
1198c64f063Swiz 	/* The following +1 is to pass '\0' at the end of src to mbrtowc(). */
1208c64f063Swiz 	const char *endptr = src + strlen(src) + 1;
12161f28255Scgd 
1228c64f063Swiz 	/*
1238c64f063Swiz 	 * We have to reset src_state each time in this function, because
1248c64f063Swiz 	 * the codeset of src pathname may not match with current locale.
1258c64f063Swiz 	 * Note that if we pass NULL instead of src_state to mbrtowc(),
1268c64f063Swiz 	 * there is no way to reset the state.
1278c64f063Swiz 	 */
1288c64f063Swiz 	memset(&src_state, 0, sizeof(src_state));
1298c64f063Swiz 	memset(&stdout_state, 0, sizeof(stdout_state));
1308c64f063Swiz 	while (src < endptr) {
1318c64f063Swiz 		wchar_t wc;
1328c64f063Swiz 		size_t rv, span = endptr - src;
1338c64f063Swiz 
1348c64f063Swiz #if 0
1357dd0dd9eSsoda 		/*
1367dd0dd9eSsoda 		 * XXX - we should fix libc instead.
1377dd0dd9eSsoda 		 * Theoretically this should work, but our current
1387dd0dd9eSsoda 		 * implementation of iso2022 module doesn't actually work
1397dd0dd9eSsoda 		 * as expected, if there are redundant escape sequences
1407dd0dd9eSsoda 		 * which exceed 32 bytes.
1418c64f063Swiz 		 */
1428c64f063Swiz 		if (span > MB_CUR_MAX)
1438c64f063Swiz 			span = MB_CUR_MAX;
1448c64f063Swiz #endif
1458c64f063Swiz 		rv = mbrtowc(&wc, src, span, &src_state);
1468c64f063Swiz 		if (rv == 0) { /* assert(wc == L'\0'); */
1478c64f063Swiz 			/* The following may output a shift sequence. */
1488c64f063Swiz 			n += printwc(wc, &stdout_state);
1498c64f063Swiz 			break;
1508c64f063Swiz 		}
1518c64f063Swiz 		if (rv == (size_t)-1) { /* probably errno == EILSEQ */
1528c64f063Swiz 			n += printwc(L'?', &stdout_state);
1538c64f063Swiz 			/* try to skip 1byte, because there is no better way */
1548c64f063Swiz 			src++;
1558c64f063Swiz 			memset(&src_state, 0, sizeof(src_state));
1568c64f063Swiz 		} else if (rv == (size_t)-2) {
1578c64f063Swiz 			if (span < MB_CUR_MAX) { /* incomplete char */
1588c64f063Swiz 				n += printwc(L'?', &stdout_state);
1598c64f063Swiz 				break;
1608c64f063Swiz 			}
1618c64f063Swiz 			src += span; /* a redundant shift sequence? */
1628c64f063Swiz 		} else {
1638c64f063Swiz 			n += printwc(iswprint(wc) ? wc : L'?', &stdout_state);
1648c64f063Swiz 			src += rv;
1658c64f063Swiz 		}
1668c64f063Swiz 	}
1672ccef82cSjschauma 	return n;
16861f28255Scgd }
169