1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-04 09:09:56 +00:00

Added -b option to display unprintables in octal.

PR: 1315
This commit is contained in:
Dag-Erling Smørgrav 1998-04-21 22:02:01 +00:00
parent 4431d4ba23
commit 7ea3064820
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=35373
6 changed files with 71 additions and 11 deletions

View File

@ -31,7 +31,7 @@
* SUCH DAMAGE.
*
* from: @(#)extern.h 8.1 (Berkeley) 5/31/93
* $Id: extern.h,v 1.4 1997/02/22 14:03:54 peter Exp $
* $Id: extern.h,v 1.5 1997/08/07 15:33:45 steve Exp $
*/
int acccmp __P((const FTSENT *, const FTSENT *));
@ -49,3 +49,5 @@ void printcol __P((DISPLAY *));
void printlong __P((DISPLAY *));
void printscol __P((DISPLAY *));
void usage __P((void));
int len_octal __P((char *, int));
int prn_octal __P((char *));

View File

@ -33,7 +33,7 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)ls.1 8.7 (Berkeley) 7/29/94
.\" $Id: ls.1,v 1.15 1997/09/18 06:55:21 charnier Exp $
.\" $Id: ls.1,v 1.16 1997/12/25 09:36:39 hoek Exp $
.\"
.Dd July 29, 1994
.Dt LS 1
@ -43,7 +43,7 @@
.Nd list directory contents
.Sh SYNOPSIS
.Nm ls
.Op Fl ACFLRTWacdfgikloqrstu1
.Op Fl ACFLRTWabcdfgikloqrstu1
.Op Ar file ...
.Sh DESCRIPTION
For each operand that names a
@ -100,6 +100,9 @@ Display whiteouts when scanning directories.
.It Fl a
Include directory entries whose names begin with a
dot (.).
.It Fl b
Force printing of non-graphic characters in file names as \\xxx,
where xxx is the numeric value of the character in octal.
.It Fl c
Use time when file status was last changed for sorting or printing.
.It Fl d

View File

@ -45,7 +45,7 @@ static const char copyright[] =
static char sccsid[] = "@(#)ls.c 8.5 (Berkeley) 4/2/94";
#else
static const char rcsid[] =
"$Id: ls.c,v 1.16 1997/08/07 22:28:23 steve Exp $";
"$Id: ls.c,v 1.17 1997/09/18 06:42:27 sef Exp $";
#endif
#endif /* not lint */
@ -88,6 +88,7 @@ int f_longform; /* long listing format */
int f_newline; /* if precede with newline */
int f_nonprint; /* show unprintables as ? */
int f_nosort; /* don't sort output */
int f_octal; /* show unprintables as \xxx */
int f_recursive; /* ls subdirectories also */
int f_reversesort; /* reverse whatever sort is used */
int f_sectime; /* print the real time for all files */
@ -135,7 +136,7 @@ main(argc, argv)
f_listdot = 1;
fts_options = FTS_PHYSICAL;
while ((ch = getopt(argc, argv, "1ACFLRTWacdfgikloqrstu")) != -1) {
while ((ch = getopt(argc, argv, "1ACFLRTWabcdfgikloqrstu")) != -1) {
switch (ch) {
/*
* The -1, -C and -l options all override each other so shell
@ -199,6 +200,7 @@ main(argc, argv)
break;
case 'q':
f_nonprint = 1;
f_octal = 0;
break;
case 'r':
f_reversesort = 1;
@ -215,6 +217,10 @@ main(argc, argv)
case 'W':
f_whiteout = 1;
break;
case 'b':
f_octal = 1;
f_nonprint = 0;
break;
default:
case '?':
usage();
@ -426,6 +432,10 @@ display(p, list)
prcopy(cur->fts_name, cur->fts_name, cur->fts_namelen);
if (cur->fts_namelen > maxlen)
maxlen = cur->fts_namelen;
if (f_octal) {
int t = len_octal(cur->fts_name, cur->fts_namelen);
if (t > maxlen) maxlen = t;
}
if (needstats) {
sp = cur->fts_statp;
if (sp->st_blocks > maxblock)

View File

@ -35,7 +35,7 @@
* SUCH DAMAGE.
*
* from: @(#)ls.h 8.1 (Berkeley) 5/31/93
* $Id: ls.h,v 1.6 1997/04/29 10:03:05 dfr Exp $
* $Id: ls.h,v 1.7 1997/08/07 15:33:48 steve Exp $
*/
#define NO_PRINT 1
@ -46,6 +46,7 @@ extern int f_accesstime; /* use time of last access */
extern int f_flags; /* show flags associated with a file */
extern int f_inode; /* print inode */
extern int f_longform; /* long listing format */
extern int f_octal; /* print unprintables in octal */
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

@ -39,7 +39,7 @@
static char sccsid[] = "@(#)print.c 8.4 (Berkeley) 4/17/94";
#else
static const char rcsid[] =
"$Id: print.c,v 1.13 1997/08/07 15:33:48 steve Exp $";
"$Id: print.c,v 1.14 1997/08/07 22:28:24 steve Exp $";
#endif
#endif /* not lint */
@ -127,7 +127,8 @@ printlong(dp)
printtime(sp->st_ctime);
else
printtime(sp->st_mtime);
(void)printf("%s", p->fts_name);
if (f_octal) (void)prn_octal(p->fts_name);
else (void)printf("%s", p->fts_name);
if (f_type)
(void)printtype(sp->st_mode);
if (S_ISLNK(sp->st_mode))
@ -222,7 +223,7 @@ printaname(p, inodefield, sizefield)
if (f_size)
chcnt += printf("%*qd ",
(int)sizefield, howmany(sp->st_blocks, blocksize));
chcnt += printf("%s", p->fts_name);
chcnt += f_octal ? prn_octal(p->fts_name) : printf("%s", p->fts_name);
if (f_type)
chcnt += printtype(sp->st_mode);
return (chcnt);
@ -303,5 +304,9 @@ printlink(p)
return;
}
path[lnklen] = '\0';
(void)printf(" -> %s", path);
if (f_octal) {
(void)printf(" -> ");
(void)prn_octal(path);
}
else (void)printf(" -> %s", path);
}

View File

@ -39,7 +39,7 @@
static char sccsid[] = "@(#)util.c 8.3 (Berkeley) 4/2/94";
#else
static const char rcsid[] =
"$Id: util.c,v 1.10 1997/08/07 15:33:50 steve Exp $";
"$Id: util.c,v 1.11 1997/08/07 22:28:25 steve Exp $";
#endif
#endif /* not lint */
@ -47,6 +47,7 @@ static const char rcsid[] =
#include <sys/stat.h>
#include <ctype.h>
#include <err.h>
#include <fts.h>
#include <stdio.h>
#include <stdlib.h>
@ -68,6 +69,44 @@ prcopy(src, dest, len)
}
}
/*
* The fts system makes it difficult to replace fts_name with a different-
* sized string, so we just calculate the real length here and do the
* conversion in prn_octal()
*/
int
len_octal(s, len)
char *s;
int len;
{
int r;
while (len--)
if (isprint(*s++)) r++; else r += 4;
return r;
}
int
prn_octal(s)
char *s;
{
unsigned char ch;
int len = 0;
while ((ch = *s++))
{
if (isprint(ch)) putchar(ch), len++;
else {
putchar('\\');
putchar('0' + (ch >> 6));
putchar('0' + ((ch >> 3) & 3));
putchar('0' + (ch & 3));
len += 4;
}
}
return len;
}
void
usage()
{