-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmunge_pg_93_to_82.c
88 lines (73 loc) · 1.91 KB
/
munge_pg_93_to_82.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char **argv){
if( argc != 2 ){
puts("ERROR! expected exactly 1 argument");
exit(1);
}
printf("Opening file '%s'\n", argv[1]);
FILE *f = fopen(argv[1], "r+b");
if( ! f ){
printf("Failed to open file\n");
exit(1);
}
char read[100];
// Replace line
// SET lock_timeout = 0;
// With
// --T lock_timeout = 0;
if( fseek(f, 62, SEEK_SET) ){
printf("First seek failed\n");
exit(1);
}
// check line before destroying
char *exp1 = "SET lock_timeout = 0;";
if( fread(read, 1, 21, f) != 21 ){
printf("First read failed\n");
exit(1);
}
if( strncmp(exp1, read, 21) ){
printf("First strncmp failed\n");
exit(1);
}
if( fseek(f, 62, SEEK_SET) ){
printf("First re-seek failed\n");
exit(1);
}
// comment out line
if( fwrite("--", 1, 2, f) != 2 ){
printf("First write failed\n");
exit(1);
}
if( fseek(f, 406, SEEK_SET) ){
printf("Second seek failed\n");
exit(1);
}
// Replace line
char * exp2 = "CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;";
// With
char * line = "CREATE PROCEDURAL LANGUAGE plpgsql;---------------------------";
if( fread(read, 1, 62, f) != 62 ){
printf("Second read failed\n");
exit(1);
}
if( strncmp(exp2, read, 62) ){
printf("Second strncmp failed\n");
read[62] = '\0';
printf("Expected '%s'\n", exp2);
printf("Got '%s'\n", read);
exit(1);
}
if( fseek(f, 406, SEEK_SET) ){
printf("Second re-seek failed\n");
exit(1);
}
// comment out line
if( fwrite(line, 1, 62, f) != 62 ){
printf("Second write failed\n");
exit(1);
}
puts("Success");
fclose(f);
}