diff options
author | Markus Uhlin <markus@nifty-networks.net> | 2023-12-25 17:28:14 +0100 |
---|---|---|
committer | Markus Uhlin <markus@nifty-networks.net> | 2023-12-25 17:28:14 +0100 |
commit | d7e47955e60dfaf582542a3ce3a33643a4c90890 (patch) | |
tree | 9e9e1df80ed29de924c56533f2627cd1498b9e9f /FICS | |
parent | 1d3cda0e62bb6550f2a455855f8a970271d825f0 (diff) |
Reformatted truncate_file()
Diffstat (limited to 'FICS')
-rw-r--r-- | FICS/utils.c | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/FICS/utils.c b/FICS/utils.c index 7b2b0bf..ed70941 100644 --- a/FICS/utils.c +++ b/FICS/utils.c @@ -705,49 +705,58 @@ PUBLIC char *tenth_str(unsigned t, int spaces) return hms((t + 5) / 10, 0, 1, spaces); /* Round it */ } +/* + * XXX: if lines in the file are greater than 1024 bytes in length, + * this won't work! + */ +PUBLIC int +truncate_file(char *file, int lines) +{ #define MAX_TRUNC_SIZE 100 + FILE *fp; + char tBuf[MAX_TRUNC_SIZE][MAX_LINE_SIZE]; + int bptr = 0, trunc = 0, i; -/* Warning, if lines in the file are greater than 1024 bytes in length, this - won't work! */ -/* nasty bug fixed by mann, 3-12-95 */ -PUBLIC int truncate_file(char *file, int lines) -{ - FILE *fp; - int bptr = 0, trunc = 0, i; - char tBuf[MAX_TRUNC_SIZE][MAX_LINE_SIZE]; + if (lines > MAX_TRUNC_SIZE) + lines = MAX_TRUNC_SIZE; - if (lines > MAX_TRUNC_SIZE) - lines = MAX_TRUNC_SIZE; - fp = fopen(file, "r"); - if (!fp) - return 1; - while (!feof(fp)) { - fgets(tBuf[bptr], MAX_LINE_SIZE, fp); - if (feof(fp)) - break; - if (tBuf[bptr][strlen(tBuf[bptr]) - 1] != '\n') { /* Line too long */ - fclose(fp); - return -1; - } - bptr++; - if (bptr == lines) { - trunc = 1; - bptr = 0; - } - } - fclose(fp); - if (trunc) { - fp = fopen(file, "w"); - for (i = 0; i < lines; i++) { - fputs(tBuf[bptr], fp); - bptr++; - if (bptr == lines) { - bptr = 0; - } - } - fclose(fp); - } - return 0; + if ((fp = fopen(file, "r")) == NULL) + return 1; + + while (!feof(fp)) { + fgets(tBuf[bptr], MAX_LINE_SIZE, fp); + + if (feof(fp)) + break; + + if (tBuf[bptr][strlen(tBuf[bptr]) - 1] != '\n') { + // Line too long + fclose(fp); + return -1; + } + + if (++bptr == lines) { + trunc = 1; + bptr = 0; + } + } + + fclose(fp); + + if (trunc) { + fp = fopen(file, "w"); + + for (i = 0; i < lines; i++) { + fputs(tBuf[bptr], fp); + + if (++bptr == lines) + bptr = 0; + } + + fclose(fp); + } + + return 0; } /* |