util.c revision 35417
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[] = 4235417Sdes "$Id: util.c,v 1.12 1998/04/21 22:02:01 des Exp $"; 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 591556Srgrimesvoid 601556Srgrimesprcopy(src, dest, len) 611556Srgrimes char *src, *dest; 621556Srgrimes int len; 631556Srgrimes{ 6414952Sache unsigned char ch; 651556Srgrimes 661556Srgrimes while (len--) { 673459Sache ch = *src++; 681556Srgrimes *dest++ = isprint(ch) ? ch : '?'; 691556Srgrimes } 701556Srgrimes} 711556Srgrimes 7235373Sdes/* 7335373Sdes * The fts system makes it difficult to replace fts_name with a different- 7435373Sdes * sized string, so we just calculate the real length here and do the 7535373Sdes * conversion in prn_octal() 7635417Sdes * 7735417Sdes * XXX when using f_octal_escape (-b) rather than f_octal (-B), the 7835417Sdes * length computed by len_octal may be too big. I just can't be buggered 7935417Sdes * to fix this as an efficient fix would involve a lookup table. Same goes 8035417Sdes * for the rather inelegant code in prn_octal. 8135417Sdes * 8235417Sdes * DES 1998/04/23 8335373Sdes */ 8435417Sdes 8535373Sdesint 8635373Sdeslen_octal(s, len) 8735373Sdes char *s; 8835373Sdes int len; 8935373Sdes{ 9035373Sdes int r; 9135373Sdes 9235373Sdes while (len--) 9335417Sdes if (isprint(*s++)) r++; else r += 4; 9435373Sdes return r; 9535373Sdes} 9635373Sdes 9735373Sdesint 9835373Sdesprn_octal(s) 9935373Sdes char *s; 10035373Sdes{ 10135373Sdes unsigned char ch; 10235373Sdes int len = 0; 10335373Sdes 10435373Sdes while ((ch = *s++)) 10535373Sdes { 10635417Sdes if (isprint(ch)) putchar(ch), len++; 10735417Sdes else if (f_octal_escape) { 10835417Sdes putchar('\\'); 10935417Sdes switch (ch) { 11035417Sdes case 0: 11135417Sdes putchar('0'); 11235417Sdes break; 11335417Sdes case '\\': 11435417Sdes putchar('\\'); 11535417Sdes break; 11635417Sdes case '\?': 11735417Sdes putchar('?'); 11835417Sdes break; 11935417Sdes case '\'': 12035417Sdes putchar('\''); 12135417Sdes break; 12235417Sdes case '\"': 12335417Sdes putchar('"'); 12435417Sdes break; 12535417Sdes case '\a': 12635417Sdes putchar('a'); 12735417Sdes break; 12835417Sdes case '\b': 12935417Sdes putchar('b'); 13035417Sdes break; 13135417Sdes case '\f': 13235417Sdes putchar('f'); 13335417Sdes break; 13435417Sdes case '\n': 13535417Sdes putchar('n'); 13635417Sdes break; 13735417Sdes case '\r': 13835417Sdes putchar('r'); 13935417Sdes break; 14035417Sdes case '\t': 14135417Sdes putchar('t'); 14235417Sdes break; 14335417Sdes case '\v': 14435417Sdes putchar('v'); 14535417Sdes break; 14635417Sdes default: 14735417Sdes putchar('0' + (ch >> 6)); 14835417Sdes putchar('0' + ((ch >> 3) & 3)); 14935417Sdes putchar('0' + (ch & 3)); 15035417Sdes len += 2; 15135417Sdes break; 15235417Sdes } 15335417Sdes len += 2; 15435417Sdes } 15535417Sdes else { 15635417Sdes putchar('\\'); 15735417Sdes putchar('0' + (ch >> 6)); 15835417Sdes putchar('0' + ((ch >> 3) & 3)); 15935417Sdes putchar('0' + (ch & 3)); 16035417Sdes len += 4; 16135417Sdes } 16235373Sdes } 16335373Sdes return len; 16435373Sdes} 16535373Sdes 1661556Srgrimesvoid 1671556Srgrimesusage() 1681556Srgrimes{ 16920417Ssteve#ifdef BSD4_4_LITE 1701556Srgrimes (void)fprintf(stderr, "usage: ls [-1ACFLRTacdfiklqrstu] [file ...]\n"); 17120417Ssteve#else 17224301Sobrien (void)fprintf(stderr, "usage: ls [-ACFLRTWacdfgikloqrstu1] [file ...]\n"); 17320417Ssteve#endif 1741556Srgrimes exit(1); 1751556Srgrimes} 176