| 1 | /** |
| 2 | * rolldice.c - v1.14 - 18 December 2012 |
| 3 | * (c) Stevie Strickland, 1999-2012 |
| 4 | * |
| 5 | * This program has been placed under the GPL. Any bugfixes or enhancements |
| 6 | * will be greatly appreciated :) |
| 7 | * |
| 8 | * Stevie Strickland - [email protected] |
| 9 | */ |
| 10 | |
| 11 | #include "rolldice.h" |
| 12 | |
| 13 | // File pointer for random device |
| 14 | static FILE* ran_dev; |
| 15 | |
| 16 | // Local functions |
| 17 | int get_num_dice(int temp_int, int default_num); |
| 18 | int get_num_sides(char *dice_string, int temp_int); |
| 19 | int get_num_drop(char *dice_string, int temp_int); |
| 20 | int get_num_rolls(int temp_int); |
| 21 | int get_mutiplier(char *dice_string, int temp_int); |
| 22 | int get_plus_modifier(char *dice_string, int temp_int); |
| 23 | int get_minus_modifier(char *dice_string, int temp_int); |
| 24 | int is_too_big(int num); |
| 25 | void print_parse_error(const char * label, const int too_big_error); |
| 26 | |
| 27 | |
| 28 | void init_random(rand_type rand_file) { |
| 29 | if(rand_file == RANDOM) { |
| 30 | if((ran_dev = fopen("/dev/random", "r")) == NULL) { |
| 31 | fprintf(stderr, "Error in opening /dev/random!\n"); |
| 32 | exit(EX_OSFILE); |
| 33 | } |
| 34 | } |
| 35 | else if((ran_dev = fopen("/dev/urandom", "r")) == NULL) { |
| 36 | fprintf(stderr, "Error in opening /dev/urandom!\n"); |
| 37 | exit(EX_OSFILE); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | static int get_random(int sides) { |
| 42 | unsigned int ret_value; |
| 43 | |
| 44 | if(!(fread(&ret_value, sizeof(unsigned int), 1, ran_dev) == 1)) { |
| 45 | fprintf(stderr, "Error in reading random device!\n"); |
| 46 | exit(EX_OSFILE); |
| 47 | } |
| 48 | return (int)(ret_value % sides); |
| 49 | } |
| 50 | |
| 51 | /* rolldie() - Rolls a single die |
| 52 | * |
| 53 | * Parameters: int num_sides - number of sides of the die to roll |
| 54 | * Returns: int - the result of the roll |
| 55 | */ |
| 56 | int rolldie ( int num_sides ) { |
| 57 | |
| 58 | return (1 + get_random(num_sides)); |
| 59 | } |
| 60 | |
| 61 | /* parse_string() - Parses a string for dice rolling attributes |
| 62 | * |
| 63 | * Parameters: char *dice_string - string to parse |
| 64 | * int *dice_nums - array of size DICE_ARRAY_SIZE |
| 65 | * where the parsed values will be stored |
| 66 | * Returns: int - return non-zero value if error occured |
| 67 | */ |
| 68 | int parse_string(char *dice_string, int *dice_nums) { |
| 69 | int temp_int = -1, res_int; |
| 70 | const int DEFAULT_NUM_DICE = 1; |
| 71 | |
| 72 | if (*dice_string == '\0' && dice_nums[NUM_INITIALIZED]) { |
| 73 | return 0; |
| 74 | } |
| 75 | else { |
| 76 | dice_nums[NUM_ROLLS] = 1; |
| 77 | dice_nums[NUM_DICE] = DEFAULT_NUM_DICE; |
| 78 | dice_nums[NUM_SIDES] = 6; |
| 79 | dice_nums[MULTIPLIER] = 1; |
| 80 | dice_nums[MODIFIER] = 0; |
| 81 | dice_nums[NUM_DROP] = 0; |
| 82 | dice_nums[NUM_INITIALIZED] = 1; |
| 83 | } |
| 84 | |
| 85 | while(*dice_string != '\0') { |
| 86 | if( isdigit(*dice_string) ) { |
| 87 | sscanf(dice_string, "%d", &temp_int); |
| 88 | while(isdigit(*(++dice_string))); |
| 89 | } |
| 90 | else { |
| 91 | switch(*dice_string) { |
| 92 | case 'd': |
| 93 | dice_nums[NUM_DICE] = get_num_dice(temp_int, DEFAULT_NUM_DICE); |
| 94 | dice_string++; |
| 95 | res_int = get_num_sides(dice_string, temp_int); |
| 96 | if (!res_int){ |
| 97 | return 1; |
| 98 | } else { |
| 99 | dice_nums[NUM_SIDES] = res_int; |
| 100 | } |
| 101 | break; |
| 102 | case 's': |
| 103 | dice_string++; |
| 104 | res_int = get_num_drop(dice_string, temp_int); |
| 105 | if (!res_int){ |
| 106 | return 1; |
| 107 | } else { |
| 108 | dice_nums[NUM_DROP] = res_int; |
| 109 | } |
| 110 | break; |
| 111 | case 'x': |
| 112 | dice_string++; |
| 113 | res_int = get_num_rolls(temp_int); |
| 114 | if (!res_int){ |
| 115 | return 1; |
| 116 | } else { |
| 117 | dice_nums[NUM_ROLLS] = res_int; |
| 118 | } |
| 119 | break; |
| 120 | case '*': |
| 121 | dice_string++; |
| 122 | res_int = get_mutiplier(dice_string, temp_int); |
| 123 | if (!res_int){ |
| 124 | return 1; |
| 125 | } else { |
| 126 | dice_nums[MULTIPLIER] = res_int; |
| 127 | } |
| 128 | break; |
| 129 | case '+': |
| 130 | dice_string++; |
| 131 | res_int = get_plus_modifier(dice_string, temp_int); |
| 132 | if (!res_int){ |
| 133 | return 1; |
| 134 | } else { |
| 135 | dice_nums[MODIFIER] = res_int; |
| 136 | } |
| 137 | break; |
| 138 | case '-': |
| 139 | dice_string++; |
| 140 | res_int = get_minus_modifier(dice_string, temp_int); |
| 141 | if (!res_int){ |
| 142 | return 1; |
| 143 | } else { |
| 144 | dice_nums[MODIFIER] = res_int; |
| 145 | } |
| 146 | break; |
| 147 | default: |
| 148 | dice_string++; |
| 149 | break; |
| 150 | } |
| 151 | temp_int = 0; |
| 152 | } |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int get_num_dice(int temp_int, int default_num){ |
| 158 | if( (temp_int <= 0 ) || is_too_big(temp_int) ) |
| 159 | return default_num; |
| 160 | else |
| 161 | return temp_int; |
| 162 | } |
| 163 | |
| 164 | int is_too_big(int num){ |
| 165 | return num >= SHRT_MAX; |
| 166 | } |
| 167 | |
| 168 | void print_parse_error(const char * label, const int too_big_error){ |
| 169 | if (too_big_error){ |
| 170 | fprintf(stderr, "rolldice: Requested %s is too large\n", label); |
| 171 | } |
| 172 | else{ |
| 173 | fprintf(stderr, "rolldice: Problems with the malformed dice string (in %s)\n", label); |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | int get_num_sides(char *dice_string, int temp_int){ |
| 179 | const char *PERCENT = "%"; |
| 180 | if(strncmp(dice_string, PERCENT, 1) == 0){ |
| 181 | temp_int = 100; |
| 182 | return temp_int; |
| 183 | } |
| 184 | else if( (sscanf(dice_string, "%d", &temp_int) < 1 ) || |
| 185 | (temp_int < 2) || is_too_big(temp_int) ) { |
| 186 | print_parse_error("number of dice faces", is_too_big(temp_int)); |
| 187 | return 0; |
| 188 | } else { |
| 189 | return temp_int; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | int get_num_drop(char *dice_string, int temp_int){ |
| 194 | if( (sscanf(dice_string, "%d", &temp_int) < 1) || |
| 195 | (temp_int < 0) || is_too_big(temp_int) ) { |
| 196 | print_parse_error("number of dropped dice", is_too_big(temp_int)); |
| 197 | return 0; |
| 198 | } else { |
| 199 | return temp_int; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | int get_num_rolls(int temp_int){ |
| 204 | if( ( temp_int < 1 ) || is_too_big(temp_int) ) { |
| 205 | print_parse_error("number of rolled dice", is_too_big(temp_int)); |
| 206 | return 0; |
| 207 | } else { |
| 208 | return temp_int; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | int get_mutiplier(char *dice_string, int temp_int){ |
| 213 | if( (sscanf(dice_string, "%d", &temp_int) < 1) || |
| 214 | (temp_int < 0) || is_too_big(temp_int) ) { |
| 215 | print_parse_error("multiplier", is_too_big(temp_int)); |
| 216 | return 0; |
| 217 | } else { |
| 218 | return temp_int; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | int get_plus_modifier(char *dice_string, int temp_int){ |
| 223 | if( (sscanf(dice_string, "%d", &temp_int) < 1) || |
| 224 | (temp_int < 0) || is_too_big(temp_int) ) { |
| 225 | print_parse_error("add modifier", is_too_big(temp_int)); |
| 226 | return 0; |
| 227 | } else { |
| 228 | return temp_int; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int get_minus_modifier(char *dice_string, int temp_int){ |
| 233 | if( (sscanf(dice_string, "%d", &temp_int) < 1) || |
| 234 | (temp_int < 0) || is_too_big(temp_int) ) { |
| 235 | print_parse_error("minus modifier", is_too_big(temp_int)); |
| 236 | return 0; |
| 237 | } else { |
| 238 | temp_int = - temp_int; |
| 239 | return temp_int; |
| 240 | } |
| 241 | } |
| 242 |