]> granicus.if.org Git - fortune-mod/blob - fortune-mod/util/randstr.c
appveyor fix #1: better windows/etc. compat
[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, *Outf;
94
95 static off_t pos, Seekpts[2]; /* seek pointers to fortunes */
96
97 #include "fortune-util.h"
98
99 static void getargs(char *av[])
100 {
101     av += optind + 1;
102
103     if (*av)
104     {
105         input_filename = *av;
106         input_fn_2_data_fn();
107     }
108     else
109         /*    {
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]
113          * [outputfile]\n");
114          */
115         exit(1);
116     /*    } */
117 }
118
119 /*
120  * get_pos:
121  *      Get the position from the pos file, if there is one.  If not,
122  *      return a random number.
123  */
124 static void get_pos(STRFILE *fp)
125 {
126     pos = random() % fp->str_numstr;
127     if (++(pos) >= fp->str_numstr)
128         pos -= fp->str_numstr;
129 }
130
131 /*
132  * get_fort:
133  *      Get the fortune data file's seek pointer for the next fortune.
134  */
135 static void get_fort(STRFILE fp)
136 {
137     get_pos(&fp);
138     fseek(Dataf, (long)(sizeof fp + pos * sizeof Seekpts[0]), SEEK_SET);
139     if (!fread(Seekpts, sizeof Seekpts, 1, Dataf))
140     {
141         exit(1);
142     }
143     Seekpts[0] = ntohl(Seekpts[0]);
144     Seekpts[1] = ntohl(Seekpts[1]);
145 }
146
147 static void display(FILE *fp, STRFILE table)
148 {
149     char *p, ch;
150     char line[BUFSIZ];
151     int i;
152
153     fseek(fp, (long)Seekpts[0], SEEK_SET);
154     for (i = 0; fgets(line, sizeof line, fp) && !STR_ENDSTRING(line, table);
155          i++)
156     {
157         if (table.str_flags & STR_ROTATED)
158             for (p = line; (ch = *p); ++p)
159             {
160                 if (isupper(ch))
161                     *p = 'A' + (ch - 'A' + 13) % 26;
162                 else if (islower(ch))
163                     *p = 'a' + (ch - 'a' + 13) % 26;
164             }
165         fputs(line, stdout);
166     }
167     fflush(stdout);
168 }
169
170 int main(int ac GCC_UNUSED, char **av)
171 {
172     static STRFILE tbl; /* description table */
173
174     getargs(av);
175     if (!(Inf = fopen(input_filename, "r")))
176     {
177         perror(input_filename);
178         exit(1);
179     }
180     if (!(Dataf = fopen(data_filename, "r")))
181     {
182         perror(data_filename);
183         exit(1);
184     }
185     if (!fread((char *)&tbl, sizeof tbl, 1, Dataf))
186     {
187         exit(1);
188     }
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);
194
195     srandom((int)(time((time_t *)NULL) + getpid()));
196     get_fort(tbl);
197     display(Inf, tbl);
198
199     exit(0);
200
201     fclose(Inf);
202     fclose(Dataf);
203     fclose(Outf);
204     exit(0);
205 }