From alane@wwweasel.geeksrus.net  Sun Apr  7 22:53:34 2002
Return-Path: <alane@wwweasel.geeksrus.net>
Received: from wwweasel.geeksrus.net (wwweasel.geeksrus.net [64.8.210.226])
	by hub.freebsd.org (Postfix) with ESMTP
	id 17BDE37B419; Sun,  7 Apr 2002 22:53:33 -0700 (PDT)
Received: (from alane@localhost)
	by wwweasel.geeksrus.net (8.11.6/8.11.6) id g385suf08156;
	Mon, 8 Apr 2002 01:54:56 -0400 (EDT)
	(envelope-from alane)
Message-Id: <200204080554.g385suf08156@wwweasel.geeksrus.net>
Date: Mon, 8 Apr 2002 01:54:56 -0400 (EDT)
From: Alan Eldridge <ports@geeksrus.net>
Reply-To: Alan Eldridge <ports@geeksrus.net>
To: FreeBSD-gnats-submit@freebsd.org
Cc: imp@freebsd.org
Subject: games/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         36867
>Category:       bin
>Synopsis:       [patch] games/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    edwin
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Apr 07 23:00:03 PDT 2002
>Closed-Date:    Mon Jul 14 10:49:54 UTC 2008
>Last-Modified:  Mon Jul 14 11:00:12 UTC 2008
>Originator:     Alan Eldridge
>Release:        FreeBSD 4.5-STABLE i386
>Organization:
Geeksrus.NET
>Environment:
System: FreeBSD wwweasel.geeksrus.net 4.5-STABLE FreeBSD 4.5-STABLE #0: Mon Mar 11 00:59:22 EST 2002 root@wwweasel.geeksrus.net:/usr/obj/usr/src/sys/WWWEASEL i386

>Description:

This is Cc'd to imp@, since that's the owner of the last change to
fortune/fortune.c.

This PR is marked serious because:

1. Right now, there is no way to add a port containing fortune files and
have it work transparently, since installing in /usr is frowned upon
for ports.

2. I have a port of fortune files I am waiting to submit until this
change goes into -STABLE; my alternative is to include a gross
kluge[1] script (as another port) to make it transparent. Yuck.

About the patch:

This patch adds an environment variable FORTUNE_PATH, which works like
PATH for fortune files.

The patch is kind of complex because of the grotesque nature of the
code, and the desire to neither break existing behavior or rewrite the
whole damned thing. IOW, it could've been a lot simpler, but that
would've meant rewriting the program.

[1] The is no "d" in Kluge. It's a brand name. 8=P
>How-To-Repeat:

>Fix:

There are two patches here, since there are both -stable and -current
versions of fortune.c.

Patch for -stable:
==8<====8<====8<====8<====8<====8<====8<====8<====8<====8<==
--- fortune/fortune.c.orig	Sun Jul  1 20:35:27 2001
+++ fortune/fortune.c	Sun Mar 31 04:50:55 2002
@@ -128,6 +128,9 @@
 
 STRFILE		Noprob_tbl;		/* sum of data for all no prob files */
 
+char	*Fortune_path;
+char	**Fortune_path_arr;
+
 int	 add_dir __P((FILEDESC *));
 int	 add_file __P((int,
 	    char *, char *, FILEDESC **, FILEDESC **, FILEDESC *));
@@ -142,6 +145,7 @@
 void	 get_pos __P((FILEDESC *));
 void	 get_tbl __P((FILEDESC *));
 void	 getargs __P((int, char *[]));
+void	 getpath __P((void));
 void	 init_prob __P((void));
 int	 is_dir __P((char *));
 int	 is_fortfile __P((char *, char **, char **, int));
@@ -196,6 +200,7 @@
 
 	(void) setlocale(LC_ALL, "");
 
+	getpath();
 	getargs(ac, av);
 
 #ifndef NO_REGEX
@@ -409,17 +414,36 @@
 {
 	int	i, percent;
 	char	*sp;
+	char	**pstr;
 
 	if (file_cnt == 0) {
 		if (Find_files) {
 			Fortunes_only = TRUE;
-			i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
-					&File_tail, NULL);
+			pstr = Fortune_path_arr;
+			i = 0;
+			while (*pstr) {
+				i += add_file(NO_PROB, *pstr++, NULL, 
+					      &File_list, &File_tail, NULL);
+			}
 			Fortunes_only = FALSE;
-			return i;
-		} else
-			return add_file(NO_PROB, "fortunes", FORTDIR,
-					&File_list, &File_tail, NULL);
+			if (!i) {
+			  fprintf(stderr, "No fortunes found in %s.\n",
+				  Fortune_path);
+			}
+			return i != 0;
+		} else {
+			pstr = Fortune_path_arr;
+			i = 0;
+			while (*pstr) {
+				i += add_file(NO_PROB, "fortunes", *pstr++,
+					      &File_list, &File_tail, NULL);
+			}
+			if (!i) {
+			  fprintf(stderr, "No fortunes found in %s.\n",
+				  Fortune_path);
+			}
+			return i != 0;
+		}
 	}
 	for (i = 0; i < file_cnt; i++) {
 		percent = NO_PROB;
@@ -454,10 +478,22 @@
 				sp = files[i];
 			}
 		}
-		if (strcmp(sp, "all") == 0)
-			sp = FORTDIR;
-		if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
+		if (strcmp(sp, "all") == 0) {
+			pstr = Fortune_path_arr;
+			i = 0;
+			while (*pstr) {
+				i += add_file(NO_PROB, *pstr++, NULL, 
+					      &File_list, &File_tail, NULL);
+			}
+			if (!i) {
+			  fprintf(stderr, "No fortunes found in %s.\n",
+				  Fortune_path);
+			  return FALSE;
+			}
+		} else if (!add_file(percent, sp, NULL, &File_list, 
+				     &File_tail, NULL)) {
 			return FALSE;
+		}
 	}
 	return TRUE;
 }
@@ -530,11 +566,24 @@
 			file = off_name(file);
 			goto over;
 		}
-		if (dir == NULL && file[0] != '/')
-			return add_file(percent, file, FORTDIR, head, tail,
-					parent);
+		if (dir == NULL && file[0] != '/') {
+			int i = 0;
+			char **pstr = Fortune_path_arr;
+
+			while (*pstr) {
+				i += add_file(percent, file, *pstr++, 
+					      head, tail, parent);
+			}
+			if (!i) {
+			  fprintf(stderr, "No '%s' found in %s.\n",
+				  file, Fortune_path);
+			}
+			return i != 0;
+		}
+		/*
 		if (parent == NULL)
 			perror(path);
+		*/
 		if (was_malloc)
 			free(path);
 		return FALSE;
@@ -1410,4 +1459,49 @@
 #endif	/* NO_REGEX */
 	(void) fprintf(stderr, "[[#%%] file/directory/all]\n");
 	exit(1);
+}
+
+/*
+ * getpath
+ * 	Set up file search patch from environment var FORTUNE_PATH;
+ *	if not set, use the compiled in FORTDIR.
+ */
+
+void
+getpath()
+{
+    int nstr;
+    char *pch, **ppch, *str, *path;
+
+    Fortune_path = getenv("FORTUNE_PATH");
+	
+    if (!Fortune_path) {
+	Fortune_path = "";
+    }
+    path = strdup(Fortune_path);
+
+    for (nstr = 2, pch = path; *pch; pch++) {
+	if (*pch == ':') {
+	    nstr++;
+	}
+    }
+
+    ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *));
+	
+    nstr = 0;
+    str = strtok(path, ":");
+    while (str) {
+	if (is_dir(str)) {
+	    nstr++;
+	    *ppch++ = str;
+	}
+	str = strtok(NULL, ":");
+    }
+    if (!nstr) {
+	free(path);
+	Fortune_path_arr[0] = FORTDIR;
+	if (strlen(Fortune_path)) {
+	    fprintf(stderr, "Ignoring FORTUNE_PATH; no directories found.\n");
+	}
+    }
 }
==8<====8<====8<====8<====8<====8<====8<====8<====8<====8<==

Patch for -current:
==8<====8<====8<====8<====8<====8<====8<====8<====8<====8<==
--- fortune/fortune.c.orig	Mon Apr  8 01:36:31 2002
+++ fortune/fortune.c	Mon Apr  8 01:38:19 2002
@@ -128,6 +128,9 @@
 
 STRFILE		Noprob_tbl;		/* sum of data for all no prob files */
 
+char	*Fortune_path;
+char	**Fortune_path_arr;
+
 int	 add_dir(FILEDESC *);
 int	 add_file __P((int,
 	    char *, char *, FILEDESC **, FILEDESC **, FILEDESC *));
@@ -142,6 +145,7 @@
 void	 get_pos(FILEDESC *);
 void	 get_tbl(FILEDESC *);
 void	 getargs(int, char *[]);
+void	 getpath(void);
 void	 init_prob(void);
 int	 is_dir(char *);
 int	 is_fortfile(char *, char **, char **, int);
@@ -196,6 +200,7 @@
 
 	(void) setlocale(LC_ALL, "");
 
+	getpath();
 	getargs(ac, av);
 
 #ifndef NO_REGEX
@@ -409,17 +414,36 @@
 {
 	int	i, percent;
 	char	*sp;
+	char	**pstr;
 
 	if (file_cnt == 0) {
 		if (Find_files) {
 			Fortunes_only = TRUE;
-			i = add_file(NO_PROB, FORTDIR, NULL, &File_list,
-					&File_tail, NULL);
+			pstr = Fortune_path_arr;
+			i = 0;
+			while (*pstr) {
+				i += add_file(NO_PROB, *pstr++, NULL, 
+					      &File_list, &File_tail, NULL);
+			}
 			Fortunes_only = FALSE;
-			return i;
-		} else
-			return add_file(NO_PROB, "fortunes", FORTDIR,
-					&File_list, &File_tail, NULL);
+			if (!i) {
+			  fprintf(stderr, "No fortunes found in %s.\n",
+				  Fortune_path);
+			}
+			return i != 0;
+		} else {
+			pstr = Fortune_path_arr;
+			i = 0;
+			while (*pstr) {
+				i += add_file(NO_PROB, "fortunes", *pstr++,
+					      &File_list, &File_tail, NULL);
+			}
+			if (!i) {
+			  fprintf(stderr, "No fortunes found in %s.\n",
+				  Fortune_path);
+			}
+			return i != 0;
+		}
 	}
 	for (i = 0; i < file_cnt; i++) {
 		percent = NO_PROB;
@@ -454,10 +478,22 @@
 				sp = files[i];
 			}
 		}
-		if (strcmp(sp, "all") == 0)
-			sp = FORTDIR;
-		if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
+		if (strcmp(sp, "all") == 0) {
+			pstr = Fortune_path_arr;
+			i = 0;
+			while (*pstr) {
+				i += add_file(NO_PROB, *pstr++, NULL, 
+					      &File_list, &File_tail, NULL);
+			}
+			if (!i) {
+			  fprintf(stderr, "No fortunes found in %s.\n",
+				  Fortune_path);
+			  return FALSE;
+			}
+		} else if (!add_file(percent, sp, NULL, &File_list, 
+				     &File_tail, NULL)) {
 			return FALSE;
+		}
 	}
 	return TRUE;
 }
@@ -530,11 +566,24 @@
 			file = off_name(file);
 			goto over;
 		}
-		if (dir == NULL && file[0] != '/')
-			return add_file(percent, file, FORTDIR, head, tail,
-					parent);
+		if (dir == NULL && file[0] != '/') {
+			int i = 0;
+			char **pstr = Fortune_path_arr;
+
+			while (*pstr) {
+				i += add_file(percent, file, *pstr++, 
+					      head, tail, parent);
+			}
+			if (!i) {
+			  fprintf(stderr, "No '%s' found in %s.\n",
+				  file, Fortune_path);
+			}
+			return i != 0;
+		}
+		/*
 		if (parent == NULL)
 			perror(path);
+		*/
 		if (was_malloc)
 			free(path);
 		return FALSE;
@@ -1410,4 +1459,49 @@
 #endif	/* NO_REGEX */
 	(void) fprintf(stderr, "[[#%%] file/directory/all]\n");
 	exit(1);
+}
+
+/*
+ * getpath
+ * 	Set up file search patch from environment var FORTUNE_PATH;
+ *	if not set, use the compiled in FORTDIR.
+ */
+
+void
+getpath()
+{
+    int nstr;
+    char *pch, **ppch, *str, *path;
+
+    Fortune_path = getenv("FORTUNE_PATH");
+	
+    if (!Fortune_path) {
+	Fortune_path = "";
+    }
+    path = strdup(Fortune_path);
+
+    for (nstr = 2, pch = path; *pch; pch++) {
+	if (*pch == ':') {
+	    nstr++;
+	}
+    }
+
+    ppch = Fortune_path_arr = (char **)calloc(nstr, sizeof(char *));
+	
+    nstr = 0;
+    str = strtok(path, ":");
+    while (str) {
+	if (is_dir(str)) {
+	    nstr++;
+	    *ppch++ = str;
+	}
+	str = strtok(NULL, ":");
+    }
+    if (!nstr) {
+	free(path);
+	Fortune_path_arr[0] = FORTDIR;
+	if (strlen(Fortune_path)) {
+	    fprintf(stderr, "Ignoring FORTUNE_PATH; no directories found.\n");
+	}
+    }
 }
==8<====8<====8<====8<====8<====8<====8<====8<====8<====8<==





>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->johan 
Responsible-Changed-By: johan 
Responsible-Changed-When: Tue Jul 23 08:07:39 PDT 2002 
Responsible-Changed-Why:  
I intend t have a look at this. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=36867 
Responsible-Changed-From-To: johan->freebsd-bugs 
Responsible-Changed-By: johan 
Responsible-Changed-When: Sun Nov 24 12:57:31 PST 2002 
Responsible-Changed-Why:  
Back to the pool since I lost interest. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=36867 
Responsible-Changed-From-To: freebsd-bugs->edwin 
Responsible-Changed-By: edwin 
Responsible-Changed-When: Sun Nov 4 10:56:08 UTC 2007 
Responsible-Changed-Why:  
Looks interesting. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=36867 
State-Changed-From-To: open->analyzed 
State-Changed-By: edwin 
State-Changed-When: Sun Nov 4 11:21:39 UTC 2007 
State-Changed-Why:  
Code still works, submitted to mentor 

http://www.freebsd.org/cgi/query-pr.cgi?pr=36867 
State-Changed-From-To: analyzed->patched 
State-Changed-By: edwin 
State-Changed-When: Tue Nov 6 22:03:39 UTC 2007 
State-Changed-Why:  
Commited to HEAD 

http://www.freebsd.org/cgi/query-pr.cgi?pr=36867 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/36867: commit references a PR
Date: Tue,  6 Nov 2007 22:04:14 +0000 (UTC)

 edwin       2007-11-06 22:03:24 UTC
 
   FreeBSD src repository
 
   Modified files:
     games/fortune/fortune fortune.c 
   Log:
   games/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely
   
           This patch adds an environment variable FORTUNE_PATH, which
           works like PATH for fortune files.
   
   PR:             bin/36867
   Submitted by:   Alan Eldridge <ports@geeksrus.net>
   
   [patch] fortune -e implementation bug
   
           Fix the behaviour of "-e file1 file2" to equally pick them
           instead of only picking the first one.
   
   PR:             bin/70182
   Submitted by:   Martin Kulas <coolaz@web.de>
   
   MFC after:      1 week
   Approved by:    grog (mentor)
   
   Revision  Changes    Path
   1.30      +106 -14   src/games/fortune/fortune/fortune.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 

From: Edwin Groothuis <edwin@freebsd.org>
To: FreeBSD Gnats Submit <freebsd-gnats-submit@freebsd.org>
Cc:  
Subject: Re: bin/36867: [patch] games/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely
Date: Wed, 7 Nov 2007 09:41:58 +1100

 Patch for man-page
 
 diff --git a/games/fortune/fortune/fortune.6 b/games/fortune/fortune/fortune.6
 index 969079a..440449d 100644
 --- a/games/fortune/fortune/fortune.6
 +++ b/games/fortune/fortune/fortune.6
 @@ -167,6 +167,14 @@ is equivalent to
  .Bd -literal -offset indent
  fortune 50% funny 50% not-funny
  .Ed
 +.Sh ENVIRONMENT
 +.Bl -tag -width Pa -compact
 +.It FORTUNE_PATH
 +The search path for the data files. It is a colon-separated list
 +of directories in which
 +.Nm
 +looks for data files. If not set, it will default to /usr/games/fortune.
 +.El
  .Sh FILES
  .Bl -tag -width Pa -compact
  .It Pa /usr/games/fortune
 
 -- 
 Edwin Groothuis
 edwin@freebsd.org
 http://www.mavetju.org
State-Changed-From-To: patched->closed 
State-Changed-By: edwin 
State-Changed-When: Mon Jul 14 10:49:48 UTC 2008 
State-Changed-Why:  
Committed, thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=36867 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/36867: commit references a PR
Date: Mon, 14 Jul 2008 10:49:19 +0000 (UTC)

 edwin       2008-07-14 10:49:01 UTC
 
   FreeBSD src repository
 
   Modified files:        (Branch: RELENG_7)
     games/fortune/fortune fortune.6 fortune.c 
   Log:
   SVN rev 180505 on 2008-07-14 10:49:01Z by edwin
   
   MFC of revision 173401, 173414 and 173396:
   
   gaames/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely
   
           This patch adds an environment variable FORTUNE_PATH, which
           works like PATH for fortune files.
   
   PR:             bin/36867
   Submitted by:   Alan Eldridge <ports@geeksrus.net>
   
   [patch] fortune -e implementation bug
   
           Fix the behaviour of "-e file1 file2" to equally pick them
           instead of only picking the first one.
   
   PR:             bin/70182
   Submitted by:   Martin Kulas <coolaz@web.de>
   
   Revision   Changes    Path
   1.20.10.1  +36 -31    src/games/fortune/fortune/fortune.6
   1.29.2.1   +117 -19   src/games/fortune/fortune/fortune.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/36867: commit references a PR
Date: Mon, 14 Jul 2008 10:50:03 +0000 (UTC)

 edwin       2008-07-14 10:49:32 UTC
 
   FreeBSD src repository
 
   Modified files:        (Branch: RELENG_6)
     games/fortune/fortune fortune.6 fortune.c 
   Log:
   SVN rev 180506 on 2008-07-14 10:49:32Z by edwin
   
   MFC of revision 173401, 173414 and 173396:
   
   gaames/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely
   
           This patch adds an environment variable FORTUNE_PATH, which
           works like PATH for fortune files.
   
   PR:             bin/36867
   Submitted by:   Alan Eldridge <ports@geeksrus.net>
   
   [patch] fortune -e implementation bug
   
           Fix the behaviour of "-e file1 file2" to equally pick them
           instead of only picking the first one.
   
   PR:             bin/70182
   Submitted by:   Martin Kulas <coolaz@web.de>
   
   Revision  Changes    Path
   1.20.2.1  +36 -31    src/games/fortune/fortune/fortune.6
   1.27.2.1  +117 -19   src/games/fortune/fortune/fortune.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
>Unformatted:
Add "[patch]" to synopsis -- vs@2004-07-29
