From ce382cc2cb5061fa29a1a3bf26bf808a00a96a49 Mon Sep 17 00:00:00 2001 From: Stefan Seyfried Date: Sun, 8 Feb 2015 13:05:53 +0100 Subject: [PATCH] helpers: add "backtick" function for shell-like `command` --- src/system/helpers.cpp | 17 +++++++++++++++++ src/system/helpers.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index 56984f41e..80e6604e1 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -363,6 +363,23 @@ std::string find_executable(const char *name) return ""; } +std::string backtick(std::string command) +{ + char *buf = NULL; + size_t n = 0; + pid_t pid; + FILE *p = my_popen(pid, command.c_str(), "r"); + if (! p) + return ""; + std::string out = ""; + while (getline(&buf, &n, p) >= 0) + out.append(std::string(buf)); + free(buf); + fclose(p); + waitpid(pid, NULL, 0); + return out; +} + std::string _getPathName(std::string &path, std::string sep) { size_t pos = path.find_last_of(sep); diff --git a/src/system/helpers.h b/src/system/helpers.h index 692d435ac..ea8131647 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -55,6 +55,8 @@ bool get_mem_usage(unsigned long &total, unsigned long &free); void mySleep(int sec); std::string find_executable(const char *name); +/* basically what "foo=`command`" does in the shell */ +std::string backtick(std::string command); bool hdd_get_standby(const char * fname); void hdd_flush(const char * fname);