int main(int argc, char** argv) { // Parse arguments size_t blocksize = 1; size_t stride = 1024; while (argc >= 3) { if (strcmp(argv[1], "-b") == 0) { blocksize = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else if (strcmp(argv[1], "-s") == 0) { stride = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else break; } // Allocate buffer, open files, measure file sizes assert(blocksize > 0); char* buf = malloc(blocksize); const char* in_filename = argc >= 2 ? argv[1] : NULL; io61_profile_begin(); io61_file* inf = io61_open_check(in_filename, O_RDONLY); size_t inf_size = io61_filesize(inf); if ((ssize_t) inf_size < 0) { fprintf(stderr, "stridecat61: input file is not seekable\n"); exit(1); } io61_file* outf = io61_fdopen(STDOUT_FILENO, O_WRONLY); // Copy file data size_t pos = 0, written = 0; while (written < inf_size) { // Copy a block ssize_t amount = io61_read(inf, buf, blocksize); if (amount <= 0) break; io61_write(outf, buf, amount); written += amount; // Move `inf` file position to next stride pos += stride; if (pos >= inf_size) { pos = (pos % stride) + blocksize; if (pos + blocksize > stride) blocksize = stride - pos; } int r = io61_seek(inf, pos); assert(r >= 0); } io61_close(inf); io61_close(outf); io61_profile_end(); }
int main(int argc, char** argv) { // Parse arguments ssize_t inf_size = -1; while (argc >= 3) { if (strcmp(argv[1], "-s") == 0) { inf_size = (ssize_t) strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else break; } // Open files, measure file sizes const char* in_filename = argc >= 2 ? argv[1] : NULL; io61_profile_begin(); io61_file* inf = io61_open_check(in_filename, O_RDONLY); io61_file* outf = io61_fdopen(STDOUT_FILENO, O_WRONLY); if (inf_size < 0) inf_size = io61_filesize(inf); if (inf_size < 0) { fprintf(stderr, "reverse61: can't get size of input file\n"); exit(1); } if (io61_seek(inf, 0) < 0) { fprintf(stderr, "reverse61: input file is not seekable\n"); exit(1); } while (inf_size != 0) { --inf_size; io61_seek(inf, inf_size); int ch = io61_readc(inf); io61_writec(outf, ch); } io61_close(inf); io61_close(outf); io61_profile_end(); }
int main(int argc, char** argv) { // Parse arguments size_t blocksize = 4096; size_t inf_size = -1; srandom(83419); while (argc >= 3) { if (strcmp(argv[1], "-b") == 0) { blocksize = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else if (strcmp(argv[1], "-r") == 0) { srandom(strtoul(argv[2], 0, 0)); argc -= 2, argv += 2; } else if (strcmp(argv[1], "-s") == 0) { inf_size = strtoul(argv[2], 0, 0); argc -= 2, argv += 2; } else break; } // Allocate buffer, open files, measure file sizes assert(blocksize > 0); char* buf = (char*) malloc(blocksize); const char* in_filename = argc >= 2 ? argv[1] : NULL; io61_profile_begin(); io61_file* inf = io61_open_check(in_filename, O_RDONLY); if ((ssize_t) inf_size < 0) inf_size = io61_filesize(inf); if ((ssize_t) inf_size < 0) { fprintf(stderr, "reordercat61: can't get size of input file\n"); exit(1); } if (io61_seek(inf, 0) < 0) { fprintf(stderr, "reordercat61: input file is not seekable\n"); exit(1); } io61_file* outf = io61_fdopen(STDOUT_FILENO, O_WRONLY); if (io61_seek(outf, 0) < 0) { fprintf(stderr, "reordercat61: output file is not seekable\n"); exit(1); } // Calculate random permutation of file's blocks size_t nblocks = inf_size / blocksize; if (nblocks > (30 << 20)) { fprintf(stderr, "reordercat61: file too large\n"); exit(1); } else if (nblocks * blocksize != inf_size) { fprintf(stderr, "reordercat61: input file size not a multiple of block size\n"); exit(1); } size_t* blockpos = (size_t*) malloc(sizeof(size_t) * nblocks); for (size_t i = 0; i < nblocks; ++i) blockpos[i] = i; // Copy file data while (nblocks != 0) { // Choose block to read size_t index = random() % nblocks; size_t pos = blockpos[index] * blocksize; blockpos[index] = blockpos[nblocks - 1]; --nblocks; // Transfer that block io61_seek(inf, pos); ssize_t amount = io61_read(inf, buf, blocksize); if (amount <= 0) break; io61_seek(outf, pos); io61_write(outf, buf, amount); } io61_close(inf); io61_close(outf); io61_profile_end(); free(buf); }