-
Notifications
You must be signed in to change notification settings - Fork 3
/
fortune.c
113 lines (72 loc) · 1.53 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
/* fortune.c */
# include <stdio.h>
# include <sys/types.h>
# include <sys/stat.h>
#
#include <fcntl.h>
#include "larncons.h"
#include "larndata.h"
#include "larnfunc.h"
static void msdosfortune(void);
static int fortsize = 0;
void outfortune(void)
{
lprcat("\nThe cookie was delicious.");
if (c[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)
{
int fd, status, i;
char buf[BUFSIZ], ch;
/* We couldn't open fortunes */
if (fortsize < 0) {
return;
}
fd = open(fortfile, O_RDONLY | O_BINARY);
if (fd < 0) {
/* Don't try opening it again */
fortsize = -1;
return;
}
if (fortsize == 0) {
fortsize = (int)lseek(fd, 0L, 2);
}
if (lseek(fd, (long)rund(fortsize), 0) < 0) {
return;
}
/*
* Skip to next newline or EOF
*/
do {
status = read(fd, &ch, 1);
} while (status != EOF && ch != '\n');
if (status == EOF) {
/* back to the beginning */
if (lseek(fd, 0L, 0) < 0) {
return;
}
}
/*
* Read in the line. Search for CR ('\r'), not NL
*/
for (i = 0; i < BUFSIZ - 1; i++) {
if (read(fd, &buf[i], 1) == EOF || buf[i] == '\r') {
break;
}
}
buf[i] = '\0';
/*
* And spit it out
*/
lprcat(" Inside you find a scrap of paper that says:\n");
lprcat(buf);
close(fd);
}