util.c revision 61324
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 61324 2000-06-06 07:29:43Z ache $"; 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{ 9035441Sache int r = 0; 9135373Sdes 9235373Sdes while (len--) 9335440Sache if (isprint((unsigned char)*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 { 10635426Sdes if (isprint(ch) && (ch != '\"') && (ch != '\\')) 10735426Sdes putchar(ch), len++; 10835417Sdes else if (f_octal_escape) { 10935417Sdes putchar('\\'); 11035417Sdes switch (ch) { 11135417Sdes case '\\': 11235417Sdes putchar('\\'); 11335417Sdes break; 11435417Sdes case '\"': 11535417Sdes putchar('"'); 11635417Sdes break; 11735417Sdes case '\a': 11835417Sdes putchar('a'); 11935417Sdes break; 12035417Sdes case '\b': 12135417Sdes putchar('b'); 12235417Sdes break; 12335417Sdes case '\f': 12435417Sdes putchar('f'); 12535417Sdes break; 12635417Sdes case '\n': 12735417Sdes putchar('n'); 12835417Sdes break; 12935417Sdes case '\r': 13035417Sdes putchar('r'); 13135417Sdes break; 13235417Sdes case '\t': 13335417Sdes putchar('t'); 13435417Sdes break; 13535417Sdes case '\v': 13635417Sdes putchar('v'); 13735417Sdes break; 13835417Sdes default: 13935417Sdes putchar('0' + (ch >> 6)); 14040300Sdes putchar('0' + ((ch >> 3) & 7)); 14140300Sdes putchar('0' + (ch & 7)); 14235417Sdes len += 2; 14335417Sdes break; 14435417Sdes } 14535417Sdes len += 2; 14635417Sdes } 14735417Sdes else { 14835417Sdes putchar('\\'); 14935417Sdes putchar('0' + (ch >> 6)); 15040300Sdes putchar('0' + ((ch >> 3) & 7)); 15140300Sdes putchar('0' + (ch & 7)); 15235417Sdes len += 4; 15335417Sdes } 15435373Sdes } 15535373Sdes return len; 15635373Sdes} 15735373Sdes 1581556Srgrimesvoid 1591556Srgrimesusage() 1601556Srgrimes{ 16161324Sache (void)fprintf(stderr, 16261324Sache#ifdef COLORLS 16361324Sache "usage: ls [-ACFGHLPRTWacdfgiklnoqrstu1]" 16461324Sache#else 16561324Sache "usage: ls [-ACFHLPRTWacdfgiklnoqrstu1]" 16661324Sache#endif 16735426Sdes " [file ...]\n"); 1681556Srgrimes exit(1); 1691556Srgrimes} 170