2 * The following code has been derived chiefly from the BSD distributions
3 * of the utility program unstr and the random quote displayer fortune.
4 * The utility produced by this code shares characteristics of both
5 * (it might be regarded as a minimalist implementation of fortune, for
6 * large values of 'minimal'), and is here offered so as to have the
7 * minimal necessary tools for rabbiting a strfile routine into some other,
8 * more significant program, all in one place. A programmer who cares and
9 * has the proper training could probably clean this up significantly; it's
10 * all stolen code (first rule of programming: steal) hacked together to
11 * fit. Or, to paraphrase the old saw about how the British built ships,
12 * it's coded by the mile and cut off to order. In that analogy, this
13 * program's about an inch--and separated with an axe.
15 * Axe murderess programming. Wotta concept!
17 * Use at your own peril, especially as a pattern (kludge, kludge!). This
18 * program, at least, shouldn't have any real chance of corrupting data,
19 * though; it opens files ro and dumps to the screen. If you redirect
20 * output, you definitely do so at your own peril (I lost six hours of
21 * editing on a fortune file that way, by redirecting the output of unstr
22 * before it had an outputfile option, trying to skip over the mv x.sorted
23 * x step. Axe murderess redirection, in that case).
25 * Blame Amy A. Lewis. September, 1995. alewis@email.unc.edu
29 * Copyright (c) 1991, 1993
30 * The Regents of the University of California. All rights reserved.
32 * This code is derived from software contributed to Berkeley by
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 /* randstr repeats the minimum functionality of the fortune program. It
65 * finds the fortune text or data file specified on the command line --
66 * one or the other, not both -- generates a random number, and displays
67 * the text string indexed. No provision is made for any other command
68 * line switches. At all.
72 * randstr filename[.ext]
74 * Example: run sed or Perl over your /etc/passwd, and kick out a
75 * strfile-format file containing lognames on the first line and full
76 * names on the second. Write a script called 'lottery' which is
77 * called once a month from crontab; it in turn calls randstr lusers,
78 * and the winning luser gets a prize notification sent by email from
79 * the lottery script. Living up to promises is optional.
81 * Note: if you're a sysadmin who regularly reads _Mein Kampf_ for the
82 * deep truths buried in it, and believe in Truth, Justice, and the
83 * American Family, you could use this to replace fortune, by pointing
84 * it at a small, Family Values database. The great advantage to this,
85 * in my opinion, is that it wouldn't take up any disk space at all.
86 * Who're you gonna quote? Dan Quayle?
89 #include "fortune-mod-common.h"
91 static char *input_filename, data_filename[MAXPATHLEN];
93 static FILE *Inf, *Dataf, *Outf;
95 static off_t pos, Seekpts[2]; /* seek pointers to fortunes */
97 #include "fortune-util.h"
99 static void getargs(char *av[])
105 input_filename = *av;
106 input_fn_2_data_fn();
110 * Don't write out errors here, either; trust in exit codes and sh
111 * fprintf(stderr, "No input file name\n");
112 * fprintf(stderr, "Usage:\n\tunstr [-c C] datafile[.ext]
121 * Get the position from the pos file, if there is one. If not,
122 * return a random number.
124 static void get_pos(STRFILE *fp)
126 pos = random() % fp->str_numstr;
127 if (++(pos) >= fp->str_numstr)
128 pos -= fp->str_numstr;
133 * Get the fortune data file's seek pointer for the next fortune.
135 static void get_fort(STRFILE fp)
138 fseek(Dataf, (long)(sizeof fp + pos * sizeof Seekpts[0]), SEEK_SET);
139 if (!fread(Seekpts, sizeof Seekpts, 1, Dataf))
143 Seekpts[0] = ntohl(Seekpts[0]);
144 Seekpts[1] = ntohl(Seekpts[1]);
147 static void display(FILE *fp, STRFILE table)
153 fseek(fp, (long)Seekpts[0], SEEK_SET);
154 for (i = 0; fgets(line, sizeof line, fp) && !STR_ENDSTRING(line, table);
157 if (table.str_flags & STR_ROTATED)
158 for (p = line; (ch = *p); ++p)
161 *p = 'A' + (ch - 'A' + 13) % 26;
162 else if (islower(ch))
163 *p = 'a' + (ch - 'a' + 13) % 26;
170 int main(int ac GCC_UNUSED, char **av)
172 static STRFILE tbl; /* description table */
175 if (!(Inf = fopen(input_filename, "r")))
177 perror(input_filename);
180 if (!(Dataf = fopen(data_filename, "r")))
182 perror(data_filename);
185 if (!fread((char *)&tbl, sizeof tbl, 1, Dataf))
189 tbl.str_version = ntohl(tbl.str_version);
190 tbl.str_numstr = ntohl(tbl.str_numstr);
191 tbl.str_longlen = ntohl(tbl.str_longlen);
192 tbl.str_shortlen = ntohl(tbl.str_shortlen);
193 tbl.str_flags = ntohl(tbl.str_flags);
195 srandom((int)(time((time_t *)NULL) + getpid()));