7 static int search_gcda(const char *str, int len)
10 for (i = 0; i < len - 6; i++)
11 if (str[i] == '.' && str[i+1] == 'g' && str[i+2] == 'c' &&
12 str[i+3] == 'd' && str[i+4] == 'a' && str[i+5] == 0)
17 static int is_good_char(char c)
19 return c >= ' ' && c < 0x7f;
22 static int is_good_path(char *path)
24 int len = strlen(path);
29 FILE *f = fopen(path, "rb");
38 printf("not good path: %s\n", path);
42 int main(int argc, char *argv[])
47 int l, pos, pos1, old_len, cwd_len;
49 if (argc != 2) return 1;
51 getcwd(cwd, sizeof(cwd));
52 cwd_len = strlen(cwd);
53 if (cwd[cwd_len-1] != '/') {
58 f = fopen(argv[1], "rb+");
59 if (f == NULL) return 2;
64 l = fread(buff, 1, sizeof(buff), f);
70 pos1 = search_gcda(buff + pos, l - pos);
72 fseek(f, -6, SEEK_CUR);
77 while (pos > 0 && is_good_char(buff[pos-1])) pos--;
80 fseek(f, -(sizeof(buff) + 16), SEEK_CUR);
84 // paths must start with /
85 while (pos < l && buff[pos] != '/') pos++;
89 if (!is_good_path(p)) {
94 if (strncmp(p, cwd, cwd_len) != 0) {
95 printf("can't handle: %s\n", p);
100 memmove(p, p + cwd_len, old_len - cwd_len + 1);
101 fseek(f, -(sizeof(buff) - pos), SEEK_CUR);
102 fwrite(p, 1, old_len, f);