Source-Changes-HG archive

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

[src/netbsd-2]: src/usr.bin/find This adds -fprint function. The primary name...



details:   https://anonhg.NetBSD.org/src/rev/885c39b425f2
branches:  netbsd-2
changeset: 564088:885c39b425f2
user:      reed <reed%NetBSD.org@localhost>
date:      Tue Oct 11 22:56:27 2005 +0000

description:
This adds -fprint function. The primary name "-fprint" (but not the
code) comes from findutils; it behaves the same.

>From my manpage addition:

 -fprint filename
         This primary always evaluates to true.  This creates filename or
         overwrites the file if it already exists.  The file is created at
         startup.  It writes the pathname of the current file to this
         file, followed by a newline character.  The file will be empty if
         no files are matched.

Here is an example usage:

find /etc \( -name "*pass*" -fprint file1 \) -o \( -group operator -fprint file2 \) -o -name "w*"

This was discussed on tech-userlevel.

This creates the file as command line argument parsing time.
If there is an error somewhere on that line, such as missing values
or mismatched parenthesis, then a file may still be created.
(Even if a later -fprint filename is unwritable.) This is similar
behaviour to findutils. (It has been suggested that this find could
be code to create the files in an extra stage after the command-line
argument parsing and before the actual function processing.)

I will add -fprintx and -fprint0 soon.

diffstat:

 usr.bin/find/extern.h   |   3 ++-
 usr.bin/find/find.1     |  12 +++++++++++-
 usr.bin/find/find.c     |  10 +++++-----
 usr.bin/find/find.h     |   6 ++++--
 usr.bin/find/function.c |  42 ++++++++++++++++++++++++++++++++++++++++--
 usr.bin/find/option.c   |   5 +++--
 6 files changed, 65 insertions(+), 13 deletions(-)

diffs (211 lines):

diff -r f2151301d7ef -r 885c39b425f2 usr.bin/find/extern.h
--- a/usr.bin/find/extern.h     Tue Oct 11 21:10:23 2005 +0000
+++ b/usr.bin/find/extern.h     Tue Oct 11 22:56:27 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.20 2003/08/07 11:13:40 agc Exp $  */
+/*     $NetBSD: extern.h,v 1.20.4.1 2005/10/11 22:56:27 reed Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -58,6 +58,7 @@
 PLAN   *c_execdir __P((char ***, int));
 PLAN   *c_flags __P((char ***, int));
 PLAN   *c_follow __P((char ***, int));
+PLAN   *c_fprint __P((char ***, int));
 PLAN   *c_fstype __P((char ***, int));
 PLAN   *c_group __P((char ***, int));
 PLAN   *c_iname __P((char ***, int));
diff -r f2151301d7ef -r 885c39b425f2 usr.bin/find/find.1
--- a/usr.bin/find/find.1       Tue Oct 11 21:10:23 2005 +0000
+++ b/usr.bin/find/find.1       Tue Oct 11 22:56:27 2005 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: find.1,v 1.47.2.1 2004/03/31 18:08:25 tron Exp $
+.\"    $NetBSD: find.1,v 1.47.2.1.2.1 2005/10/11 22:56:27 reed Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -259,6 +259,15 @@
 for more information about file flags.)
 .It Ic -follow
 Follow symbolic links.
+.It Ic -fprint Ar filename
+This primary always evaluates to true.
+This creates
+.Ar filename
+or overwrites the file if it already exists.
+The file is created at startup.
+It writes the pathname of the current file to this file, followed
+by a newline character.
+The file will be empty if no files are matched.
 .It Ic -fstype Ar type
 True if the file is contained in a file system of type
 .Ar type .
@@ -435,6 +444,7 @@
 by a newline character.
 If none of
 .Ic -exec ,
+.Ic -fprint ,
 .Ic -ls ,
 .Ic -ok ,
 .Ic -print0 ,
diff -r f2151301d7ef -r 885c39b425f2 usr.bin/find/find.c
--- a/usr.bin/find/find.c       Tue Oct 11 21:10:23 2005 +0000
+++ b/usr.bin/find/find.c       Tue Oct 11 22:56:27 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: find.c,v 1.18.2.1 2004/03/31 18:07:46 tron Exp $       */
+/*     $NetBSD: find.c,v 1.18.2.1.2.1 2005/10/11 22:56:27 reed Exp $   */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "from: @(#)find.c       8.5 (Berkeley) 8/5/94";
 #else
-__RCSID("$NetBSD: find.c,v 1.18.2.1 2004/03/31 18:07:46 tron Exp $");
+__RCSID("$NetBSD: find.c,v 1.18.2.1.2.1 2005/10/11 22:56:27 reed Exp $");
 #endif
 #endif /* not lint */
 
@@ -98,9 +98,9 @@
        }
 
        /*
-        * if the user didn't specify one of -print, -ok or -exec, then -print
-        * is assumed so we bracket the current expression with parens, if
-        * necessary, and add a -print node on the end.
+        * if the user didn't specify one of -print, -ok, -fprint, or -exec,
+        * then -print is assumed so we bracket the current expression with
+        * parens, if necessary, and add a -print node on the end.
         */
        if (!isoutput) {
                if (plan == NULL) {
diff -r f2151301d7ef -r 885c39b425f2 usr.bin/find/find.h
--- a/usr.bin/find/find.h       Tue Oct 11 21:10:23 2005 +0000
+++ b/usr.bin/find/find.h       Tue Oct 11 22:56:27 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: find.h,v 1.18 2003/08/07 11:13:41 agc Exp $    */
+/*     $NetBSD: find.h,v 1.18.4.1 2005/10/11 22:56:27 reed Exp $       */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -41,7 +41,7 @@
        N_AND = 1,                              /* must start > 0 */
        N_AMIN, N_ANEWER, N_ATIME, N_CLOSEPAREN, N_CMIN, N_CNEWER, N_CTIME,
        N_DEPTH, N_EMPTY,
-       N_EXEC, N_EXECDIR, N_EXPR, N_FLAGS, N_FOLLOW, N_FSTYPE, N_GROUP,
+       N_EXEC, N_EXECDIR, N_EXPR, N_FLAGS, N_FOLLOW, N_FPRINT, N_FSTYPE, N_GROUP,
        N_INAME, N_INUM, N_IREGEX, N_LINKS, N_LS, N_MINDEPTH, N_MAXDEPTH,
        N_MMIN, N_MTIME, N_NAME, N_NEWER, N_NOGROUP, N_NOT, N_NOUSER, N_OK,
        N_OPENPAREN, N_OR, N_PATH, N_PERM, N_PRINT, N_PRINT0, N_PRINTX,
@@ -83,6 +83,7 @@
                int _max_data;                  /* tree depth */
                int _min_data;                  /* tree depth */
                regex_t _regexp_data;           /* compiled regexp */
+               FILE *_fprint_file;             /* file stream for -fprint */
        } p_un;
 } PLAN;
 #define        a_data          p_un._a_data
@@ -103,6 +104,7 @@
 #define        max_data        p_un._max_data
 #define        min_data        p_un._min_data
 #define        regexp_data     p_un._regexp_data
+#define        fprint_file     p_un._fprint_file
 
 typedef struct _option {
        char *name;                     /* option name */
diff -r f2151301d7ef -r 885c39b425f2 usr.bin/find/function.c
--- a/usr.bin/find/function.c   Tue Oct 11 21:10:23 2005 +0000
+++ b/usr.bin/find/function.c   Tue Oct 11 22:56:27 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: function.c,v 1.46 2003/08/07 11:13:41 agc Exp $        */
+/*     $NetBSD: function.c,v 1.46.4.1 2005/10/11 22:56:27 reed Exp $   */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "from: @(#)function.c   8.10 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: function.c,v 1.46 2003/08/07 11:13:41 agc Exp $");
+__RCSID("$NetBSD: function.c,v 1.46.4.1 2005/10/11 22:56:27 reed Exp $");
 #endif
 #endif /* not lint */
 
@@ -88,6 +88,7 @@
        int     f_exec __P((PLAN *, FTSENT *));
        int     f_execdir __P((PLAN *, FTSENT *));
        int     f_flags __P((PLAN *, FTSENT *));
+       int     f_fprint __P((PLAN *, FTSENT *));
        int     f_fstype __P((PLAN *, FTSENT *));
        int     f_group __P((PLAN *, FTSENT *));
        int     f_iname __P((PLAN *, FTSENT *));
@@ -708,6 +709,43 @@
        return (palloc(N_FOLLOW, f_always_true));
 }
  
+/* -fprint functions --
+ *
+ *     Causes the current pathame to be written to the defined output file.
+ */
+int
+f_fprint(plan, entry)
+       PLAN *plan;
+       FTSENT *entry;
+{
+
+       if (-1 == fprintf(plan->fprint_file, "%s\n", entry->fts_path))
+               warn("fprintf");
+
+       return(1);
+
+       /* no descriptors are closed; they will be closed by
+          operating system when this find command exits.  */
+}
+ 
+PLAN *
+c_fprint(argvp, isok)
+       char ***argvp;
+       int isok;
+{
+       PLAN *new;
+
+       isoutput = 1; /* do not assume -print */
+
+       new = palloc(N_FPRINT, f_fprint);
+
+       if (NULL == (new->fprint_file = fopen(**argvp, "w")))
+               err(1, "-fprint: %s: cannot create file", **argvp);
+
+       (*argvp)++;
+       return (new);
+}
+
 /*
  * -fstype functions --
  *
diff -r f2151301d7ef -r 885c39b425f2 usr.bin/find/option.c
--- a/usr.bin/find/option.c     Tue Oct 11 21:10:23 2005 +0000
+++ b/usr.bin/find/option.c     Tue Oct 11 22:56:27 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: option.c,v 1.20 2003/08/07 11:13:43 agc Exp $  */
+/*     $NetBSD: option.c,v 1.20.4.1 2005/10/11 22:56:27 reed Exp $     */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "from: @(#)option.c     8.2 (Berkeley) 4/16/94";
 #else
-__RCSID("$NetBSD: option.c,v 1.20 2003/08/07 11:13:43 agc Exp $");
+__RCSID("$NetBSD: option.c,v 1.20.4.1 2005/10/11 22:56:27 reed Exp $");
 #endif
 #endif /* not lint */
 
@@ -74,6 +74,7 @@
        { "-execdir",   N_EXECDIR,      c_execdir,      1 },
        { "-flags",     N_FLAGS,        c_flags,        1 },
        { "-follow",    N_FOLLOW,       c_follow,       0 },
+       { "-fprint",    N_FPRINT,       c_fprint,       1 },
        { "-fstype",    N_FSTYPE,       c_fstype,       1 },
        { "-group",     N_GROUP,        c_group,        1 },
        { "-iname",     N_INAME,        c_iname,        1 },



Home | Main Index | Thread Index | Old Index