util.c revision 61324
1237263Snp/*
2237263Snp * Copyright (c) 1989, 1993, 1994
3237263Snp *	The Regents of the University of California.  All rights reserved.
4237263Snp *
5237263Snp * This code is derived from software contributed to Berkeley by
6237263Snp * Michael Fischbein.
7237263Snp *
8237263Snp * Redistribution and use in source and binary forms, with or without
9237263Snp * modification, are permitted provided that the following conditions
10237263Snp * are met:
11237263Snp * 1. Redistributions of source code must retain the above copyright
12237263Snp *    notice, this list of conditions and the following disclaimer.
13237263Snp * 2. Redistributions in binary form must reproduce the above copyright
14237263Snp *    notice, this list of conditions and the following disclaimer in the
15237263Snp *    documentation and/or other materials provided with the distribution.
16237263Snp * 3. All advertising materials mentioning features or use of this software
17237263Snp *    must display the following acknowledgement:
18237263Snp *	This product includes software developed by the University of
19237263Snp *	California, Berkeley and its contributors.
20237263Snp * 4. Neither the name of the University nor the names of its contributors
21237263Snp *    may be used to endorse or promote products derived from this software
22237263Snp *    without specific prior written permission.
23237263Snp *
24237263Snp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25237263Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26237263Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27237263Snp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28237263Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29237263Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30237263Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31237263Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32245468Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33237263Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34237263Snp * SUCH DAMAGE.
35237263Snp */
36237263Snp
37237263Snp#ifndef lint
38237263Snp#if 0
39237263Snpstatic char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/2/94";
40237263Snp#else
41237263Snpstatic const char rcsid[] =
42237263Snp  "$FreeBSD: head/bin/ls/util.c 61324 2000-06-06 07:29:43Z ache $";
43237263Snp#endif
44237263Snp#endif /* not lint */
45237263Snp
46237263Snp#include <sys/types.h>
47237263Snp#include <sys/stat.h>
48237263Snp
49237263Snp#include <ctype.h>
50237263Snp#include <err.h>
51237263Snp#include <fts.h>
52237263Snp#include <stdio.h>
53237263Snp#include <stdlib.h>
54245468Snp#include <string.h>
55245468Snp
56239544Snp#include "ls.h"
57237263Snp#include "extern.h"
58237263Snp
59237263Snpvoid
60237263Snpprcopy(src, dest, len)
61237263Snp	char *src, *dest;
62237263Snp	int len;
63237263Snp{
64237263Snp	unsigned char ch;
65237263Snp
66237263Snp	while (len--) {
67237263Snp		ch = *src++;
68237263Snp		*dest++ = isprint(ch) ? ch : '?';
69245276Snp	}
70245276Snp}
71245276Snp
72237263Snp/*
73237263Snp * The fts system makes it difficult to replace fts_name with a different-
74237263Snp * sized string, so we just calculate the real length here and do the
75237263Snp * conversion in prn_octal()
76237263Snp *
77237263Snp * XXX when using f_octal_escape (-b) rather than f_octal (-B), the
78237263Snp * length computed by len_octal may be too big. I just can't be buggered
79237263Snp * to fix this as an efficient fix would involve a lookup table. Same goes
80237263Snp * for the rather inelegant code in prn_octal.
81237263Snp *
82237263Snp *                                              DES 1998/04/23
83237263Snp */
84237263Snp
85237263Snpint
86237263Snplen_octal(s, len)
87237263Snp        char *s;
88245276Snp	int len;
89237263Snp{
90237263Snp	int r = 0;
91245276Snp
92245276Snp	while (len--)
93237263Snp		if (isprint((unsigned char)*s++)) r++; else r += 4;
94245276Snp	return r;
95245276Snp}
96245276Snp
97245276Snpint
98245276Snpprn_octal(s)
99245276Snp        char *s;
100245276Snp{
101245276Snp        unsigned char ch;
102245276Snp	int len = 0;
103245276Snp
104245276Snp        while ((ch = *s++))
105237263Snp	{
106245276Snp	        if (isprint(ch) && (ch != '\"') && (ch != '\\'))
107245276Snp		        putchar(ch), len++;
108245276Snp	        else if (f_octal_escape) {
109245276Snp	                putchar('\\');
110237263Snp		        switch (ch) {
111245276Snp			case '\\':
112245276Snp			        putchar('\\');
113245276Snp				break;
114245276Snp			case '\"':
115245276Snp			        putchar('"');
116245276Snp				break;
117245276Snp			case '\a':
118245276Snp			        putchar('a');
119245276Snp				break;
120245276Snp			case '\b':
121245276Snp			        putchar('b');
122245276Snp				break;
123245276Snp			case '\f':
124245276Snp			        putchar('f');
125245276Snp				break;
126245276Snp			case '\n':
127245276Snp			        putchar('n');
128251518Snp				break;
129245276Snp			case '\r':
130245276Snp			        putchar('r');
131245276Snp				break;
132245276Snp			case '\t':
133245276Snp			        putchar('t');
134245276Snp				break;
135245276Snp			case '\v':
136245276Snp			        putchar('v');
137245276Snp				break;
138245276Snp 		        default:
139245276Snp		                putchar('0' + (ch >> 6));
140245276Snp		                putchar('0' + ((ch >> 3) & 7));
141245276Snp		                putchar('0' + (ch & 7));
142245276Snp		                len += 2;
143245276Snp			        break;
144237263Snp		        }
145245276Snp		        len += 2;
146245276Snp	        }
147245276Snp		else {
148245276Snp			putchar('\\');
149245276Snp	                putchar('0' + (ch >> 6));
150245276Snp	                putchar('0' + ((ch >> 3) & 7));
151237263Snp	                putchar('0' + (ch & 7));
152245276Snp	                len += 4;
153245276Snp		}
154245276Snp	}
155245276Snp	return len;
156237263Snp}
157237263Snp
158245276Snpvoid
159237263Snpusage()
160237263Snp{
161237263Snp	(void)fprintf(stderr,
162237263Snp#ifdef COLORLS
163245276Snp	"usage: ls [-ACFGHLPRTWacdfgiklnoqrstu1]"
164237263Snp#else
165237263Snp	"usage: ls [-ACFHLPRTWacdfgiklnoqrstu1]"
166237263Snp#endif
167245276Snp		      " [file ...]\n");
168237263Snp	exit(1);
169237263Snp}
170245276Snp