| 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"cxx . . $ `<file>/rolldice.h`/ |
| 12 | |
| 13 | // File pointer for random device |
| 14 | static FILEcxx . . $ FILE#* ran_devcxx . . $ ran_dev.; |
| 15 | |
| 16 | // Local functions |
| 17 | int get_num_dicecxx . . $ get_num_dice(9b79fb6aee4c0440).(int temp_int, int default_num); |
| 18 | int get_num_sidescxx . . $ get_num_sides(6abd07ddb1a912f0).(char *dice_string, int temp_int); |
| 19 | int get_num_dropcxx . . $ get_num_drop(6abd07ddb1a912f0).(char *dice_string, int temp_int); |
| 20 | int get_num_rollscxx . . $ get_num_rolls(7864480464b09eea).(int temp_int); |
| 21 | int get_mutipliercxx . . $ get_mutiplier(6abd07ddb1a912f0).(char *dice_string, int temp_int); |
| 22 | int get_plus_modifiercxx . . $ get_plus_modifier(6abd07ddb1a912f0).(char *dice_string, int temp_int); |
| 23 | int get_minus_modifiercxx . . $ get_minus_modifier(6abd07ddb1a912f0).(char *dice_string, int temp_int); |
| 24 | int is_too_bigcxx . . $ is_too_big(7864480464b09eea).(int num); |
| 25 | void print_parse_errorcxx . . $ print_parse_error(34d26762b42c433a).(const char * label, const int too_big_error); |
| 26 | |
| 27 | |
| 28 | void init_randomcxx . . $ init_random(bc2330f64d909df9).(rand_typecxx . . $ rand_type# rand_file) { |
| 29 | if(rand_file == RANDOMcxx . . $ RANDOM.) { |
| 30 | if((ran_devcxx . . $ ran_dev. = fopencxx . . $ fopen(82ec0787cd37e0a3).("/dev/random", "r")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 31 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Error in opening /dev/random!\n"); |
| 32 | exitcxx . . $ exit(3ca292c338df9bb3).(EX_OSFILEcxx . . $ `<external>/5948936369402635282/sysexits.h:104:9`!); |
| 33 | } |
| 34 | } |
| 35 | else if((ran_devcxx . . $ ran_dev. = fopencxx . . $ fopen(82ec0787cd37e0a3).("/dev/urandom", "r")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 36 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Error in opening /dev/urandom!\n"); |
| 37 | exitcxx . . $ exit(3ca292c338df9bb3).(EX_OSFILEcxx . . $ `<external>/5948936369402635282/sysexits.h:104:9`!); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | static int get_randomcxx . . $ get_random(7864480464b09eea).(int sides) { |
| 42 | unsigned int ret_value; |
| 43 | |
| 44 | if(!(freadcxx . . $ fread(738fc8ac1d711e58).(&ret_value, sizeof(unsigned int), 1, ran_devcxx . . $ ran_dev.) == 1)) { |
| 45 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Error in reading random device!\n"); |
| 46 | exitcxx . . $ exit(3ca292c338df9bb3).(EX_OSFILEcxx . . $ `<external>/5948936369402635282/sysexits.h:104:9`!); |
| 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 rolldiecxx . . $ rolldie(7864480464b09eea). ( int num_sides ) { |
| 57 | |
| 58 | return (1 + get_randomcxx . . $ get_random(7864480464b09eea).(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_stringcxx . . $ parse_string(b0a0d720b596a511).(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_INITIALIZEDcxx . . $ `rolldice.h:44:9`!]) { |
| 73 | return 0; |
| 74 | } |
| 75 | else { |
| 76 | dice_nums[NUM_ROLLScxx . . $ `rolldice.h:38:9`!] = 1; |
| 77 | dice_nums[NUM_DICEcxx . . $ `rolldice.h:39:9`!] = DEFAULT_NUM_DICE; |
| 78 | dice_nums[NUM_SIDEScxx . . $ `rolldice.h:40:9`!] = 6; |
| 79 | dice_nums[MULTIPLIERcxx . . $ `rolldice.h:41:9`!] = 1; |
| 80 | dice_nums[MODIFIERcxx . . $ `rolldice.h:42:9`!] = 0; |
| 81 | dice_nums[NUM_DROPcxx . . $ `rolldice.h:43:9`!] = 0; |
| 82 | dice_nums[NUM_INITIALIZEDcxx . . $ `rolldice.h:44:9`!] = 1; |
| 83 | } |
| 84 | |
| 85 | while(*dice_string != '\0') { |
| 86 | if( isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!(*dice_string) ) { |
| 87 | sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(dice_string, "%d", &temp_int); |
| 88 | while(isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!(*(++dice_string))); |
| 89 | } |
| 90 | else { |
| 91 | switch(*dice_string) { |
| 92 | case 'd': |
| 93 | dice_nums[NUM_DICEcxx . . $ `rolldice.h:39:9`!] = get_num_dicecxx . . $ get_num_dice(9b79fb6aee4c0440).(temp_int, DEFAULT_NUM_DICE); |
| 94 | dice_string++; |
| 95 | res_int = get_num_sidescxx . . $ get_num_sides(6abd07ddb1a912f0).(dice_string, temp_int); |
| 96 | if (!res_int){ |
| 97 | return 1; |
| 98 | } else { |
| 99 | dice_nums[NUM_SIDEScxx . . $ `rolldice.h:40:9`!] = res_int; |
| 100 | } |
| 101 | break; |
| 102 | case 's': |
| 103 | dice_string++; |
| 104 | res_int = get_num_dropcxx . . $ get_num_drop(6abd07ddb1a912f0).(dice_string, temp_int); |
| 105 | if (!res_int){ |
| 106 | return 1; |
| 107 | } else { |
| 108 | dice_nums[NUM_DROPcxx . . $ `rolldice.h:43:9`!] = res_int; |
| 109 | } |
| 110 | break; |
| 111 | case 'x': |
| 112 | dice_string++; |
| 113 | res_int = get_num_rollscxx . . $ get_num_rolls(7864480464b09eea).(temp_int); |
| 114 | if (!res_int){ |
| 115 | return 1; |
| 116 | } else { |
| 117 | dice_nums[NUM_ROLLScxx . . $ `rolldice.h:38:9`!] = res_int; |
| 118 | } |
| 119 | break; |
| 120 | case '*': |
| 121 | dice_string++; |
| 122 | res_int = get_mutipliercxx . . $ get_mutiplier(6abd07ddb1a912f0).(dice_string, temp_int); |
| 123 | if (!res_int){ |
| 124 | return 1; |
| 125 | } else { |
| 126 | dice_nums[MULTIPLIERcxx . . $ `rolldice.h:41:9`!] = res_int; |
| 127 | } |
| 128 | break; |
| 129 | case '+': |
| 130 | dice_string++; |
| 131 | res_int = get_plus_modifiercxx . . $ get_plus_modifier(6abd07ddb1a912f0).(dice_string, temp_int); |
| 132 | if (!res_int){ |
| 133 | return 1; |
| 134 | } else { |
| 135 | dice_nums[MODIFIERcxx . . $ `rolldice.h:42:9`!] = res_int; |
| 136 | } |
| 137 | break; |
| 138 | case '-': |
| 139 | dice_string++; |
| 140 | res_int = get_minus_modifiercxx . . $ get_minus_modifier(6abd07ddb1a912f0).(dice_string, temp_int); |
| 141 | if (!res_int){ |
| 142 | return 1; |
| 143 | } else { |
| 144 | dice_nums[MODIFIERcxx . . $ `rolldice.h:42:9`!] = 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_dicecxx . . $ get_num_dice(9b79fb6aee4c0440).(int temp_int, int default_num){ |
| 158 | if( (temp_int <= 0 ) || is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int) ) |
| 159 | return default_num; |
| 160 | else |
| 161 | return temp_int; |
| 162 | } |
| 163 | |
| 164 | int is_too_bigcxx . . $ is_too_big(7864480464b09eea).(int num){ |
| 165 | return num >= SHRT_MAXcxx . . $ `<external>/67164224788392828/limits.h:106:9`!; |
| 166 | } |
| 167 | |
| 168 | void print_parse_errorcxx . . $ print_parse_error(34d26762b42c433a).(const char * label, const int too_big_error){ |
| 169 | if (too_big_error){ |
| 170 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "rolldice: Requested %s is too large\n", label); |
| 171 | } |
| 172 | else{ |
| 173 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "rolldice: Problems with the malformed dice string (in %s)\n", label); |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | int get_num_sidescxx . . $ get_num_sides(6abd07ddb1a912f0).(char *dice_string, int temp_int){ |
| 179 | const char *PERCENT = "%"; |
| 180 | if(strncmpcxx . . $ strncmp(fd278af2f295fa2e).(dice_string, PERCENT, 1) == 0){ |
| 181 | temp_int = 100; |
| 182 | return temp_int; |
| 183 | } |
| 184 | else if( (sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(dice_string, "%d", &temp_int) < 1 ) || |
| 185 | (temp_int < 2) || is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int) ) { |
| 186 | print_parse_errorcxx . . $ print_parse_error(34d26762b42c433a).("number of dice faces", is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int)); |
| 187 | return 0; |
| 188 | } else { |
| 189 | return temp_int; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | int get_num_dropcxx . . $ get_num_drop(6abd07ddb1a912f0).(char *dice_string, int temp_int){ |
| 194 | if( (sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(dice_string, "%d", &temp_int) < 1) || |
| 195 | (temp_int < 0) || is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int) ) { |
| 196 | print_parse_errorcxx . . $ print_parse_error(34d26762b42c433a).("number of dropped dice", is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int)); |
| 197 | return 0; |
| 198 | } else { |
| 199 | return temp_int; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | int get_num_rollscxx . . $ get_num_rolls(7864480464b09eea).(int temp_int){ |
| 204 | if( ( temp_int < 1 ) || is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int) ) { |
| 205 | print_parse_errorcxx . . $ print_parse_error(34d26762b42c433a).("number of rolled dice", is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int)); |
| 206 | return 0; |
| 207 | } else { |
| 208 | return temp_int; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | int get_mutipliercxx . . $ get_mutiplier(6abd07ddb1a912f0).(char *dice_string, int temp_int){ |
| 213 | if( (sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(dice_string, "%d", &temp_int) < 1) || |
| 214 | (temp_int < 0) || is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int) ) { |
| 215 | print_parse_errorcxx . . $ print_parse_error(34d26762b42c433a).("multiplier", is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int)); |
| 216 | return 0; |
| 217 | } else { |
| 218 | return temp_int; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | int get_plus_modifiercxx . . $ get_plus_modifier(6abd07ddb1a912f0).(char *dice_string, int temp_int){ |
| 223 | if( (sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(dice_string, "%d", &temp_int) < 1) || |
| 224 | (temp_int < 0) || is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int) ) { |
| 225 | print_parse_errorcxx . . $ print_parse_error(34d26762b42c433a).("add modifier", is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int)); |
| 226 | return 0; |
| 227 | } else { |
| 228 | return temp_int; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int get_minus_modifiercxx . . $ get_minus_modifier(6abd07ddb1a912f0).(char *dice_string, int temp_int){ |
| 233 | if( (sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(dice_string, "%d", &temp_int) < 1) || |
| 234 | (temp_int < 0) || is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int) ) { |
| 235 | print_parse_errorcxx . . $ print_parse_error(34d26762b42c433a).("minus modifier", is_too_bigcxx . . $ is_too_big(7864480464b09eea).(temp_int)); |
| 236 | return 0; |
| 237 | } else { |
| 238 | temp_int = - temp_int; |
| 239 | return temp_int; |
| 240 | } |
| 241 | } |
| 242 |