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