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)
This commit is contained in:
Edwin Groothuis 2007-11-06 22:03:24 +00:00
parent d21d8773c4
commit 664c060ef3
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=173396

View File

@ -126,6 +126,9 @@ FILEDESC *Fortfile; /* Fortune file to use */
STRFILE Noprob_tbl; /* sum of data for all no prob files */ STRFILE Noprob_tbl; /* sum of data for all no prob files */
char *Fortune_path;
char **Fortune_path_arr;
int add_dir(FILEDESC *); int add_dir(FILEDESC *);
int add_file __P((int, int add_file __P((int,
char *, char *, FILEDESC **, FILEDESC **, FILEDESC *)); char *, char *, FILEDESC **, FILEDESC **, FILEDESC *));
@ -140,6 +143,7 @@ void get_fort(void);
void get_pos(FILEDESC *); void get_pos(FILEDESC *);
void get_tbl(FILEDESC *); void get_tbl(FILEDESC *);
void getargs(int, char *[]); void getargs(int, char *[]);
void getpath(void);
void init_prob(void); void init_prob(void);
int is_dir(char *); int is_dir(char *);
int is_fortfile(char *, char **, char **, int); int is_fortfile(char *, char **, char **, int);
@ -177,6 +181,7 @@ char *av[];
(void) setlocale(LC_ALL, ""); (void) setlocale(LC_ALL, "");
getpath();
getargs(ac, av); getargs(ac, av);
if (Match) if (Match)
@ -374,17 +379,36 @@ int file_cnt;
{ {
int i, percent; int i, percent;
char *sp; char *sp;
char **pstr;
if (file_cnt == 0) { if (file_cnt == 0) {
if (Find_files) { if (Find_files) {
Fortunes_only = TRUE; Fortunes_only = TRUE;
i = add_file(NO_PROB, FORTDIR, NULL, &File_list, pstr = Fortune_path_arr;
&File_tail, NULL); i = 0;
while (*pstr) {
i += add_file(NO_PROB, *pstr++, NULL,
&File_list, &File_tail, NULL);
}
Fortunes_only = FALSE; Fortunes_only = FALSE;
return i; if (!i) {
} else fprintf(stderr, "No fortunes found in %s.\n",
return add_file(NO_PROB, "fortunes", FORTDIR, Fortune_path);
&File_list, &File_tail, NULL); }
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++) { for (i = 0; i < file_cnt; i++) {
percent = NO_PROB; percent = NO_PROB;
@ -419,10 +443,22 @@ int file_cnt;
sp = files[i]; sp = files[i];
} }
} }
if (strcmp(sp, "all") == 0) if (strcmp(sp, "all") == 0) {
sp = FORTDIR; pstr = Fortune_path_arr;
if (!add_file(percent, sp, NULL, &File_list, &File_tail, NULL)) i = 0;
return FALSE; 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; return TRUE;
} }
@ -495,11 +531,24 @@ FILEDESC *parent;
file = off_name(file); file = off_name(file);
goto over; goto over;
} }
if (dir == NULL && file[0] != '/') if (dir == NULL && file[0] != '/') {
return add_file(percent, file, FORTDIR, head, tail, int i = 0;
parent); 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) if (parent == NULL)
perror(path); perror(path);
*/
if (was_malloc) if (was_malloc)
free(path); free(path);
return FALSE; return FALSE;
@ -898,7 +947,7 @@ init_prob()
if (num_noprob > 1) { if (num_noprob > 1) {
frac = percent / num_noprob; frac = percent / num_noprob;
DPRINTF(1, (stderr, ", frac = %d%%", frac)); 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) { if (fp->percent == NO_PROB) {
fp->percent = frac; fp->percent = frac;
percent -= frac; percent -= frac;
@ -1368,3 +1417,46 @@ usage()
(void) fprintf(stderr, " [[N%%] file/directory/all]\n"); (void) fprintf(stderr, " [[N%%] file/directory/all]\n");
exit(1); 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");
}
}