Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/ls Add -O (only leaf files) and -P (print full path), fr...



details:   https://anonhg.NetBSD.org/src/rev/b7296de705a9
branches:  trunk
changeset: 326863:b7296de705a9
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Feb 20 18:56:36 2014 +0000

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

diffstat:

 bin/ls/ls.1    |  10 +++++++---
 bin/ls/ls.c    |  26 ++++++++++++++++++--------
 bin/ls/ls.h    |   4 +++-
 bin/ls/print.c |  52 +++++++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 71 insertions(+), 21 deletions(-)

diffs (232 lines):

diff -r 2b237b1d15ce -r b7296de705a9 bin/ls/ls.1
--- a/bin/ls/ls.1       Thu Feb 20 18:20:39 2014 +0000
+++ b/bin/ls/ls.1       Thu Feb 20 18:56:36 2014 +0000
@@ -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 @@
 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 @@
 (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 \&/
diff -r 2b237b1d15ce -r b7296de705a9 bin/ls/ls.c
--- a/bin/ls/ls.c       Thu Feb 20 18:20:39 2014 +0000
+++ b/bin/ls/ls.c       Thu Feb 20 18:56:36 2014 +0000
@@ -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 @@
 #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_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 @@
                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 @@
                        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 @@
                         * 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);
diff -r 2b237b1d15ce -r b7296de705a9 bin/ls/ls.h
--- a/bin/ls/ls.h       Thu Feb 20 18:20:39 2014 +0000
+++ b/bin/ls/ls.h       Thu Feb 20 18:56:36 2014 +0000
@@ -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_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;
diff -r 2b237b1d15ce -r b7296de705a9 bin/ls/print.c
--- a/bin/ls/print.c    Thu Feb 20 18:20:39 2014 +0000
+++ b/bin/ls/print.c    Thu Feb 20 18:56:36 2014 +0000
@@ -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 @@
 
 #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 @@
 
        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 @@
                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 @@
                }
        }
        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);



Home | Main Index | Thread Index | Old Index