/** * Verify that calculated hash doesn't depend on message alignment. */ static void test_alignment(void) { int i, start, hash_id, alignment_size; /* loop by sums */ for(i = 0, hash_id = 1; (hash_id & RHASH_ALL_HASHES); hash_id <<= 1, i++) { char expected_hash[130]; assert(rhash_get_digest_size(hash_id) < (int)sizeof(expected_hash)); alignment_size = (hash_id & (RHASH_TTH | RHASH_TIGER | RHASH_WHIRLPOOL | RHASH_SHA512) ? 8 : 4); /* start message with different alignment */ for(start = 0; start < alignment_size; start++) { char message[30]; char* obtained; int j, msg_length = 11 + alignment_size; /* fill the buffer fifth shifted letter sequence */ for(j = 0; j < msg_length; j++) message[start + j] = 'a' + j; message[start + j] = 0; obtained = calc_sum(message + start, hash_id); if(start == 0) { /* save original sum */ strcpy(expected_hash, obtained); } else { /* verify sum result */ assert_equals(obtained, expected_hash, rhash_get_name(hash_id), message); fflush(stdout); } } } }
int main() { int t, i = 1; int a,b; freopen("./sample.txt", "r", stdin); scanf("%d", &t); getchar(); if( (t < 1) || ( t > 100) ) return 0; while(i <= t) { scanf("%d", &a); getchar(); scanf("%d", &b); getchar(); if( (a < 0) || ( a > 100) ) continue; if( (b < 0) || ( b > 100) ) continue; if( a > b ) { printf("Case %d: 0\n", i); } else { printf("Case %d: %d\n", i, calc_sum(a, b)); } i++; } return 0; }
static int search_cmd_process(unsigned char *buf, int cmd_len, const struct sockaddr_t *dest_addr, socklen_t addrlen) { u32 len=0; mxchip_cmd_head_t *p_reply; udp_search_st *data; u16 cksum, cmd; if(device_info->udpSearch_fd == 0) return 0; if ((buf[0] != CONTROL_FLAG) || (buf[1] != 0)) return 0; if (check_sum(hugebuf, cmd_len) != 1) return 0; p_reply = (mxchip_cmd_head_t *)buf; //Use the same data block to save the RAM data = (udp_search_st *)(p_reply->data); p_reply->cmd_status = CMD_OK; cmd = p_reply->cmd; p_reply->cmd |= 0x8000; if(cmd == CMD_SEARCH){ p_reply->datalen = sizeof(udp_search_st); memcpy(data->ip, device_info->status.ip, 16); memcpy(data->mac, device_info->status.mac, 18); sprintf((char *)data->mac, "%08x", FW_VERSION); len = sizeof(mxchip_cmd_head_t) + 1 + p_reply->datalen; cksum = calc_sum(hugebuf, len-2); hugebuf[len-2] = cksum & 0x0ff; hugebuf[len-1] = (cksum & 0xff00) >> 8; sendto(device_info->udpSearch_fd, (u8*)p_reply, sizeof(mxchip_cmd_head_t) + 1 + p_reply->datalen, \ 0, dest_addr, addrlen); }
static void add_edges(graph_info *g, unsigned start, int extended_m, level *my_level) { //setup m and k[n] for the children //note that these values will not change b/w each child //of this node in the search tree g->m++; g->k[g->n - 1]++; unsigned old_max_k = g->max_k; if(g->k[g->n - 1] > g->max_k) g->max_k = g->k[g->n - 1]; //if the child has a node of degree greater than MAX_K, //don't search it if(g->k[g->n - 1] <= my_level->max_k) { for(unsigned i = start; i < g->n - 1; i++) { g->k[i]++; //same as comment above if(g->k[i] <= my_level->max_k) { unsigned old_max_k = g->max_k; if(g->k[i] > g->max_k) g->max_k = g->k[i]; g->distances[g->n*i + (g->n-1)] = g->distances[g->n*(g->n-1) + i] = 1; ADDELEMENT(GRAPHROW(g->nauty_graph, i, extended_m), g->n-1); ADDELEMENT(GRAPHROW(g->nauty_graph, g->n-1, extended_m), i); add_edges(g, i + 1, extended_m, my_level); DELELEMENT(GRAPHROW(g->nauty_graph, i, extended_m), g->n-1); DELELEMENT(GRAPHROW(g->nauty_graph, g->n-1, extended_m), i); g->distances[g->n*i + (g->n-1)] = g->distances[g->n*(g->n-1) + i] = GRAPH_INFINITY; g->max_k = old_max_k; } g->k[i]--; } } //tear down values we created in the beginning g->max_k = old_max_k; g->m--; g->k[g->n - 1]--; if(g->k[g->n - 1] > 0) { graph_info *temporary = new_graph_info(*g); fill_dist_matrix(*temporary); temporary->diameter = calc_diameter(*temporary); temporary->sum_of_distances = calc_sum(*temporary); if(!add_graph_to_level(temporary, my_level)) graph_info_destroy(temporary); } }
int main(void) { int n, sum; printf("Sum integers up to: "); scanf("%d", &n); calc_sum(n, &sum); printf("Sum is %d\n", sum); return 0; }
char process_image(hdr_ent *e, FILE *finput) { if (is_enabled(e->image_type)) { char * filename = calloc(strlen(odir)+BUFSIZ, 1); FILE * foutput; char buf[BUFSIZ]; size_t start = le32toh(e->start_s) * 512; size_t missing = le32toh(e->sectors_t) * 512; struct bootheader * hdr; snprintf(filename, BUFSIZ, "%s/%s.img", odir, getImageType(e->image_type)); foutput = fopen(filename, "wb"); if (!foutput) ERROR("ERROR: failed to open output %s\n", filename); MESSAGE("Saving %s image in %s\n", getImageType(e->image_type), filename); hdr = (struct bootheader *)malloc(SECTOR); memcpy(hdr, header512, SECTOR); hdr->sectors_t = e->sectors_t; hdr->image_type = e->image_type; hdr->xor56 = calc_sum(hdr); if (fwrite(hdr, SECTOR, 1, foutput) != 1) ERROR("ERROR: failed to write output header\n"); fflush(foutput); if (fseek(finput, start, SEEK_SET) == -1) ERROR("ERROR: failed to seek in input\n"); while (missing > 0) { size_t size = fread(buf, 1, ((missing > BUFSIZ) ? BUFSIZ : missing), finput); if (size != 0) { fwrite(buf, 1, size, foutput); missing -= size; } else if (missing > 0){ ERROR("ERROR: failed to read from input\n"); } } free(filename); fclose(foutput); return 0; } else { MESSAGE("Skipping %s image\n", getImageType(e->image_type)); return 1; } }
int main() { int n; int upper; char src_buf[1024]; char dst_buf[1024]; printf("Please input sum: "); scanf("%d", &upper); calc_sum(&n, upper); gen_random(src_buf, sizeof(src_buf)); /* Copy to local buffer */ buf_cpy(dst_buf, src_buf, sizeof(dst_buf)); /* Copy to global buffer */ buf_cpy(g_buf, src_buf, sizeof(g_buf)); return 0; }
int main() { try { // read N - amount of numbers to calculate sum of int amount = read_amount(); // read set of numbers vector<double> numbers = read_numbers(); // output sum output_sum(calc_sum(numbers, amount), amount); // output diff numbers output_diff_vector(calc_diff_vector(numbers)); } catch (exception& e) { cerr << e.what(); return 1; } catch (...) { cerr << "Unknown error"; return 2; } }
int main(void){ unsigned int digit = 10, limit = fact(10); unsigned int j,sum=0,hits=0; while(next_perm()){ if(unusual()){ results = realloc(results,(hits+1)*sizeof *results); results[hits] = malloc(len*sizeof *results[0]); memcpy(results[hits],perm,len*sizeof perm[0]); //printf("%d : ",j); printResult(results[hits]); hits++; } //next_perm(); } calc_sum(results,hits); print_sum(); //printf("sum is %d\n",sum); return 0; }
graph_info *graph_info_from_nauty(graph *g, int n) { graph_info *ret = malloc(sizeof(graph_info)); ret->n = n; ret->distances = malloc(n * n * sizeof(*ret->distances)); ret->k = malloc(n * sizeof(*ret->k)); int m = (n + WORDSIZE - 1) / WORDSIZE; ret->m = 0; //total number of edges for (int i = 0; i < n; i++) { ret->k[i] = 0; for (int j = 0; j < n; j++) { if(i == j) ret->distances[n*i + j] = 0; else if(ISELEMENT(GRAPHROW(g, i, m), j)) { ret->distances[n*i + j] = 1; ret->k[i]++; ret->m++; } else ret->distances[n*i + j] = GRAPH_INFINITY; } } ret->m /= 2; ret->max_k = 0; for (int i = 0; i < n; i++) if (ret->k[i] > ret->max_k) ret->max_k = ret->k[i]; floyd_warshall(*ret); ret->sum_of_distances = calc_sum(*ret); ret->diameter = calc_diameter(*ret); ret->nauty_graph = malloc(n * m * sizeof(graph)); ret->gcan = NULL; memcpy(ret->nauty_graph, g, n * m * sizeof(graph)); return ret; }
int main(void) { int begin,end; re: printf("Please put number of begin and end."); scanf("%d",&begin,&end); if(begin >= end){ goto re; } int sum; sum = calc_sum(begin,end); printf("sum = %d",sum); return 0; }
void process_block(InputIterator bytes_begin, InputIterator bytes_end) { sum_ = calc_sum(bytes_begin, bytes_end); }
SPROUT_CXX14_CONSTEXPR void process_block(InputIterator bytes_begin, InputIterator bytes_end) { sum_ = calc_sum(bytes_begin, bytes_end); }
SPROUT_CONSTEXPR xor8 const c_process_block(InputIterator bytes_begin, InputIterator bytes_end) const { return xor8(calc_sum(bytes_begin, bytes_end)); }
SPROUT_CONSTEXPR sum_basic const c_process_block(InputIterator bytes_begin, InputIterator bytes_end) const { return sum_basic(calc_sum(bytes_begin, bytes_end)); }
/*** create new linregmodel object * INPUT: * mm_dense *y: dense vector * mm_sparse *x: sparse general / symmetric matrix * const double lambda2: regularization parameter * const mm_real *d: general linear operator of penalty * const mm_real *w: L1 penalty factor * PreProc proc: specify pre-processing for y and x * DO_CENTERING_Y: do centering of y * DO_CENTERING_X: do centering of each column of x * DO_NORMALIZING_X: do normalizing of each column of x * DO_STANDARDIZING_X: do centering and normalizing of each column of x ***/ linregmodel * linregmodel_new (mm_real *y, mm_real *x, const mm_real *d, PreProc proc) { int j; linregmodel *lreg; /* check whether x and y are not empty */ if (!y) error_and_exit ("linregmodel_new", "y is empty.", __FILE__, __LINE__); if (!x) error_and_exit ("linregmodel_new", "x is empty.", __FILE__, __LINE__); /* check whether y is vector */ if (y->n != 1) error_and_exit ("linregmodel_new", "y must be vector.", __FILE__, __LINE__); /* check dimensions of x and y */ if (y->m != x->m) error_and_exit ("linregmodel_new", "dimensions of x and y do not match.", __FILE__, __LINE__); /* check dimensions of x and d */ if (d && x->n != d->n) error_and_exit ("linregmodel_new", "dimensions of x and d do not match.", __FILE__, __LINE__); lreg = linregmodel_alloc (); if (lreg == NULL) error_and_exit ("linregmodel_new", "failed to allocate memory for linregmodel object.", __FILE__, __LINE__); lreg->y = y; lreg->ycentered = calc_sum (lreg->y, &lreg->sy); /* if DO_CENTERING_Y is set and y is not already centered */ if ((proc & DO_CENTERING_Y) && !lreg->ycentered) { /* if lreg->y is sparse, convert to dense vector */ if (mm_real_is_sparse (lreg->y)) mm_real_sparse_to_dense (lreg->y); do_centering (lreg->y, lreg->sy); lreg->ycentered = true; } lreg->x = x; lreg->xcentered = calc_sum (lreg->x, &lreg->sx); /* if DO_CENTERING_X is set and x is not already centered */ if ((proc & DO_CENTERING_X) && !lreg->xcentered) { /* if lreg->x is sparse, convert to dense matrix */ if (mm_real_is_sparse (lreg->x)) mm_real_sparse_to_dense (lreg->x); /* if lreg->x is symmetric, convert to general matrix */ if (mm_real_is_symmetric (lreg->x)) mm_real_symmetric_to_general (lreg->x); do_centering (lreg->x, lreg->sx); lreg->xcentered = true; }; lreg->xnormalized = calc_ssq (lreg->x, &lreg->xtx); /* if DO_NORMALIZING_X is set and x is not already normalized */ if ((proc & DO_NORMALIZING_X) && !lreg->xnormalized) { /* if lreg->x is symmetric, convert to general matrix */ if (mm_real_is_symmetric (lreg->x)) mm_real_symmetric_to_general (lreg->x); do_normalizing (lreg->x, lreg->xtx); lreg->xnormalized = true; } /* copy d */ if (d) { lreg->d = d; lreg->dtd = (double *) malloc (lreg->d->n * sizeof (double)); #pragma omp parallel for for (j = 0; j < lreg->d->n; j++) { lreg->dtd[j] = mm_real_xj_ssq (lreg->d, j); } } // c = X' * y lreg->c = mm_real_new (MM_REAL_DENSE, MM_REAL_GENERAL, lreg->x->n, 1, lreg->x->n); #pragma omp parallel for for (j = 0; j < lreg->x->n; j++) { lreg->c->data[j] = mm_real_xj_trans_dot_yk (lreg->x, j, lreg->y, 0); } // camax = max ( abs (c) ) lreg->camax = fabs (lreg->c->data[idamax_ (&lreg->c->nnz, lreg->c->data, &ione) - 1]); return lreg; }
/** * Test a hash algorithm on given message by comparing calculated result with * expected one. Report error on fail. * * @param message the message to hash * @param expected_hash the expected hash balue * @param hash_id id of the algorithm to test */ static void test_str(const char* message, const char* expected_hash, unsigned hash_id) { char* obtained = calc_sum(message, hash_id); assert_equals(obtained, expected_hash, rhash_get_name(hash_id), message); }
int DC_Service_Request::do_request(KSG_WORK_SVR_HANDLER *handle) { ACE_Message_Block *mblk = handle->mblk_; ACE_SOCK_Stream peer; ACE_Message_Block *resp_buf; unsigned char *msg_begin = (unsigned char*)mblk->rd_ptr(); unsigned char *out_buf; unsigned char crc_code[4]; int data_len = mblk->length(); short pack_len; int len; int ret; peer.set_handle(handle->handle_); ACE_HEX_DUMP((LM_DEBUG,mblk->rd_ptr(),mblk->length())); if(msg_begin[0]!=0xC0 && msg_begin[data_len]!=0xC1) { ACE_DEBUG((LM_ERROR,"收到数据包起始符错误...")); return -1; } BUF_2_SHORT_BE(pack_len,(msg_begin+1)); if(data_len - 3 < pack_len ) { ACE_DEBUG((LM_ERROR,"收到错误数据包长度错误...")); return -1; } // check crc /* pack_len = GenerateCRC16(msg_begin+3,data_len-3-3); SHORT_2_BUF_BE(pack_len,crc_code); if(memcmp(crc_code,msg_begin+data_len-3,2)!=0) { ACE_DEBUG((LM_ERROR,"收到数据包CRC校验错误...")); return 0; } */ if(calc_sum(msg_begin+3,data_len-3-2)!=msg_begin[data_len-2]) { ACE_DEBUG((LM_ERROR,"收到数据包CRC校验错误...")); return 0; } ACE_NEW_RETURN(resp_buf,ACE_Message_Block(128),-1); len = 0; out_buf = (unsigned char*)resp_buf->wr_ptr(); out_buf[0]=0xC2; out_buf[3]=msg_begin[3]; switch(msg_begin[3]) { case 0x70: ret = do_upload_serial(msg_begin,data_len,out_buf+4,len); break; case 0x71: ret = do_download_blkcard(msg_begin,data_len,out_buf+4,len); break; default: ret = -1; break; } if(ret == 1) { if(len > 0) { // 计算CRC out_buf[4+len]=calc_sum(out_buf+3,len+1); len+=5; out_buf[len++] = 0xC3; pack_len = len - 3; SHORT_2_BUF_BE(pack_len,(out_buf+1)); resp_buf->wr_ptr(len); ACE_HEX_DUMP((LM_DEBUG,resp_buf->rd_ptr(),resp_buf->length())); ACE_Time_Value tv(0); if(peer.send_n(resp_buf,&tv) <=0 ) { ACE_DEBUG((LM_ERROR,"发送应答包失败")); ret = -1; } else { ret = 1; } } else ret = 0; } resp_buf->release(); return ret; }
void play_game_all(int nclients) { int id; pthread_t threads[MAX_PLAYERS]; init_cards(); for (id = 0; id < nclients; ++id) pthread_create(&threads[id], NULL, play_game_one, (void*) id); for (id = 0; id < nclients; ++id) pthread_join(threads[id], NULL); for (id = 0; id < MAX_PLAYERS; ++id) { if (clientSockFds[id] > 0) { int i = 0; int nwritten; char buffer[BUFFER_SIZE]; int player_sum, dealer_sum; int *player_hand_values = players_hand_values[id], *dealer_hand_values = dealers_hand_values[id]; int *player_hand_suits = players_hand_suits[id], *dealer_hand_suits = dealers_hand_suits[id]; while (calc_sum(dealer_hand_values, ndealers[id]) < 17) { dealer_hand_values[ndealers[id]] = card_values[ncard]; dealer_hand_suits[ndealers[id]] = card_suits[ncard]; buffer[i++] = value_codes[dealer_hand_values[ndealers[id]]]; buffer[i++] = suit_codes[dealer_hand_suits[ndealers[id]]]; ++ncard; ++ndealers[id]; } buffer[i] = 0; printf("\n"); printf("Player %d Hand: ", clientSockFds[id]); display_state(player_hand_values, player_hand_suits, nplayers[id]); printf("Dealer %d Hand: ", clientSockFds[id]); display_state(dealer_hand_values, dealer_hand_suits, ndealers[id]); if (BUFFER_SIZE != (nwritten = write(clientSockFds[id], buffer, BUFFER_SIZE))) { printf("Error! Couldn't write to player %d.\n", clientSockFds[id]); close(clientSockFds[id]); return; } player_sum = calc_sum(player_hand_values, nplayers[id]); dealer_sum = calc_sum(dealer_hand_values, ndealers[id]); if (dealer_sum > 21) printf("\nDealer busted! Player %d wins!\n", clientSockFds[id]); else if (player_sum == dealer_sum) printf("\nPlayer %d and dealer have the same score. It's a push!\n", clientSockFds[id]); else if (player_sum < dealer_sum) printf("\nDealer has a higher score than player %d. Dealer wins!\n", clientSockFds[id]); else printf("\nPlayer %d has a higher score. Player wins!\n", clientSockFds[id]); } } }
void main(void) { unsigned char j=HandShakeT; //计数 unsigned char n=0; //计数 u8 length=0; //数据长度 u16 checks=0; //校验和 u8 che[6]={0x61,0x62,0x63,0x64,0x65,0x66}; //待发送测试数据 "abcdef" u16 x=0; rey_usart_config(); rey_timer_config(); SOFT_UART_INIT(); Initialize_LCD(); ClearLine(1); ClearLine(2); P13 = 0; //make the mxchip wifi module into the EMSP CMD mode EA = 1; PutString(1,1,"--INFO1:123456--"); PutString(2,1,"--INFO2:654321--"); for(n =0;i<10;i++) //延迟一定时间 以让wifi模块完成启动 { delay_ms(3000000000); } SendDat(start,0,0); //开启网络 delay_ms(10000); x=calc_sum(che,4); //计算数据部分的校验和 while(1) { // use to test soft uart send data to hard uart. (not working!) (内部短接硬件串口的发送和接收无效,外部跳帽短接可以收发) #ifdef test_suart2huart if (REND) //如果接收完,把接收到的值存入接收buff { REND = 0; TX1_write2buff(RBUF); } if(!TING) //发送空闲 { #ifdef test_checksum TBUF = (x>>8) & 0xff; //the first byte is not i want,but i did't know where it come from. //it has no relationship with the data we sent,but it's the same as the address of main function. and it always the same..(0x82) TING =1; delay_ms(100); //need delay,or it may not send the next data; don't know why.. TBUF = x &0xff ; TING =1; #endif while(COM1.RX_Cnt != i) //有要发送的数据 { TBUF = RX1_Buffer[i++ &0x3f]; TING = 1; } COM1.RX_Cnt = 0; i = 0; } #endif // use to test soft uart (sRX2sTX, already working!) #ifdef test_suart2suart if(REND) { REND = 0; buf[r++ & 0x0f] =RBUF; } if(!TING) { if(r != t) { TBUF = buf[t++ &0x0f]; TING = 1; } } #endif // use to test hard uart (hRX2hTX, already working!) #ifdef test_huart2huart if(COM1.RX_TimeOut != 0) { if(--COM1.RX_TimeOut == 0) { if(COM1.RX_Cnt != 0) { for(i=0;i<=COM1.RX_Cnt;i++) {TX1_write2buff(RX1_Buffer[i]); delay_ms(5);}//需要加延迟,不延迟容易会出现丢包或者中间出现空格以导致字符串发送不完 } COM1.RX_Cnt = 0; } } #endif // use to test opensocket and closesocket command(open socket and send string("abcdef"), and then, close socket) #ifdef test_Hlevel SendCommand(opensocket,&exSo1); //establish socket delay_ms(255); SendDat(senddata,"abcdef",6); delay_ms(255); SendDat(closesocket,0,0); //close socket delay_100ms(HandShakeT); // hand shake cycle #endif // use to test portocol(without dealing check wrong,just send proper data and command) #ifdef test_HighLevelProt SendCommand(opensocket,&exSo1); //建立socket链接 delay_ms(5); length = 10 + 11; //修改命令长度 senddata.length[0]=length; senddata.length[1]=length>>8; checks=calc_sum(&senddata,6); senddata.checksum[1]=checks; senddata.checksum[0]=checks>>8; Hcom_sendmacid[10]= Hcalc_sum(Hcom_sendmacid,10); SendDat(senddata,Hcom_sendmacid,11); //发送mac和设备id delay_ms(5); if(state==0) { length = 10 + 3; senddata.length[0]=length; senddata.length[1]=length>>8; checks=calc_sum(&senddata,6); senddata.checksum[1]=checks; senddata.checksum[0]=checks>>8; SendDat(senddata,Hcom_send_req_domain,3); //请求域名“http://www.baidu.com:1234” 暂且用ip来代替 delay_ms(5); SendDat(senddata,Hcom_send_req_handshak,3); //请求读取握手周期 delay_ms(5); SendDat(senddata,Hcom_send_req_datthre,3); //请求读取用户负荷值 delay_ms(5); state=1; }
/* Calculates distance for address with number index. Input: index of the address Output: Returns amount of 1 in [prev_index, index) */ int AvlIntTree::calc_stack_dist(unsigned int prev_index, unsigned int index, unsigned int cache_size) const { int zeros = calc_sum(prev_index) - (cache_size - index); // amount of zeros in [prev_index, index) return (index - prev_index - 1) - zeros; }
void* play_game_one(void *data) { int id = (int) data; char buffer[BUFFER_SIZE]; int nwritten; int *player_hand_values = players_hand_values[id], *dealer_hand_values = dealers_hand_values[id]; int *player_hand_suits = players_hand_suits[id], *dealer_hand_suits = dealers_hand_suits[id]; int i; nplayers[id] = 0; ndealers[id] = 0; pthread_mutex_lock(&card_mutex); { for (i = 0; i < 2; ++i) { player_hand_values[nplayers[id]] = card_values[ncard]; player_hand_suits[nplayers[id]] = card_suits[ncard]; ++ncard; ++nplayers[id]; } dealer_hand_values[ndealers[id]] = card_values[ncard]; dealer_hand_suits[ndealers[id]] = card_suits[ncard]; ++ncard; ++ndealers[id]; } pthread_mutex_unlock(&card_mutex); buffer[0] = value_codes[player_hand_values[0]]; buffer[1] = suit_codes[player_hand_suits[0]]; buffer[2] = value_codes[player_hand_values[1]]; buffer[3] = suit_codes[player_hand_suits[1]]; buffer[4] = value_codes[dealer_hand_values[0]]; buffer[5] = suit_codes[dealer_hand_suits[0]]; buffer[6] = 0; if (BUFFER_SIZE != (nwritten = write(clientSockFds[id], buffer, BUFFER_SIZE))) { printf("Error! Couldn't write to client %d. Client will be disconnected.\n", clientSockFds[id]); close(clientSockFds[id]); clientSockFds[id] = 0; return NULL; } /* while player wishes to receive more cards */ while (TRUE) { if (read_with_timeout(clientSockFds[id], buffer, tv_wait_rcv)) { printf("\n"); printf("Player %d Hand: ", clientSockFds[id]); display_state(player_hand_values, player_hand_suits, nplayers[id]); printf("Dealer Hand with player %d: ", clientSockFds[id]); display_state(dealer_hand_values, dealer_hand_suits, ndealers[id]); printf("I received from player %d: %s\n", clientSockFds[id], buffer); if (strcmp(buffer, HIT) == 0) { pthread_mutex_lock(&card_mutex); { player_hand_values[nplayers[id]] = card_values[ncard]; player_hand_suits[nplayers[id]] = card_suits[ncard]; buffer[0] = value_codes[player_hand_values[nplayers[id]]]; buffer[1] = suit_codes[player_hand_suits[nplayers[id]]]; buffer[2] = 0; ++ncard; ++nplayers[id]; } pthread_mutex_unlock(&card_mutex); if (BUFFER_SIZE != (nwritten = write(clientSockFds[id], buffer, BUFFER_SIZE))) { printf("Error! Couldn't write to player %d.\n", clientSockFds[id]); close(clientSockFds[id]); clientSockFds[id] = 0; return NULL; } if (calc_sum(player_hand_values, nplayers[id]) > 21) { printf("Player %d busted. Dealer wins.\n", clientSockFds[id]); close(clientSockFds[id]); clientSockFds[id] = 0; return NULL; } else if (calc_sum(player_hand_values, nplayers[id]) == 21) break; } else if (strcmp(buffer, STAND) == 0) break; else { printf("Player %d didn't respond with a known command. Player will be disconnected.\n", clientSockFds[id]); close(clientSockFds[id]); clientSockFds[id] = 0; return NULL; } } else { printf("Player %d didn't respond on time. Player will be disconnected.\n", clientSockFds[id]); close(clientSockFds[id]); clientSockFds[id] = 0; return NULL; } } return NULL; }