Fixes for /usr/xbin binaries bootstrap dir.
[minix3.git] / commands / advent / database.c
blobd4c55d1b99b05b0091901a6c88d362c25f73269c
1 /* program DATABASE.C */
3 #include <string.h>
4 #include <stdio.h>
5 #include "advent.h"
6 #include "advdec.h"
7 #include "advtext.h"
9 static char oline[256];
11 _PROTOTYPE(void rdupto, (FILE *, int, int, char *));
12 _PROTOTYPE(void rdskip, (FILE *, int, int));
15 Function to scan a file up to a specified
16 point and either print or return a string.
18 void rdupto(fdi, uptoc, print, string)
19 FILE *fdi;
20 int uptoc, print;
21 char *string;
23 int c, i;
24 static _CONST unsigned char key[4] = {'c' | 0x80, 'L' | 0x80,
25 'y' | 0x80, 'D' | 0x80};
27 i = 1;
28 while ((c = getc(fdi)) != uptoc && c != EOF) {
29 if (c == '\n')
30 i = 1;
31 if (c >= 0x80)
32 c ^= key[i++ & 3];
33 if (c == '\r')
34 continue;
35 if (print)
36 putchar(c);
37 else
38 *string++ = (char) c;
40 if (!print)
41 *string = '\0';
42 return;
46 Function to read a file skipping
47 a given character a specified number
48 of times, with or without repositioning
49 the file.
51 void rdskip(fdi, skipc, n)
52 FILE *fdi;
53 int skipc, n;
55 int c;
57 while (n--)
58 while ((c = getc(fdi)) != skipc)
59 if (c == EOF)
60 bug(32);
61 return;
65 Routine to request a yes or no answer to a question.
67 boolean yes(msg1, msg2, msg3)
68 int msg1, msg2, msg3;
70 char answer[INPUTBUFLEN];
72 if (msg1)
73 rspeak(msg1);
74 do {
75 switch (*ask("\n> ", answer, sizeof(answer))) {
76 case 'n':
77 case 'N':
78 if (msg3)
79 rspeak(msg3);
80 return (FALSE);
81 case 'y':
82 case 'Y':
83 if (msg2)
84 rspeak(msg2);
85 return (TRUE);
86 default:
87 fputs("Please answer Y (yes) or N (no).", stdout);
89 } while (TRUE);
93 Print a location description from "advent4.txt"
95 void rspeak(msg)
96 int msg;
98 if (msg == 54)
99 printf("ok.\n");
100 else {
101 fseek(fd4, idx4[msg - 1], 0);
102 rdupto(fd4, '#', 1, 0);
104 return;
108 Print an item message for a given state from "advent3.txt"
110 void pspeak(item, state)
111 int item, state;
113 fseek(fd3, idx3[item - 1], 0);
114 rdskip(fd3, '/', state + 2);
115 rdupto(fd3, '/', FALSE, oline);
116 if (strncmp(oline, "<$$<", 4) != 0)
117 printf("%s", oline);
118 return;
122 Print a long location description from "advent1.txt"
124 void desclg(loc)
125 int loc;
127 fseek(fd1, idx1[loc - 1], 0);
128 rdupto(fd1, '#', 1, 0);
129 return;
133 Print a short location description from "advent2.txt"
135 void descsh(loc)
136 int loc;
138 fseek(fd2, idx2[loc - 1], 0);
139 rdupto(fd2, '#', 1, 0);
140 return;