After discussions with grog@ (mentor):

- When none of the directories in FORTUNE_PATH exist, abort instead
  of using the default FORTDIR.
- Little stylify changes.
- Add documentation about the FORTUNE_PATH variable.

MFC after:	1 week
This commit is contained in:
Edwin Groothuis 2007-11-07 01:14:28 +00:00
parent f61b0b51ed
commit e46d677790
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=173401
2 changed files with 37 additions and 22 deletions

View File

@ -167,6 +167,15 @@ is equivalent to
.Bd -literal -offset indent .Bd -literal -offset indent
fortune 50% funny 50% not-funny fortune 50% funny 50% not-funny
.Ed .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.
If none of the directories specified exist, it will print a warning and exit.
.El
.Sh FILES .Sh FILES
.Bl -tag -width Pa -compact .Bl -tag -width Pa -compact
.It Pa /usr/games/fortune .It Pa /usr/games/fortune

View File

@ -263,7 +263,7 @@ int
fortlen() fortlen()
{ {
int nchar; int nchar;
char line[BUFSIZ]; char line[BUFSIZ];
if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED))) if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
nchar = (int)(Seekpts[1] - Seekpts[0]); nchar = (int)(Seekpts[1] - Seekpts[0]);
@ -392,8 +392,8 @@ int file_cnt;
} }
Fortunes_only = FALSE; Fortunes_only = FALSE;
if (!i) { if (!i) {
fprintf(stderr, "No fortunes found in %s.\n", fprintf(stderr, "No fortunes found in %s.\n",
Fortune_path); Fortune_path);
} }
return i != 0; return i != 0;
} else { } else {
@ -404,8 +404,8 @@ int file_cnt;
&File_list, &File_tail, NULL); &File_list, &File_tail, NULL);
} }
if (!i) { if (!i) {
fprintf(stderr, "No fortunes found in %s.\n", fprintf(stderr, "No fortunes found in %s.\n",
Fortune_path); Fortune_path);
} }
return i != 0; return i != 0;
} }
@ -451,9 +451,9 @@ int file_cnt;
&File_list, &File_tail, NULL); &File_list, &File_tail, NULL);
} }
if (!i) { if (!i) {
fprintf(stderr, "No fortunes found in %s.\n", fprintf(stderr, "No fortunes found in %s.\n",
Fortune_path); Fortune_path);
return FALSE; return FALSE;
} }
} else if (!add_file(percent, sp, NULL, &File_list, } else if (!add_file(percent, sp, NULL, &File_list,
&File_tail, NULL)) { &File_tail, NULL)) {
@ -470,7 +470,7 @@ int file_cnt;
int int
add_file(percent, file, dir, head, tail, parent) add_file(percent, file, dir, head, tail, parent)
int percent; int percent;
char *file; char *file;
char *dir; char *dir;
FILEDESC **head, **tail; FILEDESC **head, **tail;
FILEDESC *parent; FILEDESC *parent;
@ -540,8 +540,8 @@ FILEDESC *parent;
head, tail, parent); head, tail, parent);
} }
if (!i) { if (!i) {
fprintf(stderr, "No '%s' found in %s.\n", fprintf(stderr, "No '%s' found in %s.\n",
file, Fortune_path); file, Fortune_path);
} }
return i != 0; return i != 0;
} }
@ -667,7 +667,7 @@ char *file;
void void
all_forts(fp, offensive) all_forts(fp, offensive)
FILEDESC *fp; FILEDESC *fp;
char *offensive; char *offensive;
{ {
char *sp; char *sp;
FILEDESC *scene, *obscene; FILEDESC *scene, *obscene;
@ -720,8 +720,8 @@ FILEDESC *fp;
{ {
DIR *dir; DIR *dir;
struct dirent *dirent; struct dirent *dirent;
auto FILEDESC *tailp; auto FILEDESC *tailp;
auto char *name; auto char *name;
(void) close(fp->fd); (void) close(fp->fd);
fp->fd = -1; fp->fd = -1;
@ -1427,13 +1427,15 @@ usage()
void void
getpath(void) getpath(void)
{ {
int nstr; int nstr, foundenv;
char *pch, **ppch, *str, *path; char *pch, **ppch, *str, *path;
foundenv = 1;
Fortune_path = getenv("FORTUNE_PATH"); Fortune_path = getenv("FORTUNE_PATH");
if (Fortune_path == NULL) {
if (Fortune_path == NULL) Fortune_path = FORTDIR;
Fortune_path = ""; foundenv = 0;
}
path = strdup(Fortune_path); path = strdup(Fortune_path);
for (nstr = 2, pch = path; *pch != '\0'; pch++) { for (nstr = 2, pch = path; *pch != '\0'; pch++) {
@ -1452,11 +1454,15 @@ getpath(void)
} }
str = strtok(NULL, ":"); str = strtok(NULL, ":");
} }
if (nstr == 0) { if (nstr == 0) {
if (foundenv == 1) {
fprintf(stderr,
"fortune: FORTUNE_PATH: None of the specified "
"directories found.\n");
exit(1);
}
free(path); free(path);
Fortune_path_arr[0] = FORTDIR; Fortune_path_arr[0] = FORTDIR;
if (strlen(Fortune_path))
fprintf(stderr,
"Ignoring FORTUNE_PATH; no directories found.\n");
} }
} }