util.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993, 1994
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Michael Fischbein.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 * 4. 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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35#if 0
36#ifndef lint
37static char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/2/94";
38#endif /* not lint */
39#endif
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: stable/11/bin/ls/util.c 330897 2018-03-14 03:19:51Z eadler $");
42
43#include <sys/types.h>
44#include <sys/stat.h>
45
46#include <ctype.h>
47#include <err.h>
48#include <fts.h>
49#include <limits.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <wchar.h>
54#include <wctype.h>
55#include <libxo/xo.h>
56
57#include "ls.h"
58#include "extern.h"
59
60int
61prn_normal(const char *field, const char *s)
62{
63	char fmt[_POSIX2_LINE_MAX];
64
65	snprintf(fmt, sizeof(fmt), "{:%s/%%hs}", field);
66	return xo_emit(fmt, s);
67#if 0
68	mbstate_t mbs;
69	wchar_t wc;
70	int i, n;
71	size_t clen;
72
73	memset(&mbs, 0, sizeof(mbs));
74	n = 0;
75	while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
76		if (clen == (size_t)-2) {
77			n += printf("%s", s);
78			break;
79		}
80		if (clen == (size_t)-1) {
81			memset(&mbs, 0, sizeof(mbs));
82			putchar((unsigned char)*s);
83			s++;
84			n++;
85			continue;
86		}
87		for (i = 0; i < (int)clen; i++)
88			putchar((unsigned char)s[i]);
89		s += clen;
90		if (iswprint(wc))
91			n += wcwidth(wc);
92	}
93	return (n);
94#endif
95}
96
97char *
98get_printable(const char *s)
99{
100	mbstate_t mbs;
101	wchar_t wc;
102	int i, n;
103	size_t clen;
104	int slen = strlen(s);
105	char *buf = alloca(slen + 1), *bp = buf;
106
107	memset(&mbs, 0, sizeof(mbs));
108	n = 0;
109	while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
110		if (clen == (size_t)-1) {
111			*bp++ = '?';
112			s++;
113			n++;
114			memset(&mbs, 0, sizeof(mbs));
115			continue;
116		}
117		if (clen == (size_t)-2) {
118			*bp++ = '?';
119			n++;
120			break;
121		}
122		if (!iswprint(wc)) {
123			*bp++ = '?';
124			s += clen;
125			n++;
126			continue;
127		}
128		for (i = 0; i < (int)clen; i++)
129			*bp++ = (unsigned char)s[i];
130		s += clen;
131		n += wcwidth(wc);
132	}
133	*bp = '\0';
134	return strdup(buf);
135}
136
137/*
138 * The fts system makes it difficult to replace fts_name with a different-
139 * sized string, so we just calculate the real length here and do the
140 * conversion in prn_octal()
141 *
142 * XXX when using f_octal_escape (-b) rather than f_octal (-B), the
143 * length computed by len_octal may be too big. I just can't be buggered
144 * to fix this as an efficient fix would involve a lookup table. Same goes
145 * for the rather inelegant code in prn_octal.
146 *
147 *						DES 1998/04/23
148 */
149
150size_t
151len_octal(const char *s, int len)
152{
153	mbstate_t mbs;
154	wchar_t wc;
155	size_t clen, r;
156
157	memset(&mbs, 0, sizeof(mbs));
158	r = 0;
159	while (len != 0 && (clen = mbrtowc(&wc, s, len, &mbs)) != 0) {
160		if (clen == (size_t)-1) {
161			r += 4;
162			s++;
163			len--;
164			memset(&mbs, 0, sizeof(mbs));
165			continue;
166		}
167		if (clen == (size_t)-2) {
168			r += 4 * len;
169			break;
170		}
171		if (iswprint(wc))
172			r++;
173		else
174			r += 4 * clen;
175		s += clen;
176	}
177	return (r);
178}
179
180char *
181get_octal(const char *s)
182{
183	static const char esc[] = "\\\\\"\"\aa\bb\ff\nn\rr\tt\vv";
184	const char *p;
185	mbstate_t mbs;
186	wchar_t wc;
187	size_t clen;
188	unsigned char ch;
189	int goodchar, i, len, prtlen;
190	int slen = strlen(s);
191	char *buf = alloca(slen * 4 + 1), *bp = buf;
192
193	memset(&mbs, 0, sizeof(mbs));
194	len = 0;
195	while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
196		goodchar = clen != (size_t)-1 && clen != (size_t)-2;
197		if (goodchar && iswprint(wc) && wc != L'\"' && wc != L'\\') {
198			for (i = 0; i < (int)clen; i++)
199				*bp++ = (unsigned char)s[i];
200			len += wcwidth(wc);
201		} else if (goodchar && f_octal_escape &&
202#if WCHAR_MIN < 0
203                    wc >= 0 &&
204#endif
205		    wc <= (wchar_t)UCHAR_MAX &&
206		    (p = strchr(esc, (char)wc)) != NULL) {
207			*bp ++ = '\\';
208			*bp++ = p[1];
209			len += 2;
210		} else {
211			if (goodchar)
212				prtlen = clen;
213			else if (clen == (size_t)-1)
214				prtlen = 1;
215			else
216				prtlen = strlen(s);
217			for (i = 0; i < prtlen; i++) {
218				ch = (unsigned char)s[i];
219				*bp++ = '\\';
220				*bp++ = '0' + (ch >> 6);
221				*bp++ = '0' + ((ch >> 3) & 7);
222				*bp++ = '0' + (ch & 7);
223				len += 4;
224			}
225		}
226		if (clen == (size_t)-2)
227			break;
228		if (clen == (size_t)-1) {
229			memset(&mbs, 0, sizeof(mbs));
230			s++;
231		} else
232			s += clen;
233	}
234
235	*bp = '\0';
236	return strdup(buf);
237}
238
239void
240usage(void)
241{
242	xo_error(
243#ifdef COLORLS
244	"usage: ls [-ABCFGHILPRSTUWZabcdfghiklmnopqrstuwxy1,] [-D format]"
245#else
246	"usage: ls [-ABCFHILPRSTUWZabcdfghiklmnopqrstuwxy1,] [-D format]"
247#endif
248		      " [file ...]\n");
249	exit(1);
250}
251