/* Copyright 2003, 2002, 2001 Mike Burns, Sean Proctor This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA The license is at . */ /* original version copyright Mike Burns, 2001. * updated version copyright Sean Proctor, 2002, 2003 * original: 9/15/01 * modified: 10/7/02 and again on 1/30/03 by Sean Proctor * Mike Burns * Sean Proctor */ #include #include #include static const char *sneaker_colors[] = {"red", "orange", "yellow", "green", "blue", "khaki", "black"}; #define NUM_SNEAKERS (sizeof(sneaker_colors) / sizeof(const char *)) struct sneakers_t { int left; int right; }; static int random_shoe(void) { /* return a number from 0 to NUM_SNEAKERS - 1 */ return (int)((double)NUM_SNEAKERS * rand() / (RAND_MAX + 1.0)); } static struct sneakers_t make_sneakers(int date) { struct sneakers_t sneakers; srand(date); sneakers.left = random_shoe(); sneakers.right = random_shoe(); if(sneakers.left == sneakers.right) { sneakers.left = random_shoe(); sneakers.right = random_shoe(); } return sneakers; } int current_date(void) { time_t t; struct tm *time_tm; char date[9]; if((t = time(NULL)) == -1 || !(time_tm = localtime(&t))) { printf("Really weird error.\n"); exit(1); } sprintf(date, "%4d%02d%02d", time_tm->tm_year + 1900, time_tm->tm_mon + 1, time_tm->tm_mday); return atoi(date); } int make_date(int argc, char *argv[]) { // If we don't have a given date, use the current one if(argc == 1) return current_date(); // If we don't have exactly one argument, or if our argument is longer // than 8 characters, we have an error if(argc != 2 || strlen(argv[1]) != 8) return 0; // return the value of the argument, 0 on error. return atoi(argv[1]); } int main (int argc, char *argv[]) { struct sneakers_t sneakers; int date; // make_date checks to make sure the input is valid and returns a // proper date depending on what the input was if(!(date = make_date(argc, argv))) { printf("usage: %s [YYYYMMDD]\n", argv[0]); exit(1); } sneakers = make_sneakers(date); printf("Sneakers for %08d:\nleft: %s\nright: %s\n", date, sneaker_colors[sneakers.left], sneaker_colors[sneakers.right]); return 0; }