int main(int argc, char **argv) { int i; Tree tree= TREE_INITIALIZER(Node_compare); mt_random_init(); for (i= 0; i < 100; ++i) { Node v= { mt_random() % 1000, i }; Node *vv= TREE_FIND(&tree, _Node, tree, &v); if (vv) { printf("already inserted "); Node_print(vv, stdout); printf("\n"); } else { printf("insert " ); Node_print(&v, stdout); printf("\n"); TREE_INSERT(&tree, _Node, tree, Node_new(v.key, v.value)); TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); } } TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); for (i= 0; i < 1000; ++i) { Node *v= Node_new(mt_random() % 1000, 0); Node *vv= TREE_FIND(&tree, _Node, tree, v); printf("looking for %d - ", v->key); if (vv) { printf("found "); Node_print(vv, stdout); printf("\n"); TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); } else { printf("not found\n"); } TREE_REMOVE(&tree, _Node, tree, v); } TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); return 0; }
//Generates a random tetramino (a number between 0 and 6) int get_next() { //Returns a random number between 0 and 6 //It uses return (int) mt_random() % 7 ; }//int get_next()
/** * eo_init * ``````` * Initialize a pump in the current working directory. * * Returns nothing. */ void eo_init(void) { struct pumpconfig_t config; unsigned long salt; char hex[65]; if (is_pump(ENV.cwd)) bye("pump exists"); make_pump(ENV.cwd); salt = mt_random(); sha256gen(hex, &salt); slcpy(config.name, curdir(), LINESIZE); slcpy(config.desc, "Default description", LINESIZE); slcpy(config.base, scwd(), LINESIZE); slcpy(config.sha2, hex, LINESIZE); slcpy(config.link, "/foo/bar/bazscript.qux", LINESIZE); slcpy(config.wait, "0", LINESIZE); write_config(&config, ENV.config); }
/* exclude_cookie is a hack used because we sometimes want to get rumors in a * context where messages such as "You swallowed the fortune!" that refer to * cookies should not appear. This has no effect for true rumors since none * of them contain such references anyway. */ char * getrumor(int truth, /* 1=true, -1=false, 0=either */ char *rumor_buf, boolean exclude_cookie, int *truth_out) { dlb *rumors; int tidbit, beginning; char *endp, line[BUFSZ], xbuf[BUFSZ]; int ltruth = 0; rumor_buf[0] = '\0'; if (true_rumor_size < 0L) /* we couldn't open RUMORFILE */ return rumor_buf; rumors = dlb_fopen(RUMORFILE, "r"); if (rumors) { int count = 0; int adjtruth; do { rumor_buf[0] = '\0'; if (true_rumor_size == 0L) { /* if this is 1st outrumor() */ init_rumors(rumors); if (true_rumor_size < 0L) { /* init failed */ sprintf(rumor_buf, "Error reading \"%.80s\".", RUMORFILE); return rumor_buf; } } /* * input: 1 0 -1 * rn2 \ +1 2=T 1=T 0=F * adj./ +0 1=T 0=F -1=F */ switch (adjtruth = truth + rn2(2)) { case 2: /* (might let a bogus input arg sneak thru) */ case 1: beginning = true_rumor_start; tidbit = mt_random() % true_rumor_size; break; case 0: /* once here, 0 => false rather than "either" */ case -1: beginning = false_rumor_start; tidbit = mt_random() % false_rumor_size; break; default: impossible("strange truth value for rumor"); return strcpy(rumor_buf, "Oops..."); } dlb_fseek(rumors, beginning + tidbit, SEEK_SET); dlb_fgets(line, sizeof line, rumors); if (!dlb_fgets(line, sizeof line, rumors) || (adjtruth > 0 && dlb_ftell(rumors) > true_rumor_end)) { /* reached end of rumors -- go back to beginning */ dlb_fseek(rumors, beginning, SEEK_SET); dlb_fgets(line, sizeof line, rumors); } if ((endp = strchr(line, '\n')) != 0) *endp = 0; strcat(rumor_buf, xcrypt(line, xbuf)); } while (count++ < 50 && exclude_cookie && (strstri(rumor_buf, "fortune") || strstri(rumor_buf, "pity"))); dlb_fclose(rumors); if (count >= 50) impossible("Can't find non-cookie rumor?"); else ltruth = (adjtruth > 0) ? 1 : -1; } else { pline("Can't open rumors file!"); true_rumor_size = -1; /* don't try to open it again */ if (truth_out) *truth_out = 0; } if (truth_out) *truth_out = ltruth; return rumor_buf; }