commit ec1fb0c7bde7316b21c4facdcd43af92792487b5 from: Omar Polo date: Thu Feb 17 15:04:11 2022 UTC change res->file to be a path instead of a FILE * commit - 146307bd98c5a6ff3e9809c5a7c9620075f51d0c commit + ec1fb0c7bde7316b21c4facdcd43af92792487b5 blob - 7fc31cbfd0f2d91692dacf5e22367df7c3f7cf88 blob + a4afe1c5043cbec97e4d0b7c29aeec7c54574e29 --- amused.h +++ amused.h @@ -80,7 +80,7 @@ struct ctl_command; struct parse_result { enum actions action; char **files; - FILE *file; + const char *file; int pretty; struct ctl_command *ctl; }; blob - 20c199461842867b4d25f524ca6c20ec688105d2 blob + cfa495be900127654257412b4d990727e73e85c0 --- ctl.c +++ ctl.c @@ -247,6 +247,7 @@ show_status(struct imsg *imsg, int *ret) static int show_load(struct parse_result *res, struct imsg *imsg, int *ret) { + FILE *f; const char *file; char *line = NULL; char path[PATH_MAX]; @@ -269,7 +270,15 @@ show_load(struct parse_result *res, struct imsg *imsg, if (imsg->hdr.type != IMSG_CTL_BEGIN) fatalx("got unexpected message %d", imsg->hdr.type); - while ((linelen = getline(&line, &linesize, res->file)) != -1) { + if (res->file == NULL) + f = stdin; + else if ((f = fopen(res->file, "r")) == NULL) { + log_warn("can't open %s", res->file); + *ret = 1; + return 1; + } + + while ((linelen = getline(&line, &linesize, f)) != -1) { if (linelen == 0) continue; line[linelen-1] = '\0'; @@ -291,10 +300,9 @@ show_load(struct parse_result *res, struct imsg *imsg, } free(line); - if (ferror(res->file)) + if (ferror(f)) fatal("getline"); - fclose(res->file); - res->file = NULL; + fclose(f); if (!any) { *ret = 1; @@ -464,11 +472,10 @@ int ctl_load(struct parse_result *res, int argc, char **argv) { if (argc < 2) - res->file = stdin; - else if (argc == 2) { - if ((res->file = fopen(argv[1], "r")) == NULL) - fatal("open %s", argv[1]); - } else + res->file = NULL; + else if (argc == 2) + res->file = argv[1]; + else ctl_usage(res->ctl); if (pledge("stdio rpath", NULL) == -1)