summaryrefslogtreecommitdiffstats
path: root/games
diff options
context:
space:
mode:
authoredwin <edwin@FreeBSD.org>2007-11-06 22:03:24 +0000
committeredwin <edwin@FreeBSD.org>2007-11-06 22:03:24 +0000
commit6aff7d5aab6bb38249f40350f4fdd24eb97c9dba (patch)
tree607e7c398671ce89fa800384a1c2aa924fabfc87 /games
parent9f312690bcb03f7e85189b0efa3824426a7e4e00 (diff)
downloadFreeBSD-src-6aff7d5aab6bb38249f40350f4fdd24eb97c9dba.zip
FreeBSD-src-6aff7d5aab6bb38249f40350f4fdd24eb97c9dba.tar.gz
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)
Diffstat (limited to 'games')
-rw-r--r--games/fortune/fortune/fortune.c120
1 files changed, 106 insertions, 14 deletions
diff --git a/games/fortune/fortune/fortune.c b/games/fortune/fortune/fortune.c
index a7a8f45..3aa27a4 100644
--- a/games/fortune/fortune/fortune.c
+++ b/games/fortune/fortune/fortune.c
@@ -126,6 +126,9 @@ FILEDESC *Fortfile; /* Fortune file to use */
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 *));
@@ -140,6 +143,7 @@ void get_fort(void);
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);
@@ -177,6 +181,7 @@ char *av[];
(void) setlocale(LC_ALL, "");
+ getpath();
getargs(ac, av);
if (Match)
@@ -374,17 +379,36 @@ int file_cnt;
{
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;
@@ -419,10 +443,22 @@ int file_cnt;
sp = files[i];
}
}
- if (strcmp(sp, "all") == 0)
- sp = FORTDIR;
- if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL))
- return FALSE;
+ 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;
}
@@ -495,11 +531,24 @@ over:
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;
@@ -898,7 +947,7 @@ init_prob()
if (num_noprob > 1) {
frac = percent / num_noprob;
DPRINTF(1, (stderr, ", frac = %d%%", frac));
- for (fp = File_list; fp != last; fp = fp->next)
+ for (fp = File_tail; fp != last; fp = fp->prev)
if (fp->percent == NO_PROB) {
fp->percent = frac;
percent -= frac;
@@ -1368,3 +1417,46 @@ usage()
(void) fprintf(stderr, " [[N%%] 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(void)
+{
+ int nstr;
+ char *pch, **ppch, *str, *path;
+
+ Fortune_path = getenv("FORTUNE_PATH");
+
+ if (Fortune_path == NULL)
+ Fortune_path = "";
+ path = strdup(Fortune_path);
+
+ for (nstr = 2, pch = path; *pch != '\0'; 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 == 0) {
+ free(path);
+ Fortune_path_arr[0] = FORTDIR;
+ if (strlen(Fortune_path))
+ fprintf(stderr,
+ "Ignoring FORTUNE_PATH; no directories found.\n");
+ }
+}
OpenPOWER on IntegriCloud