]> git.saurik.com Git - apple/file_cmds.git/blob - ls/util.c
de79f13c6802c0ca94825edf3f4546c1a79a775a
[apple/file_cmds.git] / ls / util.c
1 /*-
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Michael Fischbein.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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 * 4. 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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
33 #if 0
34 #ifndef lint
35 static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/2/94";
36 #endif /* not lint */
37 #endif
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/bin/ls/util.c,v 1.38 2005/06/03 11:05:58 dd Exp $");
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43
44 #include <ctype.h>
45 #include <err.h>
46 #include <fts.h>
47 #include <limits.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <wchar.h>
52 #include <wctype.h>
53
54 #include "ls.h"
55 #include "extern.h"
56
57 int
58 prn_normal(const char *s)
59 {
60 mbstate_t mbs;
61 wchar_t wc;
62 int i, n;
63 size_t clen;
64
65 memset(&mbs, 0, sizeof(mbs));
66 n = 0;
67 while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
68 if (clen == (size_t)-2) {
69 n += printf("%s", s);
70 break;
71 }
72 if (clen == (size_t)-1) {
73 memset(&mbs, 0, sizeof(mbs));
74 putchar((unsigned char)*s);
75 s++;
76 n++;
77 continue;
78 }
79 for (i = 0; i < (int)clen; i++)
80 putchar((unsigned char)s[i]);
81 s += clen;
82 if (iswprint(wc))
83 n += wcwidth(wc);
84 }
85 return (n);
86 }
87
88 int
89 prn_printable(const char *s)
90 {
91 mbstate_t mbs;
92 wchar_t wc;
93 int i, n;
94 size_t clen;
95
96 memset(&mbs, 0, sizeof(mbs));
97 n = 0;
98 while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) {
99 if (clen == (size_t)-1) {
100 putchar('?');
101 s++;
102 n++;
103 memset(&mbs, 0, sizeof(mbs));
104 continue;
105 }
106 if (clen == (size_t)-2) {
107 putchar('?');
108 n++;
109 break;
110 }
111 if (!iswprint(wc)) {
112 putchar('?');
113 s += clen;
114 n++;
115 continue;
116 }
117 for (i = 0; i < (int)clen; i++)
118 putchar((unsigned char)s[i]);
119 s += clen;
120 n += wcwidth(wc);
121 }
122 return (n);
123 }
124
125 /*
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()
129 *
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.
134 *
135 * DES 1998/04/23
136 */
137
138 size_t
139 len_octal(const char *s, int len)
140 {
141 mbstate_t mbs;
142 wchar_t wc;
143 size_t clen, r;
144
145 memset(&mbs, 0, sizeof(mbs));
146 r = 0;
147 while (len != 0 && (clen = mbrtowc(&wc, s, len, &mbs)) != 0) {
148 if (clen == (size_t)-1) {
149 r += 4;
150 s++;
151 len--;
152 memset(&mbs, 0, sizeof(mbs));
153 continue;
154 }
155 if (clen == (size_t)-2) {
156 r += 4 * len;
157 break;
158 }
159 if (iswprint(wc))
160 r++;
161 else
162 r += 4 * clen;
163 s += clen;
164 }
165 return (r);
166 }
167
168 int
169 prn_octal(const char *s)
170 {
171 static const char esc[] = "\\\\\"\"\aa\bb\ff\nn\rr\tt\vv";
172 const char *p;
173 mbstate_t mbs;
174 wchar_t wc;
175 size_t clen;
176 unsigned char ch;
177 int goodchar, i, len, prtlen;
178
179 memset(&mbs, 0, sizeof(mbs));
180 len = 0;
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]);
186 len += wcwidth(wc);
187 } else if (goodchar && f_octal_escape && wc >= 0 &&
188 wc <= (wchar_t)UCHAR_MAX &&
189 (p = strchr(esc, (char)wc)) != NULL) {
190 putchar('\\');
191 putchar(p[1]);
192 len += 2;
193 } else {
194 if (goodchar)
195 prtlen = clen;
196 else if (clen == (size_t)-1)
197 prtlen = 1;
198 else
199 prtlen = strlen(s);
200 for (i = 0; i < prtlen; i++) {
201 ch = (unsigned char)s[i];
202 putchar('\\');
203 putchar('0' + (ch >> 6));
204 putchar('0' + ((ch >> 3) & 7));
205 putchar('0' + (ch & 7));
206 len += 4;
207 }
208 }
209 if (clen == (size_t)-2)
210 break;
211 if (clen == (size_t)-1) {
212 memset(&mbs, 0, sizeof(mbs));
213 s++;
214 } else
215 s += clen;
216 }
217 return (len);
218 }
219
220 void
221 usage(void)
222 {
223 (void)fprintf(stderr,
224 #ifdef COLORLS
225 "usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1]"
226 #else
227 "usage: ls [-ABCFHLOPRSTUWabcdefghiklmnopqrstuwx1]"
228 #endif
229 " [file ...]\n");
230 exit(1);
231 }