-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack.c
executable file
·55 lines (41 loc) · 1.29 KB
/
stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "utils.h"
int stack_main(int argc, char *argv[]){
if ( optind == argc || argc > optind + 1) {
fprintf(stderr, "\nUsage: atlas-utils stack <zotu>\n\n");
return 1;
}
gzFile fp;
fp = strcmp(argv[1], "-")? gzopen(argv[1], "r") : gzdopen(fileno(stdin), "r");
if (fp) {
kstream_t *ks;
ks = ks_init(fp);
int i, *fields, m, n;
kstring_t kt = {0, 0, 0};
kstring_t kv = {0, 0, 0};
int *elem;
if( ks_getuntil(ks, '\n', &kt, 0) ){
if(kt.s[0] == '\t') kputc('#', &kv);
kputs(kt.s, &kv);
elem = ksplit(&kv, '\t', &m);
}else{
fprintf(stderr, "[ERR]: file empty? \n");
return 0;
}
int line = 0;
while( ks_getuntil( ks, '\n', &kt, 0) >= 0){
fields = ksplit(&kt, '\t', &n);
for (i = 0; i < n; ++i){
if(strcmp(kt.s, kv.s+elem[ i ]) != 0) printf("%s\t%s\t%s\n", kt.s, kv.s+elem[ i ], kt.s + fields[i]);
}
++line;
}
free(kv.s);
free(kt.s);
ks_destroy(ks);
gzclose(fp);
}else{
fprintf(stderr, "[ERR]: can't open file %s\n", argv[1]);
exit(1);
}
return 0;
}