static void elfbuf_handle_core_notes (struct elfbuf *elf, unsigned note_type, unsigned long hwcap_mask) { unsigned ph_idx; if (ELFBUF_EHDR (elf, e_type) != 4) exit_with_msg ("%s: not a core file", elf->path); /* Iterate over program headers. */ for (ph_idx = 0; ph_idx != ELFBUF_EHDR (elf, e_phnum); ph_idx++) { size_t offset = ELFBUF_PHDR (elf, ph_idx, p_offset); size_t filesz = ELFBUF_PHDR (elf, ph_idx, p_filesz); if (offset > elf->len || filesz > elf->len - offset) exit_with_msg ("%s: unexpected end of data", elf->path); /* Deal with NOTE segments only. */ if (ELFBUF_PHDR (elf, ph_idx, p_type) != 4) continue; elfbuf_handle_note_segment (elf, offset, filesz, note_type, hwcap_mask); } }
static void elfbuf_init_from_file (struct elfbuf *elf, const char *path) { FILE *fp = fopen (path, "rb"); unsigned char *buf; size_t len; if (fp == NULL) exit_with_msg ("%s", path); read_file (&buf, &len, fp); fclose (fp); /* Validate ELF identification. */ if (len < 16 || buf[0] != 0x7f || buf[1] != 0x45 || buf[2] != 0x4c || buf[3] != 0x46 || buf[4] < 1 || buf[4] > 2 || buf[5] < 1 || buf[5] > 2) exit_with_msg ("%s: unsupported or invalid ELF file", path); elf->path = path; elf->buf = buf; elf->len = len; elf->ei_class = buf[4]; if (ELFBUF_EHDR_LEN (elf) > len || ELFBUF_EHDR (elf, e_phoff) > len || ELFBUF_EHDR (elf, e_phnum) > ((len - ELFBUF_EHDR (elf, e_phoff)) / ELFBUF_PHDR_LEN (elf)) ) exit_with_msg ("%s: unexpected end of data", path); if (ELFBUF_EHDR (elf, e_phentsize) != ELFBUF_PHDR_LEN (elf)) exit_with_msg ("%s: inconsistent ELF header", path); }
int main (int argc, char *argv[]) { unsigned note_type; unsigned long hwcap_mask = 0; struct elfbuf elf; if (argc < 4) { abort (); } if (sscanf (argv[3], "%u", ¬e_type) != 1) exit_with_msg ("%s: bad command line arguments\n", argv[0]); if (argc >= 5) { if (sscanf (argv[4], "%lu", &hwcap_mask) != 1) exit_with_msg ("%s: bad command line arguments\n", argv[0]); } elfbuf_init_from_file (&elf, argv[1]); elfbuf_handle_core_notes (&elf, note_type, hwcap_mask); elfbuf_write_to_file (&elf, argv[2]); return 0; }
t_point *parse(t_eve *e, char *filename) { char *line; char **s; int fd[2]; int l; t_point *pts; l = 0; pts = NULL; fd[1] = 0; if ((fd[0] = open(filename, O_RDONLY)) < 0) exit_with_msg("oops, open failed"); while ((fd[1] = (get_next_line(fd[0], &line)))) { if (fd[1] == -1) exit_with_msg("oops, gnl failed"); s = ft_strsplit(line, ' '); get_line(&pts, s, l, e); free(s); l++; } e->length = l; s = NULL; close(fd[1]); ft_strdel(&line); return (pts); }
void Game::game_over() { if (lose) { // draw "Game Over" int err = wogl_render_text(350, 650, medium_font, "Game Over!"); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__); } else { //draw "You Win" int err = wogl_render_text(300, 650, medium_font, "You Win!"); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__); } }
void Game::render_tank() { int err = theTank.display(); if (err != WOGL_SUCCESS) { exit_with_msg(__LINE__); } }
int main(int argc, char *argv[]) { int socketFd; int portNum; char buffer[BUFFER_SIZE]; struct sockaddr_in serverAddr; struct hostent *server; if (argc < 3) { exit_with_msg(0, USAGE_MSG); } portNum = atoi(argv[2]); if ((socketFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { exit_with_msg(1, SOCKET_OPEN_ERR_MSG); } if ((server = gethostbyname(argv[1])) == NULL) { exit_with_msg(1, NO_HOST_MSG); } bzero((char *)&serverAddr, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serverAddr.sin_addr.s_addr, server->h_length); serverAddr.sin_port = htons(portNum); if (connect(socketFd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) { exit_with_msg(1, CONN_ERR_MSG); } printf("Client application.\n"); printf("Please enter your message. Enter %s for quit.\n", EXIT_CMD); while (1) { printf("> "); bzero(buffer, BUFFER_SIZE); fgets(buffer, BUFFER_SIZE - 1, stdin); buffer[strlen(buffer) - 1] = 0; if (strcmp(buffer, EXIT_CMD) == 0) { close(socketFd); exit_with_msg(0, "Bye."); } if (strcmp(buffer, "") != 0) { if (write(socketFd, buffer, strlen(buffer)) < 0) { exit_with_msg(1, SOCKET_WRITE_ERR_MSG); } bzero(buffer, BUFFER_SIZE); if (read(socketFd, buffer, BUFFER_SIZE - 1) < 0) { exit_with_msg(1, SOCKET_READ_ERR_MSG); } printf("< %s\n\n", buffer); } } return 0; }
static void elfbuf_write_to_file (struct elfbuf *elf, const char *path) { FILE *fp = fopen (path, "wb"); if (fp == NULL) exit_with_msg ("%s", path); write_file (elf->buf, elf->len, fp); fclose (fp); }
//Assignment 4 void Game::render_mothership() { if(!invader_group->get_mothership().isDead()) { int err = invader_group->get_mothership().display(); if (err != WOGL_SUCCESS) { exit_with_msg(__LINE__); } } }
void start_server(char *port) { struct addrinfo hints; struct addrinfo *res; struct addrinfo *p; // getaddrinfo for host memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if (getaddrinfo(NULL, port, &hints, &res) != 0) { exit_with_msg(1, "getaddrinfo() error"); } // socket and bind for (p = res; p != NULL; p = p->ai_next) { listenFd = socket(p->ai_family, p->ai_socktype, 0); if (listenFd == -1) { continue; } if (bind(listenFd, p->ai_addr, p->ai_addrlen) == 0) { break; } } if (p == NULL) { exit_with_msg(1, "socket() or bind() error"); } freeaddrinfo(res); // listen for incoming connections if (listen(listenFd, QUEUED_REQUESTS_MAX) != 0) { exit_with_msg(1, "listen() error"); } }
void Game::render_shields() { for(int i = 0; i < 4; i++) { if(!theShield[i].isDead()) { int err = theShield[i].display(); if (err != WOGL_SUCCESS) { exit_with_msg(__LINE__); } } } }
void Game::render_invaders() { for(int i = 0; i < invader_group->get_number_per_column(); i++) { for(int j = 0; j < invader_group->get_number_per_row(); j++) { if(!invader_group->get_invader(i,j).isDead()) { int err = invader_group->get_invader(i,j).display(); if (err != WOGL_SUCCESS) { exit_with_msg(__LINE__); } } } } }
t_point *new_point(int x, int y, int z, t_eve *e) { t_point *ret; if (!(ret = malloc(sizeof(t_point)))) exit_with_msg("oops, malloc fail"); ret->x = x; ret->y = y; ret->z = z; if (z > e->max_h) e->max_h = z; if (z < e->min_h) e->min_h = z; ret->px = e->x0 + e->ec * (e->p[0][0] * x + e->p[0][1] * y + e->p[0][2] * z); ret->py = e->y0 + e->ec * (e->p[1][0] * x + e->p[1][1] * y + e->p[1][2] * z); ret->next = NULL; return (ret); }
void Game::display() { //render invaders render_invaders(); //render shield blocks render_shields(); //render tank render_tank(); //render bullets render_bullets(); //render mines render_mines(); //render mothership //Assignment 4 render_mothership(); // draw score int err = wogl_render_text(10, 10, small_font, "Current Score:"); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__); /*err = wogl_render_text(10, 20, small_font, "Total Score:"); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__);*/ err = wogl_render_text(10, 20, small_font, "Highest Score:"); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__); stringstream current; current<<theTank.getCurrentScore(); err = wogl_render_text(85, 10, small_font, current.str().c_str()); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__); //stringstream total; //total<<total_score; //err = wogl_render_text(85, 20, small_font, total.str().c_str()); //if (err != WOGL_SUCCESS) // exit_with_msg(__LINE__); stringstream highest; highest<<theTank.getHighScore(); err = wogl_render_text(85, 20, small_font, highest.str().c_str()); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__); err = wogl_render_text(700, 10, small_font, "Tank Life:"); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__); stringstream tank_life; tank_life<<theTank.get_life(); err = wogl_render_text(750, 10, small_font, tank_life.str().c_str()); if (err != WOGL_SUCCESS) exit_with_msg(__LINE__); }
void Game::control_tank(int key) { //press ESC to exit the game if (key == WOGL_KEY_ESCAPE) { // exit nicely exit_with_msg(0); } //if (key == WOGL_KEY_UP) // { // } //if (key == WOGL_KEY_DOWN) // { // } // press left and right keys to control the tank if (key == WOGL_KEY_LEFT) { theTank.move_left(); } if (key == WOGL_KEY_RIGHT) { theTank.move_right(); } if(key == WOGL_KEY_SPACE) { if(!theTank.isDead()) { //Assignment 4 if(tank_can_shoot()) tank_bullets.push_back(theTank.fire()); } } }
static void elfbuf_handle_note_segment (struct elfbuf *elf, size_t offset, size_t len, unsigned note_type, unsigned long hwcap_mask) { size_t pos = 0; while (pos + 12 < len) { uint32_t *note = (uint32_t *) (elf->buf + offset + pos); size_t desc_pos = pos + 12 + ((note[0] + 3) & ~3); size_t next_pos = desc_pos + ((note[1] + 3) & ~3); if (desc_pos > len || next_pos > len) exit_with_msg ("%s: corrupt notes data", elf->path); if (note[2] == note_type) note[2] |= 0xff000000; else if (note[2] == 6 && hwcap_mask != 0) elfbuf_handle_auxv (elf, offset + desc_pos, note[1], hwcap_mask); pos = next_pos; } }
int main(int argc, char* argv[]) { int id; char port[6]; struct sockaddr_in clientAddr; socklen_t addrLen; // default values documentRoot = getenv("PWD"); strcpy(port, "8080"); // parsing the command line arguments char arg; while ((arg = getopt(argc, argv, "p:r:h")) != -1) { switch (arg) { case 'r': documentRoot = malloc(strlen(optarg)); strcpy(documentRoot, optarg); break; case 'p': strcpy(port, optarg); break; case 'h': exit_with_msg(0, "Usage: web_server -p <port> -r <document_root>"); break; case '?': exit_with_msg(1, "Wrong arguments given"); break; default: exit(1); } } // setting all elements to -1: signifies there is no client connected int i; for (i = 0; i < CONNECTIONS_MAX; i++) { clients[i] = -1; } start_server(port); printf("Web Server started at port %s with root directory as %s\n", port, documentRoot); // accept connections id = 0; while (1) { addrLen = sizeof(clientAddr); clients[id] = accept(listenFd, (struct sockaddr *) &clientAddr, &addrLen); if (clients[id] < 0) { exit_with_msg(1, "accept() error"); } else { if (fork() == 0) { respond(id); exit(0); } } while (clients[id] != -1) { id = (id + 1) % CONNECTIONS_MAX; } } return 0; }
// client connection void respond(int id) { int fd; int received; int bytesRead; char path[PATH_MAX]; char msg[MSG_MAX]; char dataToSend[DATA_MAX]; char prefix[HTTP_VER_LEN]; char header_msg[MSG_MAX]; char *reqLine[3]; memset((void*) msg, (int) '\0', MSG_MAX); received = recv(clients[id], msg, MSG_MAX, 0); if (received < 0) { // receive error exit_with_msg(1, "recv() error"); } else if (received == 0) { // receive socket closed exit_with_msg(1, "client disconnected upexpectedly"); } else { // message received //printf("%s", msg); // debug only reqLine[0] = strtok(msg, " \t\n"); if (strncmp(reqLine[0], GET_REQ, strlen(GET_REQ)) == 0) { reqLine[1] = strtok(NULL, " \t"); reqLine[2] = strtok(NULL, " \t\n"); if (strncmp(reqLine[2], HTTP_1_0, strlen(HTTP_1_0)) != 0 && strncmp(reqLine[2], HTTP_1_1, strlen(HTTP_1_1)) != 0) { create_header(header_msg, HTTP_1_0, BAD_REQ_MSG); send(clients[id], header_msg, strlen(header_msg), 0); } else { if (strncmp(reqLine[2], HTTP_1_0, strlen(HTTP_1_0)) == 0) { strncpy(prefix, HTTP_1_0, strlen(HTTP_1_0)); } else { strncpy(prefix, HTTP_1_1, strlen(HTTP_1_0)); } if (strncmp(reqLine[1], "/\0", 2) == 0) { reqLine[1] = (char *) INDEX_FILE_NAME; // if no file is specified, index.html will be opened by default } strcpy(path, documentRoot); strcpy(&path[strlen(documentRoot)], reqLine[1]); if ((fd = open(path, O_RDONLY)) != -1) { // file found printf("file: %s\n", path); create_header(header_msg, prefix, OK_MSG); send(clients[id], header_msg, strlen(header_msg), 0); while ((bytesRead = read(fd, dataToSend, DATA_MAX)) > 0) { write(clients[id], dataToSend, bytesRead); } } else { create_header(header_msg, prefix, NOT_FOUND_MSG); send(clients[id], header_msg, strlen(header_msg), 0); write(clients[id], NOT_FOUND_BODY, strlen(NOT_FOUND_BODY)); } } } } // closing socket shutdown(clients[id], SHUT_RDWR); // all further send and recieve operations are DISABLED close(clients[id]); clients[id] = -1; }