From b6e3f6cb5d1051777c35988f201e6730525e3389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Wed, 19 Jun 2024 23:52:56 +0900 Subject: =?UTF-8?q?=E6=8E=83=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/packages.c | 57 +++++++-------------------------------------------------- 1 file changed, 7 insertions(+), 50 deletions(-) (limited to 'src/packages.c') diff --git a/src/packages.c b/src/packages.c index e89eb82..3505815 100644 --- a/src/packages.c +++ b/src/packages.c @@ -1,65 +1,22 @@ #include "packages.h" +#include "common.h" -#include -#include -#include +#if defined(__linux__) #include -#if defined(__linux__) #include "distro.h" - -/*const char *distroname;*/ #endif -const char *run_package_command(const char *command) { - char buf[64]; - char *out = NULL; - size_t outsize = 0; - - FILE *p = popen(command, "r"); - if (!p) { - fprintf(stderr, "パッケージコマンドを実効に失敗: %s", command); - return NULL; - } - - while (fgets(buf, sizeof(buf), p) != NULL) { - buf[strcspn(buf, "\n")] = '\0'; - - size_t len = strlen(buf); - char *nout = realloc(out, outsize + len + 1); - if (nout == NULL) { - perror("メモリの役割に失敗"); - free(out); - pclose(p); - return NULL; - } - - out = nout; - - memccpy(out + outsize, buf, sizeof(buf), len); - outsize += len; - out[outsize] = '\0'; - } - - pclose(p); - - return out; -} - -void display_packages() { +const char *display_packages() { #if defined(__OpenBSD__) || defined(__NetBSD__) - printf("%s (pkg_info)", run_package_command("pkg_info -a | wc -l | " - "sed \"s/ //g\"")); + return run_command_s("pkg_info -a | wc -l | sed \"s/ //g\""); #elif defined(__FreeBSD__) || defined(__DragonFly__) - printf("%s (pkg)", run_package_command("pkg info -a | wc -l | " - "sed \"s/ //g\"")); + return run_command_s("pkg info -a | wc -l | sed \"s/ //g\""); #elif defined(__linux__) if (access("/bin/xbps-query", F_OK) != -1) { - printf("%s (xbps-query)", run_package_command("xbps-query -l | wc -l | " - "sed \"s/ //g\"")); + return run_command_s("xbps-query -l | wc -l | sed \"s/ //g\""); } else if (access("/usr/bin/dpkg-query", F_OK) != -1) { - printf("%s (dpkg)", run_package_command("dpkg-query -f '.\n' -W | wc -l | " - "sed \"s/ //g\"")); + return run_command_s("dpkg-query -f '.\n' -W | wc -l | sed \"s/ //g\""); } #endif } -- cgit v1.2.3