1178825Sdfr/*- 2233294Sstas * Copyright (c) 1989, 1993, 1994 3233294Sstas * The Regents of the University of California. All rights reserved. 4233294Sstas * 5178825Sdfr * This code is derived from software contributed to Berkeley by 6233294Sstas * Michael Fischbein. 7233294Sstas * 8233294Sstas * Redistribution and use in source and binary forms, with or without 9178825Sdfr * modification, are permitted provided that the following conditions 10233294Sstas * are met: 11233294Sstas * 1. Redistributions of source code must retain the above copyright 12178825Sdfr * notice, this list of conditions and the following disclaimer. 13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 14233294Sstas * notice, this list of conditions and the following disclaimer in the 15233294Sstas * documentation and/or other materials provided with the distribution. 16178825Sdfr * 4. Neither the name of the University nor the names of its contributors 17233294Sstas * may be used to endorse or promote products derived from this software 18233294Sstas * without specific prior written permission. 19233294Sstas * 20178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30233294Sstas * SUCH DAMAGE. 31233294Sstas */ 32178825Sdfr 33178825Sdfr#if 0 34178825Sdfr#ifndef lint 35178825Sdfrstatic char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/2/94"; 36233294Sstas#endif /* not lint */ 37178825Sdfr#endif 38178825Sdfr#include <sys/cdefs.h> 39178825Sdfr__FBSDID("$FreeBSD$"); 40178825Sdfr 41178825Sdfr#include <sys/types.h> 42178825Sdfr#include <sys/stat.h> 43178825Sdfr 44178825Sdfr#include <ctype.h> 45178825Sdfr#include <err.h> 46178825Sdfr#include <fts.h> 47178825Sdfr#include <limits.h> 48178825Sdfr#include <stdio.h> 49178825Sdfr#include <stdlib.h> 50178825Sdfr#include <string.h> 51178825Sdfr#include <wchar.h> 52178825Sdfr#include <wctype.h> 53178825Sdfr 54178825Sdfr#include "ls.h" 55178825Sdfr#include "extern.h" 56178825Sdfr 57178825Sdfrint 58178825Sdfrprn_normal(const char *s) 59178825Sdfr{ 60178825Sdfr mbstate_t mbs; 61178825Sdfr wchar_t wc; 62178825Sdfr int i, n; 63178825Sdfr size_t clen; 64178825Sdfr 65178825Sdfr memset(&mbs, 0, sizeof(mbs)); 66178825Sdfr n = 0; 67178825Sdfr while ((clen = mbrtowc(&wc, s, MB_LEN_MAX, &mbs)) != 0) { 68178825Sdfr if (clen == (size_t)-2) { 69178825Sdfr n += printf("%s", s); 70178825Sdfr break; 71178825Sdfr } 72178825Sdfr if (clen == (size_t)-1) { 73178825Sdfr memset(&mbs, 0, sizeof(mbs)); 74178825Sdfr putchar((unsigned char)*s); 75178825Sdfr s++; 76178825Sdfr n++; 77178825Sdfr continue; 78178825Sdfr } 79178825Sdfr for (i = 0; i < (int)clen; i++) 80178825Sdfr putchar((unsigned char)s[i]); 81178825Sdfr s += clen; 82178825Sdfr if (iswprint(wc)) 83178825Sdfr n += wcwidth(wc); 84178825Sdfr } 85178825Sdfr return (n); 86} 87 88int 89prn_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 138size_t 139len_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 168int 169prn_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 && 188#if WCHAR_MIN < 0 189 wc >= 0 && 190#endif 191 wc <= (wchar_t)UCHAR_MAX && 192 (p = strchr(esc, (char)wc)) != NULL) { 193 putchar('\\'); 194 putchar(p[1]); 195 len += 2; 196 } else { 197 if (goodchar) 198 prtlen = clen; 199 else if (clen == (size_t)-1) 200 prtlen = 1; 201 else 202 prtlen = strlen(s); 203 for (i = 0; i < prtlen; i++) { 204 ch = (unsigned char)s[i]; 205 putchar('\\'); 206 putchar('0' + (ch >> 6)); 207 putchar('0' + ((ch >> 3) & 7)); 208 putchar('0' + (ch & 7)); 209 len += 4; 210 } 211 } 212 if (clen == (size_t)-2) 213 break; 214 if (clen == (size_t)-1) { 215 memset(&mbs, 0, sizeof(mbs)); 216 s++; 217 } else 218 s += clen; 219 } 220 return (len); 221} 222 223void 224usage(void) 225{ 226 (void)fprintf(stderr, 227#ifdef COLORLS 228 "usage: ls [-ABCFGHILPRSTUWZabcdfghiklmnopqrstuwxy1,] [-D format]" 229#else 230 "usage: ls [-ABCFHILPRSTUWZabcdfghiklmnopqrstuwxy1,] [-D format]" 231#endif 232 " [file ...]\n"); 233 exit(1); 234} 235