diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 41 |
1 files changed, 22 insertions, 19 deletions
@@ -43,7 +43,7 @@ struct command commands[] = { { "auth", auth_cmd, "Login to upload files to a private dir." } }; -struct parseinfo cached; +struct parseinfo cached = { 0 }; char *resultdir; int echo = 0, loggedin = 0; @@ -52,7 +52,7 @@ save_submission(struct parseinfo *info, char *stldata, int stlsize) { char *dirpath = NULL, *infopath = NULL, *modelpath = NULL; FILE *f = NULL; - int status = 0; + int status = OK; DIR *d; if (loggedin) @@ -64,12 +64,12 @@ save_submission(struct parseinfo *info, char *stldata, int stlsize) modelpath = aprintf("%s/%s", dirpath, "model"); if (!(f = fopen(modelpath, "w+"))) goto fail; if (fwrite(stldata, 1, stlsize, f) != stlsize) goto fail; - NFCLOSE(f); + FCLOSE(f); infopath = aprintf("%s/%s", dirpath, "info"); if (!(f = fopen(infopath, "w+"))) goto fail; if (save_info(info, f) != OK) goto fail; - NFCLOSE(f); + FCLOSE(f); exit: if (f) fclose(f); @@ -85,7 +85,7 @@ fail: if (modelpath) remove(modelpath); if (dirpath) remove(dirpath); - status = 1; + status = FAIL; goto exit; } @@ -99,9 +99,9 @@ access_authorized(const char *file) int handle_download(const char *scandir) { - char *infopath, *modelpath; + char *infopath = NULL, *modelpath = NULL; size_t i, size; - int status = 0; + int status = OK; FILE *f; infopath = aprintf("%s/%s", scandir, "info"); @@ -109,12 +109,13 @@ handle_download(const char *scandir) ERR("Selected result is missing!\n"); goto fail; } + free_info(&cached); if (load_info(&cached, f) != OK) { ERR("Failed to parse info file!\n"); goto fail; } - NFCLOSE(f); + FCLOSE(f); print_info(&cached); @@ -134,7 +135,7 @@ handle_download(const char *scandir) printf("Here you go.. (%liB)\n", size); while ((i = getc(f)) != EOF) putc(i, stdout); - NFCLOSE(f); + FCLOSE(f); } exit: @@ -144,7 +145,7 @@ exit: return status; fail: - status = 1; + status = FAIL; goto exit; } @@ -276,12 +277,12 @@ search_cmd(const char *arg) } scandir = aprintf("%s/%s", resultdir, paths[which]); - if (handle_download(scandir)) goto cleanup; - NFREE(scandir); + if (handle_download(scandir) != OK) goto cleanup; + FREE(scandir); } cleanup: - free(scandir); + FREE(scandir); for (i = 0; i < pathc; i++) free(paths[i]); free(paths); @@ -309,13 +310,16 @@ list_cmd(const char *arg) path = aprintf("%s/%s/info", resultdir, de->d_name); if ((f = fopen(path, "r")) && load_info(&info, f) == OK) { print_info(&info); + free_info(&info); } else { - ERR("Failed to read saved file info!\n"); + ERR("Failed to read file info!\n"); } if (f) fclose(f); free(path); } } + + closedir(d); } void @@ -345,7 +349,7 @@ auth_cmd(const char *arg) free(resultdir); resultdir = ndir; loggedin = 1; - cached.valid = 0; + free_info(&cached); } void @@ -359,7 +363,6 @@ cleanexit() int main() { - struct command *c; const char *cmd, *envstr; char *cp, *arg; int exit, i, cmdlen; @@ -389,9 +392,9 @@ main() cmdlen = cp ? cp - cmd : strlen(cmd); for (i = 0; i < ARRSIZE(commands); i++) { - c = &commands[i]; - if (!strncmp(c->name, cmd, cmdlen) && !c->name[cmdlen]) { - c->func(arg); + if (!strncmp(commands[i].name, cmd, cmdlen) + && !commands[i].name[cmdlen]) { + commands[i].func(arg); break; } } |
