-
Notifications
You must be signed in to change notification settings - Fork 2
/
fortune.c
118 lines (97 loc) · 1.83 KB
/
fortune.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* fortune.c */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined NIX
#include <fcntl.h>
#endif
#include "includes/larncons.h"
#include "includes/larndata.h"
#include "includes/larnfunc.h"
#include "includes/fortune.h"
#include "includes/global.h"
#include "includes/io.h"
static void msdosfortune (void);
static int fortsize = 0;
void
outfortune (void)
{
lprcat ("\nThe cookie was delicious.");
if (cdesc[BLINDCOUNT])
{
return;
}
msdosfortune ();
}
/*
* Rumors has been entirely rewritten to be disk based. This is marginally
* slower, but requires no mallocked memory. Notice this in only valid for
* files smaller than 32K.
*/
static void
msdosfortune (void)
{
size_t status, i;
FILE *fp;
char buf[BUFSIZ], ch;
/* We couldn't open fortunes */
if (fortsize < 0)
{
return;
}
#ifdef WINDOWS_VS
fopen_s(&fp, fortfile, "r");
#else
fp = fopen (fortfile, "r");
#endif
if (fp == 0)
{
/* Don't try opening it again */
fortsize = -1;
return;
}
if (fortsize == 0)
{
fseek (fp, 0L, SEEK_END);
fortsize = ftell (fp);
}
if (fseek (fp, (long) rund (fortsize), SEEK_SET) < 0)
{
return;
}
/*
* Skip to next newline or EOF
*/
do
{
status = fread (&ch, sizeof (char), 1, fp);
}
while (status != EOF && ch != '\n');
if (status == EOF)
{
/* back to the beginning */
if (fseek (fp, 0L, SEEK_SET) < 0)
{
return;
}
}
/*
* Read in the line.
*/
for (i = 0; i < BUFSIZ - 1; i++)
{
if (fread (&buf[i], sizeof (char), 1, fp) != 1 || buf[i] == '\n')
{
break;
}
}
buf[i] = '\0';
/*
* And spit it out
*/
lprcat (" Inside you find a scrap of paper that says:\n");
lprcat (buf);
fclose (fp);
}