castching.c revision 1.1
11.1Sperry/* $NetBSD: castching.c,v 1.1 2005/06/30 13:30:33 perry Exp $ */ 21.1Sperry 31.1Sperry/* 41.1Sperry * Copyright (c) 1988, 1993 51.1Sperry * The Regents of the University of California. All rights reserved. 61.1Sperry * 71.1Sperry * This code is derived from software contributed to Berkeley by 81.1Sperry * Guy Harris. 91.1Sperry * 101.1Sperry * Redistribution and use in source and binary forms, with or without 111.1Sperry * modification, are permitted provided that the following conditions 121.1Sperry * are met: 131.1Sperry * 1. Redistributions of source code must retain the above copyright 141.1Sperry * notice, this list of conditions and the following disclaimer. 151.1Sperry * 2. Redistributions in binary form must reproduce the above copyright 161.1Sperry * notice, this list of conditions and the following disclaimer in the 171.1Sperry * documentation and/or other materials provided with the distribution. 181.1Sperry * 3. All advertising materials mentioning features or use of this software 191.1Sperry * must display the following acknowledgement: 201.1Sperry * This product includes software developed by the University of 211.1Sperry * California, Berkeley and its contributors. 221.1Sperry * 4. Neither the name of the University nor the names of its contributors 231.1Sperry * may be used to endorse or promote products derived from this software 241.1Sperry * without specific prior written permission. 251.1Sperry * 261.1Sperry * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271.1Sperry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281.1Sperry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291.1Sperry * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301.1Sperry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311.1Sperry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321.1Sperry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 331.1Sperry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 341.1Sperry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351.1Sperry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361.1Sperry * SUCH DAMAGE. 371.1Sperry */ 381.1Sperry 391.1Sperry#include <sys/cdefs.h> 401.1Sperry#ifndef lint 411.1Sperry__COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\ 421.1Sperry The Regents of the University of California. All rights reserved.\n"); 431.1Sperry#endif /* not lint */ 441.1Sperry 451.1Sperry#ifndef lint 461.1Sperry#if 0 471.1Sperrystatic char sccsid[] = "@(#)ching.cno.c 8.1 (Berkeley) 5/31/93"; 481.1Sperry#else 491.1Sperry__RCSID("$NetBSD: castching.c,v 1.1 2005/06/30 13:30:33 perry Exp $"); 501.1Sperry#endif 511.1Sperry#endif /* not lint */ 521.1Sperry 531.1Sperry/* 541.1Sperry * castching - Read a question, cast a change, and output the line 551.1Sperry * values to the standard output for processing by "printching". 561.1Sperry */ 571.1Sperry#include <stdio.h> 581.1Sperry#include <stdlib.h> 591.1Sperry#include <time.h> 601.1Sperry#include <unistd.h> 611.1Sperry#include "ching.h" 621.1Sperry 631.1Sperrytime_t now; /* current time */ 641.1Sperry 651.1Sperryunsigned seed; /* seed for random number generator */ 661.1Sperryint getquest(void); 671.1Sperryunsigned getrand(void); 681.1Sperryunsigned getrnum(void); 691.1Sperrychar *change(void); 701.1Sperry 711.1Sperrychar string[6+1]; /* where the actual change string is put */ 721.1Sperry 731.1Sperryint table[2][2][2] = { 741.1Sperry { { OYIN, YYANG,}, { YYANG, YYIN,} }, 751.1Sperry { { YYANG, YYIN,}, { YYIN, OYANG,} }, 761.1Sperry}; 771.1Sperry 781.1Sperry/*ARGSUSED*/ 791.1Sperryint 801.1Sperrymain(int argc, char **argv) 811.1Sperry{ 821.1Sperry time(&now); 831.1Sperry /* randomize */ 841.1Sperry seed = (int)now + getquest() + getgid() + getuid() + getpid(); 851.1Sperry printf("%s\n", change()); 861.1Sperry exit(0); 871.1Sperry} 881.1Sperry 891.1Sperry/* 901.1Sperry * Hash the question by adding all the characters together. 911.1Sperry */ 921.1Sperryint 931.1Sperrygetquest(void) 941.1Sperry{ 951.1Sperry int result; 961.1Sperry int c; 971.1Sperry 981.1Sperry result = 0; 991.1Sperry while ((c = getchar()) != EOF) 1001.1Sperry result += c; 1011.1Sperry return(result); 1021.1Sperry} 1031.1Sperry 1041.1Sperry/* 1051.1Sperry * Get a set of six lines making up a change. 1061.1Sperry */ 1071.1Sperrychar * 1081.1Sperrychange(void) 1091.1Sperry{ 1101.1Sperry int i; 1111.1Sperry 1121.1Sperry for (i = 0; i < 6; i++) 1131.1Sperry string[i] = table[getrnum()&01][getrnum()&01][getrnum()&01] + '0'; 1141.1Sperry string[i] = '\0'; 1151.1Sperry return(string); 1161.1Sperry} 1171.1Sperry 1181.1Sperry/* 1191.1Sperry * Get a number more random than what getrand() gives. 1201.1Sperry */ 1211.1Sperryunsigned 1221.1Sperrygetrnum(void) 1231.1Sperry{ 1241.1Sperry return((getrand())>>(getrand()%17)); 1251.1Sperry} 1261.1Sperry 1271.1Sperry/* 1281.1Sperry * Get a random number. 1291.1Sperry */ 1301.1Sperryunsigned 1311.1Sperrygetrand(void) 1321.1Sperry{ 1331.1Sperry return(seed = (seed*13077) + 6925); 1341.1Sperry} 135