fortune(6) switch to 3-clause BSDL; style(9)

This reduces the diff to other *BSD and makes it possible to actually
see the code differences.

Approved by:	ed (Co-mentor)
This commit is contained in:
uqs 2010-02-15 15:10:21 +00:00
parent cea5b4c8de
commit 6dbf26d197
7 changed files with 236 additions and 259 deletions

View File

@ -12,11 +12,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.
.\"

View File

@ -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.
*
@ -48,41 +44,42 @@ static const char sccsid[] = "@(#)fortune.c 8.1 (Berkeley) 5/31/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
# 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 <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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
# define TRUE 1
# define FALSE 0
# define bool short
#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 1
#define FALSE 0
#define bool short
# 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;
@ -102,7 +99,7 @@ typedef struct fd {
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 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 */
@ -173,9 +170,9 @@ static regex_t Re_pat;
int
main(int argc, char *argv[])
{
#ifdef OK_TO_WRITE_DISK
#ifdef OK_TO_WRITE_DISK
int fd;
#endif /* OK_TO_WRITE_DISK */
#endif /* OK_TO_WRITE_DISK */
(void) setlocale(LC_ALL, "");
@ -193,33 +190,33 @@ main(int argc, char *argv[])
display(Fortfile);
#ifdef OK_TO_WRITE_DISK
#ifdef OK_TO_WRITE_DISK
if ((fd = creat(Fortfile->posfile, 0666)) < 0) {
perror(Fortfile->posfile);
exit(1);
}
#ifdef LOCK_EX
#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 */
#endif /* LOCK_EX */
write(fd, (char *) &Fortfile->pos, sizeof Fortfile->pos);
if (!Fortfile->was_pos_file)
(void) chmod(Fortfile->path, 0666);
#ifdef LOCK_EX
#ifdef LOCK_EX
(void) flock(fd, LOCK_UN);
#endif /* LOCK_EX */
#endif /* OK_TO_WRITE_DISK */
#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 */
return (0);
}
void
@ -230,7 +227,7 @@ display(FILEDESC *fp)
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)
@ -265,14 +262,15 @@ fortlen(void)
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))
nchar += strlen(line);
}
Fort_len = nchar;
return nchar;
return (nchar);
}
/*
@ -288,7 +286,7 @@ getargs(int argc, char *argv[])
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)
@ -297,11 +295,11 @@ getargs(int argc, char *argv[])
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;
@ -355,7 +353,7 @@ getargs(int argc, char *argv[])
error = regcomp(&Re_pat, pat, REG_BASIC);
if (error) {
fprintf(stderr, "regcomp(%s) fails\n", pat);
exit (1);
exit(1);
}
}
}
@ -377,7 +375,7 @@ form_file_list(char **files, int file_cnt)
pstr = Fortune_path_arr;
i = 0;
while (*pstr) {
i += add_file(NO_PROB, *pstr++, NULL,
i += add_file(NO_PROB, *pstr++, NULL,
&File_list, &File_tail, NULL);
}
Fortunes_only = FALSE;
@ -385,7 +383,7 @@ form_file_list(char **files, int file_cnt)
fprintf(stderr, "No fortunes found in %s.\n",
Fortune_path);
}
return i != 0;
return (i != 0);
} else {
pstr = Fortune_path_arr;
i = 0;
@ -397,7 +395,7 @@ form_file_list(char **files, int file_cnt)
fprintf(stderr, "No fortunes found in %s.\n",
Fortune_path);
}
return i != 0;
return (i != 0);
}
}
for (i = 0; i < file_cnt; i++) {
@ -410,11 +408,11 @@ form_file_list(char **files, int file_cnt)
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
@ -428,7 +426,7 @@ form_file_list(char **files, int file_cnt)
else if (*++sp == '\0') {
if (++i >= file_cnt) {
fprintf(stderr, "percentages must precede files\n");
return FALSE;
return (FALSE);
}
sp = files[i];
}
@ -437,20 +435,21 @@ form_file_list(char **files, int file_cnt)
pstr = Fortune_path_arr;
i = 0;
while (*pstr) {
i += add_file(NO_PROB, *pstr++, NULL,
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;
return (FALSE);
}
} else if (!add_file(percent, sp, NULL, &File_list,
} else if (!add_file(percent, sp, NULL, &File_list,
&File_tail, NULL)) {
return FALSE;
return (FALSE);
}
}
return TRUE;
return (TRUE);
}
/*
@ -482,7 +481,7 @@ add_file(int percent, const char *file, const char *dir, FILEDESC **head,
if ((isdir = is_dir(path)) && parent != NULL) {
if (was_malloc)
free(tpath);
return FALSE; /* don't recurse */
return (FALSE); /* don't recurse */
}
offensive = NULL;
if (!isdir && parent == NULL && (All_forts || Offend) &&
@ -501,7 +500,7 @@ add_file(int percent, const char *file, const char *dir, FILEDESC **head,
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
@ -525,14 +524,14 @@ add_file(int percent, const char *file, const char *dir, FILEDESC **head,
char **pstr = Fortune_path_arr;
while (*pstr) {
i += add_file(percent, file, *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;
return (i != 0);
}
/*
if (parent == NULL)
@ -540,7 +539,7 @@ add_file(int percent, const char *file, const char *dir, FILEDESC **head,
*/
if (was_malloc)
free(tpath);
return FALSE;
return (FALSE);
}
DPRINTF(2, (stderr, "path = \"%s\"\n", path));
@ -564,9 +563,9 @@ add_file(int percent, const char *file, const char *dir, FILEDESC **head,
free(tpath);
do_free(fp->datfile);
do_free(fp->posfile);
free((char *) fp);
free(fp);
do_free(offensive);
return FALSE;
return (FALSE);
}
/*
* If the user said -a, we need to make this node a pointer to
@ -588,11 +587,11 @@ add_file(int percent, const char *file, const char *dir, FILEDESC **head,
fp->next = *head;
*head = fp;
}
#ifdef OK_TO_WRITE_DISK
#ifdef OK_TO_WRITE_DISK
fp->was_pos_file = (access(fp->posfile, W_OK) >= 0);
#endif /* OK_TO_WRITE_DISK */
#endif /* OK_TO_WRITE_DISK */
return TRUE;
return (TRUE);
}
/*
@ -604,7 +603,7 @@ 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;
@ -617,7 +616,8 @@ new_fp(void)
fp->parent = NULL;
fp->datfile = NULL;
fp->posfile = NULL;
return fp;
return (fp);
}
/*
@ -630,7 +630,8 @@ off_name(const char *file)
char *new;
new = copy(file, (unsigned int) (strlen(file) + 2));
return strcat(new, "-o");
return (strcat(new, "-o"));
}
/*
@ -643,6 +644,7 @@ is_off_name(const char *file)
int len;
len = strlen(file);
return (len >= 3 && file[len - 2] == '-' && file[len - 1] == 'o');
}
@ -657,13 +659,13 @@ 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();
@ -690,9 +692,9 @@ all_forts(FILEDESC *fp, char *offensive)
obscene->datfile = datfile;
obscene->posfile = posfile;
obscene->read_tbl = FALSE;
#ifdef OK_TO_WRITE_DISK
#ifdef OK_TO_WRITE_DISK
obscene->was_pos_file = (access(obscene->posfile, W_OK) >= 0);
#endif /* OK_TO_WRITE_DISK */
#endif /* OK_TO_WRITE_DISK */
}
/*
@ -704,14 +706,14 @@ 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));
@ -728,9 +730,10 @@ add_dir(FILEDESC *fp)
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);
}
/*
@ -740,10 +743,11 @@ add_dir(FILEDESC *fp)
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);
}
@ -779,7 +783,7 @@ is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
i = strlen(file);
if (Offend ^ (file[i - 2] == '-' && file[i - 1] == 'o')) {
DPRINTF(2, (stderr, "FALSE (offending file)\n"));
return FALSE;
return (FALSE);
}
}
@ -789,18 +793,18 @@ is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
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) {
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);
}
}
@ -813,22 +817,23 @@ is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
DPRINTF(0, (stderr, "Warning: file \"%s\" unreadable\n", datfile));
#endif
free(datfile);
return FALSE;
return (FALSE);
}
if (datp != NULL)
*datp = datfile;
else
free(datfile);
if (posp != NULL) {
#ifdef OK_TO_WRITE_DISK
#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 */
#endif /* OK_TO_WRITE_DISK */
}
DPRINTF(2, (stderr, "TRUE\n"));
return TRUE;
return (TRUE);
}
/*
@ -838,14 +843,15 @@ is_fortfile(const char *file, char **datp, char **posp, int check_for_offend)
char *
copy(const char *str, unsigned int len)
{
char *new, *sp;
char *new, *sp;
new = do_malloc(len + 1);
sp = new;
do {
*sp++ = *str;
} while (*str++);
return new;
return (new);
}
/*
@ -855,13 +861,14 @@ copy(const char *str, unsigned int len)
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);
}
/*
@ -898,8 +905,7 @@ init_prob(void)
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));
@ -907,14 +913,12 @@ init_prob(void)
(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);
@ -935,7 +939,7 @@ init_prob(void)
DPRINTF(1, (stderr, ", residual = %d%%", percent));
}
else
DPRINTF(1, (stderr,
DPRINTF(1, (stderr,
", %d%% distributed over remaining fortunes\n",
percent));
}
@ -1003,8 +1007,8 @@ get_fort(void)
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]);
@ -1027,7 +1031,7 @@ pick_child(FILEDESC *parent)
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);
@ -1042,7 +1046,7 @@ pick_child(FILEDESC *parent)
}
DPRINTF(1, (stderr, " using %s, %u\n", fp->name,
fp->tbl.str_numstr));
return fp;
return (fp);
}
}
@ -1092,7 +1096,7 @@ open_fp(FILEDESC *fp)
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);
}
@ -1106,14 +1110,14 @@ open_dat(FILEDESC *fp)
void
get_pos(FILEDESC *fp)
{
#ifdef OK_TO_WRITE_DISK
#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 ||
#ifdef OK_TO_WRITE_DISK
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)
@ -1136,13 +1140,13 @@ get_pos(FILEDESC *fp)
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);
}
@ -1268,7 +1272,8 @@ conv_pat(char *orig)
*sp++ = *orig;
}
*sp = '\0';
return new;
return (new);
}
/*
@ -1285,8 +1290,8 @@ find_matches(void)
Found_one = FALSE;
matches_in_list(File_list);
return Found_one;
/* NOTREACHED */
return (Found_one);
}
/*
@ -1307,11 +1312,12 @@ maxlen_in_list(FILEDESC *list)
}
else {
get_tbl(fp);
if (fp->tbl.str_longlen > (unsigned)maxlen)
if (fp->tbl.str_longlen > (unsigned int)maxlen)
maxlen = fp->tbl.str_longlen;
}
}
return maxlen;
return (maxlen);
}
/*
@ -1323,8 +1329,8 @@ 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) {

View File

@ -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,6 +27,7 @@
* SUCH DAMAGE.
*
* @(#)pathnames.h 8.1 (Berkeley) 5/31/93
* $FreeBSD$
*/
#define FORTDIR "/usr/share/games/fortune"

View File

@ -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.
.\"

View File

@ -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.
*
@ -48,16 +44,18 @@ static const char sccsid[] = "@(#)strfile.c 8.1 (Berkeley) 5/31/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
# include <sys/param.h>
# include <sys/endian.h>
# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
# include <string.h>
# include <time.h>
# include <locale.h>
# include <unistd.h>
# include "strfile.h"
#include <sys/param.h>
#include <sys/endian.h>
#include <ctype.h>
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "strfile.h"
/*
* This program takes a file composed of strings separated by
@ -83,26 +81,19 @@ __FBSDID("$FreeBSD$");
* Added ordering options.
*/
# define TRUE 1
# define FALSE 0
#define STORING_PTRS (Oflag || Rflag)
#define CHUNKSIZE 512
# define STORING_PTRS (Oflag || Rflag)
# define CHUNKSIZE 512
# define ALLOC(ptr,sz) { \
#define ALLOC(ptr, sz) do { \
if (ptr == NULL) \
ptr = malloc(CHUNKSIZE * sizeof *ptr); \
ptr = malloc(CHUNKSIZE * sizeof(*ptr)); \
else if (((sz) + 1) % CHUNKSIZE == 0) \
ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof *ptr); \
ptr = realloc(ptr, ((sz) + CHUNKSIZE) * sizeof(*ptr)); \
if (ptr == NULL) { \
fprintf(stderr, "out of space\n"); \
exit(1); \
} \
}
#ifdef NO_VOID
# define void char
#endif
} while (0)
typedef struct {
int first;
@ -113,12 +104,12 @@ static char *Infile = NULL, /* input file name */
Outfile[MAXPATHLEN] = "", /* output file name */
Delimch = '%'; /* delimiting character */
static int Cflag = FALSE; /* embedded comments */
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 int Cflag = false; /* embedded comments */
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 uint32_t Num_pts = 0; /* number of pointers/strings */
static off_t *Seekpts;
@ -129,13 +120,13 @@ static STRFILE Tbl; /* statistics table */
static STR *Firstch; /* first chars of each string */
static void add_offset(FILE *, off_t);
static int cmp_str(const void *, const void *);
static int stable_collate_range_cmp(int, int);
static void do_order(void);
static void getargs(int, char **);
static void randomize(void);
static void usage(void);
static void add_offset(FILE *, off_t);
static int cmp_str(const void *, const void *);
static int stable_collate_range_cmp(int, int);
static void do_order(void);
static void getargs(int, char **);
static void randomize(void);
static void usage(void);
/*
* main:
@ -149,17 +140,16 @@ static void usage(void);
int
main(int ac, char *av[])
{
char *sp, dc;
FILE *inf, *outf;
off_t last_off, pos, *p;
size_t length;
int first;
uint32_t cnt;
char *nsp;
STR *fp;
static char string[257];
char *sp, *nsp, dc;
FILE *inf, *outf;
off_t last_off, pos, *p;
size_t length;
int first;
uint32_t cnt;
STR *fp;
static char string[257];
(void) setlocale(LC_ALL, "");
setlocale(LC_ALL, "");
getargs(ac, av); /* evalute arguments */
dc = Delimch;
@ -173,7 +163,7 @@ main(int ac, char *av[])
exit(1);
}
if (!STORING_PTRS)
(void) fseek(outf, (long) sizeof Tbl, 0);
fseek(outf, (long)sizeof(Tbl), SEEK_SET);
/*
* Write the strings onto the file
@ -212,7 +202,7 @@ main(int ac, char *av[])
else
fp->first = *nsp;
fp->pos = Seekpts[Num_pts - 1];
first = FALSE;
first = false;
}
} while (sp != NULL);
@ -220,7 +210,7 @@ main(int ac, char *av[])
* write the tables in
*/
(void) fclose(inf);
fclose(inf);
Tbl.str_numstr = Num_pts - 1;
if (Cflag)
@ -252,13 +242,13 @@ main(int ac, char *av[])
Tbl.str_longlen = htobe32(Tbl.str_longlen);
Tbl.str_shortlen = htobe32(Tbl.str_shortlen);
Tbl.str_flags = htobe32(Tbl.str_flags);
(void) fwrite((char *) &Tbl, sizeof Tbl, 1, outf);
fwrite((char *)&Tbl, sizeof(Tbl), 1, outf);
if (STORING_PTRS) {
for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
*p = htobe64(*p);
(void) fwrite(Seekpts, sizeof *Seekpts, (size_t) Num_pts, outf);
fwrite(Seekpts, sizeof(*Seekpts), (size_t)Num_pts, outf);
}
(void) fclose(outf);
fclose(outf);
exit(0);
}
@ -268,7 +258,7 @@ main(int ac, char *av[])
void
getargs(int argc, char **argv)
{
int ch;
int ch;
while ((ch = getopt(argc, argv, "Cc:iorsx")) != -1)
switch(ch) {
@ -306,22 +296,22 @@ getargs(int argc, char **argv)
if (*argv) {
Infile = *argv;
if (*++argv)
(void) strcpy(Outfile, *argv);
strcpy(Outfile, *argv);
}
if (!Infile) {
puts("No input file name");
usage();
}
if (*Outfile == '\0') {
(void) strcpy(Outfile, Infile);
(void) strcat(Outfile, ".dat");
strcpy(Outfile, Infile);
strcat(Outfile, ".dat");
}
}
void
usage(void)
{
(void) fprintf(stderr,
fprintf(stderr,
"strfile [-Ciorsx] [-c char] source_file [output_file]\n");
exit(1);
}
@ -337,7 +327,7 @@ add_offset(FILE *fp, off_t off)
if (!STORING_PTRS) {
beoff = htobe64(off);
fwrite(&beoff, 1, sizeof beoff, fp);
fwrite(&beoff, 1, sizeof(beoff), fp);
} else {
ALLOC(Seekpts, Num_pts + 1);
Seekpts[Num_pts] = off;
@ -353,19 +343,19 @@ void
do_order(void)
{
uint32_t i;
off_t *lp;
STR *fp;
off_t *lp;
STR *fp;
Sort_1 = fopen(Infile, "r");
Sort_2 = fopen(Infile, "r");
qsort(Firstch, (size_t) Tbl.str_numstr, sizeof *Firstch, cmp_str);
qsort(Firstch, (size_t)Tbl.str_numstr, sizeof(*Firstch), cmp_str);
i = Tbl.str_numstr;
lp = Seekpts;
fp = Firstch;
while (i--)
*lp++ = fp++->pos;
(void) fclose(Sort_1);
(void) fclose(Sort_2);
fclose(Sort_1);
fclose(Sort_2);
Tbl.str_flags |= STR_ORDERED;
}
@ -389,27 +379,25 @@ stable_collate_range_cmp(int c1, int c2)
int
cmp_str(const void *s1, const void *s2)
{
const STR *p1, *p2;
int c1, c2;
int n1, n2;
int r;
const STR *p1, *p2;
int c1, c2, n1, n2, r;
# define SET_N(nf,ch) (nf = (ch == '\n'))
# define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char) Delimch && nf))
#define SET_N(nf,ch) (nf = (ch == '\n'))
#define IS_END(ch,nf) (ch == EOF || (ch == (unsigned char)Delimch && nf))
p1 = (const STR *) s1;
p2 = (const STR *) s2;
c1 = (unsigned char) p1->first;
c2 = (unsigned char) p2->first;
p1 = (const STR *)s1;
p2 = (const STR *)s2;
c1 = (unsigned char)p1->first;
c2 = (unsigned char)p2->first;
if ((r = stable_collate_range_cmp(c1, c2)) != 0)
return (r);
(void) fseeko(Sort_1, p1->pos, 0);
(void) fseeko(Sort_2, p2->pos, 0);
fseeko(Sort_1, p1->pos, SEEK_SET);
fseeko(Sort_2, p2->pos, SEEK_SET);
n1 = FALSE;
n2 = FALSE;
n1 = false;
n2 = false;
while (!isalnum(c1 = getc(Sort_1)) && c1 != '\0' && c1 != EOF)
SET_N(n1, c1);
while (!isalnum(c2 = getc(Sort_2)) && c2 != '\0' && c2 != EOF)
@ -433,6 +421,7 @@ cmp_str(const void *s1, const void *s2)
c1 = 0;
if (IS_END(c2, n2))
c2 = 0;
return (stable_collate_range_cmp(c1, c2));
}
@ -446,8 +435,8 @@ void
randomize(void)
{
uint32_t cnt, i;
off_t tmp;
off_t *sp;
off_t tmp;
off_t *sp;
#if __FreeBSD_version < 800041
srandomdev();

View File

@ -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.
*

View File

@ -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.
*
@ -61,14 +57,15 @@ __FBSDID("$FreeBSD$");
* Ken Arnold Aug 13, 1978
*/
# include <sys/param.h>
# include <sys/endian.h>
# include <stdio.h>
# include <ctype.h>
# include <err.h>
# include <stdlib.h>
# include <string.h>
# include "strfile.h"
#include <sys/param.h>
#include <sys/endian.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "strfile.h"
char *Infile, /* name of input file */
Datafile[MAXPATHLEN], /* name of data file */
@ -80,22 +77,22 @@ void order_unstr(STRFILE *);
/* ARGSUSED */
int
main(int ac, char **av)
main(int argc, char *argv[])
{
static STRFILE tbl; /* description table */
static STRFILE tbl; /* description table */
if (ac != 2) {
(void)fprintf(stderr, "usage: unstr datafile\n");
if (argc != 2) {
fprintf(stderr, "usage: unstr datafile\n");
exit(1);
}
Infile = av[1];
(void) strcpy(Datafile, Infile);
(void) strcat(Datafile, ".dat");
Infile = argv[1];
strcpy(Datafile, Infile);
strcat(Datafile, ".dat");
if ((Inf = fopen(Infile, "r")) == NULL)
err(1, "%s", Infile);
if ((Dataf = fopen(Datafile, "r")) == NULL)
err(1, "%s", Datafile);
(void) fread((char *) &tbl, sizeof tbl, 1, Dataf);
fread((char *)&tbl, sizeof(tbl), 1, Dataf);
tbl.str_version = be32toh(tbl.str_version);
tbl.str_numstr = be32toh(tbl.str_numstr);
tbl.str_longlen = be32toh(tbl.str_longlen);
@ -105,8 +102,8 @@ main(int ac, char **av)
errx(1, "nothing to do -- table in file order");
Delimch = tbl.str_delim;
order_unstr(&tbl);
(void) fclose(Inf);
(void) fclose(Dataf);
fclose(Inf);
fclose(Dataf);
exit(0);
}
@ -114,17 +111,17 @@ void
order_unstr(STRFILE *tbl)
{
uint32_t i;
char *sp;
off_t pos;
char buf[BUFSIZ];
char *sp;
off_t pos;
char buf[BUFSIZ];
for (i = 0; i < tbl->str_numstr; i++) {
(void) fread(&pos, 1, sizeof pos, Dataf);
(void) fseeko(Inf, be64toh(pos), 0);
fread(&pos, 1, sizeof(pos), Dataf);
fseeko(Inf, be64toh(pos), SEEK_SET);
if (i != 0)
(void) printf("%c\n", Delimch);
printf("%c\n", Delimch);
for (;;) {
sp = fgets(buf, sizeof buf, Inf);
sp = fgets(buf, sizeof(buf), Inf);
if (sp == NULL || STR_ENDSTRING(sp, *tbl))
break;
else