Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/ls Implement '-b' option, which, following FreeBSD, Linu...



details:   https://anonhg.NetBSD.org/src/rev/c57b35616abb
branches:  trunk
changeset: 551963:c57b35616abb
user:      jschauma <jschauma%NetBSD.org@localhost>
date:      Sun Sep 14 19:16:05 2003 +0000

description:
Implement '-b' option, which, following FreeBSD, Linux and (I think) Solaris
prints octal escapes for nongraphic characters.

diffstat:

 bin/ls/extern.h |   3 ++-
 bin/ls/ls.1     |   6 ++++--
 bin/ls/ls.c     |  14 +++++++++++---
 bin/ls/ls.h     |   3 ++-
 bin/ls/print.c  |  22 ++++++++++++++--------
 bin/ls/util.c   |  34 ++++++++++++++++++++++++++++++----
 6 files changed, 63 insertions(+), 19 deletions(-)

diffs (255 lines):

diff -r 99358d05d8c5 -r c57b35616abb bin/ls/extern.h
--- a/bin/ls/extern.h   Sun Sep 14 18:41:56 2003 +0000
+++ b/bin/ls/extern.h   Sun Sep 14 19:16:05 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.14 2003/08/07 09:05:14 agc Exp $  */
+/*     $NetBSD: extern.h,v 1.15 2003/09/14 19:16:05 jschauma Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -50,6 +50,7 @@
 void    printlong(DISPLAY *);
 void    printscol(DISPLAY *);
 void    printstream(DISPLAY *);
+int     safe_print(const char *);
 void    usage(void);
 
 #include "stat_flags.h"
diff -r 99358d05d8c5 -r c57b35616abb bin/ls/ls.1
--- a/bin/ls/ls.1       Sun Sep 14 18:41:56 2003 +0000
+++ b/bin/ls/ls.1       Sun Sep 14 19:16:05 2003 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: ls.1,v 1.42 2003/09/02 08:36:20 wiz Exp $
+.\"    $NetBSD: ls.1,v 1.43 2003/09/14 19:16:06 jschauma Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1991, 1993, 1994
 .\"    The Regents of the University of California.  All rights reserved.
@@ -40,7 +40,7 @@
 .Nd list directory contents
 .Sh SYNOPSIS
 .Nm
-.Op Fl AaCcdFfgikLlmnopqRrSsTtuWx1
+.Op Fl AabCcdFfgikLlmnopqRrSsTtuWx1
 .Op Ar
 .Sh DESCRIPTION
 For each operand that names a
@@ -77,6 +77,8 @@
 Include directory entries whose names begin with a
 dot
 .Pq Sq \&. .
+.It Fl b
+Print octal escapes for nongraphic characters.
 .It Fl C
 Force multi-column output; this is the default when output is to a terminal.
 .It Fl c
diff -r 99358d05d8c5 -r c57b35616abb bin/ls/ls.c
--- a/bin/ls/ls.c       Sun Sep 14 18:41:56 2003 +0000
+++ b/bin/ls/ls.c       Sun Sep 14 19:16:05 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ls.c,v 1.50 2003/08/07 09:05:15 agc Exp $      */
+/*     $NetBSD: ls.c,v 1.51 2003/09/14 19:16:06 jschauma Exp $ */
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)ls.c       8.7 (Berkeley) 8/5/94";
 #else
-__RCSID("$NetBSD: ls.c,v 1.50 2003/08/07 09:05:15 agc Exp $");
+__RCSID("$NetBSD: ls.c,v 1.51 2003/09/14 19:16:06 jschauma Exp $");
 #endif
 #endif /* not lint */
 
@@ -86,6 +86,7 @@
 int f_accesstime;              /* use time of last access */
 int f_column;                  /* columnated format */
 int f_columnacross;            /* columnated format, sorted across */
+int f_escape;                  /* print octal escapes for nongraphic characters */
 int f_flags;                   /* show flags associated with a file */
 int f_grouponly;               /* long listing without owner */
 int f_inode;                   /* print inode */
@@ -133,7 +134,7 @@
                f_listdot = 1;
 
        fts_options = FTS_PHYSICAL;
-       while ((ch = getopt(argc, argv, "1ACFLRSTWacdfgiklmnopqrstux")) != -1) {
+       while ((ch = getopt(argc, argv, "1ACFLRSTWabcdfgiklmnopqrstux")) != -1) {
                switch (ch) {
                /*
                 * The -1, -C, -l, -m and -x options all override each other so
@@ -194,6 +195,11 @@
                case 'A':
                        f_listdot = 1;
                        break;
+               /* the -b option turns off the -q option. */
+               case 'b':
+                       f_escape = 1;
+                       f_nonprint = 0;
+                       break;
                /* The -d option turns off the -R option. */
                case 'd':
                        f_listdir = 1;
@@ -218,8 +224,10 @@
                case 'p':
                        f_typedir = 1;
                        break;
+               /* the -q option turns off the -b option. */
                case 'q':
                        f_nonprint = 1;
+                       f_escape = 0;
                        break;
                case 'r':
                        f_reversesort = 1;
diff -r 99358d05d8c5 -r c57b35616abb bin/ls/ls.h
--- a/bin/ls/ls.h       Sun Sep 14 18:41:56 2003 +0000
+++ b/bin/ls/ls.h       Sun Sep 14 19:16:05 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ls.h,v 1.13 2003/08/07 09:05:15 agc Exp $      */
+/*     $NetBSD: ls.h,v 1.14 2003/09/14 19:16:06 jschauma Exp $ */
 
 /*
  * Copyright (c) 1989, 1993
@@ -39,6 +39,7 @@
 extern long blocksize;         /* block size units */
 
 extern int f_accesstime;       /* use time of last access */
+extern int f_escape;           /* print octal escapes for nongraphic characters */
 extern int f_flags;            /* show flags associated with a file */
 extern int f_grouponly;                /* long listing without owner */
 extern int f_inode;            /* print inode */
diff -r 99358d05d8c5 -r c57b35616abb bin/ls/print.c
--- a/bin/ls/print.c    Sun Sep 14 18:41:56 2003 +0000
+++ b/bin/ls/print.c    Sun Sep 14 19:16:05 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: print.c,v 1.35 2003/08/07 09:05:15 agc Exp $   */
+/*     $NetBSD: print.c,v 1.36 2003/09/14 19:16:06 jschauma Exp $      */
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)print.c    8.5 (Berkeley) 7/28/94";
 #else
-__RCSID("$NetBSD: print.c,v 1.35 2003/08/07 09:05:15 agc Exp $");
+__RCSID("$NetBSD: print.c,v 1.36 2003/09/14 19:16:06 jschauma Exp $");
 #endif
 #endif /* not lint */
 
@@ -129,7 +129,9 @@
                        printtime(sp->st_ctime);
                else
                        printtime(sp->st_mtime);
-               if (f_nonprint)
+               if (f_escape)
+                       (void)safe_print(p->fts_name);
+               else if (f_nonprint)
                        (void)printescaped(p->fts_name);
                else
                        (void)printf("%s", p->fts_name);
@@ -295,10 +297,12 @@
        if (f_size)
                chcnt += printf("%*llu ", sizefield,
                    (long long)howmany(sp->st_blocks, blocksize));
-       if (f_nonprint)
-           chcnt += printescaped(p->fts_name);
+       if (f_escape)
+               chcnt += safe_print(p->fts_name);
+       else if (f_nonprint)
+               chcnt += printescaped(p->fts_name);
        else
-           chcnt += printf("%s", p->fts_name);
+               chcnt += printf("%s", p->fts_name);
        if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
                chcnt += printtype(sp->st_mode);
        return (chcnt);
@@ -373,8 +377,10 @@
        }
        path[lnklen] = '\0';
        (void)printf(" -> ");
-       if (f_nonprint)
-               printescaped(path);
+       if (f_escape)
+               (void)safe_print(path);
+       else if (f_nonprint)
+               (void)printescaped(path);
        else
                (void)printf("%s", path);
 }
diff -r 99358d05d8c5 -r c57b35616abb bin/ls/util.c
--- a/bin/ls/util.c     Sun Sep 14 18:41:56 2003 +0000
+++ b/bin/ls/util.c     Sun Sep 14 19:16:05 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.c,v 1.25 2003/09/02 08:36:21 wiz Exp $    */
+/*     $NetBSD: util.c,v 1.26 2003/09/14 19:16:07 jschauma Exp $       */
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)util.c     8.5 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: util.c,v 1.25 2003/09/02 08:36:21 wiz Exp $");
+__RCSID("$NetBSD: util.c,v 1.26 2003/09/14 19:16:07 jschauma Exp $");
 #endif
 #endif /* not lint */
 
@@ -45,15 +45,41 @@
 #include <sys/stat.h>
 
 #include <ctype.h>
+#include <err.h>
 #include <fts.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <vis.h>
 
 #include "ls.h"
 #include "extern.h"
 
 int
+safe_print(const char *src)
+{
+       size_t len;
+       char *name;
+
+       len = strlen(src);
+       if (len != 0 && SIZE_T_MAX/len <= 4) {
+               errx(EXIT_FAILURE, "%s: name too long", src);
+               /* NOTREACHED */
+       }
+
+       name = (char *)malloc(4*len+1);
+       if (name != NULL) {
+               len = strvis(name, src, VIS_NL | VIS_CSTYLE);
+               printf("%s", name);
+               free(name);
+               return len;
+       } else
+               errx(EXIT_FAILURE, "out of memory!");
+               /* NOTREACHED */
+}
+
+int
 printescaped(const char *src)
 {
        unsigned char c;
@@ -64,7 +90,7 @@
                        (void)putchar(c);
                else
                        (void)putchar('?');
-       return (n);
+       return n;
 }
 
 void
@@ -72,7 +98,7 @@
 {
 
        (void)fprintf(stderr,
-           "usage: ls [-AaCcdFfgikLlmnopqRrSsTtuWx1] [file ...]\n");
+           "usage: ls [-AabCcdFfgikLlmnopqRrSsTtuWx1] [file ...]\n");
        exit(EXIT_FAILURE);
        /* NOTREACHED */
 }



Home | Main Index | Thread Index | Old Index