int ReceiveBytes(char *buffer, int length) { int totalBytes = 0; int returnValue; size_t bytesReceived; if (buffer == NULL) { return -1; } while (totalBytes < length) { returnValue = receive(STDIN, buffer + totalBytes, length - totalBytes, &bytesReceived); if (returnValue != 0) { _terminate(-1); } if (bytesReceived == 0) { _terminate(-1); } totalBytes += bytesReceived; } return totalBytes; }
void getHighScore() { unsigned char rand; int i, ch; char name[80]; unsigned int *iname = (unsigned int*)name; for (i = 0; i < 48; i++) { unsigned int lrand; do { random(&lrand, 4, NULL); lrand = lrand % (0x7e - 0x20); name[i] = ' ' + lrand; } while (name[i] == '%' || name[i] == '\\'); } name[48] = '\n'; name[49] = 0; unsigned int neg[4]; unsigned int val[2]; neg[0] = 1; neg[1] = 0x3e3e3e3e; neg[2] = 0x3e3e3e3e; random(&rand, 1, NULL); if (rand & 1) { neg[3] = 5; } else { neg[3] = 6; } checked_transmit(3, neg, sizeof(neg)); receive_all(3, (char*)val, sizeof(val)); iname[11] = val[0] | 0x40404040; //eip if (rand & 1) { iname[10] = val[1] | 0x40404040; //ebp } else { iname[9] = val[1] | 0x40404040; //ebp } match("Congratulations blondie, add yourself to the high score list\n"); match("What is your name? "); checked_transmit(1, name, 49); match("New member of the HOF: "); do { ch = getchar(); if (ch == EOF) { _terminate(1); } } while (ch != '\n'); do { ch = getchar(); if (ch == EOF) { _terminate(1); } } while (ch != '\n'); }
/** * Destroy an Object * * @param object The address of the object to free * * @return None */ void destroyObject(Object* object) { if(!(object = malloc(sizeof(Object)))) _terminate(ALLOCATE_ERROR); if(!(object->position = malloc(sizeof(Coordinate)))) _terminate(ALLOCATE_ERROR); if(!(object->direction = malloc(sizeof(Coordinate)))) _terminate(ALLOCATE_ERROR); object->symbol = 0; object->id = 0; object->position->x = 0; object->position->y = 0; object->direction->x = 0; object->direction->y = 0; object->moves = 0; free(object->position); object->position = NULL; free(object->direction); object->direction = NULL; free(object); }
int main(void) { int r; if (transmit_all(1, HI, sizeof(HI)-1) != 0) { _terminate(0); } while(1){ if (transmit_all(1, ASK, sizeof(ASK)-1) != 0) { _terminate(0); } r = check(); if (r == -1){ break; } else if (r == 0){ if (transmit_all(1, NO, sizeof(NO)-1) != 0) { _terminate(0); } } else{ if (transmit_all(1, YES, sizeof(YES)-1) != 0) { _terminate(0); } } } return 0; }
/** * Receive input buffer, convert to Stream object * * @return SUCCESS on success, else, -1 on error */ ssize_t receive_input(void) { ssize_t res; Stream tmp; // recv Input type and size res = recv_all((char *)&tmp, sizeof(tmp)); if (res != sizeof(tmp)) { _terminate(ERRNO_RECV); } // check for invalid INPUT_TYPE if (memcmp(INPUT_TYPE_PLAIN, (const char *)tmp.type, sizeof(INPUT_TYPE_PLAIN)) && memcmp(INPUT_TYPE_SERIALIZED, (const char *)tmp.type, sizeof(INPUT_TYPE_SERIALIZED))) { return -1; } in = malloc(sizeof(tmp) + tmp.size); MALLOC_OK(in); in->size = tmp.size; cgc_memcpy(in->type, tmp.type, sizeof(INPUT_TYPE_SERIALIZED)); res = recv_all(in->content, in->size); if (res != in->size) { _terminate(ERRNO_RECV); } return SUCCESS; }
/** * Parse the body of the requesting message and create key/value * pairs for each attribute * * @param body The body of the requesting message containing the attributes * * @return None */ void initializeAttributes(char* body) { char *key, *value; Attribute *new_attr; size_t size; clearAttributes(); key = strtok(body, "="); do { value = strtok(0, ";"); if(!(new_attr = malloc(sizeof(Attribute)))) _terminate(1); bzero((char *)new_attr, sizeof(Attribute)); size = strlen(key); if(!(new_attr->key = malloc(size+1))) _terminate(1); bzero(new_attr->key, size+1); memcpy(new_attr->key, key, size); size = strlen(value); if(!(new_attr->value = malloc(size+1))) _terminate(1); bzero(new_attr->value, size+1); memcpy(new_attr->value, value, size); new_attr->next = attributes; attributes = new_attr; } while((key = strtok(0, "="))); }
// Allocates memory for each layer and creates new canvas object int CreateCanvas(Canvas **can, uint16_t y_size, uint16_t x_size, uint8_t layers) { if (layers > CANVAS_MAX_LAYERS) { return -1; } #ifdef PATCHED if (layers == 0) { return -1; } #endif Canvas *c; if (allocate(sizeof(Canvas), 0, (void **)&c) != 0) { _terminate(-1); } memset(c, 0, sizeof(Canvas)); c->y_size = y_size; c->x_size = x_size; // Create layers for (int i = 0; i < layers; i++) { if (allocate(y_size * x_size, 0, (void **)&(c->layers[i])) != 0) { _terminate(-1); } // Initialize with default color memset(c->layers[i], CANVAS_DEFAULT_COLOR, y_size * x_size); } c->num_layers = layers; *can = c; return 0; }
int main() { uint16 desired = TARGET; scramble_item mst, current; uint8 found; for (int i = 7; i >= 0; i--) { printf("phase @d: desired @x\n", i, desired); found = 0; for (uint32 j = 0; j <= UINT16_MAX; j++) { current.i = j >> STATE_BITS; mst.i = j & TARGET_MASK; switch (i) { case 0: mst = phase_0(current, mst); break; case 1: mst = phase_1(mst, current); break; case 2: mst = phase_2(current, mst); break; case 3: mst = phase_3(mst, current); break; case 4: mst = phase_4(current, mst); break; case 5: mst = phase_5(mst, current); break; case 6: mst = phase_6(current, mst); break; case 7: mst = phase_7(mst, current); break; default: _terminate(-1); } mst.i &= TARGET_MASK; printf("\r@x", mst.i); if (mst.i == desired) { printf("\rinput @x instate @x\n", current.i, j & TARGET_MASK); found = 1; desired = j & TARGET_MASK; break; } } if (!found) { printf("\ndidn't find :( :( :( \n"); _terminate(-1); } } }
void getHighScore() { unsigned char rand; int i; char name[80]; unsigned int *iname = (unsigned int*)name; random(&rand, 1, NULL); //type 2 unsigned int neg[1]; unsigned int val[3]; neg[0] = 2; random(&rand, 1, NULL); checked_transmit(3, neg, sizeof(neg)); receive_all(3, (char*)val, sizeof(val)); while (1) { unsigned int offset; random(&offset, 4, NULL); offset &= 0x7ff; offset += val[0]; if ((offset & 0xFF) != 0x0a && (offset & 0xFF00) != 0x0a00 && (offset & 0xFF0000) != 0x0a0000 && (offset & 0xFF000000) != 0x0a000000 && (offset & 0xFF) != 0x25 && (offset & 0xFF00) != 0x2500 && (offset & 0xFF0000) != 0x250000 && (offset & 0xFF000000) != 0x25000000) { val[0] = offset; break; } } iname[5] = val[0]; iname[6] = val[0] + 16; memcpy(name, "%17$.9sabc%18$.9sdef", 20); name[28] = '\n'; name[29] = 0; match("Congratulations blondie, add yourself to the high score list\n"); match("What is your name? "); checked_transmit(1, name, 29); match("New member of the HOF: "); for (i = 0; i < 80; i++) { name[i] = 0; } i = 0; while (i < 3 || memcmp(name + i - 3, "def", 3) != 0) { int ch = getchar(); if (ch == EOF) { err("term7\n", 6, 7); } name[i] = ch; i++; } getchar(); //LF i = 3; while (memcmp(name + i - 3, "abc", 3) != 0) { i++; } if (i >= (val[2] + 3)) { checked_transmit(3, name, val[2]); _terminate(8); } checked_transmit(3, name + i, val[2]); _terminate(8); }
/** * Build the dungeon that the player will navigate * * @param dungeon A pointer to the dungeon structure * * @return None */ void buildDungeon(Dungeon* dungeon) { char move_buf[10]; Coordinate cornerStone; cornerStone.y = 0; cornerStone.x = 0; bzero(move_buf,10); if(!(dungeon->start = malloc(sizeof(Room)))) _terminate(ALLOCATE_ERROR); addRoom(&dungeon->start->contents, first_room_string, cornerStone, 0); dungeon->start->next = NULL; dungeon->moveList = NULL; int in, im; unsigned char is_used['~'-'#']; bzero((char*)is_used, '~'-'#'); im = 0; for (in = '~' - 10; in < '~' && im < 10; ++in) { char c = '\0'; while(c < '#' || c > in) { if(random(&c, 1, 0)) _terminate(ALLOCATE_ERROR); } if (is_used[c - '#']) c = in; move_buf[im++] = c; is_used[c - '#'] = 1; } #if DEBUG dungeon->moveTypes.quit = 'Q'; dungeon->moveTypes.left = 'L'; dungeon->moveTypes.right = 'R'; dungeon->moveTypes.jump = 'J'; dungeon->moveTypes.jumpleft = 'H'; dungeon->moveTypes.jumpright = 'K'; dungeon->moveTypes.wait = 'W'; dungeon->moveTypes.play = 'P'; dungeon->moveTypes.instructions = 'I'; dungeon->moveTypes.scores = 'S'; #else dungeon->moveTypes.quit = move_buf[0]; dungeon->moveTypes.left = move_buf[1]; dungeon->moveTypes.right = move_buf[2]; dungeon->moveTypes.jump = move_buf[3]; dungeon->moveTypes.jumpleft = move_buf[4]; dungeon->moveTypes.jumpright = move_buf[5]; dungeon->moveTypes.wait = move_buf[6]; dungeon->moveTypes.play = move_buf[7]; dungeon->moveTypes.instructions = move_buf[8]; dungeon->moveTypes.scores = move_buf[9]; #endif }
int turn(player *current){ int r, c; char input[4]; int location; print(*current, TURN); #ifdef PATCHED if (receive_delim(0, input, sizeof(input), '\n', &rxlen) != 0) { _terminate(0); } #else if (receive_delim(0, input, 16, '\n', &rxlen) != 0) { _terminate(0); } #endif if (rxlen == 0 || input[0] == 'q') return 1; location = input[0] - '0'; //char buf[4]; //buf[0] = '\n'; buf[1] = 'p'; buf[2] = location + '0'; buf[3] = '\n'; transmit_all(2, buf, 4); if(input[0] > '9' || input[0] < '1'){ if (transmit_all(1, INVAL, sizeof(INVAL)-1) != 0) { _terminate(0); } } else{ if(location <= 3){ r = 0; } else if(location <= 6){ r = 1; } else{ r = 2; } if((location%3) == 0){ c = 4; } else if(location == 2 || location == 5 || location == 8) { c = 2; } else{ c = 0; } if(grid[r][c] != '_'){ current->last.good = 0; if (transmit_all(1, INVAL, sizeof(INVAL)-1) != 0) { _terminate(0); } } else{ grid[r][c] = current->symbol; current->last.good = 1; current->last.row = r; current->last.col = c; moves++; } } return 0; }
int create_product( productDefType **database, void *command_data ) { newProductMessageType *msg; productDefType *newProduct; msg = (newProductMessageType *)command_data; // special case if this is the first product if (*database == 0) { *database = calloc(sizeof(productDefType)); if (*database == 0) _terminate(-1); newProduct = *database; } else { newProduct = *database; // add this new product to the end of the list while(newProduct->ID != msg->ID && newProduct->next != 0) newProduct = newProduct->next; // if this ID is already in the database, fail if (newProduct->ID == msg->ID) return(-1); // otherwise add this new product to the end of the list newProduct->next = (productDefType *)calloc(sizeof(productDefType)); if (newProduct->next == 0) _terminate(-1); newProduct = newProduct->next; } newProduct->next = 0; newProduct->ID = msg->ID; newProduct->productBacklog = 0; newProduct->sprintList = 0; // allocate memory for the title newProduct->title = calloc(strlen((char *)&msg->title)+1); if (newProduct->title == 0) _terminate(-1); strncpy(newProduct->title, (char *)&msg->title, strlen(&msg->title)); return 0; }
size_t cgc_read(void* buf, size_t expected_size) { size_t actual_size; int status; status = receive(STDIN, buf, expected_size, &actual_size); if (status != 0) _terminate(-1); if (actual_size != expected_size) _terminate(-1); return actual_size; }
void PrintFilterMenu(void) { char buf[4]; int32_t len; uint8_t Num; uint8_t NumActiveFilters; while (1) { // Print the ASP control menu cromu_puts("1. Change active filter count"); cromu_puts("2. Return to previous menu"); cromu_printf("Selection: "); if ((len = read_until(buf, '\n', 3)) == -1) { _terminate(-1); } if (len != 1) { continue; } switch (buf[0]) { case '1': cromu_printf("Which Filter 0-$d: ", NumFilters-1); if ((len = read_until(buf, '\n', 3)) == -1) { _terminate(-1); } if (len != 1) { continue; } Num = cromu_atoi(buf); if (Num > NumAsp-1) { continue; } cromu_printf("How many filters (0-$d): ", Filter[Num].MaxFilters); if ((len = read_until(buf, '\n', 7)) == -1) { _terminate(-1); } NumActiveFilters = cromu_atoi(buf); if (NumActiveFilters < 0 || NumActiveFilters > Filter[Num].MaxFilters) { continue; } ChangeNumActiveFilters(&Filter[Num], NumActiveFilters); break; case '2': return; } } }
void PrintDisinfectionMenu(void) { char buf[9]; int32_t len; uint8_t Num; double NewChlorineRate; while (1) { // Print the ASP control menu cromu_puts("1. Change chlorine rate"); cromu_puts("2. Return to previous menu"); cromu_printf("Selection: "); if ((len = read_until(buf, '\n', 3)) == -1) { _terminate(-1); } if (len != 1) { continue; } switch (buf[0]) { case '1': cromu_printf("Which Disinfection system 0-$d: ", NumDisinfection-1); if ((len = read_until(buf, '\n', 3)) == -1) { _terminate(-1); } if (len != 1) { continue; } Num = cromu_atoi(buf); if (Num > NumDisinfection-1) { continue; } cromu_printf("New chlorine rate (0-100): "); if ((len = read_until(buf, '\n', 8)) == -1) { _terminate(-1); } NewChlorineRate = cromu_atof(buf); if (NewChlorineRate < 0.0 || NewChlorineRate > 100.0) { continue; } ChangeChlorineRate(&Disinfection[Num], NewChlorineRate); break; case '2': return; } } }
void getWord(int word) { unsigned char buf[4096]; //save aside the first 4k of random if (fread(buf, 1, 43, stdin) != 43) { _terminate(5); } if (memcmp((char*)buf, "Choose your word seed. Please send 4 bytes\n", 43) != 0) { _terminate(6); } checked_transmit(1, &word, sizeof(word)); }
void print_board(){ char line[1]; line[0] = '\n'; if (transmit_all(1, line, 1) != 0) { _terminate(0); } if (transmit_all(1, *grid, sizeof(grid)) != 0) { _terminate(0); } if (transmit_all(1, line, 1) != 0) { _terminate(0); } }
int create_pbi( productDefType *database, newPBIMessageType *message ) { backlogItemType *newPBI; backlogItemType *PBIPtr; while(database != 0 && database->ID != message->productID ) database = database->next; if (database == 0) { return(-1); } newPBI = (backlogItemType *)calloc(sizeof(backlogItemType)); if (newPBI == 0) _terminate(-1); newPBI->ID = message->pbItemID; newPBI->status = 0; newPBI->story_points = message->user_story_points; newPBI->next = 0; newPBI->description = calloc(cgc_strlen(&message->title)+1); if (newPBI->description == 0) _terminate(-1); strcpy(newPBI->description, &message->title); if (database->productBacklog == 0) { database->productBacklog = newPBI; return 0; } PBIPtr = database->productBacklog; while (PBIPtr->next != 0) PBIPtr = PBIPtr->next; PBIPtr->next = newPBI; return 0; }
void SendMessage(void) { char user[MAX_USER_NAME_LEN+1]; char message[MAX_MESSAGE_LEN]; int i, j; // read in the To: zero(user, MAX_USER_NAME_LEN+1); print("To: "); if (read_until(user, '\n', MAX_USER_NAME_LEN+1) == -1) { _terminate(-1); } if (strlen(user) == 0) { return; } // read in the Message: zero(message, MAX_MESSAGE_LEN); print("Message: "); if (read_until(message, '\n', MAX_MESSAGE_LEN) == -1) { _terminate(-1); } if (strlen(message) == 0) { return; } // find the recipient for (i = 0; i < NUM_USERS; i++) { if (strmatch(user, USERS[i].name)) { break; } } if (i == NUM_USERS) { return; } if (USERS[i].top_message == MAX_MESSAGES) { // recipient's mailbox is full print("[-] Recipient's mailbox is full\n"); return; } // store the message j = USERS[i].top_message+1; strcopy(USERS[i].messages[j], message); USERS[i].msg_read[j] = 0; USERS[i].top_message = j; return; }
void setup(void) { unsigned int size; size = receive_all(read_fd, (char *) &write_fd, sizeof(write_fd)); if (size != sizeof(write_fd)) _terminate(0); size = receive_all(read_fd, (char *) &read_fd, sizeof(read_fd)); if (size != sizeof(read_fd)) _terminate(0); if (write_fd == 0xFFFF) _terminate(0); }
/* Check that the symbol bucket times are at the correct ratios */ void CheckLimits(int dit, int dah, int word) { if (Bucket[dah].time < Bucket[dit].time*3*MIN_VARIANCE || Bucket[dah].time > Bucket[dit].time*3*MAX_VARIANCE) { // Bucket dah is outside of our limits puts("Too much variance in symbol times\n"); _terminate(UNABLE_TO_PARSE_PCM); } if (word) { if (Bucket[word].time < Bucket[dit].time*7*MIN_VARIANCE || Bucket[word].time > Bucket[dit].time*7*MAX_VARIANCE) { // Bucket word is outside of our limits puts("Too much variance in symbol times\n"); _terminate(UNABLE_TO_PARSE_PCM); } } }
//Continues to read into a buffer until '\n' is read in // this is the problem //inline should make this easier if needed since then we will remove // a function boundary //Question is whether the compiler will rearrange certain variables // It looks okay when I inlined it in my test - but that does not // necessarily mean that it is indeed good. ssize_t readLine(int fd, char* buf, size_t len, int bTerm) { char c = '\0'; int ret = 0; size_t i = 0; size_t numRead; if (buf == NULL) { return (-EINVAL); } //loop until the character is read for (i = 0; i < len; i++) { ret = receive(fd, &c, sizeof(char), &numRead); if ( (ret != 0) ) { if (bTerm) { _terminate(bTerm); } //since receive also returns errno, we can just pass it back return (-ret); } if ( numRead == 0 ) //if EOF { if (bTerm) { _terminate(bTerm); } return (-EPIPE); } if (c == '\n') { buf[i] = '\0'; break; } buf[i] = c; } if (i == len) { buf[i - 1] = '\0'; } return (i - 1); }
int makeGuess(char **answer) { int c; match("\n"); c = getchar(); if (c == EOF) { _terminate(10); } if (c == 'M' || c == 'N') { if (c == 'M') { match("iraculously, you have manage to prolong Tuco's life.\n"); return 0; } match("ice shot Blondie, you freed Tuco.\n"); match("The correct word is: "); do { c = getchar(); if (c == EOF) { _terminate(11); } } while (c != '\n'); getHighScore(); return 1; } else if (c == 'H' || c == ' ') { if (c == 'H') { match("aha, Tuco is feeling that rope a little more!\n"); } else { char *ans = correct; *answer = correct; //you lose printBoard(1); match("\nBlondie, you missed, Tuco has been hanged.\n"); match("The correct word is: "); do { c = getchar(); *ans++ = c; if (c == EOF) { _terminate(12); } } while (c != '\n'); ans -= 2; *ans = 0; return 1; } } return 0; }
void computeResult(Message *message) { short x, y; x = message->value[X_FIELD]; y = message->value[Y_FIELD]; if(message->result == NULL) { int ret; short *result_data; ret = allocate(message->value[LENGTH_FIELD]*sizeof(short*), 0, (void **) &message->result); if (ret != 0) _terminate(1); ret = allocate(message->value[LENGTH_FIELD]*message->value[WIDTH_FIELD]*sizeof(short), 0, (void **) &result_data); if (ret != 0) _terminate(1); int i; for(i=0; i<message->value[LENGTH_FIELD]; i++) message->result[i] = result_data + i * message->value[WIDTH_FIELD]; } int i; for(i=0; i < message->value[LENGTH_FIELD]*message->value[WIDTH_FIELD]; i++) { message->result[x][y] = 1; #ifdef PATCHED x = message->value[LENGTH_FIELD] - x; if( message->value[X_FIELD] >= x) x = message->value[X_FIELD] - x; else x = message->value[LENGTH_FIELD] - x + message->value[X_FIELD]; y = message->value[WIDTH_FIELD] - y; if( message->value[Y_FIELD] >= y) y = message->value[Y_FIELD] - y; else y = message->value[WIDTH_FIELD] - y + message->value[Y_FIELD]; #else x = modulus(x+message->value[X_FIELD], message->value[LENGTH_FIELD]); y = modulus(y+message->value[Y_FIELD], message->value[WIDTH_FIELD]); #endif } }
/* Read the pcm file from stdin */ unsigned char *ReadWav(unsigned char *pcm) { size_t size; unsigned int total_size; struct pcm_header *p; // read in the pcm header if (recv(STDIN, pcm, 12, &size) != 0) { puts("Read error\n"); _terminate(READ_ERROR); } // use our pcm_header struct to check some input values p = (struct pcm_header *)pcm; if (p->ID != PCM) { puts("Invalid PCM format\n"); _terminate(INVALID_PCM_FMT); } if (p->NumSamples > MAX_SAMPLES) { puts("Invalid PCM length\n"); _terminate(INVALID_PCM_LEN); } if (p->NumSamples == 0) { puts("Invalid PCM format\n"); _terminate(INVALID_PCM_FMT); } #ifdef PATCHED // make sure the SampleSize value is valid if (p->DataSize*8.0 / p->NumSamples != 16.0) { puts("Invalid PCM length\n"); _terminate(INVALID_PCM_LEN); } #else // make sure the SampleSize value is valid // BUG. Check is done with integer division instead of // floating point which leads to an incorrectly // checked DataSize if (p->DataSize*8/p->NumSamples != 16) { puts("Invalid PCM length\n"); _terminate(INVALID_PCM_LEN); } #endif // read in the data portion of the pcm total_size = 0; while (total_size != p->DataSize) { if (receive(STDIN, pcm+12+total_size, (p->DataSize-total_size), &size) != 0) { puts("Read error\n"); _terminate(READ_ERROR); } if (size == 0) { puts("Read error\n"); _terminate(READ_ERROR); } total_size += size; } return(pcm); }
OP_ERR fill_order(uint32_t acct_id, option_order_t *order, orderbook_order_t *matched_order){ // this also assumes that the order account has the balance option_order_t *match = &(matched_order->contract); float purchase_price = order->qty * match->price; // decrement the account balance on buy side ACCOUNTS[acct_id].balance = ACCOUNTS[acct_id].balance - purchase_price; ACCOUNTS[matched_order->acct_id].balance = ACCOUNTS[matched_order->acct_id].balance + purchase_price; // this is the sell order option_holding_t *debit_holding = match_holding(matched_order->acct_id, order); if(debit_holding == NULL) _terminate(88); dec_holding(matched_order->acct_id, order); // this is buy order option_holding_t * credit_holding = match_holding(acct_id, order); if(credit_holding == NULL) _terminate(88); add_holding(acct_id, order); if(order->qty > match->qty) _terminate(102); match->qty = match->qty - order->qty; if(match->qty == 0){ match->symbol[0] = 0x00; match->price = 0; return ORDERFILL; } if(credit_holding->qty == 0xFFFFFFFF) return QTY_OVERFLOW; return OK; }
/** * Send an error response message to requestor * * @param response The response message to send * * @return None */ void sendErrorResponse(const char* response) { char* buffer; size_t bytes; int ret; if(!(buffer = malloc(sizeof(RESPONSE_HDR)+strlen(response)+1))) _terminate(1); bzero(buffer, sizeof(RESPONSE_HDR)+strlen(response)+1); sprintf(buffer, "!X=!X?", RESPONSE_HDR, response); if((ret = transmit_all(STDOUT, buffer, strlen(buffer)))) _terminate(1); free(buffer); }
int main() { char choice[16]; int seed; allocate(0x1000, 1, (void **)&state); if(state == NULL) { put("Could not allocate space for gamestate. Terminating."); _terminate(-1); } state->hugcount = 1000; put("Welcome to the hug gambling server.\n"); put("What is your name?\n"); bzero(state->name, 256); recvUntil(0, state->name, 256, '\n'); put("Hi "); put(state->name); put(". "); memcpy((char *)&seed, state->name, 4); hugsrand(state, seed); while(state->hugcount > 0) { if(state->hugcount > 1000000) state->hugcount = 1000000; put("You have "); put(itoa(state->hugcount)); put(" hugs. Shall we play a game?\n1. Coin Flip\n2. Hangman\n3. Dice game\n4. War\nq. Quit\n"); bzero(choice, 16); recvUntil(0, choice, 15, '\n'); switch(choice[0]) { case '1': coinflip(); break; case '2': hangman(); break; case '3': dicegame(); break; case '4': war(); break; case 'q': put("Thanks for playing! Don't spend all your hugs in one place.\n"); _terminate(0); } } put("You're all out of hugs :(. Thanks for playing.\n"); }
void add_freelist_block( size_t length ) { pmeta block = NULL; pmeta walker = NULL; /// Round to the nearest page /// Account for the 4 byte length field length += 4; length = (length + 4095 ) & 0xfffff000; if ( allocate( length, 0, (void**)&block) != 0 ) { printf("[ERROR] Allocating a free list block failed: $d\n", length); _terminate(-1); } bzero( block, length ); block->length = length-4; if ( lookaside[0] == NULL ) { lookaside[0] = block; return; } link( block ); return; }
void init_freelist( void ) { pmeta zero_block = NULL; pmeta base_block = NULL; if ( allocate(4096, 0, (void**)&lookaside) != 0 ) { printf("[ERROR] Malloc fail terminate\n"); _terminate(-1); } bzero( lookaside[0], 4096); zero_block = lookaside[0]; base_block = zero_block + 1; /// Keep a zero length head on the freelist for /// ease of organization zero_block->length = 0; zero_block->next = base_block; zero_block->prev = NULL; base_block->length = 4096 - sizeof(meta) - 4; base_block->prev = zero_block; base_block->next = NULL; //printf("Set up head: $x with walker: $d: $x\n", zero_block, base_block->length, base_block); return; }