32 #include <sys/select.h>
37 static const int WINDOW_SIZE = 64 * 1024;
39 static void error(
const char *format, ...)
43 vfprintf(stderr, format, ap);
48 static int write_open(
const char *fn,
int is_forced)
53 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0666)) < 0 && errno == EEXIST) {
54 fprintf(stderr,
"[bgzip] %s already exists; do you wish to overwrite (y or n)? ", fn);
55 if ( scanf(
"%c", &c) != 1 ) c =
'n';
56 if (c !=
'Y' && c !=
'y') {
57 fprintf(stderr,
"[bgzip] not overwritten\n");
63 if ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
64 fprintf(stderr,
"[bgzip] %s: Fail to write\n", fn);
71 static int bgzip_main_usage(
void)
73 fprintf(stderr,
"\n");
75 fprintf(stderr,
"Usage: bgzip [OPTIONS] [FILE] ...\n");
76 fprintf(stderr,
"Options:\n");
77 fprintf(stderr,
" -b, --offset INT decompress at virtual file pointer (0-based uncompressed offset)\n");
78 fprintf(stderr,
" -c, --stdout write on standard output, keep original files unchanged\n");
79 fprintf(stderr,
" -d, --decompress decompress\n");
80 fprintf(stderr,
" -f, --force overwrite files without asking\n");
81 fprintf(stderr,
" -h, --help give this help\n");
82 fprintf(stderr,
" -i, --index compress and create BGZF index\n");
83 fprintf(stderr,
" -I, --index-name FILE name of BGZF index file [file.gz.gzi]\n");
84 fprintf(stderr,
" -r, --reindex (re)index compressed file\n");
85 fprintf(stderr,
" -s, --size INT decompress INT bytes (uncompressed size)\n");
86 fprintf(stderr,
"\n");
90 int main(
int argc,
char **argv)
92 int c,
compress, pstdout, is_forced, index = 0, reindex = 0;
95 long start, end, size;
96 char *index_fname = NULL;
98 static struct option loptions[] =
103 {
"decompress",0,0,
'd'},
106 {
"index-name",1,0,
'I'},
112 compress = 1; pstdout = 0; start = 0; size = -1; end = -1; is_forced = 0;
113 while((c = getopt_long(argc, argv,
"cdh?fb:s:iI:r",loptions,NULL)) >= 0){
115 case 'd': compress = 0;
break;
116 case 'c': pstdout = 1;
break;
117 case 'b': start = atol(optarg); compress = 0; pstdout = 1;
break;
118 case 's': size = atol(optarg); pstdout = 1;
break;
119 case 'f': is_forced = 1;
break;
120 case 'i': index = 1;
break;
121 case 'I': index_fname = optarg;
break;
122 case 'r': reindex = 1; compress = 0;
break;
124 case '?':
return bgzip_main_usage();
127 if (size >= 0) end = start + size;
128 if (end >= 0 && end < start) {
129 fprintf(stderr,
"[bgzip] Illegal region: [%ld, %ld]\n", start, end);
134 int f_src = fileno(stdin);
135 int f_dst = fileno(stdout);
139 if ( stat(argv[optind],&sbuf)<0 )
141 fprintf(stderr,
"[bgzip] %s: %s\n", strerror(errno), argv[optind]);
145 if ((f_src = open(argv[optind], O_RDONLY)) < 0) {
146 fprintf(stderr,
"[bgzip] %s: %s\n", strerror(errno), argv[optind]);
151 f_dst = fileno(stdout);
154 char *name = malloc(strlen(argv[optind]) + 5);
155 strcpy(name, argv[optind]);
157 f_dst = write_open(name, is_forced);
158 if (f_dst < 0)
return 1;
162 else if (!pstdout && isatty(fileno((FILE *)stdout)) )
163 return bgzip_main_usage();
164 else if ( index && !index_fname )
166 fprintf(stderr,
"[bgzip] Index file name expected when writing to stdout\n");
172 buffer = malloc(WINDOW_SIZE);
173 while ((c =
read(f_src, buffer, WINDOW_SIZE)) > 0)
174 if (
bgzf_write(fp, buffer, c) < 0) error(
"Could not write %d bytes: Error %d\n", c, fp->
errcode);
182 if (argc > optind && !pstdout) unlink(argv[optind]);
192 if ( !fp ) error(
"[bgzip] Could not open file: %s\n", argv[optind]);
196 if ( !index_fname ) error(
"[bgzip] Index file name expected when reading from stdin\n");
198 if ( !fp ) error(
"[bgzip] Could not read from stdin: %s\n", strerror(errno));
206 if ( ret<0 ) error(
"Is the file gzipped or bgzipped? The latter is required for indexing.\n");
223 if ( stat(argv[optind],&sbuf)<0 )
225 fprintf(stderr,
"[bgzip] %s: %s\n", strerror(errno), argv[optind]);
229 int len = strlen(argv[optind]);
230 if ( strcmp(argv[optind]+len-3,
".gz") )
232 fprintf(stderr,
"[bgzip] %s: unknown suffix -- ignored\n", argv[optind]);
237 fprintf(stderr,
"[bgzip] Could not open file: %s\n", argv[optind]);
242 f_dst = fileno(stdout);
245 name = strdup(argv[optind]);
246 name[strlen(name) - 3] =
'\0';
247 f_dst = write_open(name, is_forced);
251 else if (!pstdout && isatty(fileno((FILE *)stdin)) )
252 return bgzip_main_usage();
255 f_dst = fileno(stdout);
258 fprintf(stderr,
"[bgzip] Could not read from stdin: %s\n", strerror(errno));
262 buffer = malloc(WINDOW_SIZE);
265 if (
bgzf_index_load(fp, argv[optind],
".gzi") < 0 ) error(
"Could not load index: %s.gzi\n", argv[optind]);
266 if (
bgzf_useek(fp, start,
SEEK_SET) < 0 ) error(
"Could not seek to %d-th (uncompressd) byte\n", start);
269 if (end < 0) c =
bgzf_read(fp, buffer, WINDOW_SIZE);
270 else c =
bgzf_read(fp, buffer, (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start));
272 if (c < 0) error(
"Could not read %d bytes: Error %d\n", (end - start > WINDOW_SIZE)? WINDOW_SIZE:(end - start), fp->
errcode);
274 if (
write(f_dst, buffer, c) != c ) error(
"Could not write %d bytes\n", c);
275 if (end >= 0 && start >= end)
break;
279 if (!pstdout) unlink(argv[optind]);