[Midnightbsd-cvs] src [8353] trunk/games/fortune/fortune: sync with freebsd 9 stable
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Sun Sep 18 14:31:16 EDT 2016
Revision: 8353
http://svnweb.midnightbsd.org/src/?rev=8353
Author: laffer1
Date: 2016-09-18 14:31:16 -0400 (Sun, 18 Sep 2016)
Log Message:
-----------
sync with freebsd 9 stable
Modified Paths:
--------------
trunk/games/fortune/fortune/Makefile
trunk/games/fortune/fortune/fortune.6
trunk/games/fortune/fortune/fortune.c
trunk/games/fortune/fortune/pathnames.h
Modified: trunk/games/fortune/fortune/Makefile
===================================================================
--- trunk/games/fortune/fortune/Makefile 2016-09-18 18:27:51 UTC (rev 8352)
+++ trunk/games/fortune/fortune/Makefile 2016-09-18 18:31:16 UTC (rev 8353)
@@ -1,10 +1,8 @@
# @(#)Makefile 8.1 (Berkeley) 5/31/93
-# $FreeBSD: src/games/fortune/fortune/Makefile,v 1.8 2005/02/17 18:06:37 ru Exp $
# $MidnightBSD$
PROG= fortune
MAN= fortune.6
-WARNS?= 2
CFLAGS+=-DDEBUG -I${.CURDIR}/../strfile
.include <bsd.prog.mk>
Modified: trunk/games/fortune/fortune/fortune.6
===================================================================
--- trunk/games/fortune/fortune/fortune.6 2016-09-18 18:27:51 UTC (rev 8352)
+++ trunk/games/fortune/fortune/fortune.6 2016-09-18 18:31:16 UTC (rev 8353)
@@ -1,42 +1,37 @@
-." Copyright (c) 1985, 1991, 1993
-." The Regents of the University of California. All rights reserved.
-."
-." This code is derived from software contributed to Berkeley by
-." Ken Arnold.
-."
-." Redistribution and use in source and binary forms, with or without
-." modification, are permitted provided that the following conditions
-." are met:
-." 1. Redistributions of source code must retain the above copyright
-." notice, this list of conditions and the following disclaimer.
-." 2. Redistributions in binary form must reproduce the above copyright
-." notice, this list of conditions and the following disclaimer in the
-." documentation and/or other materials provided with the distribution.
-." 3. All advertising materials mentioning features or use of this software
-." must display the following acknowledgement:
-." This product includes software developed by the University of
-." California, Berkeley and its contributors.
-." 4. Neither the name of the University nor the names of its contributors
-." may be used to endorse or promote products derived from this software
-." without specific prior written permission.
-."
-." THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
-." ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-." IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-." ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
-." FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-." DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-." OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-." HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-." LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-." OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-." SUCH DAMAGE.
-."
-." @(#)fortune.6 8.3 (Berkeley) 4/19/94
-." $FreeBSD: src/games/fortune/fortune/fortune.6,v 1.20 2005/02/13 23:45:45 ru Exp $
-." $MidnightBSD$
-."
-.Dd April 19, 1994
+.\" Copyright (c) 1985, 1991, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software contributed to Berkeley by
+.\" Ken Arnold.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 3. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" @(#)fortune.6 8.3 (Berkeley) 4/19/94
+.\" $MidnightBSD$
+.\"
+.Dd November 7, 2007
.Dt FORTUNE 6
.Os
.Sh NAME
@@ -47,7 +42,7 @@
.Op Fl aDefilosw
.Op Fl m Ar pattern
.Oo
-.Op Ar &N%
+.Op Ar \&N%
.Ar file Ns / Ns Ar directory Ns / Ns Cm all
.Oc
.Sh DESCRIPTION
@@ -58,7 +53,7 @@
is subdivided into those which are potentially offensive and those
which are not.
The options are as follows:
-.Bl -tag -width flag
+.Bl -tag -width indent
.It Fl a
Choose from all lists of maxims, both offensive and not.
(See the
@@ -67,7 +62,8 @@
.It Fl D
Enable additional debugging output.
Specify this option multiple times for more verbose output.
-Only available if compiled with -DDEBUG.
+Only available if compiled with
+.Li -DDEBUG .
.It Fl e
Consider all fortune files to be of equal size (see discussion below
on multiple files).
@@ -76,7 +72,7 @@
print a fortune.
.It Fl l
Long dictums only.
-.It Fl m
+.It Fl m Ar pattern
Print out all fortunes which match the regular expression
.Ar pattern .
See
@@ -94,12 +90,12 @@
grief about it, okay?)
.Ef
.Bd -unfilled -offset indent
-&... let us keep in mind the basic governing philosophy
+\&... let us keep in mind the basic governing philosophy
of The Brotherhood, as handsomely summarized in these words:
we believe in healthy, hearty laughter -- at the expense of
the whole human race, if needs be.
Needs be.
---H. Allen Smith, "Rude Jokes"
+ --H. Allen Smith, "Rude Jokes"
.Ed
.It Fl s
Short apothegms only.
@@ -117,13 +113,13 @@
The user may specify alternate sayings.
You can specify a specific file, a directory which contains one or
more files, or the special word
-.Em all
+.Cm all
which says to use all the standard databases.
Any of these may be preceded by a percentage, which is a number
.Ar N
between 0 and 100 inclusive, followed by a
-.Ar % .
-If it is, there will be a
+.Ql % .
+If it is, there will be an
.Ar N
percent probability that an adage will be picked from that file
or directory.
@@ -133,55 +129,63 @@
one of them will be based on their relative sizes.
.Pp
As an example, given two databases
-.Em funny
+.Pa funny
and
-.Em not-funny ,
+.Pa not-funny ,
with
-.Em funny
+.Pa funny
twice as big, saying
-.Bd -literal -offset indent
-fortune funny not-funny
-.Ed
.Pp
+.Dl "fortune funny not-funny"
+.Pp
will get you fortunes out of
-.Em funny
+.Pa funny
two-thirds of the time.
The command
-.Bd -literal -offset indent
-fortune 90% funny 10% not-funny
-.Ed
.Pp
+.Dl "fortune 90% funny 10% not-funny"
+.Pp
will pick out 90% of its fortunes from
-.Em funny
+.Pa funny
(the
-.Dq 10% not-funny
+.Dq Li "10% not-funny"
is unnecessary, since 10% is all that is left).
The
.Fl e
option says to consider all files equal;
thus
-.Bd -literal -offset indent
-fortune -e funny not-funny
-.Ed
.Pp
+.Dl "fortune -e funny not-funny"
+.Pp
is equivalent to
-.Bd -literal -offset indent
-fortune 50% funny 50% not-funny
-.Ed
+.Pp
+.Dl "fortune 50% funny 50% not-funny"
+.Sh ENVIRONMENT
+.Bl -tag -width ".Ev FORTUNE_PATH"
+.It Ev 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
+.Pa /usr/games/fortune .
+If none of the directories specified exist, it will print a warning and exit.
+.It Ev FORTUNE_SAVESTATE
+If set, fortune will save some state about what fortune
+it was up to on disk.
+.El
.Sh FILES
-.Bl -tag -width Pa -compact
+.Bl -tag -width ".Pa /usr/share/games/fortune/*"
.It Pa /usr/games/fortune
-.Pp
.It Pa /usr/share/games/fortune/*
the fortunes databases (those files ending
-.Dq -o
+.Dq Pa -o
contain the
-.Bf -symbolic
-offensive
-.Ef
+.Sy offensive
fortunes)
.El
.Sh SEE ALSO
+.Xr arc4random_uniform 3 ,
.Xr regcomp 3 ,
.Xr regex 3 ,
.Xr strfile 8
Modified: trunk/games/fortune/fortune/fortune.c
===================================================================
--- trunk/games/fortune/fortune/fortune.c 2016-09-18 18:27:51 UTC (rev 8352)
+++ trunk/games/fortune/fortune/fortune.c 2016-09-18 18:31:16 UTC (rev 8353)
@@ -13,11 +13,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
@@ -46,52 +42,52 @@
#endif /* not lint */
#endif
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/games/fortune/fortune/fortune.c,v 1.27 2005/02/17 18:06:37 ru Exp $");
__MBSDID("$MidnightBSD$");
-# include <sys/stat.h>
-# include <sys/endian.h>
+#include <sys/stat.h>
+#include <sys/endian.h>
-# include <dirent.h>
-# include <fcntl.h>
-# include <assert.h>
-# include <unistd.h>
-# include <stdio.h>
-# include <ctype.h>
-# include <stdlib.h>
-# include <stdbool.h>
-# include <string.h>
-# include <locale.h>
-# include <time.h>
-# include <regex.h>
-# include "strfile.h"
-# include "pathnames.h"
+#include <assert.h>
+#include <ctype.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <locale.h>
+#include <regex.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
-# define TRUE true
-# define FALSE false
+#include "strfile.h"
+#include "pathnames.h"
-# define MINW 6 /* minimum wait if desired */
-# define CPERS 20 /* # of chars for each sec */
-# define SLEN 160 /* # of chars in short fortune */
+#define TRUE true
+#define FALSE false
-# define POS_UNKNOWN ((uint32_t) -1) /* pos for file unknown */
-# define NO_PROB (-1) /* no prob specified for file */
+#define MINW 6 /* minimum wait if desired */
+#define CPERS 20 /* # of chars for each sec */
+#define SLEN 160 /* # of chars in short fortune */
-# ifdef DEBUG
-# define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
-# undef NDEBUG
-# else
-# define DPRINTF(l,x)
-# define NDEBUG 1
-# endif
+#define POS_UNKNOWN ((uint32_t) -1) /* pos for file unknown */
+#define NO_PROB (-1) /* no prob specified for file */
+#ifdef DEBUG
+#define DPRINTF(l,x) { if (Debug >= l) fprintf x; }
+#undef NDEBUG
+#else
+#define DPRINTF(l,x)
+#define NDEBUG 1
+#endif
+
typedef struct fd {
int percent;
int fd, datfd;
uint32_t pos;
FILE *inf;
- char *name;
- char *path;
+ const char *name;
+ const char *path;
char *datfile, *posfile;
bool read_tbl;
bool was_pos_file;
@@ -101,87 +97,94 @@
struct fd *next, *prev;
} FILEDESC;
-static bool Found_one; /* did we find a match? */
-static bool Find_files = FALSE; /* just find a list of proper fortune files */
-static bool Fortunes_only = FALSE; /* check only "fortunes" files */
-static bool Wait = FALSE; /* wait desired after fortune */
-static bool Short_only = FALSE; /* short fortune desired */
-static bool Long_only = FALSE; /* long fortune desired */
-static bool Offend = FALSE; /* offensive fortunes only */
-static bool All_forts = FALSE; /* any fortune allowed */
-static bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
-static bool Match = FALSE; /* dump fortunes matching a pattern */
+bool Found_one; /* did we find a match? */
+bool Find_files = FALSE; /* just find a list of proper fortune files */
+bool Fortunes_only = FALSE; /* check only "fortunes" files */
+bool Wait = FALSE; /* wait desired after fortune */
+bool Short_only = FALSE; /* short fortune desired */
+bool Long_only = FALSE; /* long fortune desired */
+bool Offend = FALSE; /* offensive fortunes only */
+bool All_forts = FALSE; /* any fortune allowed */
+bool Equal_probs = FALSE; /* scatter un-allocted prob equally */
+bool Match = FALSE; /* dump fortunes matching a pattern */
+static bool WriteToDisk = false; /* use files on disk to save state */
#ifdef DEBUG
-static bool Debug = FALSE; /* print debug messages */
+int Debug = FALSE; /* print debug messages */
#endif
-static char *Fortbuf = NULL; /* fortune buffer for -m */
+char *Fortbuf = NULL; /* fortune buffer for -m */
-static int Fort_len = 0;
+int Fort_len = 0;
-static off_t Seekpts[2]; /* seek pointers to fortunes */
+off_t Seekpts[2]; /* seek pointers to fortunes */
-static FILEDESC *File_list = NULL, /* Head of file list */
+FILEDESC *File_list = NULL, /* Head of file list */
*File_tail = NULL; /* Tail of file list */
-static FILEDESC *Fortfile; /* Fortune file to use */
+FILEDESC *Fortfile; /* Fortune file to use */
-static STRFILE Noprob_tbl; /* sum of data for all no prob files */
+STRFILE Noprob_tbl; /* sum of data for all no prob files */
-static int add_dir(FILEDESC *);
-static int add_file(int,
- char *, char *, FILEDESC **, FILEDESC **, FILEDESC *);
-static void all_forts(FILEDESC *, char *);
-static char *copy(char *, u_int);
-static void display(FILEDESC *);
-static void *do_malloc(u_int);
-static int form_file_list(char **, int);
-static int fortlen(void);
-static void get_fort(void);
-static void get_pos(FILEDESC *);
-static void get_tbl(FILEDESC *);
-static void getargs(int, char *[]);
-static void init_prob(void);
-static int is_dir(char *);
-static int is_fortfile(char *, char **, char **, int);
-static int is_off_name(char *);
-static int max(int, int);
-static FILEDESC *
+const char *Fortune_path;
+char **Fortune_path_arr;
+
+int add_dir(FILEDESC *);
+int add_file(int, const char *, const char *, FILEDESC **, FILEDESC **,
+ FILEDESC *);
+void all_forts(FILEDESC *, char *);
+char *copy(const char *, u_int);
+void display(FILEDESC *);
+void do_free(void *);
+void *do_malloc(u_int);
+int form_file_list(char **, int);
+int fortlen(void);
+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(const char *);
+int is_fortfile(const char *, char **, char **, int);
+int is_off_name(const char *);
+int max(int, int);
+FILEDESC *
new_fp(void);
-static char *off_name(char *);
-static void open_dat(FILEDESC *);
-static void open_fp(FILEDESC *);
-static FILEDESC *
+char *off_name(const char *);
+void open_dat(FILEDESC *);
+void open_fp(FILEDESC *);
+FILEDESC *
pick_child(FILEDESC *);
-static void print_file_list(void);
-static void print_list(FILEDESC *, int);
-static void sum_noprobs(FILEDESC *);
-static void sum_tbl(STRFILE *, STRFILE *);
-static void usage(void);
-static void zero_tbl(STRFILE *);
+void print_file_list(void);
+void print_list(FILEDESC *, int);
+void sum_noprobs(FILEDESC *);
+void sum_tbl(STRFILE *, STRFILE *);
+void usage(void);
+void zero_tbl(STRFILE *);
-static char *conv_pat(char *);
-static int find_matches(void);
-static void matches_in_list(FILEDESC *);
-static int maxlen_in_list(FILEDESC *);
+char *conv_pat(char *);
+int find_matches(void);
+void matches_in_list(FILEDESC *);
+int maxlen_in_list(FILEDESC *);
static regex_t Re_pat;
int
-main(int ac, char *av[])
+main(int argc, char *argv[])
{
-#ifdef OK_TO_WRITE_DISK
int fd;
-#endif /* OK_TO_WRITE_DISK */
+ if (getenv("FORTUNE_SAVESTATE") != NULL)
+ WriteToDisk = true;
+
(void) setlocale(LC_ALL, "");
- getargs(ac, av);
+ getpath();
+ getargs(argc, argv);
if (Match)
exit(find_matches() != 0);
init_prob();
- srandomdev();
do {
get_fort();
} while ((Short_only && fortlen() > SLEN) ||
@@ -189,36 +192,32 @@
display(Fortfile);
-#ifdef OK_TO_WRITE_DISK
- if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
- perror(Fortfile->posfile);
- exit(1);
+ if (WriteToDisk) {
+ if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
+ perror(Fortfile->posfile);
+ exit(1);
+ }
+ /*
+ * if we can, we exclusive lock, but since it isn't very
+ * important, we just punt if we don't have easy locking
+ * available.
+ */
+ flock(fd, LOCK_EX);
+ write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
+ if (!Fortfile->was_pos_file)
+ chmod(Fortfile->path, 0666);
+ flock(fd, LOCK_UN);
}
-#ifdef LOCK_EX
- /*
- * if we can, we exclusive lock, but since it isn't very
- * important, we just punt if we don't have easy locking
- * available.
- */
- (void) flock(fd, LOCK_EX);
-#endif /* LOCK_EX */
- write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
- if (!Fortfile->was_pos_file)
- (void) chmod(Fortfile->path, 0666);
-#ifdef LOCK_EX
- (void) flock(fd, LOCK_UN);
-#endif /* LOCK_EX */
-#endif /* OK_TO_WRITE_DISK */
if (Wait) {
if (Fort_len == 0)
(void) fortlen();
sleep((unsigned int) max(Fort_len / CPERS, MINW));
}
+
exit(0);
- /* NOTREACHED */
}
-static void
+void
display(FILEDESC *fp)
{
char *p;
@@ -226,7 +225,7 @@
char line[BUFSIZ];
open_fp(fp);
- (void) fseeko(fp->inf, Seekpts[0], 0);
+ fseeko(fp->inf, Seekpts[0], SEEK_SET);
for (Fort_len = 0; fgets(line, sizeof line, fp->inf) != NULL &&
!STR_ENDSTRING(line, fp->tbl); Fort_len++) {
if (fp->tbl.str_flags & STR_ROTATED)
@@ -251,17 +250,17 @@
* fortlen:
* Return the length of the fortune.
*/
-static int
-fortlen()
+int
+fortlen(void)
{
int nchar;
- char line[BUFSIZ];
+ char line[BUFSIZ];
if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED)))
nchar = (int)(Seekpts[1] - Seekpts[0]);
else {
open_fp(Fortfile);
- (void) fseeko(Fortfile->inf, Seekpts[0], 0);
+ fseeko(Fortfile->inf, Seekpts[0], SEEK_SET);
nchar = 0;
while (fgets(line, sizeof line, Fortfile->inf) != NULL &&
!STR_ENDSTRING(line, Fortfile->tbl))
@@ -268,25 +267,24 @@
nchar += strlen(line);
}
Fort_len = nchar;
- return nchar;
+
+ return (nchar);
}
/*
* This routine evaluates the arguments on the command line
*/
-static void
+void
getargs(int argc, char *argv[])
{
int ignore_case;
char *pat;
- extern char *optarg;
- extern int optind;
int ch;
ignore_case = FALSE;
pat = NULL;
-# ifdef DEBUG
+#ifdef DEBUG
while ((ch = getopt(argc, argv, "aDefilm:osw")) != -1)
#else
while ((ch = getopt(argc, argv, "aefilm:osw")) != -1)
@@ -295,11 +293,11 @@
case 'a': /* any fortune */
All_forts++;
break;
-# ifdef DEBUG
+#ifdef DEBUG
case 'D':
Debug++;
break;
-# endif /* DEBUG */
+#endif /* DEBUG */
case 'e':
Equal_probs++; /* scatter un-allocted prob equally */
break;
@@ -353,7 +351,7 @@
error = regcomp(&Re_pat, pat, REG_BASIC);
if (error) {
fprintf(stderr, "regcomp(%s) fails\n", pat);
- exit (1);
+ exit(1);
}
}
}
@@ -362,22 +360,41 @@
* form_file_list:
* Form the file list from the file specifications.
*/
-static int
+int
form_file_list(char **files, 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;
@@ -389,11 +406,11 @@
percent = percent * 10 + *sp - '0';
if (percent > 100) {
fprintf(stderr, "percentages must be <= 100\n");
- return FALSE;
+ return (FALSE);
}
if (*sp == '.') {
fprintf(stderr, "percentages must be integers\n");
- return FALSE;
+ return (FALSE);
}
/*
* If the number isn't followed by a '%', then
@@ -407,17 +424,30 @@
else if (*++sp == '\0') {
if (++i >= file_cnt) {
fprintf(stderr, "percentages must precede files\n");
- return FALSE;
+ return (FALSE);
}
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;
+
+ return (TRUE);
}
/*
@@ -424,28 +454,32 @@
* add_file:
* Add a file to the file list.
*/
-static int
-add_file(int percent, char *file, char *dir, FILEDESC **head, FILEDESC **tail, FILEDESC *parent)
+int
+add_file(int percent, const char *file, const char *dir, FILEDESC **head,
+ FILEDESC **tail, FILEDESC *parent)
{
FILEDESC *fp;
int fd;
- char *path, *offensive;
+ const char *path;
+ char *tpath, *offensive;
bool was_malloc;
bool isdir;
if (dir == NULL) {
path = file;
+ tpath = NULL;
was_malloc = FALSE;
}
else {
- path = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2));
- (void) strcat(strcat(strcpy(path, dir), "/"), file);
+ tpath = do_malloc((unsigned int)(strlen(dir) + strlen(file) + 2));
+ strcat(strcat(strcpy(tpath, dir), "/"), file);
+ path = tpath;
was_malloc = TRUE;
}
if ((isdir = is_dir(path)) && parent != NULL) {
if (was_malloc)
- free(path);
- return FALSE; /* don't recurse */
+ free(tpath);
+ return (FALSE); /* don't recurse */
}
offensive = NULL;
if (!isdir && parent == NULL && (All_forts || Offend) &&
@@ -453,7 +487,7 @@
offensive = off_name(path);
if (Offend) {
if (was_malloc)
- free(path);
+ free(tpath);
path = offensive;
offensive = NULL;
was_malloc = TRUE;
@@ -464,7 +498,7 @@
DPRINTF(1, (stderr, "adding file \"%s\"\n", path));
over:
- if ((fd = open(path, 0)) < 0) {
+ if ((fd = open(path, O_RDONLY)) < 0) {
/*
* This is a sneak. If the user said -a, and if the
* file we're given isn't a file, we check to see if
@@ -475,7 +509,7 @@
*/
if (All_forts && offensive != NULL) {
if (was_malloc)
- free(path);
+ free(tpath);
path = offensive;
offensive = NULL;
was_malloc = TRUE;
@@ -483,14 +517,27 @@
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;
+ free(tpath);
+ return (FALSE);
}
DPRINTF(2, (stderr, "path = \"%s\"\n", path));
@@ -511,12 +558,12 @@
"fortune:%s not a fortune file or directory\n",
path);
if (was_malloc)
- free(path);
- free(fp->datfile);
- free(fp->posfile);
- free((char *) fp);
- free(offensive);
- return FALSE;
+ free(tpath);
+ do_free(fp->datfile);
+ do_free(fp->posfile);
+ free(fp);
+ do_free(offensive);
+ return (FALSE);
}
/*
* If the user said -a, we need to make this node a pointer to
@@ -538,11 +585,10 @@
fp->next = *head;
*head = fp;
}
-#ifdef OK_TO_WRITE_DISK
- fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
-#endif /* OK_TO_WRITE_DISK */
+ if (WriteToDisk)
+ fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
- return TRUE;
+ return (TRUE);
}
/*
@@ -549,12 +595,12 @@
* new_fp:
* Return a pointer to an initialized new FILEDESC.
*/
-static FILEDESC *
-new_fp()
+FILEDESC *
+new_fp(void)
{
FILEDESC *fp;
- fp = (FILEDESC *) do_malloc(sizeof *fp);
+ fp = do_malloc(sizeof(*fp));
fp->datfd = -1;
fp->pos = POS_UNKNOWN;
fp->inf = NULL;
@@ -567,7 +613,8 @@
fp->parent = NULL;
fp->datfile = NULL;
fp->posfile = NULL;
- return fp;
+
+ return (fp);
}
/*
@@ -574,13 +621,14 @@
* off_name:
* Return a pointer to the offensive version of a file of this name.
*/
-static char *
-off_name(char *file)
+char *
+off_name(const char *file)
{
char *new;
new = copy(file, (unsigned int) (strlen(file) + 2));
- return strcat(new, "-o");
+
+ return (strcat(new, "-o"));
}
/*
@@ -587,12 +635,13 @@
* is_off_name:
* Is the file an offensive-style name?
*/
-static int
-is_off_name(char *file)
+int
+is_off_name(const char *file)
{
int len;
len = strlen(file);
+
return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
}
@@ -601,19 +650,19 @@
* Modify a FILEDESC element to be the parent of two children if
* there are two children to be a parent of.
*/
-static void
+void
all_forts(FILEDESC *fp, char *offensive)
{
char *sp;
FILEDESC *scene, *obscene;
int fd;
- auto char *datfile, *posfile;
+ char *datfile, *posfile;
if (fp->child != NULL) /* this is a directory, not a file */
return;
if (!is_fortfile(offensive, &datfile, &posfile, FALSE))
return;
- if ((fd = open(offensive, 0)) < 0)
+ if ((fd = open(offensive, O_RDONLY)) < 0)
return;
DPRINTF(1, (stderr, "adding \"%s\" because of -a\n", offensive));
scene = new_fp();
@@ -633,16 +682,15 @@
obscene->fd = fd;
obscene->inf = NULL;
obscene->path = offensive;
- if ((sp = rindex(offensive, '/')) == NULL)
+ if ((sp = strrchr(offensive, '/')) == NULL)
obscene->name = offensive;
else
obscene->name = ++sp;
obscene->datfile = datfile;
obscene->posfile = posfile;
- obscene->read_tbl = FALSE;
-#ifdef OK_TO_WRITE_DISK
- obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
-#endif /* OK_TO_WRITE_DISK */
+ obscene->read_tbl = false;
+ if (WriteToDisk)
+ obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
}
/*
@@ -649,19 +697,19 @@
* add_dir:
* Add the contents of an entire directory.
*/
-static int
+int
add_dir(FILEDESC *fp)
{
DIR *dir;
struct dirent *dirent;
- auto FILEDESC *tailp;
- auto char *name;
+ FILEDESC *tailp;
+ char *name;
(void) close(fp->fd);
fp->fd = -1;
if ((dir = opendir(fp->path)) == NULL) {
perror(fp->path);
- return FALSE;
+ return (FALSE);
}
tailp = NULL;
DPRINTF(1, (stderr, "adding dir \"%s\"\n", fp->path));
@@ -678,9 +726,10 @@
if (fp->num_children == 0) {
(void) fprintf(stderr,
"fortune: %s: No fortune files in directory.\n", fp->path);
- return FALSE;
+ return (FALSE);
}
- return TRUE;
+
+ return (TRUE);
}
/*
@@ -687,13 +736,14 @@
* is_dir:
* Return TRUE if the file is a directory, FALSE otherwise.
*/
-static int
-is_dir(char *file)
+int
+is_dir(const char *file)
{
- auto struct stat sbuf;
+ struct stat sbuf;
if (stat(file, &sbuf) < 0)
- return FALSE;
+ return (FALSE);
+
return (sbuf.st_mode & S_IFDIR);
}
@@ -705,18 +755,19 @@
* suffixes, as contained in suflist[], are ruled out.
*/
/* ARGSUSED */
-static int
-is_fortfile(char *file, char **datp, char **posp, int check_for_offend)
+int
+is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
{
int i;
- char *sp;
+ const char *sp;
char *datfile;
- static char *suflist[] = { /* list of "illegal" suffixes" */
- "dat", "pos", "c", "h", "p", "i", "f",
- "pas", "ftn", "ins.c", "ins,pas",
- "ins.ftn", "sml",
- NULL
- };
+ static const char *suflist[] = {
+ /* list of "illegal" suffixes" */
+ "dat", "pos", "c", "h", "p", "i", "f",
+ "pas", "ftn", "ins.c", "ins,pas",
+ "ins.ftn", "sml",
+ NULL
+ };
DPRINTF(2, (stderr, "is_fortfile(%s) returns ", file));
@@ -728,28 +779,28 @@
i = strlen(file);
if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
DPRINTF(2, (stderr, "FALSE (offending file)\n"));
- return FALSE;
+ return (FALSE);
}
}
- if ((sp = rindex(file, '/')) == NULL)
+ if ((sp = strrchr(file, '/')) == NULL)
sp = file;
else
sp++;
if (*sp == '.') {
DPRINTF(2, (stderr, "FALSE (file starts with '.')\n"));
- return FALSE;
+ return (FALSE);
}
if (Fortunes_only && strncmp(sp, "fortunes", 8) != 0) {
DPRINTF(2, (stderr, "FALSE (check fortunes only)\n"));
- return FALSE;
+ return (FALSE);
}
- if ((sp = rindex(sp, '.')) != NULL) {
+ if ((sp = strrchr(sp, '.')) != NULL) {
sp++;
for (i = 0; suflist[i] != NULL; i++)
if (strcmp(sp, suflist[i]) == 0) {
DPRINTF(2, (stderr, "FALSE (file has suffix \".%s\")\n", sp));
- return FALSE;
+ return (FALSE);
}
}
@@ -762,7 +813,7 @@
DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
#endif
free(datfile);
- return FALSE;
+ return (FALSE);
}
if (datp != NULL)
*datp = datfile;
@@ -769,15 +820,17 @@
else
free(datfile);
if (posp != NULL) {
-#ifdef OK_TO_WRITE_DISK
- *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
- (void) strcat(*posp, ".pos");
-#else
- *posp = NULL;
-#endif /* OK_TO_WRITE_DISK */
+ if (WriteToDisk) {
+ *posp = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */
+ strcat(*posp, ".pos");
+ }
+ else {
+ *posp = NULL;
+ }
}
DPRINTF(2, (stderr, "TRUE\n"));
- return TRUE;
+
+ return (TRUE);
}
/*
@@ -784,10 +837,10 @@
* copy:
* Return a malloc()'ed copy of the string
*/
-static char *
-copy(char *str, unsigned int len)
+char *
+copy(const char *str, unsigned int len)
{
- char *new, *sp;
+ char *new, *sp;
new = do_malloc(len + 1);
sp = new;
@@ -794,7 +847,8 @@
do {
*sp++ = *str;
} while (*str++);
- return new;
+
+ return (new);
}
/*
@@ -801,24 +855,36 @@
* do_malloc:
* Do a malloc, checking for NULL return.
*/
-static void *
+void *
do_malloc(unsigned int size)
{
- void *new;
+ void *new;
if ((new = malloc(size)) == NULL) {
(void) fprintf(stderr, "fortune: out of memory.\n");
exit(1);
}
- return new;
+
+ return (new);
}
/*
+ * do_free:
+ * Free malloc'ed space, if any.
+ */
+void
+do_free(void *ptr)
+{
+ if (ptr != NULL)
+ free(ptr);
+}
+
+/*
* init_prob:
* Initialize the fortune probabilities.
*/
-static void
-init_prob()
+void
+init_prob(void)
{
FILEDESC *fp, *last = NULL;
int percent, num_noprob, frac;
@@ -836,8 +902,7 @@
num_noprob++;
if (Equal_probs)
last = fp;
- }
- else
+ } else
percent += fp->percent;
DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's",
percent, num_noprob));
@@ -845,14 +910,12 @@
(void) fprintf(stderr,
"fortune: probabilities sum to %d%% > 100%%!\n", percent);
exit(1);
- }
- else if (percent < 100 && num_noprob == 0) {
+ } else if (percent < 100 && num_noprob == 0) {
(void) fprintf(stderr,
"fortune: no place to put residual probability (%d%% < 100%%)\n",
percent);
exit(1);
- }
- else if (percent == 100 && num_noprob != 0) {
+ } else if (percent == 100 && num_noprob != 0) {
(void) fprintf(stderr,
"fortune: no probability left to put in residual files (100%%)\n");
exit(1);
@@ -863,7 +926,7 @@
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;
@@ -872,7 +935,7 @@
last->percent = percent;
DPRINTF(1, (stderr, ", residual = %d%%", percent));
}
- else
+ else
DPRINTF(1, (stderr,
", %d%% distributed over remaining fortunes\n",
percent));
@@ -889,8 +952,8 @@
* get_fort:
* Get the fortune data file's seek pointer for the next fortune.
*/
-static void
-get_fort()
+void
+get_fort(void)
{
FILEDESC *fp;
int choice;
@@ -898,7 +961,7 @@
if (File_list->next == NULL || File_list->percent == NO_PROB)
fp = File_list;
else {
- choice = random() % 100;
+ choice = arc4random_uniform(100);
DPRINTF(1, (stderr, "choice = %d\n", choice));
for (fp = File_list; fp->percent != NO_PROB; fp = fp->next)
if (choice < fp->percent)
@@ -918,10 +981,10 @@
else {
if (fp->next != NULL) {
sum_noprobs(fp);
- choice = random() % Noprob_tbl.str_numstr;
+ choice = arc4random_uniform(Noprob_tbl.str_numstr);
DPRINTF(1, (stderr, "choice = %d (of %u) \n", choice,
Noprob_tbl.str_numstr));
- while (choice >= fp->tbl.str_numstr) {
+ while ((unsigned int)choice >= fp->tbl.str_numstr) {
choice -= fp->tbl.str_numstr;
fp = fp->next;
DPRINTF(1, (stderr,
@@ -941,8 +1004,8 @@
Fortfile = fp;
get_pos(fp);
open_dat(fp);
- (void) lseek(fp->datfd,
- (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0);
+ lseek(fp->datfd,
+ (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), SEEK_SET);
read(fp->datfd, Seekpts, sizeof Seekpts);
Seekpts[0] = be64toh(Seekpts[0]);
Seekpts[1] = be64toh(Seekpts[1]);
@@ -952,7 +1015,7 @@
* pick_child
* Pick a child from a chosen parent.
*/
-static FILEDESC *
+FILEDESC *
pick_child(FILEDESC *parent)
{
FILEDESC *fp;
@@ -959,20 +1022,20 @@
int choice;
if (Equal_probs) {
- choice = random() % parent->num_children;
+ choice = arc4random_uniform(parent->num_children);
DPRINTF(1, (stderr, " choice = %d (of %d)\n",
choice, parent->num_children));
for (fp = parent->child; choice--; fp = fp->next)
continue;
DPRINTF(1, (stderr, " using %s\n", fp->name));
- return fp;
+ return (fp);
}
else {
get_tbl(parent);
- choice = random() % parent->tbl.str_numstr;
+ choice = arc4random_uniform(parent->tbl.str_numstr);
DPRINTF(1, (stderr, " choice = %d (of %u)\n",
choice, parent->tbl.str_numstr));
- for (fp = parent->child; choice >= fp->tbl.str_numstr;
+ for (fp = parent->child; (unsigned)choice >= fp->tbl.str_numstr;
fp = fp->next) {
choice -= fp->tbl.str_numstr;
DPRINTF(1, (stderr, "\tskip %s, %u (choice = %d)\n",
@@ -980,7 +1043,7 @@
}
DPRINTF(1, (stderr, " using %s, %u\n", fp->name,
fp->tbl.str_numstr));
- return fp;
+ return (fp);
}
}
@@ -988,7 +1051,7 @@
* sum_noprobs:
* Sum up all the noprob probabilities, starting with fp.
*/
-static void
+void
sum_noprobs(FILEDESC *fp)
{
static bool did_noprobs = FALSE;
@@ -1004,7 +1067,7 @@
did_noprobs = TRUE;
}
-static int
+int
max(int i, int j)
{
return (i >= j ? i : j);
@@ -1014,7 +1077,7 @@
* open_fp:
* Assocatiate a FILE * with the given FILEDESC.
*/
-static void
+void
open_fp(FILEDESC *fp)
{
if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) {
@@ -1027,10 +1090,10 @@
* open_dat:
* Open up the dat file if we need to.
*/
-static void
+void
open_dat(FILEDESC *fp)
{
- if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, 0)) < 0) {
+ if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY)) < 0) {
perror(fp->datfile);
exit(1);
}
@@ -1041,26 +1104,24 @@
* Get the position from the pos file, if there is one. If not,
* return a random number.
*/
-static void
+void
get_pos(FILEDESC *fp)
{
-#ifdef OK_TO_WRITE_DISK
int fd;
-#endif /* OK_TO_WRITE_DISK */
assert(fp->read_tbl);
if (fp->pos == POS_UNKNOWN) {
-#ifdef OK_TO_WRITE_DISK
- if ((fd = open(fp->posfile, 0)) < 0 ||
- read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
- fp->pos = random() % fp->tbl.str_numstr;
- else if (fp->pos >= fp->tbl.str_numstr)
- fp->pos %= fp->tbl.str_numstr;
- if (fd >= 0)
- (void) close(fd);
-#else
- fp->pos = random() % fp->tbl.str_numstr;
-#endif /* OK_TO_WRITE_DISK */
+ if (WriteToDisk) {
+ if ((fd = open(fp->posfile, O_RDONLY)) < 0 ||
+ read(fd, &fp->pos, sizeof fp->pos) != sizeof fp->pos)
+ fp->pos = arc4random_uniform(fp->tbl.str_numstr);
+ else if (fp->pos >= fp->tbl.str_numstr)
+ fp->pos %= fp->tbl.str_numstr;
+ if (fd >= 0)
+ close(fd);
+ }
+ else
+ fp->pos = arc4random_uniform(fp->tbl.str_numstr);
}
if (++(fp->pos) >= fp->tbl.str_numstr)
fp->pos -= fp->tbl.str_numstr;
@@ -1071,16 +1132,16 @@
* get_tbl:
* Get the tbl data file the datfile.
*/
-static void
+void
get_tbl(FILEDESC *fp)
{
- auto int fd;
+ int fd;
FILEDESC *child;
if (fp->read_tbl)
return;
if (fp->child == NULL) {
- if ((fd = open(fp->datfile, 0)) < 0) {
+ if ((fd = open(fp->datfile, O_RDONLY)) < 0) {
perror(fp->datfile);
exit(1);
}
@@ -1110,7 +1171,7 @@
* zero_tbl:
* Zero out the fields we care about in a tbl structure.
*/
-static void
+void
zero_tbl(STRFILE *tp)
{
tp->str_numstr = 0;
@@ -1122,7 +1183,7 @@
* sum_tbl:
* Merge the tbl data of t2 into t1.
*/
-static void
+void
sum_tbl(STRFILE *t1, STRFILE *t2)
{
t1->str_numstr += t2->str_numstr;
@@ -1138,8 +1199,8 @@
* print_file_list:
* Print out the file list
*/
-static void
-print_file_list()
+void
+print_file_list(void)
{
print_list(File_list, 0);
}
@@ -1148,7 +1209,7 @@
* print_list:
* Print out the actual list, recursively.
*/
-static void
+void
print_list(FILEDESC *list, int lev)
{
while (list != NULL) {
@@ -1171,7 +1232,7 @@
* conv_pat:
* Convert the pattern to an ignore-case equivalent.
*/
-static char *
+char *
conv_pat(char *orig)
{
char *sp;
@@ -1206,7 +1267,8 @@
*sp++ = *orig;
}
*sp = '\0';
- return new;
+
+ return (new);
}
/*
@@ -1213,8 +1275,8 @@
* find_matches:
* Find all the fortunes which match the pattern we've been given.
*/
-static int
-find_matches()
+int
+find_matches(void)
{
Fort_len = maxlen_in_list(File_list);
DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len));
@@ -1223,8 +1285,8 @@
Found_one = FALSE;
matches_in_list(File_list);
- return Found_one;
- /* NOTREACHED */
+
+ return (Found_one);
}
/*
@@ -1231,7 +1293,7 @@
* maxlen_in_list
* Return the maximum fortune len in the file list.
*/
-static int
+int
maxlen_in_list(FILEDESC *list)
{
FILEDESC *fp;
@@ -1245,11 +1307,12 @@
}
else {
get_tbl(fp);
- if (fp->tbl.str_longlen > maxlen)
+ if (fp->tbl.str_longlen > (unsigned int)maxlen)
maxlen = fp->tbl.str_longlen;
}
}
- return maxlen;
+
+ return (maxlen);
}
/*
@@ -1256,13 +1319,13 @@
* matches_in_list
* Print out the matches from the files in the list.
*/
-static void
+void
matches_in_list(FILEDESC *list)
{
char *sp, *p;
FILEDESC *fp;
- int in_file;
- unsigned char ch;
+ int in_file;
+ unsigned char ch;
for (fp = list; fp != NULL; fp = fp->next) {
if (fp->child != NULL) {
@@ -1307,8 +1370,8 @@
}
}
-static void
-usage()
+void
+usage(void)
{
(void) fprintf(stderr, "fortune [-a");
#ifdef DEBUG
@@ -1319,3 +1382,52 @@
(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, foundenv;
+ char *pch, **ppch, *str, *path;
+
+ foundenv = 1;
+ Fortune_path = getenv("FORTUNE_PATH");
+ if (Fortune_path == NULL) {
+ Fortune_path = FORTDIR;
+ foundenv = 0;
+ }
+ 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) {
+ if (foundenv == 1) {
+ fprintf(stderr,
+ "fortune: FORTUNE_PATH: None of the specified "
+ "directories found.\n");
+ exit(1);
+ }
+ free(path);
+ Fortune_path_arr[0] = strdup(FORTDIR);
+ }
+}
Modified: trunk/games/fortune/fortune/pathnames.h
===================================================================
--- trunk/games/fortune/fortune/pathnames.h 2016-09-18 18:27:51 UTC (rev 8352)
+++ trunk/games/fortune/fortune/pathnames.h 2016-09-18 18:31:16 UTC (rev 8353)
@@ -10,11 +10,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
@@ -31,7 +27,8 @@
* SUCH DAMAGE.
*
* @(#)pathnames.h 8.1 (Berkeley) 5/31/93
- * $MidnightBSD$
+ * $MidnightBSD$
*/
-#define FORTDIR "/usr/share/games/fortune"
+#define FORTDIR "/usr/share/games/fortune:" \
+ "/usr/local/share/games/fortune"
More information about the Midnightbsd-cvs
mailing list