9 #define WINDOW_SIZE 4096
11 static int razf_main_usage()
14 printf("Usage: razip [options] [file] ...\n\n");
15 printf("Options: -c write on standard output, keep original files unchanged\n");
16 printf(" -d decompress\n");
17 printf(" -l list compressed file contents\n");
18 printf(" -b INT decompress at INT position in the uncompressed file\n");
19 printf(" -s INT decompress INT bytes in the uncompressed file\n");
20 printf(" -h give this help\n");
25 static int write_open(const char *fn, int is_forced)
30 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0666)) < 0 && errno == EEXIST) {
31 printf("razip: %s already exists; do you wish to overwrite (y or n)? ", fn);
33 if (c != 'Y' && c != 'y') {
34 printf("razip: not overwritten\n");
40 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
41 fprintf(stderr, "razip: %s: Fail to write\n", fn);
48 int main(int argc, char **argv)
50 int c, compress, pstdout, is_forced;
53 long start, end, size;
55 compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0;
56 while((c = getopt(argc, argv, "cdlhfb:s:")) >= 0){
58 case 'h': return razf_main_usage();
59 case 'd': compress = 0; break;
60 case 'c': pstdout = 1; break;
61 case 'l': compress = 2; break;
62 case 'b': start = atol(optarg); break;
63 case 's': size = atol(optarg); break;
64 case 'f': is_forced = 1; break;
67 if (size >= 0) end = start + size;
68 if(end >= 0 && end < start){
69 fprintf(stderr, " -- Illegal region: [%ld, %ld] --\n", start, end);
73 int f_src, f_dst = -1;
75 if((f_src = open(argv[optind], O_RDONLY)) < 0){
76 fprintf(stderr, " -- Cannot open file: %s --\n", argv[optind]);
80 f_dst = fileno(stdout);
82 char *name = malloc(sizeof(strlen(argv[optind]) + 5));
83 strcpy(name, argv[optind]);
85 f_dst = write_open(name, is_forced);
86 if (f_dst < 0) return 1;
90 f_src = fileno(stdin);
91 f_dst = fileno(stdout);
92 } else return razf_main_usage();
93 rz = razf_dopen(f_dst, "w");
94 buffer = malloc(WINDOW_SIZE);
95 while((c = read(f_src, buffer, WINDOW_SIZE)) > 0) razf_write(rz, buffer, c);
96 razf_close(rz); // f_dst will be closed here
97 if (argc > optind) unlink(argv[optind]);
102 if(argc <= optind) return razf_main_usage();
104 rz = razf_open(argv[optind], "r");
105 if(rz->file_type == FILE_TYPE_RZ) {
106 printf("%20s%20s%7s %s\n", "compressed", "uncompressed", "ratio", "name");
107 printf("%20lld%20lld%6.1f%% %s\n", (long long)rz->end, (long long)rz->src_end, rz->end * 100.0f / rz->src_end,
109 } else fprintf(stdout, "%s is not a regular rz file\n", argv[optind]);
112 if (argc > optind && !pstdout) {
114 if (strstr(argv[optind], ".rz") - argv[optind] != strlen(argv[optind]) - 3) {
115 printf("razip: %s: unknown suffix -- ignored\n", argv[optind]);
118 name = strdup(argv[optind]);
119 name[strlen(name) - 3] = '\0';
120 f_dst = write_open(name, is_forced);
122 } else f_dst = fileno(stdout);
123 rz = razf_open(argv[optind], "r");
124 buffer = malloc(WINDOW_SIZE);
125 razf_seek(rz, start, SEEK_SET);
127 if(end < 0) c = razf_read(rz, buffer, WINDOW_SIZE);
128 else c = razf_read(rz, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start));
131 write(f_dst, buffer, c);
132 if(end >= 0 && start >= end) break;
135 if (!pstdout) unlink(argv[optind]);