diff options
Diffstat (limited to 'FICS')
-rw-r--r-- | FICS/adminproc.c | 23 | ||||
-rw-r--r-- | FICS/command.c | 9 | ||||
-rw-r--r-- | FICS/comproc.c | 18 |
3 files changed, 32 insertions, 18 deletions
diff --git a/FICS/adminproc.c b/FICS/adminproc.c index 2478cab..dfe528e 100644 --- a/FICS/adminproc.c +++ b/FICS/adminproc.c @@ -225,11 +225,19 @@ add_item(char *new_item, char *filename) FILE *new_fp, *old_fp; char junk[MAX_LINE_SIZE] = { '\0' }; char tmp_file[MAX_FILENAME_SIZE] = { '\0' }; + int fd; msnprintf(tmp_file, sizeof tmp_file, "%s/.tmp.idx", news_dir); - if ((new_fp = fopen(tmp_file, "w")) == NULL) + fd = open(tmp_file, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR); + + if (fd < 0) + return 0; + else if ((new_fp = fdopen(fd, "w")) == NULL) { + close(fd); return 0; + } + fprintf(new_fp, "%s", new_item); if ((old_fp = fopen(filename, "r")) == NULL) @@ -384,6 +392,7 @@ com_anews(int p, param_list param) char filename[MAX_FILENAME_SIZE] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; char *junkp = NULL; + const char *v_scan_junk = "%" SCNd64 " " "%9s"; int found = 0; int64_t lval = 0; time_t crtime = 0; @@ -395,7 +404,6 @@ com_anews(int p, param_list param) return COM_OK; } -#define SCAN_JUNK ("%" SCNd64 " " "%9s") _Static_assert(9 < ARRAY_SIZE(count), "Array too small"); if (param[0].type == 0) { @@ -413,7 +421,7 @@ com_anews(int p, param_list param) fclose(fp); return COM_FAILED; } - if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + if (sscanf(junk, v_scan_junk, &lval, count) != 2) { warnx("%s: sscanf() error: too few items", __func__); fclose(fp); return COM_FAILED; @@ -442,7 +450,7 @@ com_anews(int p, param_list param) fclose(fp); return COM_FAILED; } - if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + if (sscanf(junk, v_scan_junk, &lval, count) != 2) { warnx("%s: sscanf() error: too few items", __func__); fclose(fp); return COM_FAILED; @@ -466,8 +474,11 @@ com_anews(int p, param_list param) break; if (strlen(junk) > 1) { - if (sscanf(junkp, SCAN_JUNK, &lval, count) != 2) - warnx("%s: sscanf() error...", __func__); + if (sscanf(junkp, v_scan_junk, &lval, + count) != 2) { + warnx("%s: sscanf() error...", + __func__); + } crtime = lval; diff --git a/FICS/command.c b/FICS/command.c index 301c305..bf69bf0 100644 --- a/FICS/command.c +++ b/FICS/command.c @@ -722,6 +722,7 @@ rscan_news(FILE *fp, int p, time_t lc) char count[10] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; char *junkp = NULL; + const char *scan_fmt = "%" SCNd64 " " "%9s"; int64_t lval = 0; time_t crtime = 0; @@ -731,7 +732,7 @@ rscan_news(FILE *fp, int p, time_t lc) _Static_assert(ARRAY_SIZE(count) > 9, "Unexpected array size"); - if (sscanf(junk, ("%" SCNd64 " " "%9s"), &lval, count) != 2) { + if (sscanf(junk, scan_fmt, &lval, count) != 2) { warnx("%s: sscanf() error: too few items", __func__); return; } @@ -755,12 +756,12 @@ rscan_news(FILE *fp, int p, time_t lc) PRIVATE void check_news(int p, int admin) { -#define SCAN_JUNK ("%" SCNd64 " " "%9s") FILE *fp = NULL; char count[10] = { '\0' }; char filename[MAX_FILENAME_SIZE] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; char *junkp = NULL; + const char *v_scan_fmt = "%" SCNd64 " " "%9s"; int64_t lval = 0; time_t crtime = 0; time_t lc = player_lastconnect(p); @@ -791,7 +792,7 @@ check_news(int p, int admin) warnx("%s: fgets() error", __func__); fclose(fp); return; - } else if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + } else if (sscanf(junk, v_scan_fmt, &lval, count) != 2) { warnx("%s: sscanf() error", __func__); fclose(fp); return; @@ -841,7 +842,7 @@ check_news(int p, int admin) warnx("%s: fgets() error", __func__); fclose(fp); return; - } else if (sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + } else if (sscanf(junk, v_scan_fmt, &lval, count) != 2) { warnx("%s: sscanf() error", __func__); fclose(fp); return; diff --git a/FICS/comproc.c b/FICS/comproc.c index 06592cc..b2504fd 100644 --- a/FICS/comproc.c +++ b/FICS/comproc.c @@ -136,6 +136,7 @@ rscan_news2(FILE *fp, int p, int num) char count[10] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; char *junkp; + const char *v_scan_fmt = "%" SCNd64 " " "%9s"; int64_t lval; time_t crtime; @@ -143,7 +144,7 @@ rscan_news2(FILE *fp, int p, int num) return; if (fgets(junk, sizeof junk, fp) == NULL || feof(fp) || - sscanf(junk, "%" SCNd64 " " "%9s", &lval, count) != 2) + sscanf(junk, v_scan_fmt, &lval, count) != 2) return; rscan_news2(fp, p, num - 1); @@ -164,6 +165,7 @@ com_news(int p, param_list param) char filename[MAX_FILENAME_SIZE] = { '\0' }; char junk[MAX_LINE_SIZE] = { '\0' }; char *junkp = NULL; + const char *v_scan_fmt = "%" SCNd64 " " "%9s"; int found = 0; int64_t lval = 0; time_t crtime = 0; @@ -175,7 +177,6 @@ com_news(int p, param_list param) return COM_OK; } -#define SCAN_JUNK ("%" SCNd64 " " "%9s") _Static_assert(9 < ARRAY_SIZE(count), "'count' too small"); if (param[0].type == 0) { @@ -187,7 +188,7 @@ com_news(int p, param_list param) pprintf(p, "Index of recent news items:\n"); if (fgets(junk, sizeof junk, fp) == NULL || - sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + sscanf(junk, v_scan_fmt, &lval, count) != 2) { warnx("%s: error: fgets() or sscanf()", __func__); fclose(fp); return COM_FAILED; @@ -212,7 +213,7 @@ com_news(int p, param_list param) pprintf(p, "Index of all news items:\n"); if (fgets(junk, sizeof junk, fp) == NULL || - sscanf(junk, SCAN_JUNK, &lval, count) != 2) { + sscanf(junk, v_scan_fmt, &lval, count) != 2) { warnx("%s: error: fgets() or sscanf()", __func__); fclose(fp); return COM_FAILED; @@ -238,7 +239,7 @@ com_news(int p, param_list param) if (fgets(junk, sizeof junk, fp) == NULL || feof(fp)) break; - if (sscanf(junkp, SCAN_JUNK, &lval, count) != 2) + if (sscanf(junkp, v_scan_fmt, &lval, count) != 2) warnx("%s: sscanf() error...", __func__); crtime = lval; @@ -765,6 +766,8 @@ plogins(int p, char *fname) FILE *fp = NULL; char ipstr[20] = { '\0' }; char loginName[MAX_LOGIN_NAME + 1] = { '\0' }; + const char *v_scan_fmt = "%" SCNu16 " %19s " "%" SCNd64 " " + "%d %19s\n"; int registered = 0; int64_t lval = 0; time_t tval = 0; @@ -778,10 +781,9 @@ plogins(int p, char *fname) _Static_assert(19 < ARRAY_SIZE(ipstr), "'ipstr' too small"); _Static_assert(19 < ARRAY_SIZE(loginName), "'loginName' too small"); -#define SCAN_FMT ("%" SCNu16 " %19s " "%" SCNd64 " " "%d %19s\n") while (!feof(fp)) { - if (fscanf(fp, SCAN_FMT, &inout, loginName, &lval, ®istered, - ipstr) != 5) { + if (fscanf(fp, v_scan_fmt, &inout, loginName, &lval, + ®istered, ipstr) != 5) { fprintf(stderr, "FICS: Error in login info format. " "%s\n", fname); fclose(fp); |