/*START_OF_MAIN*/ int main() { int i=0; combine_type person_info; read_and_write("person_info.txt","person_info.bin"); read_binary("person_info.bin",person_info); return 0; /*END_OF_MAIN*/ }
static int gen_multy_bakup_files(b_fil* file_list, FILE_DESC input_file_desc, SLONG file_num) { /******************************************************************** ** ** g e n _ m u l t y _ b a c k u p _ f i l e s ** ********************************************************************* ** ** Functional description: ** ** processing input data from stdin and splits the data into ** multiple back-up files. ** ** allocates an 16K bytes I/O buffer ** intilializes header record common fields ** do forever ** walk through the backup file chain ** intilializes header record unique fields ** open backup file ** writes out header record to backup file ** points to the next backup file in the chain ** calculates the actual file size ( minus header record length ) ** if the actual file size less than 16K bytes ** set I/O size to actual file size ** otherwise ** set I/O size to 16K byte long ** when it is the last backup file ** reads data from standard input as much as indicated by I/O size ** and writes it out to the last backup file until no EOF. ** issues error message when disk space full condition is detected ** otherwise reads and writes to backup files util EOF ** if disk full cobdition is detected ** flush the remaining data in the I/O buffer to subsequence ** backup files ** go back to normal read and write process util EOF ** ********************************************************************* */ SLONG byte_write, ret_cd; TEXT header_str[header_rec_len], num_arr[5]; header_rec hdr_rec; // CVC: there's a can of worms here. First, this function assumes it can free // the io_buffer's allocated memory without keeping a second copy of that pointer. // However, io_buffer can't be declared UCHAR* const because its address is // passed to final_read_and_write() and read_and_write() and both functions // thus suggest, by taking a UCHAR** that they can change the pointer's address; // but in practice they never affect it, so fixing those functions to take simply // UCHAR* would allow the correct declaration for io_buffer to succeed. //UCHAR* const io_buffer = (UCHAR *) malloc(IO_BUFFER_SIZE); UCHAR* io_buffer = (UCHAR *) malloc(IO_BUFFER_SIZE); if (!io_buffer) { fprintf(stderr, "I/O buffer allocation failed\n"); return FB_FAILURE; } size_t pos; for (pos = 0; pos < header_rec_len; pos++) header_str[pos] = BLANK; pos = 0; ret_cd = set_hdr_str(header_str, header_rec_name, pos, sizeof(hdr_rec.name)); size_t indx; for (indx = 0; indx < sizeof(hdr_rec.name); indx++) hdr_rec.name[indx] = BLANK; pos = pos + sizeof(hdr_rec.name); time_t clock = time(0); // was SLONG ret_cd = set_hdr_str(header_str, ctime(&clock), pos, sizeof(hdr_rec.date_time)); for (indx = 0; indx < sizeof(hdr_rec.date_time); indx++) hdr_rec.date_time[indx] = BLANK; pos = pos + sizeof(hdr_rec.date_time); ret_cd = set_hdr_str(header_str, ", file No. ", pos, sizeof(hdr_rec.text1)); for (indx = 0; indx < sizeof(hdr_rec.text1); indx++) hdr_rec.text1[indx] = BLANK; for (indx = 0; indx < sizeof(hdr_rec.num); indx++) hdr_rec.num[indx] = BLANK; pos = pos + sizeof(hdr_rec.text1) + sizeof(hdr_rec.num); ret_cd = set_hdr_str(header_str, " of ", pos, sizeof(hdr_rec.text2)); for (indx = 0; indx < sizeof(hdr_rec.text2); indx++) hdr_rec.text2[indx] = BLANK; ret_cd = conv_ntoc(file_num, num_arr); if (ret_cd == FB_FAILURE) { free(io_buffer); fprintf(stderr, "gsplit could not convert numeric data to character data\n"); return FB_FAILURE; } num_arr[sizeof(num_arr) - 1] = TERMINAL; pos = pos + sizeof(hdr_rec.text2); ret_cd = set_hdr_str(header_str, num_arr, pos, sizeof(hdr_rec.total)); for (indx = 0; indx < sizeof(hdr_rec.total); indx++) hdr_rec.total[indx] = BLANK; pos = pos + sizeof(hdr_rec.total); ret_cd = set_hdr_str(header_str, ", ", pos, sizeof(hdr_rec.text3)); for (indx = 0; indx < sizeof(hdr_rec.text3); indx++) hdr_rec.text3[indx] = BLANK; for (indx = 0; indx < sizeof(hdr_rec.fl_name); indx++) hdr_rec.fl_name[indx] = BLANK; FILE_DESC output_fl_desc = 0; bool end_of_input = false, flush_done = false; const TEXT* file_name = NULL; SLONG io_size = 0; b_fil* fl_ptr = file_list; SINT64 byte_read = 0; SINT64 file_size = 0; while (true) { if (fl_ptr != NULL) { byte_read = 0; byte_write = 0; if (!fl_ptr->b_fil_next && (fl_ptr->b_fil_size == 0)) { fl_ptr->b_fil_size = MIN_FILE_SIZE; } file_size = fl_ptr->b_fil_size - header_rec_len; file_name = fl_ptr->b_fil_name; output_fl_desc = open_platf(file_name, 1); if (output_fl_desc == INVALID_HANDLE_VALUE) { free(io_buffer); fprintf(stderr, "can not open back up file %s\n", file_name); return FB_FAILURE; } ret_cd = write_header(fl_ptr, hdr_rec, output_fl_desc, header_str); if (ret_cd == FB_FAILURE) { free(io_buffer); fprintf(stderr, "could not write header record to file %s\n", file_name); return FB_FAILURE; } fl_ptr = fl_ptr->b_fil_next; } if (file_size < IO_BUFFER_SIZE) io_size = (SLONG) file_size; else io_size = IO_BUFFER_SIZE; if (!fl_ptr) { while (!end_of_input) { ret_cd = final_read_and_write(input_file_desc, output_fl_desc, file_name, io_size, &io_buffer, &end_of_input); if (ret_cd == FB_FAILURE) { free(io_buffer); return FB_FAILURE; } if (end_of_input) { free(io_buffer); return FB_SUCCESS; } } } else { while ((file_size > byte_read) && (fl_ptr != NULL)) { ret_cd = read_and_write(input_file_desc, output_fl_desc, /*file_name,*/ io_size, file_size, &io_buffer, &end_of_input, &byte_read, &byte_write); switch (ret_cd) { case FB_FAILURE: free(io_buffer); return FB_FAILURE; case FILE_IS_FULL: { byte_read = 0; // reset byte read count, prepare for next read const UCHAR* remaining_io = io_buffer + byte_write; SLONG remaining_io_len = IO_BUFFER_SIZE - byte_write; while (!flush_done && (fl_ptr != NULL)) { if (!fl_ptr->b_fil_next && fl_ptr->b_fil_size == 0) fl_ptr->b_fil_size = MIN_FILE_SIZE; file_size = fl_ptr->b_fil_size - header_rec_len; file_name = fl_ptr->b_fil_name; output_fl_desc = open_platf(file_name, 1); if (output_fl_desc == INVALID_HANDLE_VALUE) { free(io_buffer); fprintf(stderr, "can not open back up file %s\n", file_name); return FB_FAILURE; } ret_cd = write_header(fl_ptr, hdr_rec, output_fl_desc, header_str); if (ret_cd == FB_FAILURE) { free(io_buffer); fprintf(stderr, "fail to write header rec to file %s\n", file_name); return FB_FAILURE; } fl_ptr = fl_ptr->b_fil_next; if (!fl_ptr) { ret_cd = final_flush_io_buff(remaining_io, remaining_io_len, output_fl_desc); if (ret_cd == FB_FAILURE) { fprintf(stderr, "gsplit could not do backup due"); fprintf(stderr, " to lack of space or I/O problem\n"); free(io_buffer); return FB_FAILURE; } } else { // got a lot of backup files ret_cd = flush_io_buff(remaining_io, remaining_io_len, output_fl_desc, file_size, &byte_write, &flush_done); if (ret_cd == FB_FAILURE) { fprintf(stderr, "gsplit could not do backup due"); fprintf(stderr, " I/O problem\n"); free(io_buffer); return FB_FAILURE; } if (flush_done) { file_size = file_size - byte_write; byte_write = 0; } else { remaining_io = remaining_io + byte_write; remaining_io_len = remaining_io_len - byte_write; } } } // end of while loop break; } // case FILE_IS_FULL default: break; } if (end_of_input) { free(io_buffer); return FB_SUCCESS; } } } } // end of while ( true ) }
static gpointer worker_thread( gpointer data) { XferElement *elt = XFER_ELEMENT(data); XferElementGlue *self = XFER_ELEMENT_GLUE(data); switch (mech_pair(elt->input_mech, elt->output_mech)) { case mech_pair(XFER_MECH_READFD, XFER_MECH_WRITEFD): read_and_write(self); break; case mech_pair(XFER_MECH_READFD, XFER_MECH_PUSH_BUFFER): case mech_pair(XFER_MECH_WRITEFD, XFER_MECH_PUSH_BUFFER): read_and_push(self); break; case mech_pair(XFER_MECH_PULL_BUFFER, XFER_MECH_READFD): case mech_pair(XFER_MECH_PULL_BUFFER, XFER_MECH_WRITEFD): pull_and_write(self); break; case mech_pair(XFER_MECH_PULL_BUFFER, XFER_MECH_PUSH_BUFFER): pull_and_push(self); break; case mech_pair(XFER_MECH_READFD, XFER_MECH_DIRECTTCP_LISTEN): case mech_pair(XFER_MECH_WRITEFD, XFER_MECH_DIRECTTCP_LISTEN): if ((self->output_data_socket = do_directtcp_connect(self, elt->downstream->input_listen_addrs)) == -1) break; self->write_fdp = &self->output_data_socket; read_and_write(self); break; case mech_pair(XFER_MECH_PULL_BUFFER, XFER_MECH_DIRECTTCP_LISTEN): if ((self->output_data_socket = do_directtcp_connect(self, elt->downstream->input_listen_addrs)) == -1) break; self->write_fdp = &self->output_data_socket; pull_and_write(self); break; case mech_pair(XFER_MECH_DIRECTTCP_LISTEN, XFER_MECH_READFD): case mech_pair(XFER_MECH_DIRECTTCP_LISTEN, XFER_MECH_WRITEFD): if ((self->input_data_socket = do_directtcp_accept(self, &self->input_listen_socket)) == -1) break; self->read_fdp = &self->input_data_socket; read_and_write(self); break; case mech_pair(XFER_MECH_DIRECTTCP_LISTEN, XFER_MECH_PUSH_BUFFER): if ((self->input_data_socket = do_directtcp_accept(self, &self->input_listen_socket)) == -1) break; self->read_fdp = &self->input_data_socket; read_and_push(self); break; case mech_pair(XFER_MECH_DIRECTTCP_CONNECT, XFER_MECH_PULL_BUFFER): case mech_pair(XFER_MECH_DIRECTTCP_LISTEN, XFER_MECH_PULL_BUFFER): case mech_pair(XFER_MECH_READFD, XFER_MECH_PULL_BUFFER): case mech_pair(XFER_MECH_WRITEFD, XFER_MECH_READFD): case mech_pair(XFER_MECH_WRITEFD, XFER_MECH_PULL_BUFFER): case mech_pair(XFER_MECH_PUSH_BUFFER, XFER_MECH_READFD): case mech_pair(XFER_MECH_PUSH_BUFFER, XFER_MECH_WRITEFD): case mech_pair(XFER_MECH_PUSH_BUFFER, XFER_MECH_PULL_BUFFER): case mech_pair(XFER_MECH_PUSH_BUFFER, XFER_MECH_DIRECTTCP_LISTEN): case mech_pair(XFER_MECH_PUSH_BUFFER, XFER_MECH_DIRECTTCP_CONNECT): default: g_assert_not_reached(); break; case mech_pair(XFER_MECH_WRITEFD, XFER_MECH_DIRECTTCP_CONNECT): case mech_pair(XFER_MECH_READFD, XFER_MECH_DIRECTTCP_CONNECT): if ((self->output_data_socket = do_directtcp_accept(self, &self->output_listen_socket)) == -1) break; self->write_fdp = &self->output_data_socket; read_and_write(self); break; case mech_pair(XFER_MECH_DIRECTTCP_CONNECT, XFER_MECH_WRITEFD): case mech_pair(XFER_MECH_DIRECTTCP_CONNECT, XFER_MECH_READFD): if ((self->input_data_socket = do_directtcp_connect(self, elt->upstream->output_listen_addrs)) == -1) break; self->read_fdp = &self->input_data_socket; read_and_write(self); break; case mech_pair(XFER_MECH_DIRECTTCP_CONNECT, XFER_MECH_PUSH_BUFFER): if ((self->input_data_socket = do_directtcp_connect(self, elt->upstream->output_listen_addrs)) == -1) break; self->read_fdp = &self->input_data_socket; read_and_push(self); break; case mech_pair(XFER_MECH_PULL_BUFFER, XFER_MECH_DIRECTTCP_CONNECT): if ((self->output_data_socket = do_directtcp_accept(self, &self->output_listen_socket)) == -1) break; self->write_fdp = &self->output_data_socket; pull_and_write(self); break; case mech_pair(XFER_MECH_DIRECTTCP_LISTEN, XFER_MECH_DIRECTTCP_CONNECT): /* TODO: use async accept's here to avoid order dependency */ if ((self->output_data_socket = do_directtcp_accept(self, &self->output_listen_socket)) == -1) break; self->write_fdp = &self->output_data_socket; if ((self->input_data_socket = do_directtcp_accept(self, &self->input_listen_socket)) == -1) break; self->read_fdp = &self->input_data_socket; read_and_write(self); break; case mech_pair(XFER_MECH_DIRECTTCP_CONNECT, XFER_MECH_DIRECTTCP_LISTEN): /* TODO: use async connects and select() to avoid order dependency here */ if ((self->input_data_socket = do_directtcp_connect(self, elt->upstream->output_listen_addrs)) == -1) break; self->read_fdp = &self->input_data_socket; if ((self->output_data_socket = do_directtcp_connect(self, elt->downstream->input_listen_addrs)) == -1) break; self->write_fdp = &self->output_data_socket; read_and_write(self); break; } send_xfer_done(self); return NULL; }
static int write_wrapper(struct asfd *asfd, struct iobuf *wbuf) { while(asfd->append_all_to_write_buffer(asfd, wbuf)) if(read_and_write(asfd)) return -1; return 0; }
static int flush_asio(struct asfd *asfd) { while(asfd->writebuflen>0) if(read_and_write(asfd)) return -1; return 0; }
int main(int argc, char **argv) { static const struct option longopts[] = { { "help", 0, 0, 'h' }, { "verbose", 0, 0, 'v' }, { "listen", 0, 0, 'l' }, { "connect", 0, 0, 'c' }, { "node", 1, 0, 'n' }, { "host", 1, 0, 'n' }, { "service", 1, 0, 's' }, { "family", 1, 0, 'f' }, { "socktype", 1, 0, 't' }, { "ping", 0, 0, 'I' }, { "protocol", 1, 0, 'p' }, { "backends", 1, 0, 'b' }, { "srv", 0, 0, 'S' }, { "address", 1, 0, 'a' }, { "port", 1, 0, 'P' }, { "class", 1, 0, 'C' }, { "type", 1, 0, 'T' }, { NULL, 0, 0, 0 } }; static const char *opts = "hvcn::s:f:t:p:b:Sa:P:"; int opt, idx = 0; bool do_connect = false; bool do_listen = false; bool ping = false; char *nodename = NULL, *servname = NULL; char *address_str = NULL, *port_str = NULL; int cls = ns_c_in, type = 0; netresolve_t context; netresolve_query_t query; netresolve_set_log_level(NETRESOLVE_LOG_LEVEL_ERROR); context = netresolve_context_new(); if (!context) { error("netresolve: %s\n", strerror(errno)); return EXIT_FAILURE; } while ((opt = getopt_long(count_argv(argv), argv, opts, longopts, &idx)) != -1) { switch (opt) { case 'h': usage(); case 'v': netresolve_set_log_level(NETRESOLVE_LOG_LEVEL_DEBUG); break; case 'l': do_listen = true; break; case 'c': do_connect = true; break; case 'I': ping = true; break; case 'n': nodename = optarg; break; case 's': servname = optarg; break; case 'f': netresolve_context_set_options(context, NETRESOLVE_OPTION_FAMILY, netresolve_family_from_string(optarg), NETRESOLVE_OPTION_DONE); break; case 't': netresolve_context_set_options(context, NETRESOLVE_OPTION_SOCKTYPE, netresolve_socktype_from_string(optarg), NETRESOLVE_OPTION_DONE); break; case 'p': netresolve_context_set_options(context, NETRESOLVE_OPTION_PROTOCOL, netresolve_protocol_from_string(optarg), NETRESOLVE_OPTION_DONE); break; case 'b': netresolve_set_backend_string(context, optarg); break; case 'S': netresolve_context_set_options(context, NETRESOLVE_OPTION_DNS_SRV_LOOKUP, (int) true, NETRESOLVE_OPTION_DONE); break; case 'a': address_str = optarg; break; case 'P': port_str = optarg; break; case 'C': #ifdef USE_LDNS cls = ldns_get_rr_class_by_name(optarg); #else cls = strtoll(optarg, NULL, 10); #endif break; case 'T': #ifdef USE_LDNS type = ldns_get_rr_type_by_name(optarg); #else type = strtoll(optarg, NULL, 10); #endif break; default: exit(EXIT_FAILURE); } } if (argv[optind]) usage(); if (do_listen || do_connect) { netresolve_query_t query; int sock = -1; struct pollfd fds[2]; /* Linux: I found an interesting inconsistency where zero socktype * is supported by the kernel but not when combined with * `SOCK_NONBLOCK` which is internally used by netresolve * socket API implementation. */ if (!context->request.socktype && !context->request.protocol) { context->request.socktype = SOCK_STREAM; context->request.protocol = IPPROTO_TCP; } if (do_listen) { if (!(query = netresolve_listen(context, nodename, servname, 0, 0, 0))) { error("netresolve: Cannot create listening socket: %s", strerror(errno)); return EXIT_FAILURE; } netresolve_accept(query, on_accept, &sock); } else { query = netresolve_connect(context, nodename, servname, 0, 0, 0, on_socket, &sock); while (sock == -1) { error("netresolve: Socket connection failed: %s", strerror(errno)); if (errno == ENETUNREACH) { netresolve_connect_next(query); continue; } return EXIT_FAILURE; } netresolve_connect_free(query); } debug("Connected."); fds[0].fd = 0; fds[0].events = POLLIN; fds[1].fd = sock; fds[1].events = POLLIN; while (true) { if (poll(fds, 2, -1) == -1) { fprintf(stderr, "poll: %s\n", strerror(errno)); break; } if (fds[0].revents & (POLLIN | POLLHUP)) read_and_write(0, sock, sock); if (fds[1].revents & POLLIN) read_and_write(sock, 1, sock); } return EXIT_SUCCESS; } else if (type) query = netresolve_query_dns(context, nodename, cls, type, NULL, NULL); else if (address_str || port_str) { Address address; int family, ifindex; if (!netresolve_backend_parse_address(address_str, &address, &family, &ifindex)) return EXIT_FAILURE; query = netresolve_query_reverse(context, family, &address, ifindex, -1, port_str ? strtol(port_str, NULL, 10) : 0, NULL, NULL); } else query = netresolve_query_forward(context, nodename, servname, NULL, NULL); if (!query) { fprintf(stderr, "netresolve: %s\n", strerror(errno)); return EXIT_FAILURE; } debug("%s", netresolve_get_request_string(query)); if (ping) { for (int i = 0; i < netresolve_query_get_count(query); i++) if (run_ping(query, i)) goto out; error("netresolve: ping failed"); goto out; } const char *response_string = netresolve_get_response_string(query); char *dns_string = get_dns_string(query); if (response_string) printf("%s", response_string); if (dns_string) { printf("%s", dns_string); free(dns_string); } out: netresolve_context_free(context); return EXIT_SUCCESS; }
int main(int argc, char* argv[]) { int which = 0; data_t *heap_data = (data_t *)malloc(sizeof(data_t)); if (argc >= 2) { which = atoi(argv[1]); } init(&global_data); init(heap_data); switch(which) { case 0: cilk_spawn increment_i(&global_data); // write, read race mult_double(&global_data); cilk_sync; assert(__cilksan_error_count() == 3); break; case 1: cilk_spawn mult_double(&global_data); increment_i(&global_data); // write, read race cilk_sync; assert(__cilksan_error_count() == 3); break; case 2: cilk_spawn mult_double(&global_data); update_str(&global_data, 3, 8); // read, write race cilk_sync; assert(__cilksan_error_count() == 2); break; case 3: cilk_spawn increment_i(&global_data); // write, read race read_double(&global_data); cilk_sync; assert(__cilksan_error_count() == 1); break; case 4: cilk_spawn read_double(&global_data); increment_i(&global_data); // read, write race cilk_sync; assert(__cilksan_error_count() == 1); break; case 5: cilk_spawn read_double(&global_data); update_str(&global_data, 3, 8); // read, write race cilk_sync; assert(__cilksan_error_count() == 1); break; case 6: cilk_spawn read_str(&global_data, 2, 4); update_str(&global_data, 3, 8); // read, write race cilk_sync; assert(__cilksan_error_count() == 1); break; case 7: cilk_spawn increment_i(heap_data); // write, read race mult_double(heap_data); cilk_sync; assert(__cilksan_error_count() == 3); break; case 8: cilk_spawn mult_double(heap_data); increment_i(heap_data); // write, read race cilk_sync; assert(__cilksan_error_count() == 3); break; case 9: cilk_spawn mult_double(heap_data); update_str(heap_data, 3, 8); // write, write race cilk_sync; assert(__cilksan_error_count() == 2); break; case 10: cilk_spawn increment_i(heap_data); // write, read race read_double(heap_data); cilk_sync; assert(__cilksan_error_count() == 1); break; case 11: cilk_spawn read_double(heap_data); increment_i(heap_data); // read, write race cilk_sync; // to be sure that compiler doesn't optimize it away. global_int = heap_data->i; assert(__cilksan_error_count() == 1); break; case 12: cilk_spawn read_double(heap_data); update_str(heap_data, 3, 8); // read, write race cilk_sync; assert(__cilksan_error_count() == 1); break; case 13: cilk_spawn update_str(heap_data, 8, 11); read_str(heap_data, 10, 12); // write, read race cilk_sync; assert(__cilksan_error_count() == 1); break; case 14: cilk_spawn update_str(&global_data, 8, 12); // no race mult_double(&global_data); cilk_sync; assert(__cilksan_error_count() == 0); break; case 15: cilk_spawn update_str(&global_data, 4, 12); // no race read_i(&global_data); cilk_sync; assert(__cilksan_error_count() == 0); break; case 16: cilk_spawn update_str(heap_data, 8, 12); // no race mult_double(heap_data); cilk_sync; assert(__cilksan_error_count() == 0); break; case 17: cilk_spawn update_str(heap_data, 4, 12); // no race read_i(heap_data); cilk_sync; assert(__cilksan_error_count() == 0); break; case 18: cilk_spawn update_str(heap_data, 3, 5); // no race update_str(heap_data, 5, 8); cilk_sync; assert(__cilksan_error_count() == 0); break; case 19: read_and_write(&global_data); // read write race, but just 1 read_and_write(heap_data); cilk_sync; assert(__cilksan_error_count() == 1); break; } cilk_sync; free(heap_data); return 0; }