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
.\" 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 @@ Always set for the super-user.
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 @@ of the file for sorting
.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 @@ This is the default when output is not to a terminal.
.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 ,

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
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
#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 rval = EXIT_SUCCESS; /* exit value - set if error encountered */
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_longform; /* long listing format */
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 @@ ls_main(int argc, char *argv[])
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 @@ ls_main(int argc, char *argv[])
case 'A':
f_listdot = 1;
break;
/* the -b option turns off the -q option. */
case 'b':
f_escape = 1;
/* 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_nonprint = 0;
f_octal = 0;
f_octal_escape = 1;
break;
/* The -d option turns off the -R option. */
case 'd':
@ -224,10 +232,11 @@ ls_main(int argc, char *argv[])
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 @@ ls_main(int argc, char *argv[])
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();

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
@ -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 */

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
@ -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 @@ printlong(DISPLAY *dp)
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 @@ printaname(FTSENT *p, int inodefield, int sizefield)
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 @@ printlink(FTSENT *p)
}
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);

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
@ -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 @@ safe_print(const char *src)
{
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 @@ safe_print(const char *src)
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 @@ usage(void)
{
(void)fprintf(stderr,
"usage: ls [-AabCcdFfgikLlmnopqRrSsTtuWx1] [file ...]\n");
"usage: ls [-AaBbCcdFfgikLlmnopqRrSsTtuWwx1] [file ...]\n");
exit(EXIT_FAILURE);
/* NOTREACHED */
}