2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)util.c 8.3 (Berkeley) 4/2/94
33 * $FreeBSD: src/bin/ls/util.c,v 1.35 2004/05/03 11:48:55 tjr Exp $
36 #include <sys/types.h>
53 prn_normal(const char *s
)
60 memset(&mbs
, 0, sizeof(mbs
));
62 while ((clen
= mbrtowc(&wc
, s
, MB_LEN_MAX
, &mbs
)) != 0) {
63 if (clen
== (size_t)-2) {
67 if (clen
== (size_t)-1) {
68 memset(&mbs
, 0, sizeof(mbs
));
69 putchar((unsigned char)*s
);
74 for (i
= 0; i
< (int)clen
; i
++)
75 putchar((unsigned char)s
[i
]);
84 prn_printable(const char *s
)
91 memset(&mbs
, 0, sizeof(mbs
));
93 while ((clen
= mbrtowc(&wc
, s
, MB_LEN_MAX
, &mbs
)) != 0) {
94 if (clen
== (size_t)-1) {
98 memset(&mbs
, 0, sizeof(mbs
));
101 if (clen
== (size_t)-2) {
112 for (i
= 0; i
< (int)clen
; i
++)
113 putchar((unsigned char)s
[i
]);
121 * The fts system makes it difficult to replace fts_name with a different-
122 * sized string, so we just calculate the real length here and do the
123 * conversion in prn_octal()
125 * XXX when using f_octal_escape (-b) rather than f_octal (-B), the
126 * length computed by len_octal may be too big. I just can't be buggered
127 * to fix this as an efficient fix would involve a lookup table. Same goes
128 * for the rather inelegant code in prn_octal.
134 len_octal(const char *s
, int len
)
140 memset(&mbs
, 0, sizeof(mbs
));
142 while (len
!= 0 && (clen
= mbrtowc(&wc
, s
, len
, &mbs
)) != 0) {
143 if (clen
== (size_t)-1) {
147 memset(&mbs
, 0, sizeof(mbs
));
150 if (clen
== (size_t)-2) {
164 prn_octal(const char *s
)
166 static const char esc
[] = "\\\\\"\"\aa\bb\ff\nn\rr\tt\vv";
172 int goodchar
, i
, len
, prtlen
;
174 memset(&mbs
, 0, sizeof(mbs
));
176 while ((clen
= mbrtowc(&wc
, s
, MB_LEN_MAX
, &mbs
)) != 0) {
177 goodchar
= clen
!= (size_t)-1 && clen
!= (size_t)-2;
178 if (goodchar
&& iswprint(wc
) && wc
!= L
'\"' && wc
!= L
'\\') {
179 for (i
= 0; i
< (int)clen
; i
++)
180 putchar((unsigned char)s
[i
]);
182 } else if (goodchar
&& f_octal_escape
&& wc
>= 0 &&
183 wc
<= (wchar_t)UCHAR_MAX
&&
184 (p
= strchr(esc
, (char)wc
)) != NULL
) {
191 else if (clen
== (size_t)-1)
195 for (i
= 0; i
< prtlen
; i
++) {
196 ch
= (unsigned char)s
[i
];
198 putchar('0' + (ch
>> 6));
199 putchar('0' + ((ch
>> 3) & 7));
200 putchar('0' + (ch
& 7));
204 if (clen
== (size_t)-2)
206 if (clen
== (size_t)-1) {
207 memset(&mbs
, 0, sizeof(mbs
));
220 "usage: ls [-1ABCFGHILPRSTW_abcdfghiklmnopqrstuwxy] [-D format]"
222 "usage: ls [-1ABCFHILPRSTW_abcdfghiklmnopqrstuwxy] [-D format]"