From f92788d6284384fc7efa4336b03a38ab6488b845 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 30 Mar 2020 13:18:36 +0300 Subject: [PATCH] pass through clang-format. --- fortune-mod/.clang-format | 16 + fortune-mod/.tidyallrc | 3 + fortune-mod/fortune/fortune.c | 867 +++++++++++++------------- fortune-mod/util/fortune-mod-common.h | 28 +- fortune-mod/util/getopt.c | 179 +++--- fortune-mod/util/getopt.h | 12 +- fortune-mod/util/randstr.c | 46 +- fortune-mod/util/rot.c | 7 +- fortune-mod/util/strfile.c | 197 +++--- fortune-mod/util/strfile.h | 27 +- fortune-mod/util/unstr.c | 64 +- 11 files changed, 752 insertions(+), 694 deletions(-) create mode 100644 fortune-mod/.clang-format diff --git a/fortune-mod/.clang-format b/fortune-mod/.clang-format new file mode 100644 index 0000000..9678a4e --- /dev/null +++ b/fortune-mod/.clang-format @@ -0,0 +1,16 @@ +--- +BasedOnStyle: LLVM +AlignAfterOpenBracket: DontAlign +BreakBeforeBraces: Custom +IndentWidth: 4 +SortIncludes: false +BraceWrapping: + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterExternBlock: false + AfterNamespace: true + AfterStruct: true + BeforeCatch: true + BeforeElse: true diff --git a/fortune-mod/.tidyallrc b/fortune-mod/.tidyallrc index 28a3b28..874a64f 100644 --- a/fortune-mod/.tidyallrc +++ b/fortune-mod/.tidyallrc @@ -1,3 +1,6 @@ +[ClangFormat] +select = **/*.{c,h} + [PerlTidy] select = **/*.{pl,pm,t} argv = -ci=4 -bl -cti=0 diff --git a/fortune-mod/fortune/fortune.c b/fortune-mod/fortune/fortune.c index 5c8b65b..aa3805c 100644 --- a/fortune-mod/fortune/fortune.c +++ b/fortune-mod/fortune/fortune.c @@ -77,7 +77,7 @@ * Added to debian by Alastair McKinstry, , 2002-07-31 */ -#if 0 /* comment out the stuff here, and get rid of silly warnings */ +#if 0 /* comment out the stuff here, and get rid of silly warnings */ #ifndef lint static char copyright[] = "@(#) Copyright (c) 1986, 1993\n\ @@ -96,50 +96,51 @@ static char rcsid[] = "$NetBSD: fortune.c,v 1.8 1995/03/23 08:28:40 cgd Exp $"; #endif /* not lint */ #endif /* killing warnings */ -#define PROGRAM_NAME "fortune-mod" +#define PROGRAM_NAME "fortune-mod" #include "fortune-mod-common.h" -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #ifndef _WIN32 -#include +#include #define O_BINARY 0 #define WITH_RECODE #endif #ifdef WITH_RECODE -#include +#include #endif - #ifdef HAVE_REGEX_H -#include +#include #endif #ifdef HAVE_REGEXP_H -#include +#include #endif #ifdef HAVE_RX_H -#include +#include #endif -#include "config.h" +#include "config.h" -#define TRUE 1 -#define FALSE 0 +#define TRUE 1 +#define FALSE 0 -#define MINW 6 /* minimum wait if desired */ -#define CPERS 20 /* # of chars for each sec */ +#define MINW 6 /* minimum wait if desired */ +#define CPERS 20 /* # of chars for each sec */ -#define POS_UNKNOWN ((int32_t) -1) /* pos for file unknown */ -#define NO_PROB (-1) /* no prob specified for file */ +#define POS_UNKNOWN ((int32_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; +#define DPRINTF(l, x) \ + if (Debug >= l) \ + fprintf x; #else -#define DPRINTF(l,x) +#define DPRINTF(l, x) #endif typedef struct fd @@ -158,51 +159,51 @@ typedef struct fd int num_children; struct fd *child, *parent; struct fd *next, *prev; -} -FILEDESC; - -static char * env_lang; - -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 */ +} FILEDESC; + +static char *env_lang; + +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; static bool No_recode = FALSE; /* Do we want to stop recoding from occuring */ -static 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 -static bool Match = FALSE; /* dump fortunes matching a pattern */ +static bool Match = FALSE; /* dump fortunes matching a pattern */ #endif #ifdef DEBUG -static bool Debug = FALSE; /* print debug messages */ +static bool Debug = FALSE; /* print debug messages */ #endif -static unsigned char *Fortbuf = NULL; /* fortune buffer for -m */ +static unsigned char *Fortbuf = NULL; /* fortune buffer for -m */ -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 */ +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 */ -static int32_t Seekpts[2]; /* seek pointers to fortunes */ +static int32_t Seekpts[2]; /* seek pointers to fortunes */ -static FILEDESC *File_list = NULL, /* Head of file list */ - *File_tail = NULL; /* Tail of file list */ -static FILEDESC *Fortfile; /* Fortune file to use */ +static FILEDESC *File_list = NULL, /* Head of file list */ + *File_tail = NULL; /* Tail of file list */ +static FILEDESC *Fortfile; /* Fortune file to use */ -static STRFILE Noprob_tbl; /* sum of data for all no prob files */ +static STRFILE Noprob_tbl; /* sum of data for all no prob files */ #ifdef POSIX_REGEX -#define RE_COMP(p) regcomp(&Re_pat, (p), REG_NOSUB) -#define BAD_COMP(f) ((f) != 0) -#define RE_EXEC(p) (regexec(&Re_pat, (p), 0, NULL, 0) == 0) +#define RE_COMP(p) regcomp(&Re_pat, (p), REG_NOSUB) +#define BAD_COMP(f) ((f) != 0) +#define RE_EXEC(p) (regexec(&Re_pat, (p), 0, NULL, 0) == 0) static regex_t Re_pat; #else @@ -218,9 +219,9 @@ int add_dir(register FILEDESC *); static unsigned long my_random(unsigned long base) { - FILE * fp; + FILE *fp; unsigned long long l = 0; - char * hard_coded_val; + char *hard_coded_val; hard_coded_val = getenv("FORTUNE_MOD_RAND_HARD_CODED_VALS"); if (hard_coded_val) @@ -232,7 +233,7 @@ static unsigned long my_random(unsigned long base) goto fallback; } fp = fopen("/dev/urandom", "rb"); - if (! fp) + if (!fp) { goto fallback; } @@ -250,35 +251,34 @@ fallback: static char *program_version(void) { static char buf[BUFSIZ]; - (void) sprintf(buf, "%s version %s", PROGRAM_NAME, VERSION); + (void)sprintf(buf, "%s version %s", PROGRAM_NAME, VERSION); return buf; } static void __attribute__((noreturn)) usage(void) { - (void) fprintf(stderr, "%s\n",program_version()); - (void) fprintf(stderr, "fortune [-a"); -#ifdef DEBUG - (void) fprintf(stderr, "D"); + (void)fprintf(stderr, "%s\n", program_version()); + (void)fprintf(stderr, "fortune [-a"); +#ifdef DEBUG + (void)fprintf(stderr, "D"); #endif /* DEBUG */ - (void) fprintf(stderr, "f"); + (void)fprintf(stderr, "f"); #ifndef NO_REGEX - (void) fprintf(stderr, "i"); + (void)fprintf(stderr, "i"); #endif /* NO_REGEX */ - (void) fprintf(stderr, "l"); + (void)fprintf(stderr, "l"); #ifndef NO_OFFENSIVE - (void) fprintf(stderr, "o"); + (void)fprintf(stderr, "o"); #endif - (void) fprintf(stderr, "sw]"); + (void)fprintf(stderr, "sw]"); #ifndef NO_REGEX - (void) fprintf(stderr, " [-m pattern]"); + (void)fprintf(stderr, " [-m pattern]"); #endif /* NO_REGEX */ - (void) fprintf(stderr, " [-n number] [ [#%%] file/directory/all]\n"); + (void)fprintf(stderr, " [-n number] [ [#%%] file/directory/all]\n"); exit(1); } -#define STR(str) ((str) == NULL ? "NULL" : (str)) - +#define STR(str) ((str) == NULL ? "NULL" : (str)) /* * calc_equal_probs: @@ -303,17 +303,18 @@ static void calc_equal_probs(void) * print_list: * Print out the actual list, recursively. */ -static void print_list(register FILEDESC * list, int lev) +static void print_list(register FILEDESC *list, int lev) { while (list != NULL) { fprintf(stderr, "%*s", lev * 4, ""); if (list->percent == NO_PROB) if (!Equal_probs) -/* This, with some changes elsewhere, gives proper percentages for every case - * fprintf(stderr, "___%%"); */ - fprintf(stderr, "%5.2f%%", (100.0 - Spec_prob) * - list->tbl.str_numstr / Noprob_tbl.str_numstr); + /* This, with some changes elsewhere, gives proper percentages + * for every case fprintf(stderr, "___%%"); */ + fprintf(stderr, "%5.2f%%", + (100.0 - Spec_prob) * list->tbl.str_numstr / + Noprob_tbl.str_numstr); else if (lev == 0) fprintf(stderr, "%5.2f%%", 100.0 / Num_files); else @@ -322,7 +323,7 @@ static void print_list(register FILEDESC * list, int lev) fprintf(stderr, "%5.2f%%", 1.0 * list->percent); fprintf(stderr, " %s", STR(list->name)); DPRINTF(1, (stderr, " (%s, %s, %s)\n", STR(list->path), - STR(list->datfile), STR(list->posfile))); + STR(list->datfile), STR(list->posfile))); putc('\n', stderr); if (list->child != NULL) print_list(list->child, lev + 1); @@ -341,7 +342,7 @@ static char *conv_pat(register char *orig) register unsigned int cnt; register char *new; - cnt = 1; /* allow for '\0' */ + cnt = 1; /* allow for '\0' */ for (sp = orig; *sp != '\0'; sp++) if (isalpha(*sp)) cnt += 4; @@ -387,7 +388,7 @@ static void *do_malloc(size_t size) if ((new = malloc(size)) == NULL) { - (void) fprintf(stderr, "fortune: out of memory.\n"); + (void)fprintf(stderr, "fortune: out of memory.\n"); exit(1); } return new; @@ -416,8 +417,7 @@ static char *copy(char *str, unsigned int len) do { *sp++ = *str; - } - while (*str++); + } while (*str++); return new; } @@ -429,7 +429,7 @@ static FILEDESC *new_fp(void) { register FILEDESC *fp; - fp = (FILEDESC *) do_malloc(sizeof *fp); + fp = (FILEDESC *)do_malloc(sizeof *fp); fp->datfd = -1; fp->pos = POS_UNKNOWN; fp->inf = NULL; @@ -465,7 +465,9 @@ static inline void debugprint(const char *msg, ...) va_end(ap); } #else -#define debugprint(format, ...) {} +#define debugprint(format, ...) \ + { \ + } #endif /* * is_dir: @@ -475,12 +477,13 @@ static int is_dir(const char *const file) { auto struct stat sbuf; - if (stat(file, &sbuf) < 0) { - debugprint( "is_dir failed for file=<%s>\n", file); + if (stat(file, &sbuf) < 0) + { + debugprint("is_dir failed for file=<%s>\n", file); return -1; } - const bool ret = ( (sbuf.st_mode & S_IFDIR) ? true : false); - debugprint( "is_dir for file=<%s> gave ret=<%d>\n", file, ret); + const bool ret = ((sbuf.st_mode & S_IFDIR) ? true : false); + debugprint("is_dir for file=<%s> gave ret=<%d>\n", file, ret); return ret; } @@ -494,14 +497,14 @@ static int is_existant(char *file) if (stat(file, &staat) == 0) return TRUE; - switch(errno) + switch (errno) { - case ENOENT: - case ENOTDIR: - return FALSE; - default: - perror("fortune: bad juju in is_existant"); - exit(1); + case ENOENT: + case ENOTDIR: + return FALSE; + default: + perror("fortune: bad juju in is_existant"); + exit(1); } } @@ -517,13 +520,9 @@ static int is_fortfile(char *file, char **datp) register int i; register char *sp; register char *datfile; - 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 - }; + 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)); @@ -547,7 +546,7 @@ static int is_fortfile(char *file, char **datp) } } - datfile = copy(file, (unsigned int) (strlen(file) + 4)); /* +4 for ".dat" */ + datfile = copy(file, (unsigned int)(strlen(file) + 4)); /* +4 for ".dat" */ strcat(datfile, ".dat"); if (access(datfile, R_OK) < 0) { @@ -582,7 +581,7 @@ static bool path_is_absolute(const char *const path) * Add a file to the file list. */ static int add_file(int percent, register const char *file, const char *dir, - FILEDESC ** head, FILEDESC ** tail, FILEDESC * parent) + FILEDESC **head, FILEDESC **tail, FILEDESC *parent) { register FILEDESC *fp; register int fd = -1; @@ -599,11 +598,12 @@ static int add_file(int percent, register const char *file, const char *dir, } else { - path = do_malloc((unsigned int) (strlen(dir) + strlen(file) + 2)); - (void) strcat(strcat(strcpy(path, dir), "/"), file); + path = do_malloc((unsigned int)(strlen(dir) + strlen(file) + 2)); + (void)strcat(strcat(strcpy(path, dir), "/"), file); was_malloc = TRUE; } - if (*path == '/' && !is_existant(path)) /* If doesn't exist, don't do anything. */ + if (*path == '/' && + !is_existant(path)) /* If doesn't exist, don't do anything. */ { if (was_malloc) free(path); @@ -614,78 +614,87 @@ static int add_file(int percent, register const char *file, const char *dir, { if (was_malloc) free(path); - return FALSE; /* don't recurse */ + return FALSE; /* don't recurse */ } DPRINTF(1, (stderr, "trying to add file \"%s\"\n", path)); - if ( - ( + if (( #ifdef _WIN32 - (!isdir) && + (!isdir) && #endif - ( (fd = open(path, O_RDONLY|O_BINARY)) < 0) - ) - || !path_is_absolute(path)) + ((fd = open(path, O_RDONLY | O_BINARY)) < 0)) || + !path_is_absolute(path)) { - debugprint("sarahhhhh fd=%d path=<%s> dir=<%s> file=<%s> percent=%d\n", fd, path, dir, file, percent); - found = FALSE; - if (dir == NULL && (strchr(file,'/') == NULL)) + debugprint("sarahhhhh fd=%d path=<%s> dir=<%s> file=<%s> percent=%d\n", + fd, path, dir, file, percent); + found = FALSE; + if (dir == NULL && (strchr(file, '/') == NULL)) { - if ( ((sp = strrchr(file,'-')) != NULL) && (strcmp(sp,"-o") == 0) ) + if (((sp = strrchr(file, '-')) != NULL) && (strcmp(sp, "-o") == 0)) { /* BSD-style '-o' offensive file suffix */ *sp = '\0'; - found = (add_file(percent, file, LOCOFFDIR, head, tail, parent)) - || add_file(percent, file, OFFDIR, head, tail, parent); + found = + (add_file(percent, file, LOCOFFDIR, head, tail, parent)) || + add_file(percent, file, OFFDIR, head, tail, parent); /* put the suffix back in for better identification later */ *sp = '-'; } else if (All_forts) - found = (add_file(percent, file, LOCFORTDIR, head, tail, parent) - || add_file(percent, file, LOCOFFDIR, head, tail, parent) - || add_file(percent, file, FORTDIR, head, tail, parent) - || add_file(percent, file, OFFDIR, head, tail, parent)); + found = + (add_file(percent, file, LOCFORTDIR, head, tail, parent) || + add_file( + percent, file, LOCOFFDIR, head, tail, parent) || + add_file(percent, file, FORTDIR, head, tail, parent) || + add_file(percent, file, OFFDIR, head, tail, parent)); else if (Offend) - found = (add_file(percent, file, LOCOFFDIR, head, tail, parent) - || add_file(percent, file, OFFDIR, head, tail, parent)); + found = + (add_file(percent, file, LOCOFFDIR, head, tail, parent) || + add_file(percent, file, OFFDIR, head, tail, parent)); else - found = (add_file(percent, file, LOCFORTDIR, head, tail, parent) - || add_file(percent, file, FORTDIR, head, tail, parent)); + found = + (add_file(percent, file, LOCFORTDIR, head, tail, parent) || + add_file(percent, file, FORTDIR, head, tail, parent)); } if (!found && parent == NULL && dir == NULL) { /* don't display an error when trying language specific files */ - if (env_lang) { - char *lang; - char llang[512]; - char langdir[1024]; - int ret=0; - char *p; + if (env_lang) + { + char *lang; + char llang[512]; + char langdir[1024]; + int ret = 0; + char *p; - strncpy(llang,env_lang,sizeof(llang)); - llang[sizeof(llang)-1] = '\0'; - lang=llang; + strncpy(llang, env_lang, sizeof(llang)); + llang[sizeof(llang) - 1] = '\0'; + lang = llang; - /* the language string can be like "es:fr_BE:ga" */ - while (!ret && lang && (*lang)) { - p=strchr(lang,':'); - if (p) *p++='\0'; - snprintf(langdir,sizeof(langdir),"%s/%s", - FORTDIR,lang); - - if (strncmp(path,lang,2) == 0) - ret=1; - else if (strncmp(path,langdir,strlen(FORTDIR)+3) == 0) - ret=1; - lang=p; + /* the language string can be like "es:fr_BE:ga" */ + while (!ret && lang && (*lang)) + { + p = strchr(lang, ':'); + if (p) + *p++ = '\0'; + snprintf(langdir, sizeof(langdir), "%s/%s", FORTDIR, lang); + + if (strncmp(path, lang, 2) == 0) + ret = 1; + else if (strncmp(path, langdir, strlen(FORTDIR) + 3) == 0) + ret = 1; + lang = p; + } + if (!ret) + { + debugprint("moshe\n"); + perror(path); + } } - if (!ret) { - debugprint("moshe\n"); - perror(path); + else + { + debugprint("abe\n"); + perror(path); } - } else { - debugprint( "abe\n"); - perror(path); - } } if (was_malloc) @@ -699,41 +708,39 @@ static int add_file(int percent, register const char *file, const char *dir, fp->fd = fd; fp->percent = percent; - fp->name = do_malloc (strlen (file) + (size_t)1); - strncpy (fp->name, file, strlen (file) + (size_t)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) + (size_t)1); - strncpy (fp->path, path, strlen (path) + 1UL); + fp->path = do_malloc(strlen(path) + (size_t)1); + strncpy(fp->path, path, strlen(path) + 1UL); - //FIXME + // FIXME fp->utf8_charset = FALSE; - testpath = do_malloc(strlen (path) + 4UL); + testpath = do_malloc(strlen(path) + 4UL); sprintf(testpath, "%s.u8", path); -// fprintf(stderr, "State mal: %s\n", testpath); - if(stat(testpath, &statbuf) == 0) + // fprintf(stderr, "State mal: %s\n", testpath); + if (stat(testpath, &statbuf) == 0) fp->utf8_charset = TRUE; - free (testpath); + free(testpath); testpath = NULL; -// fprintf(stderr, "Is utf8?: %i\n", fp->utf8_charset ); + // fprintf(stderr, "Is utf8?: %i\n", fp->utf8_charset ); fp->parent = parent; - if ((isdir && !add_dir(fp)) || - (!isdir && - !is_fortfile(path, &fp->datfile))) + if ((isdir && !add_dir(fp)) || (!isdir && !is_fortfile(path, &fp->datfile))) { if (parent == NULL) - fprintf(stderr, - "fortune:%s not a fortune file or directory\n", - path); + fprintf( + stderr, "fortune:%s not a fortune file or directory\n", path); if (was_malloc) free(path); do_free(fp->datfile); do_free(fp->posfile); do_free(fp->name); do_free(fp->path); - if (fp->fd >= 0) close(fp->fd); + if (fp->fd >= 0) + close(fp->fd); free(fp); return FALSE; } @@ -741,14 +748,16 @@ static int add_file(int percent, register const char *file, const char *dir, /* This is a hack to come around another hack - add_dir returns success * if the directory is allowed to be empty, but we can not handle an * empty directory... */ - if (isdir && fp->num_children == 0) { + if (isdir && fp->num_children == 0) + { if (was_malloc) free(path); do_free(fp->datfile); do_free(fp->posfile); do_free(fp->name); do_free(fp->path); - if(fp->fd >= 0) close(fp->fd); + if (fp->fd >= 0) + close(fp->fd); free(fp); return TRUE; } @@ -780,13 +789,13 @@ static int add_file(int percent, register const char *file, const char *dir, static int names_compare(const void *a, const void *b) { - return strcmp(*(const char**)a, *(const char**)b); + return strcmp(*(const char **)a, *(const char **)b); } /* * add_dir: * Add the contents of an entire directory. */ -int add_dir(register FILEDESC * fp) +int add_dir(register FILEDESC *fp) { register DIR *dir; register struct dirent *dirent; @@ -808,10 +817,10 @@ int add_dir(register FILEDESC * fp) fp->num_children = 0; max_count_names = 200; count_names = 0; - names = malloc(sizeof(names[0])*max_count_names); - if (! names) + names = malloc(sizeof(names[0]) * max_count_names); + if (!names) { - debugprint( "zach\n"); + debugprint("zach\n"); perror("Out of RAM!"); exit(-1); } @@ -823,8 +832,8 @@ int add_dir(register FILEDESC * fp) if (count_names == max_count_names) { max_count_names += 200; - names = realloc(names, sizeof(names[0])*max_count_names); - if (! names) + names = realloc(names, sizeof(names[0]) * max_count_names); + if (!names) { debugprint("rebecca\n"); perror("Out of RAM!"); @@ -836,7 +845,7 @@ int add_dir(register FILEDESC * fp) closedir(dir); qsort(names, count_names, sizeof(names[0]), names_compare); - for (i=0; i < count_names; ++i) + for (i = 0; i < count_names; ++i) { if (add_file(NO_PROB, names[i], fp->path, &fp->child, &tailp, fp)) { @@ -853,28 +862,30 @@ int add_dir(register FILEDESC * fp) * allowed to be empty. * - Brian Bassett (brianb@debian.org) 1999/07/31 */ - if (strcmp(LOCFORTDIR, fp->path) == 0 || strcmp(LOCOFFDIR, fp->path) == 0) + if (strcmp(LOCFORTDIR, fp->path) == 0 || + strcmp(LOCOFFDIR, fp->path) == 0) { return TRUE; } - fprintf(stderr, - "fortune: %s: No fortune files in directory.\n", fp->path); + fprintf( + stderr, "fortune: %s: No fortune files in directory.\n", fp->path); return FALSE; } return TRUE; } /* -* form_file_list: -* Form the file list from the file specifications. -*/ + * form_file_list: + * Form the file list from the file specifications. + */ static int top_level__add_file(const char *dirpath) { return add_file(NO_PROB, dirpath, NULL, &File_list, &File_tail, NULL); } -static int cond_top_level__add_file(const char *dirpath, const char*possible_dup) +static int cond_top_level__add_file( + const char *dirpath, const char *possible_dup) { if (!strcmp(dirpath, possible_dup)) { @@ -885,7 +896,8 @@ static int cond_top_level__add_file(const char *dirpath, const char*possible_dup static int top_level_LOCFORTDIR(void) { - return (top_level__add_file(LOCFORTDIR) | cond_top_level__add_file(FORTDIR, LOCFORTDIR)); + return (top_level__add_file(LOCFORTDIR) | + cond_top_level__add_file(FORTDIR, LOCFORTDIR)); } static int form_file_list(register char **files, register int file_cnt) @@ -893,52 +905,57 @@ static int form_file_list(register char **files, register int file_cnt) register int i, percent; register char *sp; char langdir[1024]; - char fullpathname[512],locpathname[512]; + char fullpathname[512], locpathname[512]; if (file_cnt == 0) { if (All_forts) - return (top_level__add_file(LOCFORTDIR) - | top_level__add_file(LOCOFFDIR) - | cond_top_level__add_file(FORTDIR, LOCFORTDIR) - | cond_top_level__add_file(OFFDIR, LOCOFFDIR)); + return (top_level__add_file(LOCFORTDIR) | + top_level__add_file(LOCOFFDIR) | + cond_top_level__add_file(FORTDIR, LOCFORTDIR) | + cond_top_level__add_file(OFFDIR, LOCOFFDIR)); else if (Offend) - return ( top_level__add_file(LOCOFFDIR) - | cond_top_level__add_file(OFFDIR, LOCOFFDIR)); - else { - if (env_lang) { + return (top_level__add_file(LOCOFFDIR) | + cond_top_level__add_file(OFFDIR, LOCOFFDIR)); + else + { + if (env_lang) + { char *lang; char llang[512]; - int ret=0; + int ret = 0; char *p; - strncpy(llang,env_lang,sizeof(llang)); - llang[sizeof(llang)-1] = '\0'; - lang=llang; + strncpy(llang, env_lang, sizeof(llang)); + llang[sizeof(llang) - 1] = '\0'; + lang = llang; /* the language string can be like "es:fr_BE:ga" */ - while ( lang && (*lang)) { - p=strchr(lang,':'); - if (p) *p++='\0'; + while (lang && (*lang)) + { + p = strchr(lang, ':'); + if (p) + *p++ = '\0'; /* first try full locale */ - ret=add_file(NO_PROB, lang, NULL, &File_list, - &File_tail, NULL); + ret = add_file( + NO_PROB, lang, NULL, &File_list, &File_tail, NULL); /* if not try language name only (two first chars) */ - if (!ret) { + if (!ret) + { char ll[3]; - strncpy(ll,lang,2); - ll[2]='\0'; - ret=add_file(NO_PROB, ll, NULL, - &File_list, &File_tail, NULL); + strncpy(ll, lang, 2); + ll[2] = '\0'; + ret = add_file( + NO_PROB, ll, NULL, &File_list, &File_tail, NULL); } /* if we have found one we have finished */ if (ret) return ret; - lang=p; + lang = p; } /* default */ return top_level_LOCFORTDIR(); @@ -994,87 +1011,98 @@ static int form_file_list(register char **files, register int file_cnt) } if (strcmp(sp, "all") == 0) { - snprintf(fullpathname,sizeof(fullpathname),"%s",FORTDIR); - snprintf(locpathname,sizeof(locpathname),"%s",LOCFORTDIR); + snprintf(fullpathname, sizeof(fullpathname), "%s", FORTDIR); + snprintf(locpathname, sizeof(locpathname), "%s", LOCFORTDIR); } /* if it isn't an absolute path or relative to . or .. make it an absolute path relative to FORTDIR */ else { - if (strncmp(sp,"/",1)!=0 && strncmp(sp,"./",2)!=0 && - strncmp(sp,"../",3)!=0) + if (strncmp(sp, "/", 1) != 0 && strncmp(sp, "./", 2) != 0 && + strncmp(sp, "../", 3) != 0) { - snprintf(fullpathname,sizeof(fullpathname), - "%s/%s",FORTDIR,sp); - snprintf(locpathname,sizeof(locpathname), - "%s/%s",LOCFORTDIR,sp); + snprintf( + fullpathname, sizeof(fullpathname), "%s/%s", FORTDIR, sp); + snprintf( + locpathname, sizeof(locpathname), "%s/%s", LOCFORTDIR, sp); } else { - snprintf(fullpathname,sizeof(fullpathname),"%s",sp); - snprintf(locpathname,sizeof(locpathname),"%s",sp); + snprintf(fullpathname, sizeof(fullpathname), "%s", sp); + snprintf(locpathname, sizeof(locpathname), "%s", sp); } } - if (env_lang) { + if (env_lang) + { char *lang; char llang[512]; - int ret=0; + int ret = 0; char *p; - strncpy(llang,env_lang,sizeof(llang)); - llang[sizeof(llang)-1] = '\0'; - lang=llang; + strncpy(llang, env_lang, sizeof(llang)); + llang[sizeof(llang) - 1] = '\0'; + lang = llang; /* the language string can be like "es:fr_BE:ga" */ - while (!ret && lang && (*lang)) { - p=strchr(lang,':'); - if (p) *p++='\0'; + while (!ret && lang && (*lang)) + { + p = strchr(lang, ':'); + if (p) + *p++ = '\0'; /* first try full locale */ - snprintf(langdir,sizeof(langdir),"%s/%s/%s", - FORTDIR, lang, sp); - ret=add_file(percent, langdir, NULL, &File_list, - &File_tail, NULL); + snprintf( + langdir, sizeof(langdir), "%s/%s/%s", FORTDIR, lang, sp); + ret = add_file( + percent, langdir, NULL, &File_list, &File_tail, NULL); /* if not try language name only (two first chars) */ - if (!ret) { + if (!ret) + { char ll[3]; - strncpy(ll,lang,2); - ll[2]='\0'; - snprintf(langdir,sizeof(langdir), - "%s/%s/%s", FORTDIR, ll, sp); - ret=add_file(percent, langdir, NULL, - &File_list, &File_tail, NULL); + strncpy(ll, lang, 2); + ll[2] = '\0'; + snprintf( + langdir, sizeof(langdir), "%s/%s/%s", FORTDIR, ll, sp); + ret = add_file( + percent, langdir, NULL, &File_list, &File_tail, NULL); } - lang=p; + lang = p; } /* default */ if (!ret) - ret=add_file(percent, fullpathname, NULL, &File_list, - &File_tail, NULL); - if ( !ret && strncmp(fullpathname, locpathname, sizeof(fullpathname))) - ret=add_file(percent, locpathname, NULL, &File_list, - &File_tail, NULL); + ret = add_file( + percent, fullpathname, NULL, &File_list, &File_tail, NULL); + if (!ret && + strncmp(fullpathname, locpathname, sizeof(fullpathname))) + ret = add_file( + percent, locpathname, NULL, &File_list, &File_tail, NULL); - if (!ret) { - snprintf (locpathname, sizeof (locpathname), "%s/%s", getenv ("PWD"), sp); + if (!ret) + { + snprintf(locpathname, sizeof(locpathname), "%s/%s", + getenv("PWD"), sp); - ret = add_file (percent, locpathname, NULL, &File_list, &File_tail, NULL); + ret = add_file( + percent, locpathname, NULL, &File_list, &File_tail, NULL); } - if (!ret) { + if (!ret) + { return FALSE; } - if (strncmp(fullpathname, locpathname, sizeof(fullpathname)) && strcmp(sp, "all") == 0) { - add_file(percent, locpathname, NULL, &File_list, &File_tail, NULL); + if (strncmp(fullpathname, locpathname, sizeof(fullpathname)) && + strcmp(sp, "all") == 0) + { + add_file( + percent, locpathname, NULL, &File_list, &File_tail, NULL); } } - else - if (!add_file(percent, fullpathname, NULL, &File_list, - &File_tail, NULL)) - return FALSE; + else if (!add_file( + percent, fullpathname, NULL, &File_list, &File_tail, NULL)) + return FALSE; } return TRUE; } @@ -1106,77 +1134,79 @@ static void getargs(int argc, char **argv) #define OFFENSIVE_GETOPT "o" #endif - while ((ch = getopt(argc, argv, "ac" DEBUG_GETOPT "efilm:n:" OFFENSIVE_GETOPT "suvw")) != EOF) + while ((ch = getopt(argc, argv, + "ac" DEBUG_GETOPT "efilm:n:" OFFENSIVE_GETOPT "suvw")) != EOF) switch (ch) - { - case 'a': /* any fortune */ - All_forts = TRUE; - break; + { + case 'a': /* any fortune */ + All_forts = TRUE; + break; #ifdef DEBUG - case 'D': - Debug++; - break; + case 'D': + Debug++; + break; #endif /* DEBUG */ - case 'e': - Equal_probs = TRUE; /* scatter un-allocted prob equally */ - break; - case 'f': /* find fortune files */ - Find_files = TRUE; - break; - case 'l': /* long ones only */ - Long_only = TRUE; - Short_only = FALSE; - break; - case 'n': - SLEN = atoi(optarg); - break; + case 'e': + Equal_probs = TRUE; /* scatter un-allocted prob equally */ + break; + case 'f': /* find fortune files */ + Find_files = TRUE; + break; + case 'l': /* long ones only */ + Long_only = TRUE; + Short_only = FALSE; + break; + case 'n': + SLEN = atoi(optarg); + break; #ifndef NO_OFFENSIVE - case 'o': /* offensive ones only */ - Offend = TRUE; - break; + case 'o': /* offensive ones only */ + Offend = TRUE; + break; #endif - case 's': /* short ones only */ - Short_only = TRUE; - Long_only = FALSE; - break; - case 'w': /* give time to read */ - Wait = TRUE; - break; -#ifdef NO_REGEX - case 'i': /* case-insensitive match */ - case 'm': /* dump out the fortunes */ - (void) fprintf(stderr, - "fortune: can't match fortunes on this system (Sorry)\n"); - exit(0); -#else /* NO_REGEX */ - case 'm': /* dump out the fortunes */ - Match = TRUE; - pat = optarg; - break; - case 'i': /* case-insensitive match */ - ignore_case++; - break; -#endif /* NO_REGEX */ - case 'u': /* Don't recode the fortune */ - No_recode = TRUE; - break; - case 'v': - (void) printf("%s\n", program_version()); - exit(0); - case 'c': - Show_filename = TRUE; - break; - case '?': - default: - usage(); - } + case 's': /* short ones only */ + Short_only = TRUE; + Long_only = FALSE; + break; + case 'w': /* give time to read */ + Wait = TRUE; + break; +#ifdef NO_REGEX + case 'i': /* case-insensitive match */ + case 'm': /* dump out the fortunes */ + (void)fprintf(stderr, + "fortune: can't match fortunes on this system (Sorry)\n"); + exit(0); +#else /* NO_REGEX */ + case 'm': /* dump out the fortunes */ + Match = TRUE; + pat = optarg; + break; + case 'i': /* case-insensitive match */ + ignore_case++; + break; +#endif /* NO_REGEX */ + case 'u': /* Don't recode the fortune */ + No_recode = TRUE; + break; + case 'v': + (void)printf("%s\n", program_version()); + exit(0); + case 'c': + Show_filename = TRUE; + break; + case '?': + default: + usage(); + } argc -= optind; argv += optind; if (!form_file_list(argv, argc)) { - if (!ErrorMessage) fprintf (stderr, "No fortunes found\n"); - exit(1); /* errors printed through form_file_list() */ + if (!ErrorMessage) + fprintf(stderr, "No fortunes found\n"); + exit(1); /* errors printed through form_file_list() */ } #ifdef DEBUG /* if (Debug >= 1) @@ -1191,7 +1221,7 @@ static void getargs(int argc, char **argv) if (BAD_COMP(RE_COMP(pat))) { fprintf(stderr, "bad pattern: %s\n", pat); - exit (1); + exit(1); } if (ignore_case) { @@ -1229,27 +1259,25 @@ static void init_prob(void) else percent += fp->percent; DPRINTF(1, (stderr, "summing probabilities:%d%% with %d NO_PROB's\n", - percent, num_noprob)); + percent, num_noprob)); if (percent > 100) { - fprintf(stderr, - "fortune: probabilities sum to %d%%!\n", percent); + fprintf(stderr, "fortune: probabilities sum to %d%%!\n", percent); exit(1); } else if (percent < 100 && num_noprob == 0) { fprintf(stderr, - "fortune: no place to put residual probability (%d%%)\n", - percent); + "fortune: no place to put residual probability (%d%%)\n", percent); exit(1); } else if (percent == 100 && num_noprob != 0) { - fprintf(stderr, - "fortune: no probability left to put in residual files\n"); + fprintf( + stderr, "fortune: no probability left to put in residual files\n"); exit(1); } - Spec_prob = percent; /* this is for -f when % is specified on cmd line */ + Spec_prob = percent; /* this is for -f when % is specified on cmd line */ percent = 100 - percent; if (Equal_probs) { @@ -1271,16 +1299,15 @@ static void init_prob(void) } else { - DPRINTF(1, (stderr, - ", %d%% distributed over remaining fortunes\n", - percent)); + DPRINTF(1, (stderr, ", %d%% distributed over remaining fortunes\n", + percent)); } } DPRINTF(1, (stderr, "\n")); #ifdef DEBUG /* if (Debug >= 1) - * print_list(File_list, 0); *//* Causes crash with new %% code */ + * print_list(File_list, 0); *//* Causes crash with new %% code */ #endif } @@ -1288,7 +1315,7 @@ static void init_prob(void) * zero_tbl: * Zero out the fields we care about in a tbl structure. */ -static void zero_tbl(register STRFILE * tp) +static void zero_tbl(register STRFILE *tp) { tp->str_numstr = 0; tp->str_longlen = 0; @@ -1299,7 +1326,7 @@ static void zero_tbl(register STRFILE * tp) * sum_tbl: * Merge the tbl data of t2 into t1. */ -static void sum_tbl(register STRFILE * t1, register STRFILE * t2) +static void sum_tbl(register STRFILE *t1, register STRFILE *t2) { t1->str_numstr += t2->str_numstr; if (t1->str_longlen < t2->str_longlen) @@ -1312,7 +1339,7 @@ static void sum_tbl(register STRFILE * t1, register STRFILE * t2) * get_tbl: * Get the tbl data file the datfile. */ -static void get_tbl(FILEDESC * fp) +static void get_tbl(FILEDESC *fp) { auto int fd; register FILEDESC *child; @@ -1339,51 +1366,45 @@ static void get_tbl(FILEDESC * fp) } /* End */ #endif - if ((fd = open(fp->datfile, O_RDONLY|O_BINARY)) < 0) + if ((fd = open(fp->datfile, O_RDONLY | O_BINARY)) < 0) { perror(fp->datfile); exit(1); } if (read(fd, &fp->tbl.str_version, sizeof fp->tbl.str_version) != - sizeof fp->tbl.str_version) + sizeof fp->tbl.str_version) { - fprintf(stderr, - "fortune: %s corrupted\n", fp->path); + fprintf(stderr, "fortune: %s corrupted\n", fp->path); exit(1); } if (read(fd, &fp->tbl.str_numstr, sizeof fp->tbl.str_numstr) != - sizeof fp->tbl.str_numstr) + sizeof fp->tbl.str_numstr) { - fprintf(stderr, - "fortune: %s corrupted\n", fp->path); + fprintf(stderr, "fortune: %s corrupted\n", fp->path); exit(1); } if (read(fd, &fp->tbl.str_longlen, sizeof fp->tbl.str_longlen) != - sizeof fp->tbl.str_longlen) + sizeof fp->tbl.str_longlen) { - fprintf(stderr, - "fortune: %s corrupted\n", fp->path); + fprintf(stderr, "fortune: %s corrupted\n", fp->path); exit(1); } if (read(fd, &fp->tbl.str_shortlen, sizeof fp->tbl.str_shortlen) != - sizeof fp->tbl.str_shortlen) + sizeof fp->tbl.str_shortlen) { - fprintf(stderr, - "fortune: %s corrupted\n", fp->path); + fprintf(stderr, "fortune: %s corrupted\n", fp->path); exit(1); } if (read(fd, &fp->tbl.str_flags, sizeof fp->tbl.str_flags) != - sizeof fp->tbl.str_flags) + sizeof fp->tbl.str_flags) { - fprintf(stderr, - "fortune: %s corrupted\n", fp->path); + fprintf(stderr, "fortune: %s corrupted\n", fp->path); exit(1); } if (read(fd, &fp->tbl.stuff, sizeof fp->tbl.stuff) != - sizeof fp->tbl.stuff) + sizeof fp->tbl.stuff) { - fprintf(stderr, - "fortune: %s corrupted\n", fp->path); + fprintf(stderr, "fortune: %s corrupted\n", fp->path); exit(1); } fp->tbl.str_version = ntohl(fp->tbl.str_version); @@ -1409,7 +1430,7 @@ static void get_tbl(FILEDESC * fp) * sum_noprobs: * Sum up all the noprob probabilities, starting with fp. */ -static void sum_noprobs(register FILEDESC * fp) +static void sum_noprobs(register FILEDESC *fp) { static bool did_noprobs = FALSE; @@ -1432,7 +1453,7 @@ static void sum_noprobs(register FILEDESC * fp) * pick_child * Pick a child from a chosen parent. */ -static FILEDESC *pick_child(FILEDESC * parent) +static FILEDESC *pick_child(FILEDESC *parent) { register FILEDESC *fp; register int choice; @@ -1440,8 +1461,8 @@ static FILEDESC *pick_child(FILEDESC * parent) if (Equal_probs) { choice = my_random(parent->num_children); - DPRINTF(1, (stderr, " choice = %d (of %d)\n", - choice, 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)); @@ -1451,17 +1472,17 @@ static FILEDESC *pick_child(FILEDESC * parent) { get_tbl(parent); choice = (int)(my_random(parent->tbl.str_numstr)); - DPRINTF(1, (stderr, " choice = %d (of %ld)\n", - choice, parent->tbl.str_numstr)); + DPRINTF(1, (stderr, " choice = %d (of %ld)\n", choice, + parent->tbl.str_numstr)); for (fp = parent->child; choice >= (int)fp->tbl.str_numstr; fp = fp->next) { choice -= fp->tbl.str_numstr; - DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n", - fp->name, fp->tbl.str_numstr, choice)); + DPRINTF(1, (stderr, "\tskip %s, %ld (choice = %d)\n", fp->name, + fp->tbl.str_numstr, choice)); } - DPRINTF(1, (stderr, " using %s, %ld\n", fp->name, - fp->tbl.str_numstr)); + DPRINTF( + 1, (stderr, " using %s, %ld\n", fp->name, fp->tbl.str_numstr)); return fp; } } @@ -1470,9 +1491,10 @@ static FILEDESC *pick_child(FILEDESC * parent) * open_dat: * Open up the dat file if we need to. */ -static void open_dat(FILEDESC * fp) +static void open_dat(FILEDESC *fp) { - if (fp->datfd < 0 && (fp->datfd = open(fp->datfile, O_RDONLY|O_BINARY)) < 0) + if (fp->datfd < 0 && + (fp->datfd = open(fp->datfile, O_RDONLY | O_BINARY)) < 0) { exit(1); } @@ -1483,7 +1505,7 @@ static void open_dat(FILEDESC * fp) * Get the position from the pos file, if there is one. If not, * return a random number. */ -static void get_pos(FILEDESC * fp) +static void get_pos(FILEDESC *fp) { assert(fp->read_tbl); if (fp->pos == POS_UNKNOWN) @@ -1516,13 +1538,11 @@ static void get_fort(void) else { choice -= fp->percent; - DPRINTF(1, (stderr, - " skip \"%s\", %d%% (choice = %d)\n", - fp->name, fp->percent, choice)); + DPRINTF(1, (stderr, " skip \"%s\", %d%% (choice = %d)\n", + fp->name, fp->percent, choice)); } - DPRINTF(1, (stderr, - "using \"%s\", %d%% (choice = %d)\n", - fp->name, fp->percent, choice)); + DPRINTF(1, (stderr, "using \"%s\", %d%% (choice = %d)\n", fp->name, + fp->percent, choice)); } if (fp->percent != NO_PROB) get_tbl(fp); @@ -1533,18 +1553,16 @@ static void get_fort(void) sum_noprobs(fp); choice = (int)(my_random(Noprob_tbl.str_numstr)); DPRINTF(1, (stderr, "choice = %d (of %ld) \n", choice, - Noprob_tbl.str_numstr)); + Noprob_tbl.str_numstr)); while (choice >= (int)fp->tbl.str_numstr) { choice -= (int)fp->tbl.str_numstr; fp = fp->next; - DPRINTF(1, (stderr, - " skip \"%s\", %ld (choice = %d)\n", - fp->name, fp->tbl.str_numstr, - choice)); + DPRINTF(1, (stderr, " skip \"%s\", %ld (choice = %d)\n", + fp->name, fp->tbl.str_numstr, choice)); } - DPRINTF(1, (stderr, "using \"%s\", %ld\n", fp->name, - fp->tbl.str_numstr)); + DPRINTF(1, + (stderr, "using \"%s\", %ld\n", fp->name, fp->tbl.str_numstr)); } get_tbl(fp); } @@ -1562,9 +1580,9 @@ static void get_fort(void) get_pos(fp); open_dat(fp); lseek(fp->datfd, - (off_t) (sizeof fp->tbl + (size_t)fp->pos * sizeof Seekpts[0]), 0); - if ((read(fp->datfd, &Seekpts[0], sizeof Seekpts[0]) <0)|| - (read(fp->datfd, &Seekpts[1], sizeof Seekpts[1])<0)) + (off_t)(sizeof fp->tbl + (size_t)fp->pos * sizeof Seekpts[0]), 0); + if ((read(fp->datfd, &Seekpts[0], sizeof Seekpts[0]) < 0) || + (read(fp->datfd, &Seekpts[1], sizeof Seekpts[1]) < 0)) { exit(1); } @@ -1576,7 +1594,7 @@ static void get_fort(void) * open_fp: * Assocatiate a FILE * with the given FILEDESC. */ -static void open_fp(FILEDESC * fp) +static void open_fp(FILEDESC *fp) { if (fp->inf == NULL && (fp->inf = fdopen(fp->fd, "r")) == NULL) { @@ -1590,7 +1608,7 @@ static void open_fp(FILEDESC * fp) * maxlen_in_list * Return the maximum fortune len in the file list. */ -static int maxlen_in_list(FILEDESC * list) +static int maxlen_in_list(FILEDESC *list) { register FILEDESC *fp; register int len, maxlen; @@ -1619,7 +1637,7 @@ static int maxlen_in_list(FILEDESC * list) * matches_in_list * Print out the matches from the files in the list. */ -static void matches_in_list(FILEDESC * list) +static void matches_in_list(FILEDESC *list) { unsigned char *sp; unsigned char *p; /* -allover */ @@ -1650,10 +1668,10 @@ static void matches_in_list(FILEDESC * list) *sp = '\0'; nchar = (int)(sp - Fortbuf); - if (fp->utf8_charset && (! No_recode)) + if (fp->utf8_charset && (!No_recode)) { #ifdef WITH_RECODE - output = recode_string (request, (const char *)Fortbuf); + output = recode_string(request, (const char *)Fortbuf); #else output = strdup(Fortbuf); #endif @@ -1664,7 +1682,7 @@ static void matches_in_list(FILEDESC * list) } /* Should maybe rot13 Fortbuf -allover */ - if(fp->tbl.str_flags & STR_ROTATED) + if (fp->tbl.str_flags & STR_ROTATED) { for (p = (unsigned char *)output; (ch = *p); ++p) { @@ -1676,22 +1694,22 @@ static void matches_in_list(FILEDESC * list) } DPRINTF(1, (stdout, "nchar = %d\n", nchar)); - if ( (nchar < SLEN || !Short_only) && - (nchar > SLEN || !Long_only) && - RE_EXEC(output) ) + if ((nchar < SLEN || !Short_only) && + (nchar > SLEN || !Long_only) && RE_EXEC(output)) { if (!in_file) { - fprintf(stderr, "(%s)\n%c\n", fp->name, fp->tbl.str_delim); + fprintf( + stderr, "(%s)\n%c\n", fp->name, fp->tbl.str_delim); Found_one = TRUE; in_file = TRUE; } - fputs (output, stdout); + fputs(output, stdout); printf("%c\n", fp->tbl.str_delim); } - if (fp->utf8_charset && (! No_recode)) - free (output); + if (fp->utf8_charset && (!No_recode)) + free(output); sp = Fortbuf; } @@ -1708,7 +1726,7 @@ static int find_matches(void) Fort_len = maxlen_in_list(File_list); DPRINTF(2, (stderr, "Maximum length is %d\n", Fort_len)); /* extra length, "%\n" is appended */ - Fortbuf = do_malloc((unsigned int) Fort_len + 10); + Fortbuf = do_malloc((unsigned int)Fort_len + 10); Found_one = FALSE; matches_in_list(File_list); @@ -1717,17 +1735,18 @@ static int find_matches(void) } #endif /* NO_REGEX */ -static void display(FILEDESC * fp) +static void display(FILEDESC *fp) { register char *p, ch; unsigned char line[BUFSIZ]; open_fp(fp); - fseek(fp->inf, (long) Seekpts[0], 0); + fseek(fp->inf, (long)Seekpts[0], 0); if (Show_filename) - printf ("(%s)\n%%\n", fp->name); + printf("(%s)\n%%\n", fp->name); for (Fort_len = 0; fgets((char *)line, sizeof line, fp->inf) != NULL && - !STR_ENDSTRING(line, fp->tbl); Fort_len++) + !STR_ENDSTRING(line, fp->tbl); + Fort_len++) { if (fp->tbl.str_flags & STR_ROTATED) { @@ -1735,14 +1754,15 @@ static void display(FILEDESC * fp) { if (isupper(ch) && isascii(ch)) *p = 'A' + (ch - 'A' + 13) % 26; - else if (islower(ch) && isascii (ch)) + else if (islower(ch) && isascii(ch)) *p = 'a' + (ch - 'a' + 13) % 26; } } - if (fp->utf8_charset && (! No_recode)) { + if (fp->utf8_charset && (!No_recode)) + { char *output; #ifdef WITH_RECODE - output = recode_string (request, (const char *)line); + output = recode_string(request, (const char *)line); #else output = strdup(line); #endif @@ -1765,11 +1785,11 @@ static int fortlen(void) char line[BUFSIZ]; if (!(Fortfile->tbl.str_flags & (STR_RANDOM | STR_ORDERED))) - nchar = (Seekpts[1] - Seekpts[0]) - 2; /* for %^J delimiter */ + nchar = (Seekpts[1] - Seekpts[0]) - 2; /* for %^J delimiter */ else { open_fp(Fortfile); - fseek(Fortfile->inf, (long) Seekpts[0], 0); + fseek(Fortfile->inf, (long)Seekpts[0], 0); nchar = 0; while (fgets(line, sizeof line, Fortfile->inf) != NULL && !STR_ENDSTRING(line, Fortfile->tbl)) @@ -1779,10 +1799,7 @@ static int fortlen(void) return nchar; } -static int mymax(register int i, register int j) -{ - return (i >= j ? i : j); -} +static int mymax(register int i, register int j) { return (i >= j ? i : j); } static void free_desc(FILEDESC *ptr) { @@ -1809,27 +1826,30 @@ int main(int ac, char *av[]) const char *ctype; char *crequest; int exit_code = 0; - env_lang=getenv("LC_ALL"); - if (!env_lang) env_lang=getenv("LC_MESSAGES"); - if (!env_lang) env_lang=getenv("LANGUAGE"); - if (!env_lang) env_lang=getenv("LANG"); + env_lang = getenv("LC_ALL"); + if (!env_lang) + env_lang = getenv("LC_MESSAGES"); + if (!env_lang) + env_lang = getenv("LANGUAGE"); + if (!env_lang) + env_lang = getenv("LANG"); #ifdef _WIN32 - if (!env_lang) { - env_lang="en"; + if (!env_lang) + { + env_lang = "en"; } #endif - #ifndef DONT_CALL_GETARGS getargs(ac, av); #endif #ifdef WITH_RECODE outer = recode_new_outer(true); - request = recode_new_request (outer); + request = recode_new_request(outer); #endif - setlocale(LC_ALL,""); + setlocale(LC_ALL, ""); #ifdef _WIN32 ctype = "C"; #else @@ -1838,16 +1858,16 @@ int main(int ac, char *av[]) { ctype = "C"; } - else if(strcmp(ctype,"ANSI_X3.4-1968") == 0) + else if (strcmp(ctype, "ANSI_X3.4-1968") == 0) { - ctype="ISO-8859-1"; + ctype = "ISO-8859-1"; } #endif #ifdef WITH_RECODE crequest = malloc(strlen(ctype) + 7 + 1); sprintf(crequest, "UTF-8..%s", ctype); - recode_scan_request (request, crequest); + recode_scan_request(request, crequest); free(crequest); #endif @@ -1869,20 +1889,19 @@ int main(int ac, char *av[]) } else { - srandom((unsigned int) (time((time_t *) NULL) + getpid())); + srandom((unsigned int)(time((time_t *)NULL) + getpid())); do { get_fort(); - } - while ((Short_only && fortlen() > SLEN) || - (Long_only && fortlen() <= SLEN)); + } while ((Short_only && fortlen() > SLEN) || + (Long_only && fortlen() <= SLEN)); display(Fortfile); if (Wait) { fortlen(); - sleep((unsigned int) mymax(Fort_len / CPERS, MINW)); + sleep((unsigned int)mymax(Fort_len / CPERS, MINW)); } } cleanup: diff --git a/fortune-mod/util/fortune-mod-common.h b/fortune-mod/util/fortune-mod-common.h index 0a2407e..9d7f369 100644 --- a/fortune-mod/util/fortune-mod-common.h +++ b/fortune-mod/util/fortune-mod-common.h @@ -36,26 +36,26 @@ * @(#)strfile.h 8.1 (Berkeley) 5/31/93 */ #pragma once -#include -#include +#include +#include /* For ntohl() */ #ifdef _WIN32 -#include +#include #define getpid() 0 #else -#include -#include +#include +#include #endif -#include -#include "strfile.h" -#include -#include -#include -#include -#include -#include +#include +#include "strfile.h" +#include +#include +#include +#include +#include +#include #ifndef MAXPATHLEN -#define MAXPATHLEN 1024 +#define MAXPATHLEN 1024 #endif /* MAXPATHLEN */ #if defined(_WIN32) #include diff --git a/fortune-mod/util/getopt.c b/fortune-mod/util/getopt.c index e05f80b..c532327 100644 --- a/fortune-mod/util/getopt.c +++ b/fortune-mod/util/getopt.c @@ -1,107 +1,112 @@ #include "getopt.h" // make sure you construct the header file as dictated above /* -* Copyright (c) 1987, 1993, 1994 -* The Regents of the University of California. All rights reserved. -* -* 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. -*/ + * Copyright (c) 1987, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * 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. + */ #include #include -int opterr = 1, /* if error message should be printed */ - optind = 1, /* index into parent argv vector */ - optopt, /* character checked for validity */ - optreset; /* reset getopt */ -char *optarg; /* argument associated with option */ +int opterr = 1, /* if error message should be printed */ + optind = 1, /* index into parent argv vector */ + optopt, /* character checked for validity */ + optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ -#define BADCH (int)'?' -#define BADARG (int)':' -#define EMSG "" +#define BADCH (int)'?' +#define BADARG (int)':' +#define EMSG "" /* * getopt -- * Parse argc/argv argument vector. */ -int getopt(int nargc, char * const nargv[], const char *ostr) +int getopt(int nargc, char *const nargv[], const char *ostr) { - static char *place = EMSG; /* option letter processing */ - const char *oli; /* option letter list index */ + static char *place = EMSG; /* option letter processing */ + const char *oli; /* option letter list index */ - if (optreset || !*place) { /* update scanning pointer */ - optreset = 0; - if (optind >= nargc || *(place = nargv[optind]) != '-') { - place = EMSG; - return (-1); + if (optreset || !*place) + { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc || *(place = nargv[optind]) != '-') + { + place = EMSG; + return (-1); + } + if (place[1] && *++place == '-') + { /* found "--" */ + ++optind; + place = EMSG; + return (-1); + } + } /* option letter okay? */ + if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr, optopt))) + { + /* + * if the user didn't specify '-' as an option, + * assume it means -1. + */ + if (optopt == (int)'-') + return (-1); + if (!*place) + ++optind; + if (opterr && *ostr != ':') + (void)printf("illegal option -- %c\n", optopt); + return (BADCH); } - if (place[1] && *++place == '-') { /* found "--" */ - ++optind; - place = EMSG; - return (-1); + if (*++oli != ':') + { /* don't need argument */ + optarg = NULL; + if (!*place) + ++optind; } - } /* option letter okay? */ - if ((optopt = (int)*place++) == (int)':' || - !(oli = strchr(ostr, optopt))) { - /* - * if the user didn't specify '-' as an option, - * assume it means -1. - */ - if (optopt == (int)'-') - return (-1); - if (!*place) + else + { /* need an argument */ + if (*place) /* no white space */ + optarg = place; + else if (nargc <= ++optind) + { /* no arg */ + place = EMSG; + if (*ostr == ':') + return (BADARG); + if (opterr) + (void)printf("option requires an argument -- %c\n", optopt); + return (BADCH); + } + else /* white space */ + optarg = nargv[optind]; + place = EMSG; ++optind; - if (opterr && *ostr != ':') - (void)printf("illegal option -- %c\n", optopt); - return (BADCH); - } - if (*++oli != ':') { /* don't need argument */ - optarg = NULL; - if (!*place) - ++optind; - } - else { /* need an argument */ - if (*place) /* no white space */ - optarg = place; - else if (nargc <= ++optind) { /* no arg */ - place = EMSG; - if (*ostr == ':') - return (BADARG); - if (opterr) - (void)printf("option requires an argument -- %c\n", optopt); - return (BADCH); } - else /* white space */ - optarg = nargv[optind]; - place = EMSG; - ++optind; - } - return (optopt); /* dump back option letter */ + return (optopt); /* dump back option letter */ } - diff --git a/fortune-mod/util/getopt.h b/fortune-mod/util/getopt.h index ce450f4..3b4da8c 100644 --- a/fortune-mod/util/getopt.h +++ b/fortune-mod/util/getopt.h @@ -5,11 +5,11 @@ #ifndef GETOPT_H #define GETOPT_H -extern int opterr, /* if error message should be printed */ - optind, /* index into parent argv vector */ - optopt, /* character checked for validity */ - optreset; /* reset getopt */ -extern char *optarg; /* argument associated with option */ -int getopt(int nargc, char * const nargv[], const char *ostr) ; +extern int opterr, /* if error message should be printed */ + optind, /* index into parent argv vector */ + optopt, /* character checked for validity */ + optreset; /* reset getopt */ +extern char *optarg; /* argument associated with option */ +int getopt(int nargc, char *const nargv[], const char *ostr); #endif /* !GETOPT_H */ diff --git a/fortune-mod/util/randstr.c b/fortune-mod/util/randstr.c index a53ca81..1aeb4b2 100644 --- a/fortune-mod/util/randstr.c +++ b/fortune-mod/util/randstr.c @@ -89,14 +89,13 @@ #include "fortune-mod-common.h" #include "rinutils/unused.h" -char *Infile, /* name of input file */ - Datafile[MAXPATHLEN], /* name of data file */ - Delimch; /* delimiter character */ +char *Infile, /* name of input file */ + Datafile[MAXPATHLEN], /* name of data file */ + Delimch; /* delimiter character */ FILE *Inf, *Dataf, *Outf; -off_t pos, Seekpts[2]; /* seek pointers to fortunes */ - +off_t pos, Seekpts[2]; /* seek pointers to fortunes */ static void getargs(char *av[]) { @@ -107,8 +106,8 @@ static void getargs(char *av[]) if (*av) { Infile = *av; -/* Hmm. Don't output anything if we can help it. - * fprintf(stderr, "Input file: %s\n",Infile); */ + /* Hmm. Don't output anything if we can help it. + * fprintf(stderr, "Input file: %s\n",Infile); */ if (!strrchr(Infile, '.')) { strcpy(Datafile, Infile); @@ -122,12 +121,14 @@ static void getargs(char *av[]) } } else -/* { - * Don't write out errors here, either; trust in exit codes and sh - * fprintf(stderr, "No input file name\n"); - * fprintf(stderr, "Usage:\n\tunstr [-c C] datafile[.ext] [outputfile]\n"); - */ exit(1); -/* } */ + /* { + * Don't write out errors here, either; trust in exit codes and sh + * fprintf(stderr, "No input file name\n"); + * fprintf(stderr, "Usage:\n\tunstr [-c C] datafile[.ext] + * [outputfile]\n"); + */ + exit(1); + /* } */ } /* @@ -135,7 +136,7 @@ static void getargs(char *av[]) * Get the position from the pos file, if there is one. If not, * return a random number. */ -static void get_pos(STRFILE * fp) +static void get_pos(STRFILE *fp) { pos = random() % fp->str_numstr; if (++(pos) >= fp->str_numstr) @@ -149,7 +150,7 @@ static void get_pos(STRFILE * fp) static void get_fort(STRFILE fp) { get_pos(&fp); - fseek(Dataf, (long) (sizeof fp + pos * sizeof Seekpts[0]), 0); + fseek(Dataf, (long)(sizeof fp + pos * sizeof Seekpts[0]), 0); if (!fread(Seekpts, sizeof Seekpts, 1, Dataf)) { exit(1); @@ -158,15 +159,16 @@ static void get_fort(STRFILE fp) Seekpts[1] = ntohl(Seekpts[1]); } -static void display(FILE * fp, STRFILE table) +static void display(FILE *fp, STRFILE table) { register char *p, ch; char line[BUFSIZ]; int i; - fseek(fp, (long) Seekpts[0], 0); - for (i = 0; fgets(line, sizeof line, fp) != NULL && - !STR_ENDSTRING(line, table); i++) + fseek(fp, (long)Seekpts[0], 0); + for (i = 0; + fgets(line, sizeof line, fp) != NULL && !STR_ENDSTRING(line, table); + i++) { if (table.str_flags & STR_ROTATED) for (p = line; (ch = *p); ++p) @@ -183,7 +185,7 @@ static void display(FILE * fp, STRFILE table) int main(int ac GCC_UNUSED, char **av) { - static STRFILE tbl; /* description table */ + static STRFILE tbl; /* description table */ getargs(av); if ((Inf = fopen(Infile, "r")) == NULL) @@ -196,7 +198,7 @@ int main(int ac GCC_UNUSED, char **av) perror(Datafile); exit(1); } - if (!fread((char *) &tbl, sizeof tbl, 1, Dataf)) + if (!fread((char *)&tbl, sizeof tbl, 1, Dataf)) { exit(1); } @@ -206,7 +208,7 @@ int main(int ac GCC_UNUSED, char **av) tbl.str_shortlen = ntohl(tbl.str_shortlen); tbl.str_flags = ntohl(tbl.str_flags); - srandom((int) (time((time_t *) NULL) + getpid())); + srandom((int)(time((time_t *)NULL) + getpid())); get_fort(tbl); display(Inf, tbl); diff --git a/fortune-mod/util/rot.c b/fortune-mod/util/rot.c index 316580d..2f7e2ce 100644 --- a/fortune-mod/util/rot.c +++ b/fortune-mod/util/rot.c @@ -13,11 +13,8 @@ int main(void) while ((a = getchar()) != EOF) { - putchar( - isupper(a) ? ('A' + (a - 'A' + 13) % 26) - : islower(a) ? ('a' + (a - 'a' + 13) % 26) - : a - ); + putchar(isupper(a) ? ('A' + (a - 'A' + 13) % 26) + : islower(a) ? ('a' + (a - 'a' + 13) % 26) : a); } exit(0); } diff --git a/fortune-mod/util/strfile.c b/fortune-mod/util/strfile.c index 005dde3..8b6feed 100644 --- a/fortune-mod/util/strfile.c +++ b/fortune-mod/util/strfile.c @@ -103,53 +103,55 @@ * */ -#define FALSE 0 - -#define STORING_PTRS (Oflag || Rflag) -#define CHUNKSIZE 512 - -#define ALWAYS 1 -#define ALLOC(ptr,sz) if (ALWAYS) { \ - if (ptr == NULL) \ - ptr = malloc((unsigned int) (CHUNKSIZE * sizeof *ptr)); \ - else if (((sz) + 1) % CHUNKSIZE == 0) \ - ptr = realloc((void *) ptr, ((unsigned int) ((sz) + CHUNKSIZE) * sizeof *ptr)); \ - if (ptr == NULL) { \ - fprintf(stderr, "out of space\n"); \ - exit(1); \ - } \ - } +#define FALSE 0 + +#define STORING_PTRS (Oflag || Rflag) +#define CHUNKSIZE 512 + +#define ALWAYS 1 +#define ALLOC(ptr, sz) \ + if (ALWAYS) \ + { \ + if (ptr == NULL) \ + ptr = malloc((unsigned int)(CHUNKSIZE * sizeof *ptr)); \ + else if (((sz) + 1) % CHUNKSIZE == 0) \ + ptr = realloc((void *)ptr, \ + ((unsigned int)((sz) + CHUNKSIZE) * sizeof *ptr)); \ + if (ptr == NULL) \ + { \ + fprintf(stderr, "out of space\n"); \ + exit(1); \ + } \ + } typedef struct { char first; int32_t pos; -} -STR; +} STR; -static char *Infile = NULL, /* input file name */ - Outfile[MAXPATHLEN] = "", /* output file name */ - Delimch = '%'; /* delimiting character */ +static char *Infile = NULL, /* input file name */ + Outfile[MAXPATHLEN] = "", /* output file name */ + Delimch = '%'; /* delimiting character */ -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 */ +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 */ static int32_t *Seekpts; -static FILE *Sort_1, *Sort_2; /* pointers for sorting */ +static FILE *Sort_1, *Sort_2; /* pointers for sorting */ -static STRFILE Tbl; /* statistics table */ +static STRFILE Tbl; /* statistics table */ -static STR *Firstch; /* first chars of each string */ +static STR *Firstch; /* first chars of each string */ static void __attribute__((noreturn)) usage(void) { - fprintf(stderr, - "strfile [-iorsx] [-c char] sourcefile [datafile]\n"); + fprintf(stderr, "strfile [-iorsx] [-c char] sourcefile [datafile]\n"); exit(1); } @@ -162,41 +164,41 @@ static void getargs(int argc, char **argv) while ((ch = getopt(argc, argv, "c:iorsx")) != EOF) switch (ch) - { - case 'c': /* new delimiting char */ - Delimch = *optarg; - if (!isascii(Delimch)) - { - printf("bad delimiting character: '\\%o\n'", - (unsigned int)Delimch); - } - break; - case 'i': /* ignore case in ordering */ - Iflag++; - break; - case 'o': /* order strings */ - Oflag++; - break; - case 'r': /* randomize pointers */ - Rflag++; - break; - case 's': /* silent */ - Sflag++; - break; - case 'x': /* set the rotated bit */ - Xflag++; - break; - case '?': - default: - usage(); - } + { + case 'c': /* new delimiting char */ + Delimch = *optarg; + if (!isascii(Delimch)) + { + printf("bad delimiting character: '\\%o\n'", + (unsigned int)Delimch); + } + break; + case 'i': /* ignore case in ordering */ + Iflag++; + break; + case 'o': /* order strings */ + Oflag++; + break; + case 'r': /* randomize pointers */ + Rflag++; + break; + case 's': /* silent */ + Sflag++; + break; + case 'x': /* set the rotated bit */ + Xflag++; + break; + case '?': + default: + usage(); + } argv += optind; if (*argv) { Infile = *argv; if (*++argv) - (void) strcpy(Outfile, *argv); + (void)strcpy(Outfile, *argv); } if (!Infile) { @@ -214,7 +216,7 @@ static void getargs(int argc, char **argv) * add_offset: * Add an offset to the list, or write it out, as appropriate. */ -static void add_offset(FILE * fp, int32_t off) +static void add_offset(FILE *fp, int32_t off) { if (!STORING_PTRS) { @@ -234,7 +236,7 @@ static void add_offset(FILE * fp, int32_t off) * fix_last_offset: * Used when we have two separators in a row. */ -static void fix_last_offset(FILE * fp, int32_t off) +static void fix_last_offset(FILE *fp, int32_t off) { if (!STORING_PTRS) { @@ -256,11 +258,11 @@ static int cmp_str(const void *v1, const void *v2) register int n1, n2; register const STR *p1, *p2; -#define SET_N(nf,ch) (nf = (ch == '\n')) -#define IS_END(ch,nf) (ch == Delimch && nf) +#define SET_N(nf, ch) (nf = (ch == '\n')) +#define IS_END(ch, nf) (ch == Delimch && nf) - p1 = (const STR *) v1; - p2 = (const STR *) v2; + p1 = (const STR *)v1; + p2 = (const STR *)v2; c1 = p1->first; c2 = p2->first; if (c1 != c2) @@ -303,8 +305,7 @@ static int cmp_str(const void *v1, const void *v2) * do_order: * Order the strings alphabetically (possibly ignoring case). */ -static void - do_order(void) +static void do_order(void) { register long i; register int32_t *lp; @@ -312,10 +313,12 @@ static void Sort_1 = fopen(Infile, "r"); Sort_2 = fopen(Infile, "r"); - 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 - */ + 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 + */ i = Num_pts - 1; lp = Seekpts; fp = Firstch; @@ -363,12 +366,13 @@ static void randomize(void) register int32_t tmp; register int32_t *sp; - srandom((unsigned 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 = Tbl.str_numstr; + * See comment above. Isn't this stuff distributed worldwide? How + * embarrassing! + */ cnt = (int)Num_pts; /* @@ -404,7 +408,7 @@ int main(int ac, char **av) static char string[257]; bool len_was_set = false; - getargs(ac, av); /* evalute arguments */ + getargs(ac, av); /* evalute arguments */ if ((inf = fopen(Infile, "r")) == NULL) { perror(Infile); @@ -417,14 +421,14 @@ int main(int ac, char **av) exit(1); } if (!STORING_PTRS) - (void) fseek(outf, sizeof Tbl, 0); + (void)fseek(outf, sizeof Tbl, 0); /* * Write the strings onto the file */ Tbl.str_longlen = 0; - Tbl.str_shortlen = (unsigned int) 0xffffffff; + Tbl.str_shortlen = (unsigned int)0xffffffff; Tbl.str_delim = (uint8_t)Delimch; Tbl.str_version = STRFILE_VERSION; first = Oflag; @@ -438,11 +442,11 @@ int main(int ac, char **av) 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. - * We had to make the assignment of last_off slightly - * redundant to achieve this. - */ + /* Here's where we go back and fix things, if the + * 'fortune' just read was the null string. + * We had to make the assignment of last_off slightly + * redundant to achieve this. + */ { if (pos - last_off == 2) fix_last_offset(outf, pos); @@ -451,7 +455,7 @@ int main(int ac, char **av) } last_off = pos; add_offset(outf, pos); - if (! len_was_set) + if (!len_was_set) { Tbl.str_longlen = (uint32_t)length; Tbl.str_shortlen = (uint32_t)length; @@ -479,8 +483,7 @@ int main(int ac, char **av) fp->pos = Seekpts[Num_pts - 1]; first = FALSE; } - } - while (sp != NULL); + } while (sp != NULL); /* * write the tables in @@ -507,14 +510,16 @@ int main(int ac, char **av) puts("There was 1 string"); else printf("There were %ld strings\n", Num_pts - 1); - printf("Longest string: %lu byte%s\n", (unsigned long)(Tbl.str_longlen), + printf("Longest string: %lu byte%s\n", + (unsigned long)(Tbl.str_longlen), Tbl.str_longlen == 1 ? "" : "s"); - printf("Shortest string: %lu byte%s\n", (unsigned long)(Tbl.str_shortlen), + printf("Shortest string: %lu byte%s\n", + (unsigned long)(Tbl.str_shortlen), Tbl.str_shortlen == 1 ? "" : "s"); } } - fseek(outf, (off_t) 0, 0); + fseek(outf, (off_t)0, 0); Tbl.str_version = htonl(Tbl.str_version); Tbl.str_numstr = htonl((uint32_t)(Num_pts - 1)); /* Look, Ma! After using the variable three times, let's store @@ -523,12 +528,12 @@ int main(int ac, char **av) Tbl.str_longlen = htonl(Tbl.str_longlen); Tbl.str_shortlen = htonl(Tbl.str_shortlen); Tbl.str_flags = htonl(Tbl.str_flags); - fwrite(&Tbl.str_version, sizeof Tbl.str_version, 1, outf); - fwrite(&Tbl.str_numstr, sizeof Tbl.str_numstr, 1, outf); - fwrite(&Tbl.str_longlen, sizeof Tbl.str_longlen, 1, outf); + fwrite(&Tbl.str_version, sizeof Tbl.str_version, 1, outf); + fwrite(&Tbl.str_numstr, sizeof Tbl.str_numstr, 1, outf); + fwrite(&Tbl.str_longlen, sizeof Tbl.str_longlen, 1, outf); fwrite(&Tbl.str_shortlen, sizeof Tbl.str_shortlen, 1, outf); - fwrite(&Tbl.str_flags, sizeof Tbl.str_flags, 1, outf); - fwrite( Tbl.stuff, sizeof Tbl.stuff, 1, outf); + fwrite(&Tbl.str_flags, sizeof Tbl.str_flags, 1, outf); + fwrite(Tbl.stuff, sizeof Tbl.stuff, 1, outf); if (STORING_PTRS) { for (p = Seekpts, cnt = (int)Num_pts; cnt--; ++p) diff --git a/fortune-mod/util/strfile.h b/fortune-mod/util/strfile.h index 26dda56..582e0ca 100644 --- a/fortune-mod/util/strfile.h +++ b/fortune-mod/util/strfile.h @@ -39,19 +39,20 @@ */ #include -#define STR_ENDSTRING(line,tbl) \ +#define STR_ENDSTRING(line, tbl) \ ((line)[0] == (tbl).str_delim && (line)[1] == '\n') -typedef struct { /* information table */ -#define STRFILE_VERSION 2 - uint32_t str_version; /* version number */ - uint32_t str_numstr; /* # of strings in the file */ - uint32_t str_longlen; /* length of longest string */ - uint32_t str_shortlen; /* length of shortest string */ -#define STR_RANDOM 0x1 /* randomized pointers */ -#define STR_ORDERED 0x2 /* ordered pointers */ -#define STR_ROTATED 0x4 /* rot-13'd text */ - uint32_t str_flags; /* bit field for flags */ - uint8_t stuff[4]; /* long aligned space */ -#define str_delim stuff[0] /* delimiting character */ +typedef struct +{ /* information table */ +#define STRFILE_VERSION 2 + uint32_t str_version; /* version number */ + uint32_t str_numstr; /* # of strings in the file */ + uint32_t str_longlen; /* length of longest string */ + uint32_t str_shortlen; /* length of shortest string */ +#define STR_RANDOM 0x1 /* randomized pointers */ +#define STR_ORDERED 0x2 /* ordered pointers */ +#define STR_ROTATED 0x4 /* rot-13'd text */ + uint32_t str_flags; /* bit field for flags */ + uint8_t stuff[4]; /* long aligned space */ +#define str_delim stuff[0] /* delimiting character */ } STRFILE; diff --git a/fortune-mod/util/unstr.c b/fortune-mod/util/unstr.c index 262566e..b710e1a 100644 --- a/fortune-mod/util/unstr.c +++ b/fortune-mod/util/unstr.c @@ -91,12 +91,12 @@ static char sccsid[] = "@(#)unstr.c 8.1 (Berkeley) 5/31/93"; #include "fortune-mod-common.h" -static char *Infile, /* name of input file */ - Datafile[MAXPATHLEN], /* name of data file */ - Delimch, /* delimiter character */ - Outfile[MAXPATHLEN]; +static char *Infile, /* name of input file */ + Datafile[MAXPATHLEN], /* name of data file */ + Delimch, /* delimiter character */ + Outfile[MAXPATHLEN]; -static char NewDelch = '\0'; /* a replacement delimiter character */ +static char NewDelch = '\0'; /* a replacement delimiter character */ static FILE *Inf, *Dataf, *Outf; @@ -108,19 +108,21 @@ static void getargs(int ac, char *av[]) while ((ch = getopt(ac, av, "c:")) != EOF) switch (ch) - { - case 'c': - NewDelch = *optarg; - if (!isascii(NewDelch)) - { - fprintf(stderr, "Bad delimiting characher: '\\%o'\n", (unsigned int)NewDelch); - } - break; - case '?': - default: - fprintf(stderr, "Usage:\n\tunstr [-c C] datafile[.ext] [outputfile]\n"); - exit(1); - } + { + case 'c': + NewDelch = *optarg; + if (!isascii(NewDelch)) + { + fprintf(stderr, "Bad delimiting characher: '\\%o'\n", + (unsigned int)NewDelch); + } + break; + case '?': + default: + fprintf( + stderr, "Usage:\n\tunstr [-c C] datafile[.ext] [outputfile]\n"); + exit(1); + } av += optind; @@ -153,7 +155,10 @@ static void getargs(int ac, char *av[]) } if (!strcmp(Infile, Outfile)) { - fprintf(stderr, "The input file for strings (%s) must be different from the output file (%s)\n", Infile, Outfile); + fprintf(stderr, + "The input file for strings (%s) must be different from the output " + "file (%s)\n", + Infile, Outfile); exit(1); } } @@ -168,7 +173,7 @@ static void order_unstr(register STRFILE *tbl) for (i = 0; i <= tbl->str_numstr; i++) { - if (!fread((char *) &pos, 1, sizeof pos, Dataf)) + if (!fread((char *)&pos, 1, sizeof pos, Dataf)) { exit(1); } @@ -194,7 +199,7 @@ static void order_unstr(register STRFILE *tbl) int main(int ac, char **av) { - static STRFILE tbl; /* description table */ + static STRFILE tbl; /* description table */ getargs(ac, av); if ((Inf = fopen(Infile, "r")) == NULL) @@ -214,13 +219,18 @@ int main(int ac, char **av) perror(Outfile); exit(1); } -#define err_fread(a, b, c, d) if (!fread(a, b, c, d)) { perror("fread"); exit(1); } - err_fread(&tbl.str_version, sizeof(tbl.str_version), 1, Dataf); - err_fread(&tbl.str_numstr, sizeof(tbl.str_numstr), 1, Dataf); - err_fread(&tbl.str_longlen, sizeof(tbl.str_longlen), 1, Dataf); +#define err_fread(a, b, c, d) \ + if (!fread(a, b, c, d)) \ + { \ + perror("fread"); \ + exit(1); \ + } + err_fread(&tbl.str_version, sizeof(tbl.str_version), 1, Dataf); + err_fread(&tbl.str_numstr, sizeof(tbl.str_numstr), 1, Dataf); + err_fread(&tbl.str_longlen, sizeof(tbl.str_longlen), 1, Dataf); err_fread(&tbl.str_shortlen, sizeof(tbl.str_shortlen), 1, Dataf); - err_fread(&tbl.str_flags, sizeof(tbl.str_flags), 1, Dataf); - err_fread( tbl.stuff, sizeof(tbl.stuff), 1, Dataf); + err_fread(&tbl.str_flags, sizeof(tbl.str_flags), 1, Dataf); + err_fread(tbl.stuff, sizeof(tbl.stuff), 1, Dataf); if (!(tbl.str_flags & (STR_ORDERED | STR_RANDOM)) && (!NewDelch)) { fprintf(stderr, "nothing to do -- table in file order\n"); -- 2.40.0