int main (int argc, char **argv) { program_name = basename (argv[0]); struct options opts = {false, false, false, 0, NULL}; struct counts totals = {0, 0, 0}; scan_options (argc, argv, &opts); if (opts.file_count == 0) { count_file (stdin, NULL, &opts, &totals); }else { for (int filenr = 0; filenr < opts.file_count; ++filenr) { char *filename = opts.file_names[filenr]; if (strcmp (filename, stdin_name) == 0) { count_file (stdin, filename, &opts, &totals); }else { FILE *file = fopen (filename, "r"); if (file == NULL) { error ("%s: %s", filename, strerror (errno)); }else { count_file (file, filename, &opts, &totals); fclose (file); } } } if (opts.file_count > 1) print_count (&opts, &totals, "total"); } return exit_status; }
error_t size_file(char* path, void* state) { bwt_state_t* s = (bwt_state_t*) state; long len; FILE* f; struct stat st; int rc; rc = stat(path, &st); if( rc != 0 ) return ERR_IO_STR_OBJ("Could not stat", path); // Double-check: open the file. f = fopen(path, "r"); if( ! f ) return ERR_IO_STR_OBJ("Could not open file", path); // get the length of the file. rc = fseek(f, 0L, SEEK_END); if( rc != 0 ) return ERR_IO_STR_OBJ("Could not fseek", path); len = ftell(f); if( len < 0 ) return ERR_IO_STR_OBJ("Could not ftell", path); rewind(f); if( len != st.st_size ) return ERR_INVALID_STR("seek and stat return different lengths"); fclose(f); return count_file(&s->p, len, 0, NULL, strlen(path), (unsigned char*) path); }
int main(int argc, char *argv[]) { if (argc == 1) count_file(stdin); else for (int i = 1; i < argc; i++) { FILE *in = fopen(argv[i], "r"); if (in == NULL) { fprintf(stderr, "wc: %s: ", argv[i]); perror(""); return 1; } count_file(in); printf(" %s\n", argv[i]); fclose(in); } return 0; }
/* * Lists the contents of a .hash file */ int main(int argc, char **argv) { opts opts; int i, c; int64_t count = 0; opts.long_format = 0; opts.count_only = 0; opts.verbose = 0; while ((c = getopt(argc, argv, "lcvh")) != -1) { switch (c) { case 'l': opts.long_format = 1; break; case 'c': opts.count_only = 1; break; case 'v': opts.verbose = 1; break; case 'h': usage(0); default: usage(1); } } for (i = optind; i < argc; i++) { int64_t c; if (opts.count_only) c = count_file(argv[i], &opts); else c = list_file(argv[i], &opts); if (c < 0) return 1; if (opts.verbose) printf("%s: %"PRId64" sequences\n", argv[i], c); count += c; } if (opts.count_only) printf("%"PRId64"\n", count); return 0; }