11.3Sdholland/* $NetBSD: castching.c,v 1.3 2009/08/12 05:40:03 dholland 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.2Slukem__COPYRIGHT("@(#) Copyright (c) 1988, 1993\ 421.2Slukem The Regents of the University of California. All rights reserved."); 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.3Sdholland__RCSID("$NetBSD: castching.c,v 1.3 2009/08/12 05:40:03 dholland 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.3Sdhollandstatic time_t now; /* current time */ 641.1Sperry 651.3Sdhollandstatic unsigned seed; /* seed for random number generator */ 661.1Sperry 671.3Sdhollandstatic int getquest(void); 681.3Sdhollandstatic unsigned getrand(void); 691.3Sdhollandstatic unsigned getrnum(void); 701.3Sdhollandstatic char *change(void); 711.1Sperry 721.3Sdhollandstatic char string[6+1]; /* where the actual change string is put */ 731.3Sdholland 741.3Sdhollandstatic int table[2][2][2] = { 751.1Sperry { { OYIN, YYANG,}, { YYANG, YYIN,} }, 761.1Sperry { { YYANG, YYIN,}, { YYIN, OYANG,} }, 771.1Sperry}; 781.1Sperry 791.1Sperry/*ARGSUSED*/ 801.1Sperryint 811.1Sperrymain(int argc, char **argv) 821.1Sperry{ 831.1Sperry time(&now); 841.1Sperry /* randomize */ 851.1Sperry seed = (int)now + getquest() + getgid() + getuid() + getpid(); 861.1Sperry printf("%s\n", change()); 871.1Sperry exit(0); 881.1Sperry} 891.1Sperry 901.1Sperry/* 911.1Sperry * Hash the question by adding all the characters together. 921.1Sperry */ 931.3Sdhollandstatic int 941.1Sperrygetquest(void) 951.1Sperry{ 961.1Sperry int result; 971.1Sperry int c; 981.1Sperry 991.1Sperry result = 0; 1001.1Sperry while ((c = getchar()) != EOF) 1011.1Sperry result += c; 1021.1Sperry return(result); 1031.1Sperry} 1041.1Sperry 1051.1Sperry/* 1061.1Sperry * Get a set of six lines making up a change. 1071.1Sperry */ 1081.3Sdhollandstatic char * 1091.1Sperrychange(void) 1101.1Sperry{ 1111.1Sperry int i; 1121.1Sperry 1131.1Sperry for (i = 0; i < 6; i++) 1141.1Sperry string[i] = table[getrnum()&01][getrnum()&01][getrnum()&01] + '0'; 1151.1Sperry string[i] = '\0'; 1161.1Sperry return(string); 1171.1Sperry} 1181.1Sperry 1191.1Sperry/* 1201.1Sperry * Get a number more random than what getrand() gives. 1211.1Sperry */ 1221.3Sdhollandstatic unsigned 1231.1Sperrygetrnum(void) 1241.1Sperry{ 1251.1Sperry return((getrand())>>(getrand()%17)); 1261.1Sperry} 1271.1Sperry 1281.1Sperry/* 1291.1Sperry * Get a random number. 1301.1Sperry */ 1311.3Sdhollandstatic unsigned 1321.1Sperrygetrand(void) 1331.1Sperry{ 1341.1Sperry return(seed = (seed*13077) + 6925); 1351.1Sperry} 136