static int Sclose_process(void *handle) { process_context *pc; int fd = process_fd(handle, &pc); if ( fd >= 0 ) { int which = (int)(uintptr_t)handle & 0x3; int rc; rc = (*Sfilefunctions.close)((void*)(uintptr_t)fd); pc->open_mask &= ~(1<<which); DEBUG(Sdprintf("Closing fd[%d]; mask = 0x%x\n", which, pc->open_mask)); if ( !pc->open_mask ) { int rcw = wait_for_process(pc); return rcw ? 0 : -1; } return rc; } return -1; }
int split_input(int in_fd, int out_fd, int *child_in_fds, int *child_out_fds, int child_count) { int buff_size = 1024 * 32; void *buff = malloc(buff_size); callback_handler_t callback_handler(buff, buff_size); std::vector<int> fds; callback_handler.fd_map[in_fd] = new cin_handler_t(child_in_fds, child_count); fds.push_back(in_fd); for (int i = 0; i < child_count; ++i) { callback_handler.fd_map[child_out_fds[i]] = new child_handler_t(out_fd); fds.push_back(child_out_fds[i]); } while(true) { printf("process_fds ... \n"); int res = process_fd(&fds[0], fds.size(), callback_handler); printf("... process fds %d\n", res); if (res == -1) { free(buff); return -1; } int closed_cnt = 0; for(int i = 0; i < fds.size(); ++i) { if(fds[i] == -1) ++closed_cnt; } if (closed_cnt == fds.size()) { free(buff); return 0; } } free(buff); return 1; }
static int Scontrol_process(void *handle, int action, void *arg) { process_context *pc; int fd = process_fd(handle, &pc); switch(action) { case SIO_GETFILENO: { int *fdp = arg; *fdp = fd; return 0; } default: return (*Sfilefunctions.control)((void*)(uintptr_t)fd, action, arg); } }
int run() { fd_set fds; int max_fd; create_interface(&interface); running = true; accueil = NULL; accueil = create_channel("Accueil", 1, NULL); set_current_channel(accueil); while (running) { max_fd = init_fd(&fds); if (select(max_fd + 1, &fds, NULL, NULL, NULL) > 0) process_fd(&fds); } destroy_interface(&interface); return (0); }
static int process_file(char *path, struct ndn_charbuf *c) { int fd = -1; int res = 0; if (strcmp(path, "-") == 0) { fd = STDIN_FILENO; } else { fd = open(path, O_RDONLY); if (-1 == fd) { perror(path); return(1); } } res = process_fd(fd, c); close(fd); return(res); }
/** * Main function for cwb-atoi. * * @param argc Number of command-line arguments. * @param argv Command-line arguments. */ int main(int argc, char **argv) { FILE *fd; int i; char *progname = argv[0]; /* default case: we are reading from stdin */ fd = stdin; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'n': little_endian = 0; break; case 'l': little_endian = 1; break; case 'h': default: Rprintf( "\n"); Rprintf( "Usage: %s [options] [file]\n", argv[0]); Rprintf( "Reads one integer per line from ASCII file <file> or from standard input\n"); Rprintf( "and writes values to standard output as 32-bit integers in network format\n"); Rprintf( "(the format used by CWB binary data files).\n"); Rprintf( "Options:\n"); Rprintf( " -n convert to network format [default]\n"); Rprintf( " -l convert to little endian format\n"); Rprintf( "Part of the IMS Open Corpus Workbench v" VERSION "\n\n"); rcqp_receive_error(1); } } else if ((fd = fopen(argv[i], "rb")) == NULL) { Rprintf( "%s: Couldn't open %s\n", progname, argv[i]); rcqp_receive_error(1); } } /* now process either input file or stdin */ process_fd(fd); return 0; }
/** * Main function for cwb-itoa. * * @param argc Number of command-line arguments. * @param argv Command-line arguments. */ int main(int argc, char **argv) { FILE *fd; int i; char *progname = argv[0]; fd = stdin; /* initialisation removed from declaration for Gnuwin32 compatibility */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'n': little_endian = 0; break; case 'l': little_endian = 1; break; case 'h': default: Rprintf( "\n"); Rprintf( "Usage: %s [options] [file]\n", argv[0]); Rprintf( "Reads 32bit integers in network format from CWB binary data file <file>\n"); Rprintf( "or from standard input and prints the values as ASCII numbers on standard\n"); Rprintf( "output (one number per line).\n"); Rprintf( "Options:\n"); Rprintf( " -n read integers in network format [default]\n"); Rprintf( " -l read integers in little endian format\n"); Rprintf( "Part of the IMS Open Corpus Workbench v" VERSION "\n\n"); rcqp_receive_error(1); } } else if ((fd = fopen(argv[i], "rb")) == NULL) { Rprintf( "%s: Couldn't open %s\n", progname, argv[i]); rcqp_receive_error(1); } } /* now process either input file or stdin */ process_fd(fd); return 0; }
int main(int argc, char *argv[]) { int i; int res = 0; struct ndn_charbuf *c = ndn_charbuf_create(); int opt; while ((opt = getopt(argc, argv, "h")) != -1) { switch (opt) { case 'h': default: usage(argv[0]); } } if (argv[optind] == NULL) return (process_fd(STDIN_FILENO, c)); for (i = optind; argv[i] != 0; i++) { res |= process_file(argv[i], c); } return(res); }
int main(int argc, char *argv[]) { const char *file = NULL; char *from = ""; char *to = ""; char *output = NULL; const char *preload_modules[] = {NULL, NULL}; FILE *out = stdout; int fd; smb_iconv_t cd; /* make sure the vars that get altered (4th field) are in a fixed location or certain compilers complain */ poptContext pc; struct poptOption long_options[] = { POPT_AUTOHELP { "from-code", 'f', POPT_ARG_STRING, &from, 0, "Encoding of original text" }, { "to-code", 't', POPT_ARG_STRING, &to, 0, "Encoding for output" }, { "output", 'o', POPT_ARG_STRING, &output, 0, "Write output to this file" }, { "preload-modules", 'p', POPT_ARG_STRING, &preload_modules[0], 0, "Modules to load" }, POPT_COMMON_SAMBA POPT_TABLEEND }; setlinebuf(stdout); pc = poptGetContext("smbiconv", argc, (const char **) argv, long_options, 0); poptSetOtherOptionHelp(pc, "[FILE] ..."); while(poptGetNextOpt(pc) != -1); /* the following functions are part of the Samba debugging facilities. See lib/debug.c */ setup_logging("smbiconv", True); if (preload_modules[0]) smb_load_modules(preload_modules); if(output) { out = fopen(output, "w"); if(!out) { DEBUG(0, ("Can't open output file '%s': %s, exiting...\n", output, strerror(errno))); return 1; } } cd = smb_iconv_open(to, from); if((int)cd == -1) { DEBUG(0,("unable to find from or to encoding, exiting...\n")); return 1; } while((file = poptGetArg(pc))) { if(strcmp(file, "-") == 0) fd = 0; else { fd = open(file, O_RDONLY); if(!fd) { DEBUG(0, ("Can't open input file '%s': %s, ignoring...\n", file, strerror(errno))); continue; } } /* Loop thru all arguments */ process_fd(cd, fd, out); close(fd); } poptFreeContext(pc); fclose(out); return 0; }
static ssize_t Swrite_process(void *handle, char *buf, size_t size) { int fd = process_fd(handle, NULL); return (*Sfilefunctions.write)((void*)(uintptr_t)fd, buf, size); }
int charmap_conversion (const char *from_code, struct charmap_t *from_charmap, const char *to_code, struct charmap_t *to_charmap, int argc, int remaining, char *argv[], const char *output_file) { struct convtable *cvtbl; int status = EXIT_SUCCESS; /* We have three different cases to handle: - both, from_charmap and to_charmap, are available. This means we can assume that the symbolic names match and use them to create the mapping. - only from_charmap is available. In this case we can only hope that the symbolic names used are of the <Uxxxx> form in which case we can use a UCS4->"to_code" iconv() conversion for the second step. - only to_charmap is available. This is similar, only that we would use iconv() for the "to_code"->UCS4 conversion. We first create a table which maps input bytes into output bytes. Once this is done we can handle all three of the cases above equally. */ if (from_charmap != NULL) { if (to_charmap == NULL) cvtbl = use_from_charmap (from_charmap, to_code); else cvtbl = use_both_charmaps (from_charmap, to_charmap); } else { assert (to_charmap != NULL); cvtbl = use_to_charmap (from_code, to_charmap); } /* If we couldn't generate a table stop now. */ if (cvtbl == NULL) return EXIT_FAILURE; /* Determine output file. */ FILE *output; if (output_file != NULL && strcmp (output_file, "-") != 0) { output = fopen (output_file, "w"); if (output == NULL) error (EXIT_FAILURE, errno, _("cannot open output file")); } else output = stdout; /* We can now start the conversion. */ if (remaining == argc) { if (process_file (cvtbl, stdin, output) != 0) status = EXIT_FAILURE; } else do { int fd; if (verbose) printf ("%s:\n", argv[remaining]); if (strcmp (argv[remaining], "-") == 0) fd = 0; else { fd = open (argv[remaining], O_RDONLY); if (fd == -1) { error (0, errno, _("cannot open input file `%s'"), argv[remaining]); status = EXIT_FAILURE; continue; } } #ifdef _POSIX_MAPPED_FILES struct stat64 st; char *addr; /* We have possibilities for reading the input file. First try to mmap() it since this will provide the fastest solution. */ if (fstat64 (fd, &st) == 0 && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) != MAP_FAILED)) { /* Yes, we can use mmap(). The descriptor is not needed anymore. */ if (close (fd) != 0) error (EXIT_FAILURE, errno, _("error while closing input `%s'"), argv[remaining]); if (process_block (cvtbl, addr, st.st_size, output) < 0) { /* Something went wrong. */ status = EXIT_FAILURE; /* We don't need the input data anymore. */ munmap ((void *) addr, st.st_size); /* We cannot go on with producing output since it might lead to problem because the last output might leave the output stream in an undefined state. */ break; } /* We don't need the input data anymore. */ munmap ((void *) addr, st.st_size); } else #endif /* _POSIX_MAPPED_FILES */ { /* Read the file in pieces. */ if (process_fd (cvtbl, fd, output) != 0) { /* Something went wrong. */ status = EXIT_FAILURE; /* We don't need the input file anymore. */ close (fd); /* We cannot go on with producing output since it might lead to problem because the last output might leave the output stream in an undefined state. */ break; } /* Now close the file. */ close (fd); } } while (++remaining < argc); /* All done. */ return status; }