]> granicus.if.org Git - fortune-mod/blob - fortune-mod/util/randstr.c
prepare for a release
[fortune-mod] / fortune-mod / util / randstr.c
1 /*
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.
14  *
15  * Axe murderess programming.  Wotta concept!
16  *
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).
24  *
25  * Blame Amy A. Lewis.  September, 1995.  alewis@email.unc.edu
26  */
27
28 /*-
29  * Copyright (c) 1991, 1993
30  *      The Regents of the University of California.  All rights reserved.
31  *
32  * This code is derived from software contributed to Berkeley by
33  * Ken Arnold.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
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.
50  *
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
61  * SUCH DAMAGE.
62  */
63
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.
69  *
70  * Usage:
71  *
72  * randstr filename[.ext]
73  *
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.
80  *
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?
87  */
88
89 #include "fortune-mod-common.h"
90
91 static char *input_filename, data_filename[MAXPATHLEN];
92
93 static FILE *Inf, *Dataf;
94
95 static off_t pos, Seekpts[2]; /* seek pointers to fortunes */
96
97 #include "fortune-util.h"
98
99 static void getargs(char *argv[])
100 {
101     argv += optind + 1;
102
103     if (*argv)
104     {
105         input_filename = *argv;
106         input_fn_2_data_fn();
107     }
108     else
109     {
110         /*    {
111          * Don't write out errors here, either; trust in exit codes and sh
112          * fprintf(stderr, "No input file name\n");
113          * fprintf(stderr, "Usage:\n\tunstr [-c C] datafile[.ext]
114          * [outputfile]\n");
115          */
116         exit(1);
117         /*    } */
118     }
119 }
120
121 /*
122  * get_pos:
123  *      Get the position from the pos file, if there is one.  If not,
124  *      return a random number.
125  */
126 static void get_pos(STRFILE *fp)
127 {
128     pos = random() % fp->str_numstr;
129     if (++(pos) >= (off_t)(fp->str_numstr))
130     {
131         pos -= fp->str_numstr;
132     }
133 }
134
135 /*
136  * get_fort:
137  *      Get the fortune data file's seek pointer for the next fortune.
138  */
139 static void get_fort(STRFILE fp)
140 {
141     get_pos(&fp);
142     fseek(Dataf, (long)((long)sizeof(fp) + pos * (long)sizeof(Seekpts[0])),
143         SEEK_SET);
144     if (!fread(Seekpts, sizeof Seekpts, 1, Dataf))
145     {
146         exit(1);
147     }
148     Seekpts[0] = ntohl((uint32_t)Seekpts[0]);
149     Seekpts[1] = ntohl((uint32_t)Seekpts[1]);
150 }
151
152 static void display(FILE *fp, STRFILE table)
153 {
154     char *p, ch;
155     char line[BUFSIZ];
156     int i;
157
158     fseek(fp, (long)Seekpts[0], SEEK_SET);
159     for (i = 0; fgets(line, sizeof line, fp) && !STR_ENDSTRING(line, table);
160          i++)
161     {
162         if (table.str_flags & STR_ROTATED)
163         {
164             for (p = line; (ch = *p); ++p)
165             {
166                 if (isupper(ch))
167                 {
168                     *p = 'A' + (ch - 'A' + 13) % 26;
169                 }
170                 else if (islower(ch))
171                 {
172                     *p = 'a' + (ch - 'a' + 13) % 26;
173                 }
174             }
175         }
176         fputs(line, stdout);
177     }
178     fflush(stdout);
179 }
180
181 int main(int argc GCC_UNUSED, char **argv)
182 {
183     static STRFILE tbl; /* description table */
184
185     getargs(argv);
186     if (!(Inf = fopen(input_filename, "r")))
187     {
188         perror(input_filename);
189         exit(1);
190     }
191     if (!(Dataf = fopen(data_filename, "r")))
192     {
193         perror(data_filename);
194         exit(1);
195     }
196     if (!fread((char *)&tbl, sizeof tbl, 1, Dataf))
197     {
198         exit(1);
199     }
200     tbl.str_version = ntohl(tbl.str_version);
201     tbl.str_numstr = ntohl(tbl.str_numstr);
202     tbl.str_longlen = ntohl(tbl.str_longlen);
203     tbl.str_shortlen = ntohl(tbl.str_shortlen);
204     tbl.str_flags = ntohl(tbl.str_flags);
205
206     call_srandom();
207     get_fort(tbl);
208     display(Inf, tbl);
209
210     fclose(Inf);
211     fclose(Dataf);
212
213     return 0;
214 }