Subject: Re: multi find with different file outputs
To: der Mouse <mouse@Rodents.Montreal.QC.CA>
From: Jeremy C. Reed <reed@reedmedia.net>
List: tech-userlevel
Date: 10/06/2005 13:26:44
On Tue, 4 Oct 2005, der Mouse wrote:

...
> All of these problems would be fixed by opening the output file at
> startup rather than delaying.  The need to getcwd() (and the associated
> error condition), the untouched-if-nothing-printed surprise, and the
> race with the path to cwd no longer referring to it, all those go away
> if this is done.

Thank you for your many comments. I have simplified it much.

? usr.bin/find/)

This parenthesis file above was created by forgetting to put in file name 
after -fprint. find(1) still errored because of now missing unbalanced 
")". Should I care about this?

Now creating file at expression parsing time, creates files even if the 
find(1) expressions end up erroring out. For example:

rainier:/usr/src/src/usr.bin/find$ ./find /etc -fprint ABC -junk
find: -junk: unknown option
rainier:/usr/src/src/usr.bin/find$ ls -l ABC
-rw-r--r--  1 reed  wsrc  0 Oct  6 13:25 ABC

Is the above okay behaviour?

The new revised patch is below:

cvs diff: Diffing usr.bin/find
Index: usr.bin/find/Makefile
===================================================================
RCS file: /cvsroot/src/usr.bin/find/Makefile,v
retrieving revision 1.9
diff -b -u -r1.9 Makefile
--- usr.bin/find/Makefile	18 Sep 2002 14:00:36 -0000	1.9
+++ usr.bin/find/Makefile	6 Oct 2005 20:21:41 -0000
@@ -4,7 +4,7 @@
  .include <bsd.own.mk>

  PROG=	find
-SRCS=	find.c function.c ls.c main.c misc.c operator.c option.c stat_flags.c
+SRCS=	find.c function.c ls.c main.c misc.c operator.c option.c ../../bin/ls/stat_flags.c

  CPPFLAGS+=	-I${NETBSDSRCDIR}/bin/ls
  .PATH:	${NETBSDSRCDIR}/bin/ls
Index: usr.bin/find/extern.h
===================================================================
RCS file: /cvsroot/src/usr.bin/find/extern.h,v
retrieving revision 1.20
diff -b -u -r1.20 extern.h
--- usr.bin/find/extern.h	7 Aug 2003 11:13:40 -0000	1.20
+++ usr.bin/find/extern.h	6 Oct 2005 20:21:41 -0000
@@ -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));
Index: usr.bin/find/find.1
===================================================================
RCS file: /cvsroot/src/usr.bin/find/find.1,v
retrieving revision 1.47.2.1
diff -b -u -r1.47.2.1 find.1
--- usr.bin/find/find.1	31 Mar 2004 18:08:25 -0000	1.47.2.1
+++ usr.bin/find/find.1	6 Oct 2005 20:21:43 -0000
@@ -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 .
Index: usr.bin/find/find.h
===================================================================
RCS file: /cvsroot/src/usr.bin/find/find.h,v
retrieving revision 1.18
diff -b -u -r1.18 find.h
--- usr.bin/find/find.h	7 Aug 2003 11:13:41 -0000	1.18
+++ usr.bin/find/find.h	6 Oct 2005 20:21:43 -0000
@@ -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 */
Index: usr.bin/find/function.c
===================================================================
RCS file: /cvsroot/src/usr.bin/find/function.c,v
retrieving revision 1.46
diff -b -u -r1.46 function.c
--- usr.bin/find/function.c	7 Aug 2003 11:13:41 -0000	1.46
+++ usr.bin/find/function.c	6 Oct 2005 20:21:46 -0000
@@ -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,41 @@
  	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;
+
+	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 --
   *
Index: usr.bin/find/option.c
===================================================================
RCS file: /cvsroot/src/usr.bin/find/option.c,v
retrieving revision 1.20
diff -b -u -r1.20 option.c
--- usr.bin/find/option.c	7 Aug 2003 11:13:43 -0000	1.20
+++ usr.bin/find/option.c	6 Oct 2005 20:21:46 -0000
@@ -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 },

  Jeremy C. Reed

  	  	 	 technical support & remote administration
 	  	 	 http://www.pugetsoundtechnology.com/