diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 121 |
1 files changed, 69 insertions, 52 deletions
@@ -99,6 +99,57 @@ access_authorized(const char *file) || (!loggedin && file[0] != '.'); } +int +handle_download(const char *scandir) +{ + char *infopath, *modelpath; + size_t i, size; + FILE *f; + + infopath = aprintf("%s/%s", scandir, "info"); + if (!(f = fopen(infopath, "r"))) { + fprintf(stderr, "Selected result is missing!\n"); + goto cleanup; + } + free_info(&cached); + if (load_info(&cached, f) != OK) { + fprintf(stderr, "Failed to parse info file!\n"); + goto cleanup; + } + fclose(f); + f = NULL; + + print_info(&cached); + + if (strchr(ask("Download the model? "), 'y')) { + modelpath = aprintf("%s/%s", scandir, "model"); + if (!(f = fopen(modelpath, "r"))) { + fprintf(stderr, "Failed to access file!\n"); + goto cleanup; + } + fseek(f, 0, SEEK_END); + size = ftell(f); + fseek(f, 0, SEEK_SET); + if (size > MAXFILESIZE) { + fprintf(stderr, "File is too large to send!\n"); + goto cleanup; + } + printf("Here you go.. (%liB)\n", size); + while ((i = getc(f)) != EOF) + putc(i, stdout); + fclose(f); + f = NULL; + } + + return 0; + +cleanup: + if (f) fclose(f); + free(infopath); + free(modelpath); + return 1; +} + void cat_cmd(const char *arg) { @@ -176,14 +227,11 @@ cleanup: void search_cmd(const char *arg) { - char *end, *scandir = NULL, *infopath = NULL, - *modelpath = NULL, **paths = NULL; - int i, which, dirstart, ishidden, pathc, pathcap = 100; - const char *hash, *name; + char *end, *scandir = NULL, **paths = NULL; + int i, which, pathc, pathcap = 100; + const char *hash, *name, *resp; struct dirent *de; DIR *d = NULL; - FILE *f = NULL; - size_t size; if (arg && !strcmp(arg, "last")) { if (!cached.valid) { @@ -201,7 +249,6 @@ search_cmd(const char *arg) } paths = checkp(malloc(pathcap * sizeof(char*))); - dirstart = telldir(d); for (pathc = 0; (de = readdir(d));) { if (access_authorized(de->d_name) && !strpfcmp(hash, de->d_name + (loggedin ? 1 : 0))) { @@ -220,55 +267,25 @@ search_cmd(const char *arg) goto cleanup; } - which = strtoul(ask("Which of these results? "), &end, 10); - if (which >= pathc || which < 0 || *end) { - fprintf(stderr, "Invalid index!\n"); - goto cleanup; - } + while (1) { + resp = ask("Which result [q to quit]? "); + if (strchr(resp, 'q')) break; + which = strtoul(resp, &end, 10); + if (which >= pathc || which < 0 || *end) { + fprintf(stderr, "Invalid index!\n"); + goto cleanup; + } - scandir = aprintf("%s/%s", resultdir, paths[which]); + scandir = aprintf("%s/%s", resultdir, paths[which]); - infopath = aprintf("%s/%s", scandir, "info"); - if (!(f = fopen(infopath, "r"))) { - fprintf(stderr, "Selected result is missing!\n"); - goto cleanup; - } - free_info(&cached); - if (load_info(&cached, f) != OK) { - fprintf(stderr, "Failed to parse info file!\n"); - goto cleanup; - } - fclose(f); - f = NULL; + if (handle_download(scandir)) goto cleanup; - print_info(&cached); - - if (strchr(ask("Download the model? "), 'y')) { - modelpath = aprintf("%s/%s", scandir, "model"); - if (!(f = fopen(modelpath, "r"))) { - fprintf(stderr, "Failed to access file!\n"); - goto cleanup; - } - fseek(f, 0, SEEK_END); - size = ftell(f); - fseek(f, 0, SEEK_SET); - if (size > MAXFILESIZE) { - fprintf(stderr, "File is too large to send!\n"); - goto cleanup; - } - printf("Here you go.. (%liB)\n", size); - while ((i = getc(f)) != EOF) - putc(i, stdout); - fclose(f); - f = NULL; + free(scandir); + scandir = NULL; } cleanup: - if (f) fclose(f); - free(scandir); - free(infopath); - free(modelpath); for (i = 0; i < pathc; i++) free(paths[i]); free(paths); @@ -295,10 +312,10 @@ list_cmd(const char *arg) printf(">> %s\n", de->d_name); path = aprintf("%s/%s/info", resultdir, de->d_name); if ((f = fopen(path, "r"))) { - if (load_info(&info, f) != OK) - fprintf(stderr, "Failed to read saved file info!\n"); - else + if (load_info(&info, f) == OK) print_info(&info); + else + fprintf(stderr, "Failed to read saved file info!\n"); fclose(f); } free(path); |
