source: trunk/minix/commands/advent/setup.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.4 KB
Line 
1/** program SETUP.C *
2 * execution will read the four adventure text files *
3 * files; "advent1.txt", "advent2.txt", "advent3.txt" & *
4 * "advent4.txt". it will create the file "advtext.h" *
5 * which is an Index Sequential Access Method (ISAM) *
6 * header to be #included into "advent.c" before the *
7 * header "advdec.h" is #included. */
8
9
10#include <stdio.h>
11#include <stdlib.h>
12#include "advent.h"
13
14_PROTOTYPE(int main, (void));
15_PROTOTYPE(void file_error, (char *));
16_PROTOTYPE(void encode, (unsigned char *));
17
18int main()
19{
20
21 FILE *isam, *src, *dest;
22 char itxt[255];
23 int cnt, i;
24 long llen;
25 char filename[12];
26 static char *headername[] = {
27 "idx1[MAXLOC]", "idx2[MAXLOC]", "idx3[MAXOBJ]", "idx4[MAXMSG]",
28 };
29
30 long x29 = (1L << 29), x30 = (1L << 30);
31 if (!(x30 / 2 == x29 && 0L < x30 && x29 < x30)) {
32 fprintf(stderr, "Sorry, advent needs 32-bit `long int's.\n");
33 exit(EXIT_FAILURE);
34 }
35 isam = fopen("advtext.h", "w");
36 if (!isam) {
37 fprintf(stderr, "Sorry, I can't open advtext.h...\n");
38 exit(EXIT_FAILURE);
39 }
40 fprintf(isam, "\n/*\theader: ADVTEXT.H\t\t\t\t\t*/\n\n\n");
41
42 for (i = 1; i <= 4; i++) {
43 cnt = -1;
44 llen = 0L;
45 sprintf(filename, "advent%d.txt", i);
46 src = fopen(filename, "r");
47 if (!src)
48 file_error(filename);
49 sprintf(filename, "advent%d.dat", i);
50 dest = fopen(filename, "w");
51 if (!dest)
52 file_error(filename);
53 fprintf(isam, "long\t%s = {\n\t", headername[i - 1]);
54 while (fgets(itxt, 255, src)) {
55 encode((unsigned char *) itxt);
56 if (fprintf(dest, "%s\n", itxt) == EOF)
57 file_error(filename);
58 if (itxt[0] == '#') {
59 if (llen)
60 fprintf(isam, "%ld,%s\t", llen,
61 &"\0\0\0\0\0\0\0\n"[++cnt & 7]);
62 llen = ftell(dest);
63 if (llen <= 0) {
64 fprintf(stderr, "ftell err in %s\n", filename);
65 exit(EXIT_FAILURE);
66 } /* if (!llen) */
67 } /* if (itxt[0]) */
68 } /* while fgets */
69 if (fprintf(isam, "%ld\n\t};\n\n", llen) == EOF)
70 file_error("advtext.h");
71 fclose(src);
72 if (fclose(dest) == EOF)
73 file_error(filename);
74 }
75
76 if (fclose(isam) == EOF)
77 file_error("advtext.h");
78 return EXIT_SUCCESS;
79} /* main */
80
81void file_error(filename)
82char *filename;
83{
84 perror(filename);
85 exit(EXIT_FAILURE);
86}
87
88_CONST unsigned char key[4] = {'c' | 0x80, 'L' | 0x80, 'y' | 0x80, 'D' | 0x80};
89
90void encode(msg)
91unsigned char *msg;
92{
93 register int i;
94
95 for (i = 1; msg[i]; i++)
96 msg[i] ^= key[i & 3];
97 msg[--i] = '\0';
98 return;
99}
Note: See TracBrowser for help on using the repository browser.