int random(unsigned int min, unsigned int max, int recursion_level) { /* Returns a semi-open interval [min, max) */ static bool srand_initialized = false; if (!srand_initialized) { srand_initialized = true; srand2(); } if (recursion_level > 100) { // A fallback. int ret = min + (rand() % (int)(max - min)); return ret; } int base_random = rand(); /* in [0, RAND_MAX] */ if (RAND_MAX == base_random) return random(min, max, recursion_level + 1); /* now guaranteed to be in [0, RAND_MAX) */ int range = max - min, remainder = RAND_MAX % range, bucket = RAND_MAX / range; /* There are range buckets, plus one smaller interval within remainder of RAND_MAX */ if (base_random < RAND_MAX - remainder) { return min + base_random/bucket; } else { return random(min, max, recursion_level + 1); } }
//******************************************************************** CPacketList::CPacketList(uint mutex_timeout, uint flags) : rx_pkt_list_top(0), rx_pkt_list_tail(0), eth_rx_queueing_enabled(false), hListAccessMutex(0), mutex_timeout_secs(mutex_timeout), // first_construct(false), rx_event_active(false), hdlRxEvent(0), reporting_func(NULL), rpt_private_data(NULL) { // ZeroMemory((char *) this, sizeof(CPacketList)) ; mutex_timeout_secs = mutex_timeout ; // wsprintfA(bfr, "EVENT%u", (uint) time(NULL)) ; if (!first_construct) { first_construct = true ; srand2(time(NULL)) ; // syslog("CPacketList: init rand2\n") ; } // rpt_private_data = NULL ; // reporting_func = NULL ; // make this default to TRUE eth_rx_queueing_enabled = (flags & EPL_QUEUEING_DISABLED) ? false : true ; if (flags & EPL_USE_RX_EVENT) { rx_event_active = true ; char bfr[40] ; // wsprintfA(bfr, "EVENT%u", (uint) time(NULL)) ; wsprintfA(bfr, "EVENT%u", (uint) rand2()) ; // syslog("CPacketList starting [%X] [%s]\n", flags, bfr) ; hdlRxEvent = CreateEventA(NULL, false, false, bfr) ; if (hdlRxEvent == NULL) syslog("CreateEvent: %s\n", get_system_message()) ; // else // syslog("hdlRxEvent handle created\n") ; } hListAccessMutex = CreateMutex(NULL, false, NULL) ; if (hListAccessMutex == NULL) { report_func("CreateMutex [CPacketList]: %s\n", get_system_message()) ; } else { // note: this is not actually an error! if (GetLastError() == ERROR_ALREADY_EXISTS) { report_func("CreateMutex [CPacketList]: mutex already exists!!\n") ; } } }
//*********************************************************************** //lint -esym(1784, WinMain) int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { //*************************************************************** // note that szAppName is not a very unique name; if either: // 1. someone else created a mutex with this name, or // 2. Elsewhere in this, or some other program, we created // another mutex using szAppName, // then this would not have the results intended!! //*************************************************************** if (!WeAreAlone (szAppName)) { // The old technique: // We are already running, display message and quit // The new technique: // We are already running, switch to first instance HWND hOther = NULL; EnumWindows(searcher, (LPARAM) &hOther); if ( hOther != NULL ) { /* pop up */ SetForegroundWindow( hOther ); if ( IsIconic( hOther ) ) { /* restore */ ShowWindow( hOther, SW_RESTORE ); } /* restore */ } /* pop up */ return 0; } g_hinst = hInstance; //Plant seed for random number generator with system time time_t ti ; time(&ti) ; srand2((unsigned) ti); // sprintf(tempstr, "ti=%u, rand=%u", ti, rand()) ; // OutputDebugString(tempstr) ; // set up initial data structs // read_config_data() ; init_castle_contents() ; init_player() ; // hdlTopLevel = OpenProcess(PROCESS_ALL_ACCESS, false, _getpid()) ; HWND hwnd = CreateDialog(g_hinst, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, (DLGPROC) TermProc) ; if (hwnd == NULL) { syslog("CreateDialog: %s\n", get_system_message()) ; return 0; } HACCEL hAccel = LoadAccelerators(g_hinst, MAKEINTRESOURCE(IDR_ACCELERATOR1)); // [2920] hInstance=4194304, 4194304, 4194304 // syslog("hInstance=%u, %u, %u\n", // hInstance, // GetWindowLong(hwnd, GWL_HINSTANCE), // GetModuleHandle(NULL) // ); MSG Msg; while(GetMessage(&Msg, NULL,0,0)) { if(!TranslateAccelerator(hwnd, hAccel, &Msg) && !IsDialogMessage(hwnd, &Msg)) { // if(!IsDialogMessage(hwnd, &Msg)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } } return (int) Msg.wParam ; } //lint !e715