]>
code.delx.au - gnu-emacs/blob - lib-src/yow.c
4 * Print a quotation from Zippy the Pinhead.
5 * Qux <Kaufman-David@Yale> March 6, 1986
7 * With dynamic memory allocation.
12 #include <../src/paths.h> /* For PATH_DATA. */
18 #define YOW_FILE "yow.lines"
22 #define rootrelativepath(rel) \
24 static char res[BUFSIZE], *p;\
25 strcpy (res, argv[0]);\
26 p = res + strlen (res);\
27 while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
28 strcpy (p + 1, "../");\
39 void yow(), setup_yow();
41 if (argc
> 2 && !strcmp (argv
[1], "-f"))
42 strcpy (file
, argv
[2]);
45 sprintf (file
, "%s%s", PATH_DATA
, YOW_FILE
);
47 sprintf (file
, "%s/%s", PATH_DATA
, YOW_FILE
);
50 if ((fp
= fopen(file
, "r")) == NULL
) {
55 /* initialize random seed */
56 srand((int) (getpid() + time((long *) 0)));
65 static long header_len
;
67 #define AVG_LEN 40 /* average length of a quotation */
69 /* Sets len and header_len */
76 /* Get length of file */
77 /* Because the header (stuff before the first SEP) can be very long,
78 * thus biasing our search in favor of the first quotation in the file,
79 * we explicitly skip that. */
80 while ((c
= getc(fp
)) != SEP
) {
82 fprintf(stderr
, "File contains no separators.\n");
86 header_len
= ftell(fp
);
87 if (header_len
> AVG_LEN
)
88 header_len
-= AVG_LEN
; /* allow the first quotation to appear */
90 if (fseek(fp
, 0L, 2) == -1) {
94 len
= ftell(fp
) - header_len
;
98 /* go to a random place in the file and print the quotation there */
106 unsigned int bufsize
;
107 char *malloc(), *realloc();
109 offset
= rand() % len
+ header_len
;
110 if (fseek(fp
, offset
, 0) == -1) {
115 /* Read until SEP, read next line, print it.
116 (Note that we will never print anything before the first separator.)
117 If we hit EOF looking for the first SEP, just recurse. */
118 while ((c
= getc(fp
)) != SEP
)
124 /* Skip leading whitespace, then read in a quotation.
125 If we hit EOF before we find a non-whitespace char, recurse. */
126 while (isspace(c
= getc(fp
)))
134 buf
= malloc(bufsize
);
135 if (buf
== (char *)0) {
136 fprintf(stderr
, "can't allocate any memory\n");
141 while ((c
= getc(fp
)) != SEP
&& c
!= EOF
) {
144 if (i
== bufsize
-1) {
145 /* Yow! Is this quotation too long yet? */
147 buf
= realloc(buf
, bufsize
);
148 if (buf
== (char *)0) {
149 fprintf(stderr
, "can't allocate more memory\n");