Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*  $NetBSD: util.c,v 1.28 2005/06/17 14:36:16 hira Exp $   */
0002 
0003 /*
0004  * Copyright (c) 1989, 1993, 1994
0005  *  The Regents of the University of California.  All rights reserved.
0006  *
0007  * This code is derived from software contributed to Berkeley by
0008  * Michael Fischbein.
0009  *
0010  * Redistribution and use in source and binary forms, with or without
0011  * modification, are permitted provided that the following conditions
0012  * are met:
0013  * 1. Redistributions of source code must retain the above copyright
0014  *    notice, this list of conditions and the following disclaimer.
0015  * 2. Redistributions in binary form must reproduce the above copyright
0016  *    notice, this list of conditions and the following disclaimer in the
0017  *    documentation and/or other materials provided with the distribution.
0018  * 3. Neither the name of the University nor the names of its contributors
0019  *    may be used to endorse or promote products derived from this software
0020  *    without specific prior written permission.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0032  * SUCH DAMAGE.
0033  */
0034 
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038 
0039 #if 0
0040 #include <sys/cdefs.h>
0041 #ifndef lint
0042 #if 0
0043 static char sccsid[] = "@(#)util.c  8.5 (Berkeley) 4/28/95";
0044 #else
0045 __RCSID("$NetBSD: util.c,v 1.28 2005/06/17 14:36:16 hira Exp $");
0046 #endif
0047 #endif /* not lint */
0048 #endif
0049 
0050 #include <sys/types.h>
0051 #include <sys/stat.h>
0052 
0053 #include <ctype.h>
0054 #include "err.h"
0055 #include "fts.h"
0056 #include <limits.h>
0057 #include <stdio.h>
0058 #include <stdlib.h>
0059 #include <string.h>
0060 #include "vis.h"
0061 
0062 #include "extern-ls.h"
0063 
0064 #define SIZE_T_MAX 255
0065 
0066 int
0067 safe_print(rtems_shell_ls_globals* globals, const char *src)
0068 {
0069     size_t len;
0070     char *name;
0071     int flags;
0072 
0073     flags = VIS_NL | VIS_OCTAL;
0074     if (f_octal_escape)
0075         flags |= VIS_CSTYLE;
0076 
0077     len = strlen(src);
0078     if (len != 0 && SIZE_T_MAX/len <= 4) {
0079         errx(exit_jump, EXIT_FAILURE, "%s: name too long", src);
0080         /* NOTREACHED */
0081     }
0082 
0083     name = (char *)malloc(4*len+1);
0084     if (name != NULL) {
0085         len = strvis(name, src, flags);
0086         printf("%s", name);
0087         free(name);
0088         return len;
0089     } else
0090         errx(exit_jump, EXIT_FAILURE, "out of memory!");
0091         /* NOTREACHED */
0092 }
0093 
0094 int
0095 printescaped(rtems_shell_ls_globals* globals RTEMS_UNUSED, const char *src)
0096 {
0097     unsigned char c;
0098     int n;
0099 
0100     for (n = 0; (c = *src) != '\0'; ++src, ++n)
0101         if (isprint(c))
0102             (void)putchar(c);
0103         else
0104             (void)putchar('?');
0105     return n;
0106 }
0107 
0108 void
0109 usage(rtems_shell_ls_globals* globals)
0110 {
0111 
0112     (void)fprintf(stderr,
0113         "usage: %s [-AaBbCcdFfghikLlmnopqRrSsTtuWwx1] [file ...]\n",
0114         "ls");
0115     exit(EXIT_FAILURE);
0116     /* NOTREACHED */
0117 }