diff options
Diffstat (limited to 'FICS/makerank.c')
-rw-r--r-- | FICS/makerank.c | 120 |
1 files changed, 79 insertions, 41 deletions
diff --git a/FICS/makerank.c b/FICS/makerank.c index 57956ed..4458f31 100644 --- a/FICS/makerank.c +++ b/FICS/makerank.c @@ -24,10 +24,10 @@ static int rtype; static int GetPlayerInfo(char *fileName, ENTRY *e) { - FILE *fp; - char NameWithCase[30]; - char field[20]; - char line[100]; + FILE *fp = NULL; + char NameWithCase[30] = { '\0' }; + char field[20] = { '\0' }; + char line[100] = { '\0' }; int done = 0; e->computer = 0; @@ -38,22 +38,32 @@ GetPlayerInfo(char *fileName, ENTRY *e) } if ((fp = fopen(fileName, "r")) == NULL || - fgets(line, sizeof line - 1, fp) == NULL || - feof(fp)) + fgets(line, sizeof line, fp) == NULL || + feof(fp)) { + if (fp) + fclose(fp); return 0; + } if (!strcmp(line, "v 1\n")) { - fgets(line, 99, fp); - - sscanf(line, "%s", e->name); - - fgets(line, 99, fp); - fgets(line, 99, fp); - fgets(line, 99, fp); + _Static_assert(ARRAY_SIZE(e->name) > 19, "Array too little"); + + if (fgets(line, sizeof line, fp) == NULL || + sscanf(line, "%19s", e->name) != 1) { + warnx("%s: fgets() or sscanf() error", __func__); + fclose(fp); + return 0; + } else if (fgets(line, sizeof line, fp) == NULL || + fgets(line, sizeof line, fp) == NULL || + fgets(line, sizeof line, fp) == NULL) { + warnx("%s: fgets() error", __func__); +// fclose(fp); +// return 0; + } - if (fscanf(fp, "%u %*u %*u %*u %u %*u %*u %*u %*u %u %*u %*u " - "%*u %u %*u %*u %*u %*u %u %*u %*u %*u %u %*u %*u %*u %*u " - "%u %*u %*u %*u %u %*u %*u %*u %*u", + if (fscanf(fp, "%d %*u %*u %*u %d %*u %*u %*u %*u %d %*u %*u " + "%*u %d %*u %*u %*u %*u %d %*u %*u %*u %d %*u %*u %*u %*u " + "%d %*u %*u %*u %d %*u %*u %*u %*u", &(e->r[0].num), &(e->r[0].rating), &(e->r[1].num), @@ -64,13 +74,31 @@ GetPlayerInfo(char *fileName, ENTRY *e) &(e->r[3].rating)) != 8) { fprintf(stderr, "OOPS: couldn't parse player file %s." "\n", fileName); + fclose(fp); + return 0; } + + done = 1; } else { do { - sscanf(line, "%s", field); + _Static_assert(ARRAY_SIZE(field) > 19, + "Unexpected array length"); + _Static_assert(ARRAY_SIZE(NameWithCase) > 29, + "Unexpected array length"); + + if (sscanf(line, "%19s", field) != 1) { + warnx("%s: sscanf() error", __func__); + fclose(fp); + return 0; + } if (!strcmp(field, "Name:")) { - sscanf(line, "%*s %s", NameWithCase); + if (sscanf(line, "%*s %29s", NameWithCase) != 1) { + warnx("%s: expected name with case", + __func__); + fclose(fp); + return 0; + } if (strcasecmp(e->name, NameWithCase)) { printf("TROUBLE: %s's handle is " @@ -82,31 +110,44 @@ GetPlayerInfo(char *fileName, ENTRY *e) "strlcpy() truncated\n", __func__); } } else if (!strcmp(field, "S_NUM:")) { - sscanf(line, "%*s %d", &(e->r[0].num)); + if (sscanf(line, "%*s %d", &(e->r[0].num)) != 1) + warnx("%s: S_NUM error", __func__); } else if (!strcmp(field, "B_NUM:")) { - sscanf(line, "%*s %d", &(e->r[1].num)); + if (sscanf(line, "%*s %d", &(e->r[1].num)) != 1) + warnx("%s: B_NUM error", __func__); } else if (!strcmp(field, "W_NUM:")) { - sscanf(line, "%*s %d", &(e->r[2].num)); + if (sscanf(line, "%*s %d", &(e->r[2].num)) != 1) + warnx("%s: W_NUM error", __func__); } else if (!strcmp(field, "L_NUM:")) { - sscanf(line, "%*s %d", &(e->r[3].num)); + if (sscanf(line, "%*s %d", &(e->r[3].num)) != 1) + warnx("%s: L_NUM error", __func__); } else if (!strcmp(field, "S_RATING:")) { - sscanf(line, "%*s %d", &(e->r[0].rating)); + if (sscanf(line, "%*s %d", + &(e->r[0].rating)) != 1) + warnx("%s: S_RATING error", __func__); } else if (!strcmp(field, "B_RATING:")) { - sscanf(line, "%*s %d", &(e->r[1].rating)); + if (sscanf(line, "%*s %d", + &(e->r[1].rating)) != 1) + warnx("%s: B_RATING error", __func__); } else if (!strcmp(field, "W_RATING:")) { - sscanf(line, "%*s %d", &(e->r[2].rating)); + if (sscanf(line, "%*s %d", + &(e->r[2].rating)) != 1) + warnx("%s: W_RATING error", __func__); } else if (!strcmp(field, "L_RATING:")) { - sscanf(line, "%*s %d", &(e->r[3].rating)); + if (sscanf(line, "%*s %d", + &(e->r[3].rating)) != 1) + warnx("%s: L_RATING error", __func__); } else if (!strcmp(field, "Network:")) { done = 1; } - fgets(line, 99, fp); + if (fgets(line, sizeof line, fp) == NULL) + break; } while (!done && !feof(fp)); } fclose(fp); - return 1; + return (done ? 1 : 0); } static int @@ -117,7 +158,7 @@ LoadEntries(void) char command[90]; char letter1; char pathInput[80]; - int len, n = 0; + int n = 0; int listsize; listsize = 100; @@ -137,13 +178,11 @@ LoadEntries(void) continue; while (1) { - fgets(e.name, MAX_LOGIN_NAME, fpPlayerList); - - if (feof(fpPlayerList)) + if (fgets(e.name, sizeof(e.name), fpPlayerList) == NULL || + feof(fpPlayerList)) break; - len = strlen(e.name); - e.name[len - 1] = '\0'; + e.name[strcspn(e.name, "\n")] = '\0'; if (e.name[0] != letter1) { printf("File %c/%s: wrong directory.\n", @@ -181,9 +220,9 @@ LoadEntries(void) static int SetComputers(int n) { - FILE *fpComp; - char comp[30]; - char line[100]; + FILE *fpComp = NULL; + char comp[30] = { '\0' }; + char line[100] = { '\0' }; int i = 0; if (snprintf(line, sizeof line, "sort -f %s", COMPUTER_FILE) >= @@ -194,12 +233,11 @@ SetComputers(int n) return 0; while (i < n) { - fgets(comp, 29, fpComp); - - if (feof(fpComp)) + if (fgets(comp, sizeof comp, fpComp) == NULL || + feof(fpComp)) break; - comp[strlen(comp) - 1] = '\0'; + comp[strcspn(comp, "\n")] = '\0'; while (i < n && strcasecmp(list[i]->name, comp) < 0) i++; |