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