00001
00002 #include "util/file.hh"
00003 #include "util/read_compressed.hh"
00004
00005 #include <cstring>
00006 #include <iostream>
00007
00008 namespace {
00009 const std::size_t kBufSize = 16384;
00010 void Copy(util::ReadCompressed &from, int to) {
00011 util::scoped_malloc buffer(util::MallocOrThrow(kBufSize));
00012 while (std::size_t amount = from.Read(buffer.get(), kBufSize)) {
00013 util::WriteOrThrow(to, buffer.get(), amount);
00014 }
00015 }
00016 }
00017
00018 int main(int argc, char *argv[]) {
00019
00020 for (int i = 1; i < argc; ++i) {
00021 char *arg = argv[i];
00022 if (!strcmp(arg, "--")) break;
00023 if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
00024 std::cerr <<
00025 "A cat implementation that interprets compressed files.\n"
00026 "Usage: " << argv[0] << " [file1] [file2] ...\n"
00027 "If no file is provided, then stdin is read.\n";
00028 return 1;
00029 }
00030 }
00031
00032 try {
00033 if (argc == 1) {
00034 util::ReadCompressed in(0);
00035 Copy(in, 1);
00036 } else {
00037 for (int i = 1; i < argc; ++i) {
00038 util::ReadCompressed in(util::OpenReadOrThrow(argv[i]));
00039 Copy(in, 1);
00040 }
00041 }
00042 } catch (const std::exception &e) {
00043 std::cerr << e.what() << std::endl;
00044 return 2;
00045 }
00046 return 0;
00047 }