1*78509e7fSchristos /* $NetBSD: util.c,v 1.30 2006/12/14 14:15:26 christos Exp $ */ 249f0ad86Scgd 361f28255Scgd /* 483ede345Smycroft * Copyright (c) 1989, 1993, 1994 583ede345Smycroft * The Regents of the University of California. All rights reserved. 661f28255Scgd * 761f28255Scgd * This code is derived from software contributed to Berkeley by 861f28255Scgd * Michael Fischbein. 961f28255Scgd * 1061f28255Scgd * Redistribution and use in source and binary forms, with or without 1161f28255Scgd * modification, are permitted provided that the following conditions 1261f28255Scgd * are met: 1361f28255Scgd * 1. Redistributions of source code must retain the above copyright 1461f28255Scgd * notice, this list of conditions and the following disclaimer. 1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright 1661f28255Scgd * notice, this list of conditions and the following disclaimer in the 1761f28255Scgd * documentation and/or other materials provided with the distribution. 18b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors 1961f28255Scgd * may be used to endorse or promote products derived from this software 2061f28255Scgd * without specific prior written permission. 2161f28255Scgd * 2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3261f28255Scgd * SUCH DAMAGE. 3361f28255Scgd */ 3461f28255Scgd 35b22592e8Schristos #include <sys/cdefs.h> 3661f28255Scgd #ifndef lint 3749f0ad86Scgd #if 0 38d943cdadSjtc static char sccsid[] = "@(#)util.c 8.5 (Berkeley) 4/28/95"; 3949f0ad86Scgd #else 40*78509e7fSchristos __RCSID("$NetBSD: util.c,v 1.30 2006/12/14 14:15:26 christos Exp $"); 4149f0ad86Scgd #endif 4261f28255Scgd #endif /* not lint */ 4361f28255Scgd 4461f28255Scgd #include <sys/types.h> 45203e4227Smycroft #include <sys/stat.h> 4683ede345Smycroft 4761f28255Scgd #include <ctype.h> 482ccef82cSjschauma #include <err.h> 49203e4227Smycroft #include <fts.h> 502ccef82cSjschauma #include <limits.h> 5183ede345Smycroft #include <stdio.h> 5283ede345Smycroft #include <stdlib.h> 53203e4227Smycroft #include <string.h> 542ccef82cSjschauma #include <vis.h> 5583ede345Smycroft 56203e4227Smycroft #include "ls.h" 57203e4227Smycroft #include "extern.h" 5861f28255Scgd 59b23df5beSassar int 602ccef82cSjschauma safe_print(const char *src) 612ccef82cSjschauma { 622ccef82cSjschauma size_t len; 632ccef82cSjschauma char *name; 6421ab6335Sjschauma int flags; 6521ab6335Sjschauma 66b3389957Sjschauma flags = VIS_NL | VIS_OCTAL | VIS_WHITE; 6721ab6335Sjschauma if (f_octal_escape) 6821ab6335Sjschauma flags |= VIS_CSTYLE; 692ccef82cSjschauma 702ccef82cSjschauma len = strlen(src); 712ccef82cSjschauma if (len != 0 && SIZE_T_MAX/len <= 4) { 722ccef82cSjschauma errx(EXIT_FAILURE, "%s: name too long", src); 732ccef82cSjschauma /* NOTREACHED */ 742ccef82cSjschauma } 752ccef82cSjschauma 762ccef82cSjschauma name = (char *)malloc(4*len+1); 772ccef82cSjschauma if (name != NULL) { 7821ab6335Sjschauma len = strvis(name, src, flags); 79*78509e7fSchristos (void)printf("%s", name); 802ccef82cSjschauma free(name); 812ccef82cSjschauma return len; 822ccef82cSjschauma } else 832ccef82cSjschauma errx(EXIT_FAILURE, "out of memory!"); 842ccef82cSjschauma /* NOTREACHED */ 852ccef82cSjschauma } 862ccef82cSjschauma 872ccef82cSjschauma int 88ba2e04dcSlukem printescaped(const char *src) 8961f28255Scgd { 90b23df5beSassar unsigned char c; 91b23df5beSassar int n; 9261f28255Scgd 93b23df5beSassar for (n = 0; (c = *src) != '\0'; ++src, ++n) 94c5a80669Sassar if (isprint(c)) 95c5a80669Sassar (void)putchar(c); 96c5a80669Sassar else 97c5a80669Sassar (void)putchar('?'); 982ccef82cSjschauma return n; 9961f28255Scgd } 10061f28255Scgd 101203e4227Smycroft void 102ba2e04dcSlukem usage(void) 10361f28255Scgd { 10454a944c3Senami 105d943cdadSjtc (void)fprintf(stderr, 106a2ed3bbeShira "usage: %s [-AaBbCcdFfghikLlmnopqRrSsTtuWwx1] [file ...]\n", 107a2ed3bbeShira getprogname()); 108b7443b0fSkleink exit(EXIT_FAILURE); 1099dc385beSmycroft /* NOTREACHED */ 11061f28255Scgd } 111