Add -O (only leaf files) and -P (print full path), from tls@

This commit is contained in:
christos 2014-02-20 18:56:36 +00:00
parent ebf02f5265
commit 0c47a5c376
4 changed files with 71 additions and 21 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: ls.1,v 1.72 2012/11/20 12:37:29 abs Exp $
.\" $NetBSD: ls.1,v 1.73 2014/02/20 18:56:36 christos Exp $
.\"
.\" Copyright (c) 1980, 1990, 1991, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
@ -32,7 +32,7 @@
.\"
.\" @(#)ls.1 8.7 (Berkeley) 7/29/94
.\"
.Dd November 20, 2012
.Dd February 20, 2014
.Dt LS 1
.Os
.Sh NAME
@ -40,7 +40,7 @@
.Nd list directory contents
.Sh SYNOPSIS
.Nm
.Op Fl 1AaBbCcdFfghikLlMmnopqRrSsTtuWwx
.Op Fl 1AaBbCcdFfghikLlMmnOoPpqRrSsTtuWwx
.Op Ar
.Sh DESCRIPTION
For each
@ -183,6 +183,8 @@ The same as
except that
the owner and group IDs are displayed numerically rather than converting
to a owner or group name.
.It Fl O
Output only leaf files (not directories), eliding other ls output.
.It Fl o
Include the file flags in a long
.Pq Fl l
@ -193,6 +195,8 @@ is displayed.
(See
.Xr chflags 1
for a list of possible flags and their meanings.)
.It Fl P
Print the full pathname for each file.
.It Fl p
Display a slash
.Pq Sq \&/

View File

@ -1,4 +1,4 @@
/* $NetBSD: ls.c,v 1.70 2012/11/20 12:37:29 abs Exp $ */
/* $NetBSD: ls.c,v 1.71 2014/02/20 18:56:36 christos Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
#if 0
static char sccsid[] = "@(#)ls.c 8.7 (Berkeley) 8/5/94";
#else
__RCSID("$NetBSD: ls.c,v 1.70 2012/11/20 12:37:29 abs Exp $");
__RCSID("$NetBSD: ls.c,v 1.71 2014/02/20 18:56:36 christos Exp $");
#endif
#endif /* not lint */
@ -111,6 +111,8 @@ int f_stream; /* stream format */
int f_type; /* add type character for non-regular files */
int f_typedir; /* add type character for directories */
int f_whiteout; /* show whiteout entries */
int f_fullpath; /* print full pathname, not filename */
int f_leafonly; /* when recursing, print leaf names only */
__dead static void
usage(void)
@ -149,7 +151,7 @@ ls_main(int argc, char *argv[])
f_listdot = 1;
fts_options = FTS_PHYSICAL;
while ((ch = getopt(argc, argv, "1ABCFLMRSTWabcdfghiklmnopqrstuwx")) != -1) {
while ((ch = getopt(argc, argv, "1ABCFLMOPRSTWabcdfghiklmnopqrstuwx")) != -1) {
switch (ch) {
/*
* The -1, -C, -l, -m and -x options all override each other so
@ -253,9 +255,15 @@ ls_main(int argc, char *argv[])
f_longform = 1;
f_column = f_columnacross = f_singlecol = f_stream = 0;
break;
case 'O':
f_leafonly = 1;
break;
case 'o':
f_flags = 1;
break;
case 'P':
f_fullpath = 1;
break;
case 'p':
f_typedir = 1;
break;
@ -446,11 +454,13 @@ traverse(int argc, char *argv[], int options)
* a separator. If multiple arguments, precede each
* directory with its name.
*/
if (output)
(void)printf("\n%s:\n", p->fts_path);
else if (argc > 1) {
(void)printf("%s:\n", p->fts_path);
output = 1;
if (!f_leafonly) {
if (output)
(void)printf("\n%s:\n", p->fts_path);
else if (argc > 1) {
(void)printf("%s:\n", p->fts_path);
output = 1;
}
}
chp = fts_children(ftsp, ch_options);

View File

@ -1,4 +1,4 @@
/* $NetBSD: ls.h,v 1.18 2011/03/15 03:52:38 erh Exp $ */
/* $NetBSD: ls.h,v 1.19 2014/02/20 18:56:36 christos Exp $ */
/*
* Copyright (c) 1989, 1993
@ -53,6 +53,8 @@ extern int f_statustime; /* use time of last mode change */
extern int f_type; /* add type character for non-regular files */
extern int f_typedir; /* add type character for directories */
extern int f_nonprint; /* show unprintables as ? */
extern int f_fullpath; /* print full pathname, not filename */
extern int f_leafonly; /* when recursing, print leaf names only */
typedef struct {
FTSENT *list;

View File

@ -1,4 +1,4 @@
/* $NetBSD: print.c,v 1.52 2013/05/02 22:43:55 zafer Exp $ */
/* $NetBSD: print.c,v 1.53 2014/02/20 18:56:36 christos 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.52 2013/05/02 22:43:55 zafer Exp $");
__RCSID("$NetBSD: print.c,v 1.53 2014/02/20 18:56:36 christos Exp $");
#endif
#endif /* not lint */
@ -72,6 +72,39 @@ static time_t now;
#define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT)
static int
safe_printpath(const FTSENT *p) {
int chcnt;
if (f_fullpath) {
chcnt = safe_print(p->fts_path);
chcnt += safe_print("/");
} else
chcnt = 0;
return chcnt + safe_print(p->fts_name);
}
static int
printescapedpath(const FTSENT *p) {
int chcnt;
if (f_fullpath) {
chcnt = printescaped(p->fts_path);
chcnt += printescaped("/");
} else
chcnt = 0;
return chcnt + printescaped(p->fts_name);
}
static int
printpath(const FTSENT *p) {
if (f_fullpath)
return printf("%s/%s", p->fts_path, p->fts_name);
else
return printf("%s", p->fts_path);
}
void
printscol(DISPLAY *dp)
{
@ -95,7 +128,8 @@ printlong(DISPLAY *dp)
now = time(NULL);
printtotal(dp); /* "total: %u\n" */
if (!f_leafonly)
printtotal(dp); /* "total: %u\n" */
for (p = dp->list; p; p = p->fts_link) {
if (IS_NOPRINT(p))
@ -151,11 +185,11 @@ printlong(DISPLAY *dp)
else
printtime(sp->st_mtime);
if (f_octal || f_octal_escape)
(void)safe_print(p->fts_name);
(void)safe_printpath(p);
else if (f_nonprint)
(void)printescaped(p->fts_name);
(void)printescapedpath(p);
else
(void)printf("%s", p->fts_name);
(void)printpath(p);
if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
(void)printtype(sp->st_mode);
@ -346,11 +380,11 @@ printaname(FTSENT *p, int inodefield, int sizefield)
}
}
if (f_octal || f_octal_escape)
chcnt += safe_print(p->fts_name);
chcnt += safe_printpath(p);
else if (f_nonprint)
chcnt += printescaped(p->fts_name);
chcnt += printescapedpath(p);
else
chcnt += printf("%s", p->fts_name);
chcnt += printpath(p);
if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
chcnt += printtype(sp->st_mode);
return (chcnt);