Source-Changes-HG archive

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

[src/trunk]: src/bin/ls After last weeks addition of the '-b' flag, add '-B' ...



details:   https://anonhg.NetBSD.org/src/rev/92ccc2533b7e
branches:  trunk
changeset: 552241:92ccc2533b7e
user:      jschauma <jschauma%NetBSD.org@localhost>
date:      Mon Sep 22 02:43:19 2003 +0000

description:
After last weeks addition of the '-b' flag, add '-B' and '-w' flags,
suggested by uwe@, inspired by FreeBSD.  The three flags override
each other (and the '-q' flag) and behave as follows:

     -B      Force printing of non-printable characters in file names as
             \xxx, where xxx is the numeric value of the character in octal.

     -b      As -B, but use C escape codes whenever possible.

     -w      Force raw printing of non-printable characters.  This is the
             default when output is not to a terminal.

diffstat:

 bin/ls/ls.1    |  23 ++++++++++++++++++++---
 bin/ls/ls.c    |  31 +++++++++++++++++++++++--------
 bin/ls/ls.h    |   5 +++--
 bin/ls/print.c |  10 +++++-----
 bin/ls/util.c  |  13 +++++++++----
 5 files changed, 60 insertions(+), 22 deletions(-)

diffs (266 lines):

diff -r c699335f9b36 -r 92ccc2533b7e bin/ls/ls.1
--- a/bin/ls/ls.1       Mon Sep 22 02:42:02 2003 +0000
+++ b/bin/ls/ls.1       Mon Sep 22 02:43:19 2003 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: ls.1,v 1.44 2003/09/14 21:56:25 wiz Exp $
+.\"    $NetBSD: ls.1,v 1.45 2003/09/22 02:43:19 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 AabCcdFfgikLlmnopqRrSsTtuWx1
+.Op Fl AaBbCcdFfgikLlmnopqRrSsTtuWwx1
 .Op Ar
 .Sh DESCRIPTION
 For each operand that names a
@@ -77,8 +77,13 @@
 Include directory entries whose names begin with a
 dot
 .Pq Sq \&. .
+.It Fl B
+Force printing of non-printable characters in file names as \\xxx, where xxx
+is the numeric value of the character in octal.
 .It Fl b
-Print octal escapes for nongraphic characters.
+As
+.Fl B ,
+but use C escape codes whenever possible.
 .It Fl C
 Force multi-column output; this is the default when output is to a terminal.
 .It Fl c
@@ -184,6 +189,9 @@
 .Pq Fl t
 or printing
 .Pq Fl l .
+.It Fl w
+Force raw printing of non-printable characters.
+This is the default when output is not to a terminal.
 .It Fl W
 Display whiteouts when scanning directories.
 .It Fl x
@@ -196,6 +204,15 @@
 .El
 .Pp
 The
+.Fl B ,
+.Fl b ,
+.Fl w
+and
+.Fl q
+options all override each other; the last one specified determines
+the format used for non-printable characters.
+.Pp
+The
 .Fl 1 ,
 .Fl C ,
 .Fl l ,
diff -r c699335f9b36 -r 92ccc2533b7e bin/ls/ls.c
--- a/bin/ls/ls.c       Mon Sep 22 02:42:02 2003 +0000
+++ b/bin/ls/ls.c       Mon Sep 22 02:43:19 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ls.c,v 1.51 2003/09/14 19:16:06 jschauma Exp $ */
+/*     $NetBSD: ls.c,v 1.52 2003/09/22 02:43:20 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.51 2003/09/14 19:16:06 jschauma Exp $");
+__RCSID("$NetBSD: ls.c,v 1.52 2003/09/22 02:43:20 jschauma Exp $");
 #endif
 #endif /* not lint */
 
@@ -86,7 +86,6 @@
 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 */
@@ -96,6 +95,8 @@
 int f_nonprint;                        /* show unprintables as ? */
 int f_nosort;                  /* don't sort output */
 int f_numericonly;             /* don't convert uid/gid to name */
+int f_octal;                   /* print octal escapes for nongraphic characters */
+int f_octal_escape;            /* like f_octal but use C escapes if possible */
 int f_recursive;               /* ls subdirectories also */
 int f_reversesort;             /* reverse whatever sort is used */
 int f_sectime;                 /* print the real time for all files */
@@ -134,7 +135,7 @@
                f_listdot = 1;
 
        fts_options = FTS_PHYSICAL;
-       while ((ch = getopt(argc, argv, "1ACFLRSTWabcdfgiklmnopqrstux")) != -1) {
+       while ((ch = getopt(argc, argv, "1ABCFLRSTWabcdfgiklmnopqrstuwx")) != -1) {
                switch (ch) {
                /*
                 * The -1, -C, -l, -m and -x options all override each other so
@@ -195,10 +196,17 @@
                case 'A':
                        f_listdot = 1;
                        break;
-               /* the -b option turns off the -q option. */
+               /* the -B option turns off the -b, -q and -w options. */
+               case 'B':
+                       f_nonprint = 0;
+                       f_octal = 1;
+                       f_octal_escape = 0;
+                       break;
+               /* the -b option turns off the -B, -q and -w options. */
                case 'b':
-                       f_escape = 1;
                        f_nonprint = 0;
+                       f_octal = 0;
+                       f_octal_escape = 1;
                        break;
                /* The -d option turns off the -R option. */
                case 'd':
@@ -224,10 +232,11 @@
                case 'p':
                        f_typedir = 1;
                        break;
-               /* the -q option turns off the -b option. */
+               /* the -q option turns off the -B, -b and -w options. */
                case 'q':
                        f_nonprint = 1;
-                       f_escape = 0;
+                       f_octal = 0;
+                       f_octal_escape = 0;
                        break;
                case 'r':
                        f_reversesort = 1;
@@ -247,6 +256,12 @@
                case 'W':
                        f_whiteout = 1;
                        break;
+               /* the -w option turns off the -B, -b and -q options. */
+               case 'w':
+                       f_nonprint = 0;
+                       f_octal = 0;
+                       f_octal_escape = 0;
+                       break;
                default:
                case '?':
                        usage();
diff -r c699335f9b36 -r 92ccc2533b7e bin/ls/ls.h
--- a/bin/ls/ls.h       Mon Sep 22 02:42:02 2003 +0000
+++ b/bin/ls/ls.h       Mon Sep 22 02:43:19 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ls.h,v 1.14 2003/09/14 19:16:06 jschauma Exp $ */
+/*     $NetBSD: ls.h,v 1.15 2003/09/22 02:43:20 jschauma Exp $ */
 
 /*
  * Copyright (c) 1989, 1993
@@ -39,11 +39,12 @@
 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 */
 extern int f_longform;         /* long listing format */
+extern int f_octal;            /* print octal escapes for nongraphic characters */
+extern int f_octal_escape;     /* like f_octal but use C escapes if possible */
 extern int f_sectime;          /* print the real time for all files */
 extern int f_size;             /* list size in short listing */
 extern int f_statustime;       /* use time of last mode change */
diff -r c699335f9b36 -r 92ccc2533b7e bin/ls/print.c
--- a/bin/ls/print.c    Mon Sep 22 02:42:02 2003 +0000
+++ b/bin/ls/print.c    Mon Sep 22 02:43:19 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: print.c,v 1.36 2003/09/14 19:16:06 jschauma Exp $      */
+/*     $NetBSD: print.c,v 1.37 2003/09/22 02:43:20 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.36 2003/09/14 19:16:06 jschauma Exp $");
+__RCSID("$NetBSD: print.c,v 1.37 2003/09/22 02:43:20 jschauma Exp $");
 #endif
 #endif /* not lint */
 
@@ -129,7 +129,7 @@
                        printtime(sp->st_ctime);
                else
                        printtime(sp->st_mtime);
-               if (f_escape)
+               if (f_octal || f_octal_escape)
                        (void)safe_print(p->fts_name);
                else if (f_nonprint)
                        (void)printescaped(p->fts_name);
@@ -297,7 +297,7 @@
        if (f_size)
                chcnt += printf("%*llu ", sizefield,
                    (long long)howmany(sp->st_blocks, blocksize));
-       if (f_escape)
+       if (f_octal || f_octal_escape)
                chcnt += safe_print(p->fts_name);
        else if (f_nonprint)
                chcnt += printescaped(p->fts_name);
@@ -377,7 +377,7 @@
        }
        path[lnklen] = '\0';
        (void)printf(" -> ");
-       if (f_escape)
+       if (f_octal || f_octal_escape)
                (void)safe_print(path);
        else if (f_nonprint)
                (void)printescaped(path);
diff -r c699335f9b36 -r 92ccc2533b7e bin/ls/util.c
--- a/bin/ls/util.c     Mon Sep 22 02:42:02 2003 +0000
+++ b/bin/ls/util.c     Mon Sep 22 02:43:19 2003 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: util.c,v 1.26 2003/09/14 19:16:07 jschauma Exp $       */
+/*     $NetBSD: util.c,v 1.27 2003/09/22 02:43:20 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.26 2003/09/14 19:16:07 jschauma Exp $");
+__RCSID("$NetBSD: util.c,v 1.27 2003/09/22 02:43:20 jschauma Exp $");
 #endif
 #endif /* not lint */
 
@@ -61,6 +61,11 @@
 {
        size_t len;
        char *name;
+       int flags;
+
+       flags = VIS_NL | VIS_OCTAL;
+       if (f_octal_escape)
+               flags |= VIS_CSTYLE;
 
        len = strlen(src);
        if (len != 0 && SIZE_T_MAX/len <= 4) {
@@ -70,7 +75,7 @@
 
        name = (char *)malloc(4*len+1);
        if (name != NULL) {
-               len = strvis(name, src, VIS_NL | VIS_CSTYLE);
+               len = strvis(name, src, flags);
                printf("%s", name);
                free(name);
                return len;
@@ -98,7 +103,7 @@
 {
 
        (void)fprintf(stderr,
-           "usage: ls [-AabCcdFfgikLlmnopqRrSsTtuWx1] [file ...]\n");
+           "usage: ls [-AaBbCcdFfgikLlmnopqRrSsTtuWwx1] [file ...]\n");
        exit(EXIT_FAILURE);
        /* NOTREACHED */
 }



Home | Main Index | Thread Index | Old Index