[3899a537] | 1 | /* $NetBSD: util.c,v 1.28 2005/06/17 14:36:16 hira Exp $ */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (c) 1989, 1993, 1994 |
---|
| 5 | * The Regents of the University of California. All rights reserved. |
---|
| 6 | * |
---|
| 7 | * This code is derived from software contributed to Berkeley by |
---|
| 8 | * Michael Fischbein. |
---|
| 9 | * |
---|
| 10 | * Redistribution and use in source and binary forms, with or without |
---|
| 11 | * modification, are permitted provided that the following conditions |
---|
| 12 | * are met: |
---|
| 13 | * 1. Redistributions of source code must retain the above copyright |
---|
| 14 | * notice, this list of conditions and the following disclaimer. |
---|
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 16 | * notice, this list of conditions and the following disclaimer in the |
---|
| 17 | * documentation and/or other materials provided with the distribution. |
---|
| 18 | * 3. Neither the name of the University nor the names of its contributors |
---|
| 19 | * may be used to endorse or promote products derived from this software |
---|
| 20 | * without specific prior written permission. |
---|
| 21 | * |
---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 32 | * SUCH DAMAGE. |
---|
| 33 | */ |
---|
| 34 | |
---|
[575babc] | 35 | #ifdef HAVE_CONFIG_H |
---|
| 36 | #include "config.h" |
---|
| 37 | #endif |
---|
| 38 | |
---|
[3899a537] | 39 | #if 0 |
---|
| 40 | #include <sys/cdefs.h> |
---|
| 41 | #ifndef lint |
---|
| 42 | #if 0 |
---|
| 43 | static char sccsid[] = "@(#)util.c 8.5 (Berkeley) 4/28/95"; |
---|
| 44 | #else |
---|
| 45 | __RCSID("$NetBSD: util.c,v 1.28 2005/06/17 14:36:16 hira Exp $"); |
---|
| 46 | #endif |
---|
| 47 | #endif /* not lint */ |
---|
| 48 | #endif |
---|
| 49 | |
---|
| 50 | #include <sys/types.h> |
---|
| 51 | #include <sys/stat.h> |
---|
| 52 | |
---|
| 53 | #include <ctype.h> |
---|
[6cdaa85] | 54 | #include "err.h" |
---|
| 55 | #include "fts.h" |
---|
[3899a537] | 56 | #include <limits.h> |
---|
| 57 | #include <stdio.h> |
---|
| 58 | #include <stdlib.h> |
---|
| 59 | #include <string.h> |
---|
[6cdaa85] | 60 | #include "vis.h" |
---|
[3899a537] | 61 | |
---|
| 62 | #include "extern-ls.h" |
---|
| 63 | |
---|
| 64 | #define SIZE_T_MAX 255 |
---|
| 65 | |
---|
| 66 | int |
---|
| 67 | safe_print(rtems_shell_ls_globals* globals, const char *src) |
---|
| 68 | { |
---|
| 69 | size_t len; |
---|
| 70 | char *name; |
---|
| 71 | int flags; |
---|
| 72 | |
---|
| 73 | flags = VIS_NL | VIS_OCTAL; |
---|
| 74 | if (f_octal_escape) |
---|
| 75 | flags |= VIS_CSTYLE; |
---|
| 76 | |
---|
| 77 | len = strlen(src); |
---|
| 78 | if (len != 0 && SIZE_T_MAX/len <= 4) { |
---|
| 79 | errx(exit_jump, EXIT_FAILURE, "%s: name too long", src); |
---|
| 80 | /* NOTREACHED */ |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | name = (char *)malloc(4*len+1); |
---|
| 84 | if (name != NULL) { |
---|
| 85 | len = strvis(name, src, flags); |
---|
| 86 | printf("%s", name); |
---|
| 87 | free(name); |
---|
| 88 | return len; |
---|
| 89 | } else |
---|
| 90 | errx(exit_jump, EXIT_FAILURE, "out of memory!"); |
---|
| 91 | /* NOTREACHED */ |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | int |
---|
[f97536d] | 95 | printescaped(rtems_shell_ls_globals* globals RTEMS_UNUSED, const char *src) |
---|
[3899a537] | 96 | { |
---|
| 97 | unsigned char c; |
---|
| 98 | int n; |
---|
| 99 | |
---|
| 100 | for (n = 0; (c = *src) != '\0'; ++src, ++n) |
---|
| 101 | if (isprint(c)) |
---|
| 102 | (void)putchar(c); |
---|
| 103 | else |
---|
| 104 | (void)putchar('?'); |
---|
| 105 | return n; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void |
---|
| 109 | usage(rtems_shell_ls_globals* globals) |
---|
| 110 | { |
---|
| 111 | |
---|
| 112 | (void)fprintf(stderr, |
---|
| 113 | "usage: %s [-AaBbCcdFfghikLlmnopqRrSsTtuWwx1] [file ...]\n", |
---|
| 114 | "ls"); |
---|
| 115 | exit(EXIT_FAILURE); |
---|
| 116 | /* NOTREACHED */ |
---|
| 117 | } |
---|