static void parse_level(int fd, t_level *level) { char *line; int x; int y; y = 0; line = NULL; while (get_next_line(fd, &line) > 0) { if (y >= LEVEL_HEIGHT) handle_errors(__func__, "wrong height!", TRUE); if (line && ft_strlen(line) != LEVEL_WIDTH) handle_errors(__func__, "wrong width!", TRUE); x = 0; while (line[x] != '\0') { if (line[x] != NO_BRICK_CHAR) push_level_brick(level, x, y, line[x] - '0'); x++; } if (line) ft_strdel(&line); y++; } if (y != LEVEL_HEIGHT) handle_errors(__func__, "wrong height!", TRUE); }
int encrypt(unsigned char * plaintext, int plaintext_len, unsigned char * key, unsigned char * iv, unsigned char * ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; // init context if(!(ctx = EVP_CIPHER_CTX_new())){ handle_errors(); } // init encryption operation if(1 != EVP_EncryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, key, iv)){ handle_errors(); } // obtain encrypted output if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)){ handle_errors(); ciphertext_len = len; } ciphertext_len=len; // finalize if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)){ handle_errors(); ciphertext_len += len; } ciphertext_len += len; EVP_CIPHER_CTX_free(ctx); return ciphertext_len; }
int main(int argc, const char *argv[]) { TContact_list contact_list = {contact_list.first = NULL, contact_list.last = NULL}; get_dir(); switch(get_mode(argc, argv)){ case DEL: get_data(&contact_list); remove_from_list(&contact_list, atoi(argv[2])); save_csv(&contact_list); generate_html(&contact_list); free_list(&contact_list); break; case ADD: get_data(&contact_list); add_to_list(argv, &contact_list); sort_list(&contact_list); save_csv(&contact_list); generate_html(&contact_list); free_list(&contact_list); break; case LIST: get_data(&contact_list); sort_list(&contact_list); print_list(&contact_list); save_csv(&contact_list); generate_html(&contact_list); free_list(&contact_list); break; case SIN: get_data(&contact_list); print_single(&contact_list, atoi(argv[2])); free_list(&contact_list); break; case FIND: get_data(&contact_list); find_in_list(&contact_list, (char *)argv[2]); generate_html(&contact_list); free_list(&contact_list); break; case PLIST: get_data(&contact_list); sort_list(&contact_list); generate_plist(&contact_list); free_list(&contact_list); break; case DELN: get_data(&contact_list); delete_by_name(&contact_list, (char *)argv[2], (char *)argv[3]); save_csv(&contact_list); generate_html(&contact_list); free_list(&contact_list); default: handle_errors(); return EXIT_FAILURE; } if(handle_errors() != EXIT_SUCCESS) return EXIT_FAILURE; else return EXIT_SUCCESS; }
void collection_get_primitive(Collection* collection, size_t ndx, PrimitiveValue& value, NativeException::Marshallable& ex) { handle_errors(ex, [&]() { const size_t count = collection->size(); if (ndx >= count) throw IndexOutOfRangeException("Get from Collection", ndx, count); value.has_value = true; switch (value.type) { case realm::PropertyType::Bool: value.value.bool_value = collection->template get<bool>(ndx); break; case realm::PropertyType::Bool | realm::PropertyType::Nullable: { auto result = collection->template get<Optional<bool>>(ndx); value.has_value = !!result; value.value.bool_value = result.value_or(false); break; } case realm::PropertyType::Int: value.value.int_value = collection->template get<int64_t>(ndx); break; case realm::PropertyType::Int | realm::PropertyType::Nullable: { auto result = collection->template get<Optional<int64_t>>(ndx); value.has_value = !!result; value.value.int_value = result.value_or(0); break; } case realm::PropertyType::Float: value.value.float_value = collection->template get<float>(ndx); break; case realm::PropertyType::Float | realm::PropertyType::Nullable: { auto result = collection->template get<Optional<float>>(ndx); value.has_value = !!result; value.value.float_value = result.value_or((float)0); break; } case realm::PropertyType::Double: value.value.double_value = collection->template get<double>(ndx); break; case realm::PropertyType::Double | realm::PropertyType::Nullable: { auto result = collection->template get<Optional<double>>(ndx); value.has_value = !!result; value.value.double_value = result.value_or((double)0); break; } case realm::PropertyType::Date: value.value.int_value = to_ticks(collection->template get<Timestamp>(ndx)); break; case realm::PropertyType::Date | realm::PropertyType::Nullable: { auto result = collection->template get<Timestamp>(ndx); value.has_value = !result.is_null(); value.value.int_value = result.is_null() ? 0 : to_ticks(result); break; } default: REALM_UNREACHABLE(); } }); }
FILE *open_file (char *file_path, char *rights) { FILE *file = fopen (file_path, rights); handle_errors (file, file_path); return file; }
inline T get(Collection* collection, size_t ndx, NativeException::Marshallable& ex) { return handle_errors(ex, [&]() { const size_t count = collection->size(); if (ndx >= count) throw IndexOutOfRangeException("Get from RealmList", ndx, count); return collection->template get<T>(ndx); }); }
long bb_strtol(const char *arg, char **endp, int base) { long v; char *endptr; if (arg[0] != '-' && !isalnum(arg[0])) return ret_ERANGE(); errno = 0; v = strtol(arg, &endptr, base); return handle_errors(v, endp, endptr); }
unsigned bb_strtou(const char *arg, char **endp, int base) { unsigned long v; char *endptr; if (!isalnum(arg[0])) return ret_ERANGE(); errno = 0; v = strtoul(arg, &endptr, base); if (v > UINT_MAX) return ret_ERANGE(); return handle_errors(v, endp, endptr); }
unsigned long FAST_FUNC bb_strtoul(const char *arg, char **endp, int base) { unsigned long v; char *endptr; if (!endp) endp = &endptr; *endp = (char*) arg; if (!isalnum(arg[0])) return ret_ERANGE(); errno = 0; v = strtoul(arg, endp, base); return handle_errors(v, endp); }
unsigned long long bb_strtoull(const char *arg, char **endp, int base) { unsigned long long v; char *endptr; /* strtoul(" -4200000000") returns 94967296, errno 0 (!) */ /* I don't think that this is right. Preventing this... */ if (!isalnum(arg[0])) return ret_ERANGE(); /* not 100% correct for lib func, but convenient for the caller */ errno = 0; v = strtoull(arg, &endptr, base); return handle_errors(v, endp, endptr); }
void load_levels(t_level *levels[]) { int fd[N_LEVELS]; int i; char *path; char *level_index; i = 0; if (N_LEVELS == 0) handle_errors(__func__, "N_LEVELS is null!", TRUE); while (i < N_LEVELS) { level_index = ft_itoa(i); path = ft_strjoin(LEVEL_PATH, level_index); ft_printf("loading %s ..\n", path); if ((fd[i] = open(path, O_RDONLY)) == -1) handle_errors(__func__, "level not found!", TRUE); levels[i] = init_level(); parse_level(fd[i], levels[i]); ft_strdel(&path); ft_strdel(&level_index); i++; } }
char *get_server_dir(char set, char *tmp) { static char *serv_dir = NULL; if (set != 1) return (serv_dir); if (tmp == NULL && !(serv_dir = getcwd(NULL, 0))) { handle_errors(GETCWD, 1); return (NULL); } else if (tmp != NULL) serv_dir = tmp; return (serv_dir); }
static Block_Obj sass_parse_block(Sass_Compiler* compiler) throw() { // assert valid pointer if (compiler == 0) return 0; // The cpp context must be set by now Context* cpp_ctx = compiler->cpp_ctx; Sass_Context* c_ctx = compiler->c_ctx; // We will take care to wire up the rest compiler->cpp_ctx->c_compiler = compiler; compiler->state = SASS_COMPILER_PARSED; try { // get input/output path from options std::string input_path = safe_str(c_ctx->input_path); std::string output_path = safe_str(c_ctx->output_path); // maybe skip some entries of included files // we do not include stdin for data contexts bool skip = c_ctx->type == SASS_CONTEXT_DATA; // dispatch parse call Block_Obj root(cpp_ctx->parse()); // abort on errors if (!root) return 0; // skip all prefixed files? (ToDo: check srcmap) // IMO source-maps should point to headers already // therefore don't skip it for now. re-enable or // remove completely once this is tested size_t headers = cpp_ctx->head_imports; // copy the included files on to the context (dont forget to free later) if (copy_strings(cpp_ctx->get_included_files(skip, headers), &c_ctx->included_files) == NULL) throw(std::bad_alloc()); // return parsed block return root; } // pass errors to generic error handler catch (...) { handle_errors(c_ctx); } // error return 0; }
long FAST_FUNC bb_strtol(const char *arg, char **endp, int base) { long v; char *endptr; char first; if (!endp) endp = &endptr; *endp = (char*) arg; first = (arg[0] != '-' ? arg[0] : arg[1]); if (!isalnum(first)) return ret_ERANGE(); errno = 0; v = strtol(arg, endp, base); return handle_errors(v, endp); }
int fork_process(t_fd fd, t_sockin *s_in_client) { pid_t pid_client; if ((pid_client = fork()) == -1) return (handle_errors(FORK, 1)); if (pid_client == 0) { if (process_client(fd, s_in_client)) return (1); close_fd(fd.client_fd); return (0); } else if (close(fd.client_fd) == -1) return (-1); return (0); }
int put_into_file(t_response *response) { int fd_file; int ret; char buff[PACKAGE_SIZE]; int mode; read(response->sock_fd, (char *)&mode, sizeof(int)); if ((fd_file = open(response->data, O_CREAT | O_WRONLY, mode)) == -1) return (handle_errors(OPEN, 1)); while ((ret = read(response->sock_fd, buff, PACKAGE_SIZE)) > 0) { write(fd_file, buff, ret); if (ret < PACKAGE_SIZE) break ; } close_fd(fd_file); return (0); }
long long FAST_FUNC bb_strtoll(const char *arg, char **endp, int base) { unsigned long long v; char *endptr; char first; if (!endp) endp = &endptr; *endp = (char*) arg; /* Check for the weird "feature": * a "-" string is apparently a valid "number" for strto[u]l[l]! * It returns zero and errno is 0! :( */ first = (arg[0] != '-' ? arg[0] : arg[1]); if (!isalnum(first)) return ret_ERANGE(); errno = 0; v = strtoll(arg, endp, base); return handle_errors(v, endp); }
unsigned long long FAST_FUNC bb_strtoull(const char *arg, char **endp, int base) { unsigned long long v; char *endptr; if (!endp) endp = &endptr; *endp = (char*) arg; /* strtoul(" -4200000000") returns 94967296, errno 0 (!) */ /* I don't think that this is right. Preventing this... */ //if (!isalnum(arg[0])) return ret_ERANGE(); char first = (arg[0] != '-' ? arg[0] : arg[1]); if (!isalnum(first)) return ret_ERANGE(); /* not 100% correct for lib func, but convenient for the caller */ errno = 0; v = strtoull(arg, endp, base); return handle_errors(v, endp); }
int send_file(t_response *response) { int fd_file; char buff[PACKAGE_SIZE]; int ret; if ((fd_file = open(response->data, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) == -1) { send_request(response->sock_fd, "1"); handle_errors(OPEN, 1); return (0); } send_request(response->sock_fd, "0"); while ((ret = read(fd_file, buff, PACKAGE_SIZE)) > 0) { write(response->sock_fd, buff, ret); } close_fd(fd_file); return (0); }
int accept_clients(t_fd fd) { t_sockin s_in_client; socklen_t sock_len; sock_len = sizeof(t_sockin); while ((fd.client_fd = accept(fd.sock_fd, (struct sockaddr *)&s_in_client, &sock_len)) != -1) { if (fork_process(fd, &s_in_client) == -1) { close_fd(fd.sock_fd); return (-1); } } if (fd.client_fd == -1) { handle_errors(ACCEPT, 1); close_fd(fd.sock_fd); return (-1); } return (0); }
// heart of the trouble_code_reader module: int tr_code_proc(int msg, DIALOG *d, int c) { static char vehicle_response[1024]; // character buffer for car response static int first_read_occured = FALSE; static int receiving_response = FALSE; // flag, "are we receiving response?" static int verifying_connection = FALSE; // flag, "are we verifying connection?" static int current_request = CRQ_NONE; // NUM_OF_CODES, READ_CODES, CLEAR_CODES int response_status = EMPTY; // EMPTY, DATA, PROMPT int response_type; // BUS_BUSY, BUS_ERROR, DATA_ERROR, etc. int pending_codes_cnt = 0; char comport_buffer[256]; // temporary storage for comport data char buf1[64]; char buf2[64]; switch (msg) { case MSG_IDLE: if (!first_read_occured) { if (!simulate) read_codes(); first_read_occured = TRUE; return D_O_K; } if (simulate) break; if (comport.status == READY) { if (!receiving_response) { if (verifying_connection) { send_command("0100"); // send request that requires a response receiving_response = TRUE; // now we're waiting for response vehicle_response[0] = 0; //get buffer ready for the response start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer } else if (current_request == READ_PENDING) { send_command("07"); // request "pending" codes receiving_response = TRUE; // and receiving response vehicle_response[0] = '\0'; // clear the buffer start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer... } } else { response_status = read_comport(comport_buffer); if (response_status == DATA) // if data detected in com port buffer { // append contents of comport_buffer to vehicle_response: strcat(vehicle_response, comport_buffer); start_serial_timer(OBD_REQUEST_TIMEOUT); // we got data, reset the timer } else if (response_status == PROMPT) // if ">" is detected { receiving_response = FALSE; // we're not waiting for response any more stop_serial_timer(); // stop the timer // append contents of comport_buffer to vehicle_response: strcat(vehicle_response, comport_buffer); if (verifying_connection) // *** if we are verifying connection *** { // NOTE: we only get here if we got "NO DATA" somewhere else response_type = process_response("0100", vehicle_response); verifying_connection = FALSE; // we're not verifying connection anymore if (response_type == HEX_DATA) // if everything seems to be fine now, { if (current_request == CLEAR_CODES) alert("There may have been a temporary loss of connection.", "Please try clearing codes again.", NULL, "OK", NULL, 0, 0); else if (current_request == NUM_OF_CODES) alert("There may have been a temporary loss of connection.", "Please try reading codes again.", NULL, "OK", NULL, 0, 0); else if (current_request == READ_CODES) { current_request = READ_PENDING; break; } } else if (response_type == ERR_NO_DATA) { if (current_request == CLEAR_CODES) // if we were clearing codes, alert("Communication problem: vehicle did not confirm successful", "deletion of trouble codes. Please check connection to the vehicle,", "make sure the ignition is ON, and try clearing the codes again.", "OK", NULL, 0, 0); else // if we were reading codes or requesting number or DTCs alert("There may have been a loss of connection.", "Please check connection to the vehicle,", "and make sure the ignition is ON", "OK", NULL, 0, 0); } else display_error_message(response_type, FALSE); broadcast_dialog_message(MSG_READY, 0); // tell everyone we're done } else if (current_request == NUM_OF_CODES) // *** if we are getting number of codes *** { response_type = process_response("0101", vehicle_response); if (response_type == ERR_NO_DATA) // if we received "NO DATA" verifying_connection = TRUE; // verify connection else if (response_type != HEX_DATA) // if we got an error, handle_errors(response_type, NUM_OF_CODES); // handle it else // if process response returned HEX_DATA (i.e. there are no errors) { // extract # of codes from vehicle_response num_of_codes_reported = handle_num_of_codes(vehicle_response); send_command("03"); // request "stored" codes current_request = READ_CODES; // we're reading stored codes now receiving_response = TRUE; // and receiving response vehicle_response[0] = '\0'; // clear the buffer start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer... } } else if (current_request == READ_CODES) // if we are reading codes, { response_type = process_response("03", vehicle_response); if (response_type == ERR_NO_DATA) // vehicle didn't respond, check connection { if (num_of_codes_reported > 0) verifying_connection = TRUE; else current_request = READ_PENDING; } else if (response_type == HEX_DATA) { handle_read_codes(vehicle_response, FALSE); current_request = READ_PENDING; } else // if we got an error handle_errors(response_type, READ_CODES); } else if(current_request == READ_PENDING) // if we are reading pending codes, { response_type = process_response("07", vehicle_response); if (response_type == ERR_NO_DATA) { if (get_number_of_codes() == 0 && num_of_codes_reported == 0) alert("No Diagnostic Trouble Codes (DTCs) detected", NULL, NULL, "OK", NULL, 0, 0); } else if(response_type != HEX_DATA) // if we got an error, { handle_errors(response_type, READ_PENDING); break; } else // if there were *no* errors, pending_codes_cnt = handle_read_codes(vehicle_response, TRUE); // if number of DTCs reported by 0101 request does not equal either number or total DTCs or just stored DTCs if ((get_number_of_codes() != num_of_codes_reported) && (get_number_of_codes() - pending_codes_cnt != num_of_codes_reported)) { sprintf(buf1, "Vehicle reported %i Diagnostic Trouble Codes (DTCs).", num_of_codes_reported); sprintf(buf2, "However, %i non-pending DTC(s) have been successfully read.", get_number_of_codes() - pending_codes_cnt); alert(buf1, buf2, "Try reading codes again.", "OK", NULL, 0, 0); } populate_trouble_codes_list(); broadcast_dialog_message(MSG_READY, 0); // tell everyone we're done } else if(current_request == CLEAR_CODES) { response_type = process_response("04", vehicle_response); if (response_type == ERR_NO_DATA)// vehicle didn't respond, check connection verifying_connection = TRUE; else if(response_type != HEX_DATA) // if we got an error, handle_errors(response_type, CLEAR_CODES); else // if everything's fine (received confirmation) { clear_trouble_codes(); num_of_codes_reported = 0; mil_is_on = FALSE; broadcast_dialog_message(MSG_READY, 0); } } } else if (serial_time_out) // if request timed out, { stop_serial_timer(); receiving_response = FALSE; num_of_codes_reported = 0; mil_is_on = FALSE; clear_trouble_codes(); broadcast_dialog_message(MSG_READY, 0); if(alert("Device is not responding.", "Please check that it is connected", "and the port settings are correct", "OK", "&Configure Port", 27, 'c') == 2) display_options(); // let the user choose correct settings while (comport.status == NOT_OPEN) { if (alert("Port is not ready.", "Please check that you specified the correct port", "and that no other application is using it", "&Configure Port", "&Ignore", 'c', 'i') == 1) display_options(); // let the user choose correct settings else comport.status = USER_IGNORED; } } } } break; // end case MSG_IDLE case MSG_START: first_read_occured = FALSE; num_of_codes_reported = 0; mil_is_on = FALSE; // fall through case MSG_READY: receiving_response = FALSE; verifying_connection = FALSE; current_request = CRQ_NONE; break; case MSG_READ_CODES: if (comport.status == READY) { send_command("0101"); // request number of trouble codes start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer current_request = NUM_OF_CODES; receiving_response = TRUE; // now we're waiting for response vehicle_response[0] = 0; clear_trouble_codes(); num_of_codes_reported = 0; mil_is_on = FALSE; } else serial_time_out = TRUE; break; case MSG_CLEAR_CODES: if (comport.status == READY) { send_command("04"); // "clear codes" request current_request = CLEAR_CODES; receiving_response = TRUE; // now we're waiting for response vehicle_response[0] = 0; start_serial_timer(OBD_REQUEST_TIMEOUT); // start the timer } else serial_time_out = TRUE; break; case MSG_END: stop_serial_timer(); break; } return D_O_K; } // end of tr_codes_proc()