Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 8 Apr 2002 01:54:56 -0400 (EDT)
From:      Alan Eldridge <ports@geeksrus.net>
To:        FreeBSD-gnats-submit@FreeBSD.org
Cc:        imp@FreeBSD.org
Subject:   bin/36867: games/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely
Message-ID:  <200204080554.g385suf08156@wwweasel.geeksrus.net>

next in thread | raw e-mail | index | archive | help

>Number:         36867
>Category:       bin
>Synopsis:       games/fortune: add FORTUNE_PATH env var, so ports of fortunes can work sanely
>Confidential:   no
>Severity:       serious
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Apr 07 23:00:03 PDT 2002
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200204080554.g385suf08156>