summaryrefslogtreecommitdiff
path: root/data.c
diff options
context:
space:
mode:
authorGravatar default2022-11-23 20:25:57 +0100
committerGravatar default2022-11-23 20:25:57 +0100
commit1bd69bc6111ad750b422cfc0e9be04200d2e6e73 (patch)
treeea1957940fad2293a2b073587fbb1c92601456ba /data.c
parentMore listfile functions. (diff)
downloadsnac2-1bd69bc6111ad750b422cfc0e9be04200d2e6e73.tar.gz
snac2-1bd69bc6111ad750b422cfc0e9be04200d2e6e73.tar.xz
snac2-1bd69bc6111ad750b422cfc0e9be04200d2e6e73.zip
Some tweaks to the new listfile functions.
Diffstat (limited to 'data.c')
-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)
324 flock(fileno(i), LOCK_EX); 324 flock(fileno(i), LOCK_EX);
325 325
326 xs *nfn = xs_fmt("%s.new", fn); 326 xs *nfn = xs_fmt("%s.new", fn);
327 char line[32 + 1]; 327 char line[256];
328 328
329 if ((o = fopen(nfn, "w")) != NULL) { 329 if ((o = fopen(nfn, "w")) != NULL) {
330 while (fgets(line, sizeof(line), i) != NULL) { 330 while (fgets(line, sizeof(line), i) != NULL) {
331 line[32] = '\0';
331 if (memcmp(line, md5, 32) != 0) 332 if (memcmp(line, md5, 32) != 0)
332 fwrite(line, sizeof(line), 1, o); 333 fprintf(o, "%s\n", line);
333 } 334 }
334 335
335 fclose(o); 336 fclose(o);
336 337
337 xs *ofn = xs_fmt("%s.old", fn); 338 xs *ofn = xs_fmt("%s.bak", fn);
338 339
339 link(fn, ofn); 340 link(fn, ofn);
340 rename(nfn, fn); 341 rename(nfn, fn);
@@ -354,14 +355,14 @@ int listfile_del_md5(const char *fn, const char *md5)
354d_char *listfile_get_n(const char *fn, int max) 355d_char *listfile_get_n(const char *fn, int max)
355/* returns a list */ 356/* returns a list */
356{ 357{
357 xs *list = NULL; 358 d_char *list = NULL;
358 FILE *f; 359 FILE *f;
359 int n = 0; 360 int n = 0;
360 361
361 if ((f = fopen(fn, "r")) != NULL) { 362 if ((f = fopen(fn, "r")) != NULL) {
362 flock(fileno(f), LOCK_SH); 363 flock(fileno(f), LOCK_SH);
363 364
364 char line[32 + 1]; 365 char line[256];
365 list = xs_list_new(); 366 list = xs_list_new();
366 367
367 while (n < max && fgets(line, sizeof(line), f) != NULL) { 368 while (n < max && fgets(line, sizeof(line), f) != NULL) {
@@ -380,25 +381,25 @@ d_char *listfile_get_n(const char *fn, int max)
380d_char *listfile_get_inv_n(const char *fn, int max) 381d_char *listfile_get_inv_n(const char *fn, int max)
381/* returns a list, inversely */ 382/* returns a list, inversely */
382{ 383{
383 xs *list = NULL; 384 d_char *list = NULL;
384 FILE *f; 385 FILE *f;
385 int n = 0; 386 int n = 0;
386 387
387 if ((f = fopen(fn, "r")) != NULL) { 388 if ((f = fopen(fn, "r")) != NULL) {
388 flock(fileno(f), LOCK_SH); 389 flock(fileno(f), LOCK_SH);
389 390
390 char line[32 + 1]; 391 char line[256];
391 list = xs_list_new(); 392 list = xs_list_new();
392 393
393 /* move to the end minus one entry */ 394 /* move to the end minus one entry */
394 if (!fseek(f, 0, SEEK_END) && !fseek(f, -sizeof(line), SEEK_SET)) { 395 if (!fseek(f, 0, SEEK_END) && !fseek(f, -33, SEEK_CUR)) {
395 while (n < max && fgets(line, sizeof(line), f) != NULL) { 396 while (n < max && fgets(line, sizeof(line), f) != NULL) {
396 line[32] = '\0'; 397 line[32] = '\0';
397 list = xs_list_append(list, line); 398 list = xs_list_append(list, line);
398 n++; 399 n++;
399 400
400 /* move backwards 2 entries */ 401 /* move backwards 2 entries */
401 if (fseek(f, -sizeof(line) * 2, SEEK_SET) == -1) 402 if (fseek(f, -66, SEEK_CUR) == -1)
402 break; 403 break;
403 } 404 }
404 } 405 }