#define PROGRAM_NAME "fortune-mod"
#define PROGRAM_VERSION "9708"
-#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
-#else /* ! HAVE_STDBOOL_H */
-
-/* stdbool.h for GNU. */
-
-/* The type `bool' must promote to `int' or `unsigned int'. The constants
- `true' and `false' must have the value 0 and 1 respectively. */
-typedef enum
- {
- false = 0,
- true = 1
- } bool;
-
-/* The names `true' and `false' must also be made available as macros. */
-#define false false
-#define true true
-
-/* Signal that all the definitions are present. */
-#define __bool_true_false_are_defined 1
-
-#endif /* HAVE_STDBOOL_H */
#include <sys/types.h>
#include <sys/time.h>
#define TRUE 1
#define FALSE 0
-#define bool short
#define MINW 6 /* minimum wait if desired */
#define CPERS 20 /* # of chars for each sec */
}
FILEDESC;
-bool Found_one; /* did we find a match? */
-bool Find_files = FALSE; /* just find a list of proper fortune 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-allocated prob equally */
-bool Show_filename = FALSE;
+static bool Found_one; /* did we find a match? */
+static bool Find_files = FALSE; /* just find a list of proper fortune 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-allocated prob equally */
+static bool Show_filename = FALSE;
-bool ErrorMessage = FALSE; /* Set to true if an error message has been displayed */
+static bool ErrorMessage = FALSE; /* Set to true if an error message has been displayed */
#ifndef NO_REGEX
-bool Match = FALSE; /* dump fortunes matching a pattern */
+static bool Match = FALSE; /* dump fortunes matching a pattern */
#endif
#ifdef DEBUG
-bool Debug = FALSE; /* print debug messages */
+static bool Debug = FALSE; /* print debug messages */
#endif
-unsigned char *Fortbuf = NULL; /* fortune buffer for -m */
+static unsigned char *Fortbuf = NULL; /* fortune buffer for -m */
-int Fort_len = 0, Spec_prob = 0, /* total prob specified on cmd line */
+static int Fort_len = 0, Spec_prob = 0, /* total prob specified on cmd line */
Num_files, Num_kids, /* totals of files and children. */
SLEN = 160; /* max. characters in a "short" fortune */
-int32_t Seekpts[2]; /* seek pointers to fortunes */
+static int32_t Seekpts[2]; /* seek pointers to fortunes */
-FILEDESC *File_list = NULL, /* Head of file list */
+static FILEDESC *File_list = NULL, /* Head of file list */
*File_tail = NULL; /* Tail of file list */
-FILEDESC *Fortfile; /* Fortune file to use */
+static FILEDESC *Fortfile; /* Fortune file to use */
-STRFILE Noprob_tbl; /* sum of data for all no prob files */
+static STRFILE Noprob_tbl; /* sum of data for all no prob files */
#ifdef BSD_REGEX
#define BAD_COMP(f) ((f) != 0)
#define RE_EXEC(p) (regexec(&Re_pat, (p), 0, NULL, 0) == 0)
-regex_t Re_pat;
+static regex_t Re_pat;
#else
#define NO_REGEX
#endif /* POSIX_REGEX */
#endif /* BSD_REGEX */
-RECODE_REQUEST request;
-RECODE_OUTER outer;
+static RECODE_REQUEST request;
+static RECODE_OUTER outer;
int add_dir(register FILEDESC *);
return buf;
}
-static void usage(void)
+static void __attribute__((noreturn)) usage(void)
{
(void) fprintf(stderr, "%s\n",program_version());
(void) fprintf(stderr, "fortune [-a");
{
*sp++ = '[';
*sp++ = *orig;
- *sp++ = toupper(*orig);
+ *sp++ = (char)toupper(*orig);
*sp++ = ']';
}
else if (isupper(*orig))
{
*sp++ = '[';
*sp++ = *orig;
- *sp++ = tolower(*orig);
+ *sp++ = (char)tolower(*orig);
*sp++ = ']';
}
else
* do_malloc:
* Do a malloc, checking for NULL return.
*/
-static void *do_malloc(unsigned int size)
+static void *do_malloc(size_t size)
{
void *new;
* overhead. Files which start with ".", or which have "illegal"
* suffixes, as contained in suflist[], are ruled out.
*/
-static int is_fortfile(char *file, char **datp, char **posp)
+static int is_fortfile(char *file, char **datp)
{
register int i;
register char *sp;
fp->fd = fd;
fp->percent = percent;
- fp->name = do_malloc (strlen (file) + 1);
- strncpy (fp->name, file, strlen (file) + 1);
+ fp->name = do_malloc (strlen (file) + (size_t)1);
+ strncpy (fp->name, file, strlen (file) + (size_t)1);
- fp->path = do_malloc (strlen (path) + 1);
- strncpy (fp->path, path, strlen (path) + 1);
+ fp->path = do_malloc (strlen (path) + (size_t)1);
+ strncpy (fp->path, path, strlen (path) + 1UL);
//FIXME
fp->utf8_charset = FALSE;
- testpath = do_malloc(strlen (path) + 4);
+ testpath = do_malloc(strlen (path) + 4UL);
sprintf(testpath, "%s.u8", path);
// fprintf(stderr, "State mal: %s\n", testpath);
if(stat(testpath, &statbuf) == 0)
if ((isdir && !add_dir(fp)) ||
(!isdir &&
- !is_fortfile(path, &fp->datfile, &fp->posfile)))
+ !is_fortfile(path, &fp->datfile)))
{
if (parent == NULL)
fprintf(stderr,
{
tp->str_numstr = 0;
tp->str_longlen = 0;
- tp->str_shortlen = -1;
+ tp->str_shortlen = (uint32_t)(-1);
}
/*
else
{
get_tbl(parent);
- choice = random() % parent->tbl.str_numstr;
+ choice = (int)(random() % (long)(parent->tbl.str_numstr));
DPRINTF(1, (stderr, " choice = %d (of %ld)\n",
choice, parent->tbl.str_numstr));
- for (fp = parent->child; choice >= fp->tbl.str_numstr;
+ for (fp = parent->child; choice >= (int)fp->tbl.str_numstr;
fp = fp->next)
{
choice -= fp->tbl.str_numstr;
assert(fp->read_tbl);
if (fp->pos == POS_UNKNOWN)
{
- fp->pos = random() % fp->tbl.str_numstr;
+ fp->pos = (int32_t)(random() % fp->tbl.str_numstr);
}
- if (++(fp->pos) >= fp->tbl.str_numstr)
+ if (++(fp->pos) >= (int32_t)fp->tbl.str_numstr)
fp->pos -= fp->tbl.str_numstr;
DPRINTF(1, (stderr, "pos for %s is %ld\n", fp->name, fp->pos));
}
if (fp->next != NULL)
{
sum_noprobs(fp);
- choice = random() % Noprob_tbl.str_numstr;
+ choice = (int)(random() % (long)Noprob_tbl.str_numstr);
DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice,
Noprob_tbl.str_numstr));
- while (choice >= fp->tbl.str_numstr)
+ while (choice >= (int)fp->tbl.str_numstr)
{
- choice -= fp->tbl.str_numstr;
+ choice -= (int)fp->tbl.str_numstr;
fp = fp->next;
DPRINTF(1, (stderr,
" skip \"%s\", %ld (choice = %d)\n",
get_pos(fp);
open_dat(fp);
lseek(fp->datfd,
- (off_t) (sizeof fp->tbl + fp->pos * sizeof Seekpts[0]), 0);
+ (off_t) (sizeof fp->tbl + (size_t)fp->pos * sizeof Seekpts[0]), 0);
read(fp->datfd, &Seekpts[0], sizeof Seekpts[0]);
read(fp->datfd, &Seekpts[1], sizeof Seekpts[1]);
- Seekpts[0] = ntohl(Seekpts[0]);
- Seekpts[1] = ntohl(Seekpts[1]);
+ Seekpts[0] = (int32_t)ntohl((uint32_t)Seekpts[0]);
+ Seekpts[1] = (int32_t)ntohl((uint32_t)Seekpts[1]);
}
/*
else
{
get_tbl(fp);
- if (fp->tbl.str_longlen > maxlen)
- maxlen = fp->tbl.str_longlen;
+ if ((int)fp->tbl.str_longlen > maxlen)
+ {
+ maxlen = (int)fp->tbl.str_longlen;
+ }
}
}
return maxlen;
else
{
*sp = '\0';
- nchar = sp - Fortbuf;
+ nchar = (int)(sp - Fortbuf);
if (fp->utf8_charset)
{
print_list(File_list, 0);
exit(0);
}
- srandom((int) (time((time_t *) NULL) + getpid()));
+ srandom((unsigned int) (time((time_t *) NULL) + getpid()));
do
{
get_fort();
*
*/
-#define TRUE 1
#define FALSE 0
#define STORING_PTRS (Oflag || Rflag)
}
STR;
-char *Infile = NULL, /* input file name */
+static char *Infile = NULL, /* input file name */
Outfile[MAXPATHLEN] = "", /* output file name */
Delimch = '%'; /* delimiting character */
-int Sflag = FALSE; /* silent run flag */
-int Oflag = FALSE; /* ordering flag */
-int Iflag = FALSE; /* ignore case flag */
-int Rflag = FALSE; /* randomize order flag */
-int Xflag = FALSE; /* set rotated bit */
-long Num_pts = 0; /* number of pointers/strings */
+static int Sflag = FALSE; /* silent run flag */
+static int Oflag = FALSE; /* ordering flag */
+static int Iflag = FALSE; /* ignore case flag */
+static int Rflag = FALSE; /* randomize order flag */
+static int Xflag = FALSE; /* set rotated bit */
+static long Num_pts = 0; /* number of pointers/strings */
-int32_t *Seekpts;
+static int32_t *Seekpts;
-FILE *Sort_1, *Sort_2; /* pointers for sorting */
+static FILE *Sort_1, *Sort_2; /* pointers for sorting */
-STRFILE Tbl; /* statistics table */
+static STRFILE Tbl; /* statistics table */
-STR *Firstch; /* first chars of each string */
+static STR *Firstch; /* first chars of each string */
-static void usage(void)
+static void __attribute__((noreturn)) usage(void)
{
fprintf(stderr,
"strfile [-iorsx] [-c char] sourcefile [datafile]\n");
*/
static void add_offset(FILE * fp, int32_t off)
{
- int32_t net;
-
if (!STORING_PTRS)
{
- net = htonl(off);
+ uint32_t net;
+ net = htonl((uint32_t)off);
fwrite(&net, 1, sizeof net, fp);
}
else
*/
static void fix_last_offset(FILE * fp, int32_t off)
{
- int32_t net;
-
if (!STORING_PTRS)
{
- net = htonl(off);
- fseek(fp, -(sizeof net), SEEK_CUR);
+ uint32_t net = htonl((uint32_t)off);
+ fseek(fp, -(long)(sizeof net), SEEK_CUR);
fwrite(&net, 1, sizeof net, fp);
}
else
Sort_1 = fopen(Infile, "r");
Sort_2 = fopen(Infile, "r");
- qsort((char *) Firstch, (int) Num_pts - 1, sizeof *Firstch, cmp_str);
+ qsort((char *) Firstch, (size_t)( (int) Num_pts - 1), sizeof *Firstch, cmp_str);
/* i = Tbl.str_numstr;
* Fucking brilliant. Tbl.str_numstr was initialized to zero, and is still zero
*/
register int32_t tmp;
register int32_t *sp;
- srandom((int) (time((time_t *) NULL) + getpid()));
+ srandom((unsigned int) (time((time_t *) NULL) + getpid()));
Tbl.str_flags |= STR_RANDOM;
/* cnt = Tbl.str_numstr;
* See comment above. Isn't this stuff distributed worldwide? How embarrassing!
*/
- cnt = Num_pts;
+ cnt = (int)Num_pts;
/*
* move things around randomly
Tbl.str_longlen = 0;
Tbl.str_shortlen = (unsigned int) 0xffffffff;
- Tbl.str_delim = Delimch;
+ Tbl.str_delim = (uint8_t)Delimch;
Tbl.str_version = VERSION;
first = Oflag;
- add_offset(outf, ftell(inf));
+ add_offset(outf, (int32_t)ftell(inf));
last_off = 0;
do
{
sp = fgets(string, 256, inf);
if (sp == NULL || STR_ENDSTRING(sp, Tbl))
{
- pos = ftell(inf);
- length = pos - last_off - (sp ? strlen(sp) : 0);
+ pos = (int32_t)ftell(inf);
+ length = pos - last_off - (int32_t)(sp ? strlen(sp) : 0);
if (!length)
/* Here's where we go back and fix things, if the
* 'fortune' just read was the null string.
}
last_off = pos;
add_offset(outf, pos);
- if (Tbl.str_longlen < length)
- Tbl.str_longlen = length;
- if (Tbl.str_shortlen > length)
- Tbl.str_shortlen = length;
+ if ((int)Tbl.str_longlen < length)
+ Tbl.str_longlen = (uint32_t)length;
+ if ((int)Tbl.str_shortlen > length)
+ Tbl.str_shortlen = (uint32_t)length;
first = Oflag;
}
else if (first)
ALLOC(Firstch, Num_pts);
fp = &Firstch[Num_pts - 1];
if (Iflag && isupper(*nsp))
- fp->first = tolower(*nsp);
+ fp->first = (char)tolower(*nsp);
else
fp->first = *nsp;
fp->pos = Seekpts[Num_pts - 1];
fseek(outf, (off_t) 0, 0);
Tbl.str_version = htonl(Tbl.str_version);
- Tbl.str_numstr = htonl(Num_pts - 1);
+ Tbl.str_numstr = htonl((uint32_t)(Num_pts - 1));
/* Look, Ma! After using the variable three times, let's store
* something in it!
*/
fwrite( Tbl.stuff, sizeof Tbl.stuff, 1, outf);
if (STORING_PTRS)
{
- for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
+ for (p = Seekpts, cnt = (int)Num_pts; cnt--; ++p)
{
- *p = htonl(*p);
+ *p = (int32_t)htonl((uint32_t)*p);
fwrite(p, sizeof *p, 1, outf);
}
}