util.c revision 99109
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 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if 0
38#ifndef lint
39static char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/2/94";
40#endif /* not lint */
41#endif
42#include <sys/types.h>
43__FBSDID("$FreeBSD: head/bin/ls/util.c 99109 2002-06-30 05:13:54Z obrien $");
44
45#include <sys/types.h>
46#include <sys/stat.h>
47
48#include <ctype.h>
49#include <err.h>
50#include <fts.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54
55#include "ls.h"
56#include "extern.h"
57
58int
59prn_printable(const char *s)
60{
61	char c;
62	int n;
63
64	for (n = 0; (c = *s) != '\0'; ++s, ++n)
65		if (isprint((unsigned char)c))
66			putchar(c);
67		else
68			putchar('?');
69	return n;
70}
71
72/*
73 * The fts system makes it difficult to replace fts_name with a different-
74 * sized string, so we just calculate the real length here and do the
75 * conversion in prn_octal()
76 *
77 * XXX when using f_octal_escape (-b) rather than f_octal (-B), the
78 * length computed by len_octal may be too big. I just can't be buggered
79 * to fix this as an efficient fix would involve a lookup table. Same goes
80 * for the rather inelegant code in prn_octal.
81 *
82 *                                              DES 1998/04/23
83 */
84
85size_t
86len_octal(const char *s, int len)
87{
88	size_t r = 0;
89
90	while (len--)
91		if (isprint((unsigned const char)*s++)) r++; else r += 4;
92	return r;
93}
94
95int
96prn_octal(const char *s)
97{
98        unsigned char ch;
99	int len = 0;
100
101        while ((ch = (unsigned char)*s++)) {
102	        if (isprint(ch) && (ch != '\"') && (ch != '\\'))
103		        putchar(ch), len++;
104	        else if (f_octal_escape) {
105	                putchar('\\');
106		        switch (ch) {
107			case '\\':
108			        putchar('\\');
109				break;
110			case '\"':
111			        putchar('"');
112				break;
113			case '\a':
114			        putchar('a');
115				break;
116			case '\b':
117			        putchar('b');
118				break;
119			case '\f':
120			        putchar('f');
121				break;
122			case '\n':
123			        putchar('n');
124				break;
125			case '\r':
126			        putchar('r');
127				break;
128			case '\t':
129			        putchar('t');
130				break;
131			case '\v':
132			        putchar('v');
133				break;
134 		        default:
135		                putchar('0' + (ch >> 6));
136		                putchar('0' + ((ch >> 3) & 7));
137		                putchar('0' + (ch & 7));
138		                len += 2;
139			        break;
140		        }
141		        len += 2;
142	        }
143		else {
144			putchar('\\');
145	                putchar('0' + (ch >> 6));
146	                putchar('0' + ((ch >> 3) & 7));
147	                putchar('0' + (ch & 7));
148	                len += 4;
149		}
150	}
151	return len;
152}
153
154void
155usage(void)
156{
157	(void)fprintf(stderr,
158#ifdef COLORLS
159	"usage: ls [-ABCFGHLPRTWZabcdfghiklnoqrstu1]"
160#else
161	"usage: ls [-ABCFHLPRTWZabcdfghiklnoqrstu1]"
162#endif
163		      " [file ...]\n");
164	exit(1);
165}
166