2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/2/94";
40 #include <sys/cdefs.h>
41 #include <sys/types.h>
58 prn_normal(const char *s)
65 memset(&mbs, 0, sizeof(mbs));
67 while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
68 if (clen == (size_t)-2) {
72 if (clen == (size_t)-1) {
73 memset(&mbs, 0, sizeof(mbs));
74 putchar((unsigned char)*s);
79 for (i = 0; i < (int)clen; i++)
80 putchar((unsigned char)s[i]);
89 prn_printable(const char *s)
96 memset(&mbs, 0, sizeof(mbs));
98 while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
99 if (clen == (size_t)-1) {
103 memset(&mbs, 0, sizeof(mbs));
106 if (clen == (size_t)-2) {
117 for (i = 0; i < (int)clen; i++)
118 putchar((unsigned char)s[i]);
126 * The fts system makes it difficult to replace fts_name with a different-
127 * sized string, so we just calculate the real length here and do the
128 * conversion in prn_octal()
130 * XXX when using f_octal_escape (-b) rather than f_octal (-B), the
131 * length computed by len_octal may be too big. I just can't be buggered
132 * to fix this as an efficient fix would involve a lookup table. Same goes
133 * for the rather inelegant code in prn_octal.
139 len_octal(const char *s, int len)
145 memset(&mbs, 0, sizeof(mbs));
147 while (len != 0 && (clen = mbrtowc(&wc, s, len, &mbs)) != 0) {
148 if (clen == (size_t)-1) {
152 memset(&mbs, 0, sizeof(mbs));
155 if (clen == (size_t)-2) {
169 prn_octal(const char *s)
171 static const char esc[] = "\\\\\"\"\aa\bb\ff\nn\rr\tt\vv";
177 int goodchar, i, len, prtlen;
179 memset(&mbs, 0, sizeof(mbs));
181 while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
182 goodchar = clen != (size_t)-1 && clen != (size_t)-2;
183 if (goodchar && iswprint(wc) && wc != L'\"' && wc != L'\\') {
184 for (i = 0; i < (int)clen; i++)
185 putchar((unsigned char)s[i]);
187 } else if (goodchar && f_octal_escape &&
191 wc <= (wchar_t)UCHAR_MAX &&
192 (p = strchr(esc, (char)wc)) != NULL) {
199 else if (clen == (size_t)-1)
203 for (i = 0; i < prtlen; i++) {
204 ch = (unsigned char)s[i];
206 putchar('0' + (ch >> 6));
207 putchar('0' + ((ch >> 3) & 7));
208 putchar('0' + (ch & 7));
212 if (clen == (size_t)-2)
214 if (clen == (size_t)-1) {
215 memset(&mbs, 0, sizeof(mbs));
226 (void)fprintf(stderr,
228 "usage: ls [-ABCFGHILPRSTUWZabcdfghiklmnopqrstuvwxy1,] [--color=when] [-D format]"
230 "usage: ls [-ABCFHILPRSTUWZabcdfghiklmnopqrstuvwxy1,] [-D format]"