/* Send data through socket * @param socket: file descriptor of client * @param filename: name of the file * @param fdout: file descriptor to write * * @return: 0 on success. Error code otherwise */ int send_data(int socket, char filename[], int fdout) { struct stat file_stat; char type = END; if (stat(filename, &file_stat) < 0) return -1; if (file_stat.st_mode & S_IFDIR) { send_dir(socket, filename, search_root(filename), fdout); write(socket, &type, sizeof(char)); } else if (file_stat.st_mode & S_IFREG || file_stat.st_mode & S_IFLNK) { send_file(socket, filename, search_root(filename), fdout); write(socket, &type, sizeof(char)); } else { dprintf(fdout, "It can't be sended that kind of file\n"); dprintf(fdout, "Quitting...\n"); return -1; } dprintf(fdout, "\nSe han enviado: - %d directorios\n", DIR_S); dprintf(fdout, " - %d archivos\n", FILES_S); dprintf(fdout, "\nTransferencia realizada con éxito.\n"); write_to(socket, &DIR_S, sizeof(int)); write_to(socket, &FILES_S, sizeof(int)); return 0; }
void save_snap_to(char *path, snapshot *snap) { FILE *file = open_file(path, "wb"); write_to(file, 1, sizeof(snapshotheader), snap->header); write_to(file, snap->header->tot_nparticles, sizeof(snapshotparticle), snap->particles); fclose(file); }
u8 fork() { u8 i; u16 stack; for(i = 0; i < MAX_THREADS; i++) { if(thread_id[i] == TH_ID_UNUSED) break; } if(i == MAX_THREADS) { // thread table is full write_to(SERIAL); printf("\r\ncouldn't fork from thread %c", (u8)thread_id[thread_index]); halt(); } //write_to(SERIAL_DIRECT); //printf("forking from %u to %u\n", thread_index, i); stack = copy_stack(i); // <- new thread starts here... if(i == thread_index) return /*child*/ 1; thread_stack[i] = stack; // ...because of this line thread_id[i] = TH_ID_SOMETHING; return /*parent*/ 0; }
int show_without_file() { if (write_to(STDIN_FILENO) == -1) { goto cleanup; } return 0; cleanup: return -1; }
std::wstring wwrite(wostream& os, bool indent) { std::wostringstream s; if (write_to(s, os.entries(), indent)) { return s.str(); } else { static const std::wstring error = std::wstring(); return error; } }
/* * The real manager does this so we might * as well, too. */ void zero_card_files(void) { char zbuf[EYEFI_BUF_SIZE]; memset(&zbuf[0], 0, EYEFI_BUF_SIZE); // write_to(REQM, zbuf, EYEFI_BUF_SIZE); // write_to(REQC, zbuf, EYEFI_BUF_SIZE); write_to(RSPM, zbuf, EYEFI_BUF_SIZE); // write_to(RSPC, zbuf, EYEFI_BUF_SIZE); read_from(REQM); read_from(REQC); read_from(RSPM); // read_from(RSPC); }
int card_config_set(enum card_info_subcommand cmd, struct var_byte_response *args) { int len; struct card_config_cmd req; req.O = 'O'; req.subcommand = cmd; req.arg.len = args->len; memcpy(&req.arg.bytes[0], &args->bytes[0], args->len); // try to write a sane number of bytes len = offsetof(struct card_config_cmd, arg) + var_byte_len(args); debug_printf(2, "%s() writing %d bytes (%ld + %d)\n", __func__, len, offsetof(struct card_config_cmd, arg), var_byte_len(args)); write_to(REQM, &req, len); return wait_for_response(); }
void ui_thread() { write_to(LCD); lcd_clear(); lcd_cursor = 0; puts(" BANEL " BUILD_DATE "\n SDCC " SDCC_VERSION); wait_for_timer(T_UI, 0, 5, 0); send_message8(0, 1, 2, 3, 4, 5, 6, 7, 8); }
int show_file(char *file_name) { int fd = -1; if ((fd = open(file_name, O_RDONLY)) == -1) { fprintf(stderr, "Error while opening file. Err number: %d", errno); goto cleanup; } if (write_to(fd) == -1) { goto cleanup; } close(fd); return 0; cleanup: if (fd != -1) close(fd); return -1; }
int main(int argc, char *argv[]) { int socket; char client_name[BUF] = "anonymous"; int port = 2222; int opt; char *file, fullname[256]; int fdout = STDOUT; /**************************************/ system("clear"); // Verifico que paso bien los datos while ((opt = getopt(argc, argv, "f:p:n:h")) != -1) { switch (opt) { case 'f': file = optarg; break; case 'n': strncpy(client_name, optarg, strlen(optarg)); break; case 'h': printf("\nUsage: client [-f logfile] [-n nickname] [-p port] host\n"); printf("Cliente para la recepcion de archivos desde un server escuchando en port.\n"); printf("\tCommand Summary:\n"); printf("\t\t -f logfile\t Indica el archivo donde se guarda un log de la transferencia. Por defecto,\n"); printf("\t\t\t\t el path del archivo es la carpeta del usuario.\n"); printf("\t\t -h\t\t This help text.\n"); printf("\t\t -n nickname\t Indica el nickname del cliente. Por defecto es 'anonymous'\n"); printf("\t\t -p port\t Especifica el puerto de conexion. Por defecto es %d.\n\n", port); return 0; default: /* '?' */ fprintf(stderr, "Usage: %s [-f logfile] [-n nickname] [-p port_number] host\n", argv[0]); exit(EXIT_FAILURE); } } if (optind >= argc) { fprintf(stderr, "Expected argument after options\n"); exit(EXIT_FAILURE); } if(file != NULL) { sprintf(fullname, "/home/%s/%s", getlogin(), file); fdout = open(fullname, O_CREAT | O_RDONLY | O_WRONLY); dprintf(fdout, "Recibiendo archivos del servidor.\n\n"); } socket = clientcon(argv[optind], port); // Guardo el nickname y lo mando al server //strncpy(client_name, argv[optind], strlen(argv[optind])); write_to(socket, client_name, strlen(client_name)+1); printf("\nConectado correctamente al server.\n\n"); mkfolder(fdout, client_name); receive_data(socket, fdout); close(socket); if(fdout != STDOUT) { printf("Log guardado en el archivo '%s'.\n", fullname); close(fdout); } return 0; }
typename std::enable_if<!is_basic_json_class<T>::value,void>::type encode_ubjson(const T& val, std::ostream& os) { ubjson_encoder encoder(os); write_to(json(), val, encoder); }
typename std::enable_if<!is_basic_json_class<T>::value,void>::type encode_ubjson(const T& val, std::vector<uint8_t>& v) { ubjson_bytes_encoder encoder(v); write_to(json(), val, encoder); }
int main() { config_struct conf = load_config_from("./../../configurations.cfg"); filenames_struct filenames = generate_filenames(&conf); clock_t _c_a_c_f_f_ = start("Creating a c2c FFTW plan... "); size_t tot_num_of_grids = pow(conf.params.numOfAxisGrids, 3); fftw_complex *delta_complex; allocate((void **)&delta_complex, tot_num_of_grids, sizeof(fftw_complex)); fftw_complex *delta_fourier; allocate((void **)&delta_fourier, tot_num_of_grids, sizeof(fftw_complex)); int rank[3] = { conf.params.numOfAxisGrids, conf.params.numOfAxisGrids, conf.params.numOfAxisGrids }; fftw_plan p; p = fftw_plan_dft(3, rank, delta_complex, delta_fourier, FFTW_FORWARD, FFTW_MEASURE); done(_c_a_c_f_f_); clock_t _l_d_c_ = start("Loading density contrast... "); char *input_path = concat(2, "./../../2_griding/output/", filenames.densityContrast); double *delta_real; allocate((void **)&delta_real, tot_num_of_grids, sizeof(double)); load_density_contrast_grid(input_path, delta_real, &conf); convert_real_delta_to_complex(delta_real, delta_complex, &conf); reordering_fourier_input(delta_complex, &conf); done(_l_d_c_); clock_t _f_t_ = start("Fourier transform... "); fftw_execute(p); fftw_destroy_plan(p); done(_f_t_); clock_t _s_d_ = start("Saving data... "); char *output_path = concat(2, "./../output/", filenames.fourierTransformed); FILE * out_file; open_file(&out_file, output_path, "wb"); write_to(out_file, (void *)delta_fourier, tot_num_of_grids, sizeof(fftw_complex)); done(_s_d_); free(delta_real); fftw_free(delta_fourier); fftw_free(delta_complex); free(input_path); free(output_path); return 0; }
void testit0(void) { char c; struct testbuf tb; int i; int fdin; int fdout; //start_direct(); print_direct_status(); //enable_direct_mode(60, 120); enable_direct_mode(DIRECT_WAIT_FOREVER, DIRECT_WAIT_FOREVER); print_direct_status(); start_direct(); exit(0); //char new_cmd[] = {'O', 0x06, 0x0d, 0x0a, 0x31, 0x30, 0x2e, 0x36, 0x2e, 0x30, 0x2e, 0x31, 0x33, 0x37}; //printf("waiting...\n"); //print_transfer_status(); //exit(0); //int doagain = 1; //wlan_disable(0); //int to_test[] = {5, 8, 9, 11, 15, 16, 255, -1}; int to_test[] = {0xFF, -1}; zero_card_files(); for (i = 0; i < 100; i++) { print_transfer_status(); } exit(0); while (1) { //fprintf(stderr, "testing...\n"); for (i = 0; i < 255; i++) { int cmd = to_test[i]; if (cmd == -1) break; //zero_card_files(); card_info_cmd(cmd); printf("UNKNOWN %3d result: ", cmd); int printed = dumpbuf(eyefi_buf, 256); if (!printed) printf("\n"); print_transfer_status(); print_connected_to(); } } exit(0); scan_print_nets(); printf("WLAN enabled: %d\n", wlan_enabled()); //wlan_disable(); printf("WLAN enabled: %d\n", wlan_enabled()); for (i = 10; i <= 13; i++) { int printed; zero_card_files(); card_info_cmd(i); printf("UNKNOWN %d result:\n", i); printed = dumpbuf(eyefi_buf, 64); printf("WLAN enabled: %d\n", wlan_enabled()); } i = 0xff; card_info_cmd(i); printf("UNKNOWN %d result:", i); dumpbuf(eyefi_buf, 64); exit(3); card_info_cmd(3); printf("o3 result:\n"); dumpbuf(eyefi_buf, 64); memset(&zbuf[0], 0, EYEFI_BUF_SIZE); zbuf[0] = 'o'; zbuf[1] = 2; write_to(REQM, &zbuf[0], 16384); printf("o2 written\n"); printf("seq: %x\n", (int)eyefi_seq.seq); inc_seq(); for (i=0; i < 4; i++) { read_from(RSPC); printf("RSPC %d:\n", i); dumpbuf(eyefi_buf, 64); usleep(20000); } printf("RSPM1:\n"); read_from(RSPM); dumpbuf(eyefi_buf, 64); memset(&zbuf[0], 0, EYEFI_BUF_SIZE); write_to(RSPM, zbuf, EYEFI_BUF_SIZE); write_to(REQM, zbuf, EYEFI_BUF_SIZE); fdin = open("/home/dave/projects/eyefi/EYEFIFWU.BIN.2.0001", O_RDONLY); perror("fdin"); fdout = open("/media/EYE-FI/EYEFIFWU.BIN", O_WRONLY|O_CREAT); perror("fdout"); if (fdin <= 0 || fdout <= 0) exit(1); fd_flush(fdin); i = read(fdin, &fwbuf[0], 524288); perror("read"); if (i != 524288) exit(2); i = write(fdout, &fwbuf[0], 524288); fd_flush(fdout); perror("write"); if (i != 524288) exit(3); printf("RSPM2:\n"); read_from(RSPM); dumpbuf(eyefi_buf, 64); reboot_card(); printf("after reboot:\n"); dumpbuf(eyefi_buf, 64); printf("cic3:\n"); card_info_cmd(3); dumpbuf(eyefi_buf, 64); printf("cic2:\n"); card_info_cmd(2); dumpbuf(eyefi_buf, 64); memset(&zbuf[0], 0, EYEFI_BUF_SIZE); write_to(RSPM, zbuf, EYEFI_BUF_SIZE); write_to(REQM, zbuf, EYEFI_BUF_SIZE); printf("cic2v2:\n"); card_info_cmd(2); dumpbuf(eyefi_buf, 64); exit(0); strcpy(tb.name, "www.sr71.net/"); tb.l1 = strlen(tb.name); for (i = 0; i < 10; i++) { tb.cmd = 'O'; tb.l1 = i; write_struct(RSPM, &z); write_struct(REQM, &tb); wait_for_response(); printf("buffer after O %d:\n", i); dumpbuf(eyefi_buf, 64); printf("----------------\n"); write_struct(REQM, &tb); card_info_cmd(i); printf("card info(%d):\n", i); dumpbuf(eyefi_buf, 64); printf("-----------\n"); } return; strcpy(tb.name, "/public/eyefi/servname"); strcpy(tb.name, "/config/networks.xml"); //tb.len = strlen(tb.name); tb.l1 = 0; for (c = 'O'; c <= 'O'; c++) { tb.cmd = c; write_struct(REQM, &tb); wait_for_response(); printf("dumping buffer:\n"); dumpbuf(eyefi_buf, 64); printf("buffer dump done\n"); } }
Stencil& Stencil::page(const std::string& filename) { write_to(filename, page()); return *this; }
/* Send file through socket * @param socket: file descriptor of client * @param filename: name of the file * @param position: index of the filename in absolute path * @param fdout: file descriptor to write * * @return: return bytes sended */ int send_file(int socket, char filename[], int position, int fdout) { file_t file; struct stat stat_filename; int numb_bytes; int sended = 0; char type; bzero(&file, sizeof(file_t)); get_relative_path(file.name_of_file, filename, position); stat(filename, &stat_filename); /* if is a directory, send only the name of the directory */ if (stat_filename.st_mode & S_IFDIR) { type = (char) DIRECTORY; dprintf(fdout, "\tSending %s... ", filename); file.size_string = (unsigned int) strlen(file.name_of_file) + 1; /* tells server we are sending a directory */ write_to(socket, &type, sizeof(char)); write(socket, &file.size_string, sizeof(file.size_string)); write(socket, &file.name_of_file, (int) strlen(file.name_of_file) + 1); DIR_S++; dprintf(fdout, "OK\n"); return 0; /* if is a file */ } else if (stat_filename.st_mode & S_IFDIR || \ stat_filename.st_mode & S_IFLNK) { type = (char) FILE; /* open file to read */ file.fd_file = open(filename, O_RDONLY); file.size_file = (unsigned int) stat_filename.st_size; file.size_string = (unsigned int) strlen(file.name_of_file) + 1; dprintf(fdout, "\tSending %s... ", filename); /* tells server we are sending a file */ write_to(socket, &type, sizeof(char)); write_to(socket, &file.size_file, sizeof(file.size_file)); write_to(socket, &file.size_string, sizeof(file.size_string)); write_to(socket, &file.name_of_file, (int)strlen(file.name_of_file) + 1); while (TRUE) { numb_bytes = sendfile(socket, file.fd_file, NULL, SIZE_BUF); if (numb_bytes < 0) { error("ERROR"); return errno; } sended += numb_bytes; if (sended == file.size_file) break; } dprintf(fdout, "OK\n"); /* printf("\t\tSended %.3f of %.3f kB [%.3f%%]\n", sended/1000., file.size_file/1000., (sended*100.)/file.size_file); */ FILES_S++; close(file.fd_file); return sended; } return -1; }
/** Similar to `write_to()`, but in case of socket error, `terminate_session()` is called using `BAD_WRITE_QUIT_MSG` as a quit message. @param client The client to read from. @param buf Buffer to store the message read. @param len Maximum length of the message. This is usually bounded by the size of `buf`. This parameter avoids buffer overflow. */ inline void write_to_noerr(struct irc_client *client, char *buf, size_t len) { if (write_to(client, buf, len) == -1) { terminate_session(client, BAD_WRITE_QUIT_MSG); } }
bool wwrite(std::wostream& to, wgenerate_array& ga, bool indent) { return write_to(to, ga.get_root(), indent); }
bool write(std::ostream& to, array_writer& ga, bool indent) { return write_to(to, ga.get_nodes(), indent); }