/* Copyright 2002 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 * original: 9/15/01 updated: 10/7/02 * 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(char *)) int *make_sneakers(char *date) { /* modifies sneakers */ int *sneakers = malloc(sizeof(int) * 2); srand(atoi(date)); sneakers[0] = (int)((NUM_SNEAKERS - 1.0) * rand() / RAND_MAX); sneakers[1] = (int)((NUM_SNEAKERS - 1.0) * rand() / RAND_MAX); if(sneakers[0] == sneakers[1]) { srand((unsigned int)(20.0 * rand())); sneakers[0] = (int)((NUM_SNEAKERS - 1.0) * rand() / RAND_MAX); sneakers[1] = (int)((NUM_SNEAKERS - 1.0) * rand() / RAND_MAX); } return sneakers; } void output_sneakers(char *date, int *sneakers) { printf("Sneakers for %s: %s, %s\n", date, sneaker_colors[sneakers[0]], sneaker_colors[sneakers[1]]); } char *current_date(void) { time_t t; struct tm *time_tm; char *date = malloc(sizeof(char) * 9); if((t = time(NULL)) == -1 || !(time_tm = localtime(&t))) { printf("Really weird error."); exit(1); } sprintf(date, "%4d%02d%02d", time_tm->tm_year + 1900, time_tm->tm_mon, time_tm->tm_mday); return date; } int check_input(char *date) { int i; for(i = 0; i < 8; i++) if(!isdigit(date[i])) return 0; if(date[8]) return 0; return 1; } int main (int argc, char **argv) { int *sneakers; char *date; switch(argc) { case 1: date = current_date(); break; case 2: date = argv[1]; if(check_input(date)) break; default: printf("usage: %s [YYYYMMDD]\n", argv[0]); exit(1); } sneakers = make_sneakers(date); output_sneakers(date, sneakers); free(sneakers); free(date); return 0; }