Subject: Re: CVS commit: src/bin/ls
To: None <source-changes@NetBSD.org, tech-userlevel@netbsd.org>
From: Jan Schaumann <jschauma@netbsd.org>
List: tech-userlevel
Date: 09/14/2003 20:32:23
--VkqCAaSJIySsbD6j
Content-Type: multipart/mixed; boundary="zGQnqpIoxlsbsOfg"
Content-Disposition: inline


--zGQnqpIoxlsbsOfg
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

"Valeriy E. Ushakov" <uwe@ptc.spbu.ru> wrote:
> On Sun, Sep 14, 2003 at 19:16:08 +0000, Jan Schaumann wrote:

> > Implement '-b' option, which, following FreeBSD, Linux and (I think) So=
laris
> > prints octal escapes for nongraphic characters.
>=20
> The logical set shoule include -w, and, probably, -B.

How about the attached patch?

-Jan

--=20
"I am so amazingly cool you could keep a side of meat in me for a=20
month. I am so hip I have difficulty seeing over my pelvis."

--zGQnqpIoxlsbsOfg
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=diff
Content-Transfer-Encoding: quoted-printable

Index: ls.1
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/bin/ls/ls.1,v
retrieving revision 1.43
diff -b -u -r1.43 ls.1
--- ls.1	2003/09/14 19:16:06	1.43
+++ ls.1	2003/09/15 00:29:33
@@ -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 x=
xx
+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 termina=
l.
 .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
@@ -194,6 +202,15 @@
 Force output to be one entry per line.
 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 ,
Index: ls.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/bin/ls/ls.c,v
retrieving revision 1.51
diff -b -u -r1.51 ls.c
--- ls.c	2003/09/14 19:16:06	1.51
+++ ls.c	2003/09/15 00:29:34
@@ -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 =3D 1;
=20
 	fts_options =3D FTS_PHYSICAL;
-	while ((ch =3D getopt(argc, argv, "1ACFLRSTWabcdfgiklmnopqrstux")) !=3D -=
1) {
+	while ((ch =3D getopt(argc, argv, "1ABCFLRSTWabcdfgiklmnopqrstuwx")) !=3D=
 -1) {
 		switch (ch) {
 		/*
 		 * The -1, -C, -l, -m and -x options all override each other so
@@ -195,10 +196,17 @@
 		case 'A':
 			f_listdot =3D 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 =3D 0;
+			f_octal =3D 1;
+			f_octal_escape =3D 0;
+			break;
+		/* the -b option turns off the -B, -q and -w options. */
 		case 'b':
-			f_escape =3D 1;
 			f_nonprint =3D 0;
+			f_octal =3D 0;
+			f_octal_escape =3D 1;
 			break;
 		/* The -d option turns off the -R option. */
 		case 'd':
@@ -224,10 +232,11 @@
 		case 'p':
 			f_typedir =3D 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 =3D 1;
-			f_escape =3D 0;
+			f_octal =3D 0;
+			f_octal_escape =3D 0;
 			break;
 		case 'r':
 			f_reversesort =3D 1;
@@ -246,6 +255,12 @@
 			break;
 		case 'W':
 			f_whiteout =3D 1;
+			break;
+		/* the -w option turns off the -B, -b and -q options. */
+		case 'w':
+			f_nonprint =3D 0;
+			f_octal =3D 0;
+			f_octal_escape =3D 0;
 			break;
 		default:
 		case '?':
Index: ls.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/bin/ls/ls.h,v
retrieving revision 1.14
diff -b -u -r1.14 ls.h
--- ls.h	2003/09/14 19:16:06	1.14
+++ ls.h	2003/09/15 00:29:34
@@ -39,11 +39,12 @@
 extern long blocksize;		/* block size units */
=20
 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 */
Index: print.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/bin/ls/print.c,v
retrieving revision 1.36
diff -b -u -r1.36 print.c
--- print.c	2003/09/14 19:16:06	1.36
+++ print.c	2003/09/15 00:29:35
@@ -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 +=3D printf("%*llu ", sizefield,
 		    (long long)howmany(sp->st_blocks, blocksize));
-	if (f_escape)
+	if (f_octal || f_octal_escape)
 		chcnt +=3D safe_print(p->fts_name);
 	else if (f_nonprint)
 		chcnt +=3D printescaped(p->fts_name);
@@ -377,7 +377,7 @@
 	}
 	path[lnklen] =3D '\0';
 	(void)printf(" -> ");
-	if (f_escape)
+	if (f_octal || f_octal_escape)
 		(void)safe_print(path);
 	else if (f_nonprint)
 		(void)printescaped(path);
Index: util.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvsroot/src/bin/ls/util.c,v
retrieving revision 1.26
diff -b -u -r1.26 util.c
--- util.c	2003/09/14 19:16:07	1.26
+++ util.c	2003/09/15 00:29:35
@@ -61,7 +61,12 @@
 {
 	size_t len;
 	char *name;
+	int flags;
=20
+	flags =3D VIS_NL | VIS_OCTAL;
+	if (f_octal_escape)
+		flags |=3D VIS_CSTYLE;
+
 	len =3D strlen(src);
 	if (len !=3D 0 && SIZE_T_MAX/len <=3D 4) {
 		errx(EXIT_FAILURE, "%s: name too long", src);
@@ -70,7 +75,7 @@
=20
 	name =3D (char *)malloc(4*len+1);
 	if (name !=3D NULL) {
-		len =3D strvis(name, src, VIS_NL | VIS_CSTYLE);
+		len =3D strvis(name, src, flags);
 		printf("%s", name);
 		free(name);
 		return len;
@@ -98,7 +103,7 @@
 {
=20
 	(void)fprintf(stderr,
-	    "usage: ls [-AabCcdFfgikLlmnopqRrSsTtuWx1] [file ...]\n");
+	    "usage: ls [-AaBbCcdFfgikLlmnopqRrSsTtuWwx1] [file ...]\n");
 	exit(EXIT_FAILURE);
 	/* NOTREACHED */
 }

--zGQnqpIoxlsbsOfg--

--VkqCAaSJIySsbD6j
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (NetBSD)

iD8DBQE/ZQiXfFtkr68iakwRAskTAKDcYbkXTkjDDM5ZtFGUT1HnqzKPdwCffi7M
AR3z9HoO5lk4KvBL40DSRnI=
=NKYN
-----END PGP SIGNATURE-----

--VkqCAaSJIySsbD6j--