xref: /freebsd/bin/ls/util.c (revision 9052855a15171a46c21abfbc8ec54081a64092cd)
14b88c807SRodney W. Grimes /*
24b88c807SRodney W. Grimes  * Copyright (c) 1989, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Michael Fischbein.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
38febad2fcSSteve Price #if 0
39febad2fcSSteve Price static char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/2/94";
40febad2fcSSteve Price #else
41d46c1a60SSteve Price static const char rcsid[] =
422a456239SPeter Wemm   "$FreeBSD$";
43febad2fcSSteve Price #endif
444b88c807SRodney W. Grimes #endif /* not lint */
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes #include <sys/types.h>
474b88c807SRodney W. Grimes #include <sys/stat.h>
484b88c807SRodney W. Grimes 
494b88c807SRodney W. Grimes #include <ctype.h>
507ea30648SDag-Erling Smørgrav #include <err.h>
514b88c807SRodney W. Grimes #include <fts.h>
524b88c807SRodney W. Grimes #include <stdio.h>
534b88c807SRodney W. Grimes #include <stdlib.h>
544b88c807SRodney W. Grimes #include <string.h>
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes #include "ls.h"
574b88c807SRodney W. Grimes #include "extern.h"
584b88c807SRodney W. Grimes 
59ee579ffbSAssar Westerlund int
6046251ddeSWarner Losh prn_printable(const char *s)
614b88c807SRodney W. Grimes {
629052855aSMark Murray 	char c;
63ee579ffbSAssar Westerlund 	int n;
644b88c807SRodney W. Grimes 
65ee579ffbSAssar Westerlund 	for (n = 0; (c = *s) != '\0'; ++s, ++n)
66ee579ffbSAssar Westerlund 		if (isprint(c))
67ee579ffbSAssar Westerlund 			putchar(c);
68ee579ffbSAssar Westerlund 		else
69ee579ffbSAssar Westerlund 			putchar('?');
70ee579ffbSAssar Westerlund 	return n;
714b88c807SRodney W. Grimes }
724b88c807SRodney W. Grimes 
737ea30648SDag-Erling Smørgrav /*
747ea30648SDag-Erling Smørgrav  * The fts system makes it difficult to replace fts_name with a different-
757ea30648SDag-Erling Smørgrav  * sized string, so we just calculate the real length here and do the
767ea30648SDag-Erling Smørgrav  * conversion in prn_octal()
770d86878cSDag-Erling Smørgrav  *
780d86878cSDag-Erling Smørgrav  * XXX when using f_octal_escape (-b) rather than f_octal (-B), the
790d86878cSDag-Erling Smørgrav  * length computed by len_octal may be too big. I just can't be buggered
800d86878cSDag-Erling Smørgrav  * to fix this as an efficient fix would involve a lookup table. Same goes
810d86878cSDag-Erling Smørgrav  * for the rather inelegant code in prn_octal.
820d86878cSDag-Erling Smørgrav  *
830d86878cSDag-Erling Smørgrav  *                                              DES 1998/04/23
847ea30648SDag-Erling Smørgrav  */
850d86878cSDag-Erling Smørgrav 
869052855aSMark Murray size_t
8746251ddeSWarner Losh len_octal(const char *s, int len)
887ea30648SDag-Erling Smørgrav {
899052855aSMark Murray 	size_t r = 0;
907ea30648SDag-Erling Smørgrav 
917ea30648SDag-Erling Smørgrav 	while (len--)
929052855aSMark Murray 		if (isprint((unsigned const char)*s++)) r++; else r += 4;
937ea30648SDag-Erling Smørgrav 	return r;
947ea30648SDag-Erling Smørgrav }
957ea30648SDag-Erling Smørgrav 
967ea30648SDag-Erling Smørgrav int
9746251ddeSWarner Losh prn_octal(const char *s)
987ea30648SDag-Erling Smørgrav {
997ea30648SDag-Erling Smørgrav         unsigned char ch;
1007ea30648SDag-Erling Smørgrav 	int len = 0;
1017ea30648SDag-Erling Smørgrav 
1029052855aSMark Murray         while ((ch = (unsigned char)*s++)) {
1033a34dbf7SDag-Erling Smørgrav 	        if (isprint(ch) && (ch != '\"') && (ch != '\\'))
1043a34dbf7SDag-Erling Smørgrav 		        putchar(ch), len++;
1050d86878cSDag-Erling Smørgrav 	        else if (f_octal_escape) {
1060d86878cSDag-Erling Smørgrav 	                putchar('\\');
1070d86878cSDag-Erling Smørgrav 		        switch (ch) {
1080d86878cSDag-Erling Smørgrav 			case '\\':
1090d86878cSDag-Erling Smørgrav 			        putchar('\\');
1100d86878cSDag-Erling Smørgrav 				break;
1110d86878cSDag-Erling Smørgrav 			case '\"':
1120d86878cSDag-Erling Smørgrav 			        putchar('"');
1130d86878cSDag-Erling Smørgrav 				break;
1140d86878cSDag-Erling Smørgrav 			case '\a':
1150d86878cSDag-Erling Smørgrav 			        putchar('a');
1160d86878cSDag-Erling Smørgrav 				break;
1170d86878cSDag-Erling Smørgrav 			case '\b':
1180d86878cSDag-Erling Smørgrav 			        putchar('b');
1190d86878cSDag-Erling Smørgrav 				break;
1200d86878cSDag-Erling Smørgrav 			case '\f':
1210d86878cSDag-Erling Smørgrav 			        putchar('f');
1220d86878cSDag-Erling Smørgrav 				break;
1230d86878cSDag-Erling Smørgrav 			case '\n':
1240d86878cSDag-Erling Smørgrav 			        putchar('n');
1250d86878cSDag-Erling Smørgrav 				break;
1260d86878cSDag-Erling Smørgrav 			case '\r':
1270d86878cSDag-Erling Smørgrav 			        putchar('r');
1280d86878cSDag-Erling Smørgrav 				break;
1290d86878cSDag-Erling Smørgrav 			case '\t':
1300d86878cSDag-Erling Smørgrav 			        putchar('t');
1310d86878cSDag-Erling Smørgrav 				break;
1320d86878cSDag-Erling Smørgrav 			case '\v':
1330d86878cSDag-Erling Smørgrav 			        putchar('v');
1340d86878cSDag-Erling Smørgrav 				break;
1350d86878cSDag-Erling Smørgrav  		        default:
1360d86878cSDag-Erling Smørgrav 		                putchar('0' + (ch >> 6));
1370ffc35a4SDag-Erling Smørgrav 		                putchar('0' + ((ch >> 3) & 7));
1380ffc35a4SDag-Erling Smørgrav 		                putchar('0' + (ch & 7));
1390d86878cSDag-Erling Smørgrav 		                len += 2;
1400d86878cSDag-Erling Smørgrav 			        break;
1410d86878cSDag-Erling Smørgrav 		        }
1420d86878cSDag-Erling Smørgrav 		        len += 2;
1430d86878cSDag-Erling Smørgrav 	        }
1447ea30648SDag-Erling Smørgrav 		else {
1457ea30648SDag-Erling Smørgrav 			putchar('\\');
1467ea30648SDag-Erling Smørgrav 	                putchar('0' + (ch >> 6));
1470ffc35a4SDag-Erling Smørgrav 	                putchar('0' + ((ch >> 3) & 7));
1480ffc35a4SDag-Erling Smørgrav 	                putchar('0' + (ch & 7));
1497ea30648SDag-Erling Smørgrav 	                len += 4;
1507ea30648SDag-Erling Smørgrav 		}
1517ea30648SDag-Erling Smørgrav 	}
1527ea30648SDag-Erling Smørgrav 	return len;
1537ea30648SDag-Erling Smørgrav }
1547ea30648SDag-Erling Smørgrav 
1554b88c807SRodney W. Grimes void
15646251ddeSWarner Losh usage(void)
1574b88c807SRodney W. Grimes {
158a04eaf5bSAndrey A. Chernov 	(void)fprintf(stderr,
159a04eaf5bSAndrey A. Chernov #ifdef COLORLS
1600e8d1551SJosef Karthauser 	"usage: ls [-ABCFGHLPRTWZabcdfghiklnoqrstu1]"
161a04eaf5bSAndrey A. Chernov #else
1620e8d1551SJosef Karthauser 	"usage: ls [-ABCFHLPRTWZabcdfghiklnoqrstu1]"
163a04eaf5bSAndrey A. Chernov #endif
1643a34dbf7SDag-Erling Smørgrav 		      " [file ...]\n");
1654b88c807SRodney W. Grimes 	exit(1);
1664b88c807SRodney W. Grimes }
167