Mail Archives: djgpp/2010/01/03/16:43:19
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#define ICHING_PATH "/usr/local/lib/ching.txt"
void cast(char *hexagram)
{
int sum, c, i;
sum = 0;
while ((c = getchar()) != EOF) sum += c;
srandom(sum + getuid() + getgid() + getpid() + time(0));
for (i = 0; i < 6; i++) {
sum = 2 + ((random() >> 15) & 1);
sum += 2 + ((random() >> 15) & 1);
sum += 2 + ((random() >> 15) & 1);
hexagram[i] = '0' + sum;
}
}
int changing(char *hexagram)
{
int i;
for (i = 0; i < 6; i++) if (hexagram[i] == '6' || hexagram[i] == '9') return 1;
return 0;
}
void change(char *hexagram)
{
int i;
for (i = 0; i < 6; i++) {
if (hexagram[i] == '6') hexagram[i] = '7';
if (hexagram[i] == '9') hexagram[i] = '8';
}
}
int number(char *hexagram)
{
int i, h = 0;
static char hnum[64] = {
2, 24, 7, 19, 15, 36, 46, 11,
16, 51, 40, 54, 62, 55, 32, 34,
8, 3, 29, 60, 39, 63, 48, 5,
45, 17, 47, 58, 31, 49, 28, 43,
23, 27, 4, 41, 52, 22, 18, 26,
35, 21, 64, 38, 56, 30, 50, 14,
20, 42, 59, 61, 53, 37, 57, 9,
12, 25, 6, 10, 33, 13, 44, 1
};
for (i = 0; i < 6; i++) if (hexagram[i] == '7' || hexagram[i] == '9') h += 1 << i;
return hnum[h];
}
void print(char *hexagram)
{
FILE *text;
char line[80], top[8];
int i = -3, echo = 0;
if ((text = fopen(ICHING_PATH, "r")) == 0) {
perror(ICHING_PATH);
exit(1);
}
sprintf(top, "%d. ", number(hexagram));
fputs("\n", stdout);
while (fgets(line, sizeof(line), text) != 0) {
if (*line == '\f') *line = '\n';
if (i == -3) {
if (strstr(line, top) != 0 && strstr(line, " / ") != 0) echo = 1, i++;
}
else if (i == -2) {
if (strstr(line, "The Lines") != 0)
if (!changing(hexagram)) break;
else i++;
}
else {
if (*line == '\n') i++;
if (i < 6) echo = (hexagram[i] == '6' || hexagram[i] == '9');
else if (memcmp(hexagram, "666666", 6) && memcmp(hexagram, "999999", 6) || i > 6) break;
}
if (echo) fputs(line, stdout);
}
fclose(text);
}
int main(int argc, char *argv[])
{
int i;
char hexagram[6];
if (argc == 1) {
cast(hexagram);
i = 6;
}
else for (i = 0; i < 6 && argv[1][i] >= '6' && argv[1][i] <= '9'; i++) hexagram[i] = argv[1][i];
if (argc > 2 || i < 6) {
fprintf(stderr, "usage: %s [hexagram]\n", argv[0]);
exit(1);
}
print(hexagram);
if (changing(hexagram)) {
fputs("\n", stdout);
change(hexagram);
print(hexagram);
}
return 0;
} This program compiles correctly with gcc. gcc ching.c -o ching <cr>.
The only output this program generates is the useage message. I have
ching.txt located in /usr/local/lib/ching.txt. What has me puzzled is why
no other output is available. Is this a program that needs a different
compiler? I can build it with Power C if need be but if such a build
produces more output, what is the difference that makes gcc work in this
way?
- Raw text -