util.c revision 62597
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1989, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Michael Fischbein.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3827967Ssteve#if 0
3927967Sstevestatic char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/2/94";
4027967Ssteve#else
4127958Sstevestatic const char rcsid[] =
4250471Speter  "$FreeBSD: head/bin/ls/util.c 62597 2000-07-04 23:09:23Z assar $";
4327967Ssteve#endif
441556Srgrimes#endif /* not lint */
451556Srgrimes
461556Srgrimes#include <sys/types.h>
471556Srgrimes#include <sys/stat.h>
481556Srgrimes
491556Srgrimes#include <ctype.h>
5035373Sdes#include <err.h>
511556Srgrimes#include <fts.h>
521556Srgrimes#include <stdio.h>
531556Srgrimes#include <stdlib.h>
541556Srgrimes#include <string.h>
551556Srgrimes
561556Srgrimes#include "ls.h"
571556Srgrimes#include "extern.h"
581556Srgrimes
5962597Sassarint
6062597Sassarprn_printable(s)
6162597Sassar	const char *s;
621556Srgrimes{
6362597Sassar	unsigned char c;
6462597Sassar	int n;
651556Srgrimes
6662597Sassar	for (n = 0; (c = *s) != '\0'; ++s, ++n)
6762597Sassar		if (isprint(c))
6862597Sassar			putchar(c);
6962597Sassar		else
7062597Sassar			putchar('?');
7162597Sassar	return n;
721556Srgrimes}
731556Srgrimes
7435373Sdes/*
7535373Sdes * The fts system makes it difficult to replace fts_name with a different-
7635373Sdes * sized string, so we just calculate the real length here and do the
7735373Sdes * conversion in prn_octal()
7835417Sdes *
7935417Sdes * XXX when using f_octal_escape (-b) rather than f_octal (-B), the
8035417Sdes * length computed by len_octal may be too big. I just can't be buggered
8135417Sdes * to fix this as an efficient fix would involve a lookup table. Same goes
8235417Sdes * for the rather inelegant code in prn_octal.
8335417Sdes *
8435417Sdes *                                              DES 1998/04/23
8535373Sdes */
8635417Sdes
8735373Sdesint
8835373Sdeslen_octal(s, len)
8962597Sassar        const char *s;
9035373Sdes	int len;
9135373Sdes{
9235441Sache	int r = 0;
9335373Sdes
9435373Sdes	while (len--)
9535440Sache		if (isprint((unsigned char)*s++)) r++; else r += 4;
9635373Sdes	return r;
9735373Sdes}
9835373Sdes
9935373Sdesint
10035373Sdesprn_octal(s)
10162597Sassar        const char *s;
10235373Sdes{
10335373Sdes        unsigned char ch;
10435373Sdes	int len = 0;
10535373Sdes
10635373Sdes        while ((ch = *s++))
10735373Sdes	{
10835426Sdes	        if (isprint(ch) && (ch != '\"') && (ch != '\\'))
10935426Sdes		        putchar(ch), len++;
11035417Sdes	        else if (f_octal_escape) {
11135417Sdes	                putchar('\\');
11235417Sdes		        switch (ch) {
11335417Sdes			case '\\':
11435417Sdes			        putchar('\\');
11535417Sdes				break;
11635417Sdes			case '\"':
11735417Sdes			        putchar('"');
11835417Sdes				break;
11935417Sdes			case '\a':
12035417Sdes			        putchar('a');
12135417Sdes				break;
12235417Sdes			case '\b':
12335417Sdes			        putchar('b');
12435417Sdes				break;
12535417Sdes			case '\f':
12635417Sdes			        putchar('f');
12735417Sdes				break;
12835417Sdes			case '\n':
12935417Sdes			        putchar('n');
13035417Sdes				break;
13135417Sdes			case '\r':
13235417Sdes			        putchar('r');
13335417Sdes				break;
13435417Sdes			case '\t':
13535417Sdes			        putchar('t');
13635417Sdes				break;
13735417Sdes			case '\v':
13835417Sdes			        putchar('v');
13935417Sdes				break;
14035417Sdes 		        default:
14135417Sdes		                putchar('0' + (ch >> 6));
14240300Sdes		                putchar('0' + ((ch >> 3) & 7));
14340300Sdes		                putchar('0' + (ch & 7));
14435417Sdes		                len += 2;
14535417Sdes			        break;
14635417Sdes		        }
14735417Sdes		        len += 2;
14835417Sdes	        }
14935417Sdes		else {
15035417Sdes			putchar('\\');
15135417Sdes	                putchar('0' + (ch >> 6));
15240300Sdes	                putchar('0' + ((ch >> 3) & 7));
15340300Sdes	                putchar('0' + (ch & 7));
15435417Sdes	                len += 4;
15535417Sdes		}
15635373Sdes	}
15735373Sdes	return len;
15835373Sdes}
15935373Sdes
1601556Srgrimesvoid
1611556Srgrimesusage()
1621556Srgrimes{
16361324Sache	(void)fprintf(stderr,
16461324Sache#ifdef COLORLS
16561324Sache	"usage: ls [-ACFGHLPRTWacdfgiklnoqrstu1]"
16661324Sache#else
16761324Sache	"usage: ls [-ACFHLPRTWacdfgiklnoqrstu1]"
16861324Sache#endif
16935426Sdes		      " [file ...]\n");
1701556Srgrimes	exit(1);
1711556Srgrimes}
172