summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefault <nobody@localhost>2022-11-23 20:25:57 +0100
committerdefault <nobody@localhost>2022-11-23 20:25:57 +0100
commit1bd69bc6111ad750b422cfc0e9be04200d2e6e73 (patch)
treeea1957940fad2293a2b073587fbb1c92601456ba
parent7f458f4fd624a9182a5cf53c272f5ec6e47cb05a (diff)
Some tweaks to the new listfile functions.
-rw-r--r--data.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/data.c b/data.c
index 81ce92e..0ffc171 100644
--- a/data.c
+++ b/data.c
@@ -324,17 +324,18 @@ int listfile_del_md5(const char *fn, const char *md5)
flock(fileno(i), LOCK_EX);
xs *nfn = xs_fmt("%s.new", fn);
- char line[32 + 1];
+ char line[256];
if ((o = fopen(nfn, "w")) != NULL) {
while (fgets(line, sizeof(line), i) != NULL) {
+ line[32] = '\0';
if (memcmp(line, md5, 32) != 0)
- fwrite(line, sizeof(line), 1, o);
+ fprintf(o, "%s\n", line);
}
fclose(o);
- xs *ofn = xs_fmt("%s.old", fn);
+ xs *ofn = xs_fmt("%s.bak", fn);
link(fn, ofn);
rename(nfn, fn);
@@ -354,14 +355,14 @@ int listfile_del_md5(const char *fn, const char *md5)
d_char *listfile_get_n(const char *fn, int max)
/* returns a list */
{
- xs *list = NULL;
+ d_char *list = NULL;
FILE *f;
int n = 0;
if ((f = fopen(fn, "r")) != NULL) {
flock(fileno(f), LOCK_SH);
- char line[32 + 1];
+ char line[256];
list = xs_list_new();
while (n < max && fgets(line, sizeof(line), f) != NULL) {
@@ -380,25 +381,25 @@ d_char *listfile_get_n(const char *fn, int max)
d_char *listfile_get_inv_n(const char *fn, int max)
/* returns a list, inversely */
{
- xs *list = NULL;
+ d_char *list = NULL;
FILE *f;
int n = 0;
if ((f = fopen(fn, "r")) != NULL) {
flock(fileno(f), LOCK_SH);
- char line[32 + 1];
+ char line[256];
list = xs_list_new();
/* move to the end minus one entry */
- if (!fseek(f, 0, SEEK_END) && !fseek(f, -sizeof(line), SEEK_SET)) {
+ if (!fseek(f, 0, SEEK_END) && !fseek(f, -33, SEEK_CUR)) {
while (n < max && fgets(line, sizeof(line), f) != NULL) {
line[32] = '\0';
list = xs_list_append(list, line);
n++;
/* move backwards 2 entries */
- if (fseek(f, -sizeof(line) * 2, SEEK_SET) == -1)
+ if (fseek(f, -66, SEEK_CUR) == -1)
break;
}
}