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.
This commit is contained in:
jschauma 2003-09-22 02:43:19 +00:00
parent 616df9fb1f
commit 21ab633500
5 changed files with 61 additions and 23 deletions

View File

@ -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 .\" Copyright (c) 1980, 1990, 1991, 1993, 1994
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
@ -40,7 +40,7 @@
.Nd list directory contents .Nd list directory contents
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl AabCcdFfgikLlmnopqRrSsTtuWx1 .Op Fl AaBbCcdFfgikLlmnopqRrSsTtuWwx1
.Op Ar .Op Ar
.Sh DESCRIPTION .Sh DESCRIPTION
For each operand that names a For each operand that names a
@ -77,8 +77,13 @@ Always set for the super-user.
Include directory entries whose names begin with a Include directory entries whose names begin with a
dot dot
.Pq Sq \&. . .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 .It Fl b
Print octal escapes for nongraphic characters. As
.Fl B ,
but use C escape codes whenever possible.
.It Fl C .It Fl C
Force multi-column output; this is the default when output is to a terminal. Force multi-column output; this is the default when output is to a terminal.
.It Fl c .It Fl c
@ -184,6 +189,9 @@ of the file for sorting
.Pq Fl t .Pq Fl t
or printing or printing
.Pq Fl l . .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 .It Fl W
Display whiteouts when scanning directories. Display whiteouts when scanning directories.
.It Fl x .It Fl x
@ -196,6 +204,15 @@ This is the default when output is not to a terminal.
.El .El
.Pp .Pp
The 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 1 ,
.Fl C , .Fl C ,
.Fl l , .Fl l ,

View File

@ -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 * Copyright (c) 1989, 1993, 1994
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
#if 0 #if 0
static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94"; static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94";
#else #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
#endif /* not lint */ #endif /* not lint */
@ -86,7 +86,6 @@ int rval = EXIT_SUCCESS; /* exit value - set if error encountered */
int f_accesstime; /* use time of last access */ int f_accesstime; /* use time of last access */
int f_column; /* columnated format */ int f_column; /* columnated format */
int f_columnacross; /* columnated format, sorted across */ 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_flags; /* show flags associated with a file */
int f_grouponly; /* long listing without owner */ int f_grouponly; /* long listing without owner */
int f_inode; /* print inode */ int f_inode; /* print inode */
@ -96,6 +95,8 @@ int f_longform; /* long listing format */
int f_nonprint; /* show unprintables as ? */ int f_nonprint; /* show unprintables as ? */
int f_nosort; /* don't sort output */ int f_nosort; /* don't sort output */
int f_numericonly; /* don't convert uid/gid to name */ 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_recursive; /* ls subdirectories also */
int f_reversesort; /* reverse whatever sort is used */ int f_reversesort; /* reverse whatever sort is used */
int f_sectime; /* print the real time for all files */ int f_sectime; /* print the real time for all files */
@ -134,7 +135,7 @@ ls_main(int argc, char *argv[])
f_listdot = 1; f_listdot = 1;
fts_options = FTS_PHYSICAL; fts_options = FTS_PHYSICAL;
while ((ch = getopt(argc, argv, "1ACFLRSTWabcdfgiklmnopqrstux")) != -1) { while ((ch = getopt(argc, argv, "1ABCFLRSTWabcdfgiklmnopqrstuwx")) != -1) {
switch (ch) { switch (ch) {
/* /*
* The -1, -C, -l, -m and -x options all override each other so * The -1, -C, -l, -m and -x options all override each other so
@ -195,10 +196,17 @@ ls_main(int argc, char *argv[])
case 'A': case 'A':
f_listdot = 1; f_listdot = 1;
break; break;
/* the -b option turns off the -q option. */ /* the -B option turns off the -b, -q and -w options. */
case 'b': case 'B':
f_escape = 1;
f_nonprint = 0; 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_nonprint = 0;
f_octal = 0;
f_octal_escape = 1;
break; break;
/* The -d option turns off the -R option. */ /* The -d option turns off the -R option. */
case 'd': case 'd':
@ -224,10 +232,11 @@ ls_main(int argc, char *argv[])
case 'p': case 'p':
f_typedir = 1; f_typedir = 1;
break; break;
/* the -q option turns off the -b option. */ /* the -q option turns off the -B, -b and -w options. */
case 'q': case 'q':
f_nonprint = 1; f_nonprint = 1;
f_escape = 0; f_octal = 0;
f_octal_escape = 0;
break; break;
case 'r': case 'r':
f_reversesort = 1; f_reversesort = 1;
@ -247,6 +256,12 @@ ls_main(int argc, char *argv[])
case 'W': case 'W':
f_whiteout = 1; f_whiteout = 1;
break; 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: default:
case '?': case '?':
usage(); usage();

View File

@ -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 * Copyright (c) 1989, 1993
@ -39,11 +39,12 @@
extern long blocksize; /* block size units */ extern long blocksize; /* block size units */
extern int f_accesstime; /* use time of last access */ 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_flags; /* show flags associated with a file */
extern int f_grouponly; /* long listing without owner */ extern int f_grouponly; /* long listing without owner */
extern int f_inode; /* print inode */ extern int f_inode; /* print inode */
extern int f_longform; /* long listing format */ 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_sectime; /* print the real time for all files */
extern int f_size; /* list size in short listing */ extern int f_size; /* list size in short listing */
extern int f_statustime; /* use time of last mode change */ extern int f_statustime; /* use time of last mode change */

View File

@ -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 * Copyright (c) 1989, 1993, 1994
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94"; static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94";
#else #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
#endif /* not lint */ #endif /* not lint */
@ -129,7 +129,7 @@ printlong(DISPLAY *dp)
printtime(sp->st_ctime); printtime(sp->st_ctime);
else else
printtime(sp->st_mtime); printtime(sp->st_mtime);
if (f_escape) if (f_octal || f_octal_escape)
(void)safe_print(p->fts_name); (void)safe_print(p->fts_name);
else if (f_nonprint) else if (f_nonprint)
(void)printescaped(p->fts_name); (void)printescaped(p->fts_name);
@ -297,7 +297,7 @@ printaname(FTSENT *p, int inodefield, int sizefield)
if (f_size) if (f_size)
chcnt += printf("%*llu ", sizefield, chcnt += printf("%*llu ", sizefield,
(long long)howmany(sp->st_blocks, blocksize)); (long long)howmany(sp->st_blocks, blocksize));
if (f_escape) if (f_octal || f_octal_escape)
chcnt += safe_print(p->fts_name); chcnt += safe_print(p->fts_name);
else if (f_nonprint) else if (f_nonprint)
chcnt += printescaped(p->fts_name); chcnt += printescaped(p->fts_name);
@ -377,7 +377,7 @@ printlink(FTSENT *p)
} }
path[lnklen] = '\0'; path[lnklen] = '\0';
(void)printf(" -> "); (void)printf(" -> ");
if (f_escape) if (f_octal || f_octal_escape)
(void)safe_print(path); (void)safe_print(path);
else if (f_nonprint) else if (f_nonprint)
(void)printescaped(path); (void)printescaped(path);

View File

@ -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 * Copyright (c) 1989, 1993, 1994
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)util.c 8.5 (Berkeley) 4/28/95"; static char sccsid[] = "@(#)util.c 8.5 (Berkeley) 4/28/95";
#else #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
#endif /* not lint */ #endif /* not lint */
@ -61,6 +61,11 @@ safe_print(const char *src)
{ {
size_t len; size_t len;
char *name; char *name;
int flags;
flags = VIS_NL | VIS_OCTAL;
if (f_octal_escape)
flags |= VIS_CSTYLE;
len = strlen(src); len = strlen(src);
if (len != 0 && SIZE_T_MAX/len <= 4) { if (len != 0 && SIZE_T_MAX/len <= 4) {
@ -70,7 +75,7 @@ safe_print(const char *src)
name = (char *)malloc(4*len+1); name = (char *)malloc(4*len+1);
if (name != NULL) { if (name != NULL) {
len = strvis(name, src, VIS_NL | VIS_CSTYLE); len = strvis(name, src, flags);
printf("%s", name); printf("%s", name);
free(name); free(name);
return len; return len;
@ -98,7 +103,7 @@ usage(void)
{ {
(void)fprintf(stderr, (void)fprintf(stderr,
"usage: ls [-AabCcdFfgikLlmnopqRrSsTtuWx1] [file ...]\n"); "usage: ls [-AaBbCcdFfgikLlmnopqRrSsTtuWwx1] [file ...]\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
/* NOTREACHED */ /* NOTREACHED */
} }