commit 1bd69bc
default
·
2022-11-23 19:25:57 +0000 UTC
parent 7f458f4
Some tweaks to the new listfile functions.
1 files changed,
+10,
-9
M
data.c
M
data.c
+10,
-9
1@@ -324,17 +324,18 @@ int listfile_del_md5(const char *fn, const char *md5)
2 flock(fileno(i), LOCK_EX);
3
4 xs *nfn = xs_fmt("%s.new", fn);
5- char line[32 + 1];
6+ char line[256];
7
8 if ((o = fopen(nfn, "w")) != NULL) {
9 while (fgets(line, sizeof(line), i) != NULL) {
10+ line[32] = '\0';
11 if (memcmp(line, md5, 32) != 0)
12- fwrite(line, sizeof(line), 1, o);
13+ fprintf(o, "%s\n", line);
14 }
15
16 fclose(o);
17
18- xs *ofn = xs_fmt("%s.old", fn);
19+ xs *ofn = xs_fmt("%s.bak", fn);
20
21 link(fn, ofn);
22 rename(nfn, fn);
23@@ -354,14 +355,14 @@ int listfile_del_md5(const char *fn, const char *md5)
24 d_char *listfile_get_n(const char *fn, int max)
25 /* returns a list */
26 {
27- xs *list = NULL;
28+ d_char *list = NULL;
29 FILE *f;
30 int n = 0;
31
32 if ((f = fopen(fn, "r")) != NULL) {
33 flock(fileno(f), LOCK_SH);
34
35- char line[32 + 1];
36+ char line[256];
37 list = xs_list_new();
38
39 while (n < max && fgets(line, sizeof(line), f) != NULL) {
40@@ -380,25 +381,25 @@ d_char *listfile_get_n(const char *fn, int max)
41 d_char *listfile_get_inv_n(const char *fn, int max)
42 /* returns a list, inversely */
43 {
44- xs *list = NULL;
45+ d_char *list = NULL;
46 FILE *f;
47 int n = 0;
48
49 if ((f = fopen(fn, "r")) != NULL) {
50 flock(fileno(f), LOCK_SH);
51
52- char line[32 + 1];
53+ char line[256];
54 list = xs_list_new();
55
56 /* move to the end minus one entry */
57- if (!fseek(f, 0, SEEK_END) && !fseek(f, -sizeof(line), SEEK_SET)) {
58+ if (!fseek(f, 0, SEEK_END) && !fseek(f, -33, SEEK_CUR)) {
59 while (n < max && fgets(line, sizeof(line), f) != NULL) {
60 line[32] = '\0';
61 list = xs_list_append(list, line);
62 n++;
63
64 /* move backwards 2 entries */
65- if (fseek(f, -sizeof(line) * 2, SEEK_SET) == -1)
66+ if (fseek(f, -66, SEEK_CUR) == -1)
67 break;
68 }
69 }