File: SystemExecute.cpp

package info (click to toggle)
librepfunc 1.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: cpp: 1,768; makefile: 270
file content (25 lines) | stat: -rw-r--r-- 812 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*******************************************************************************
 * librepfunc - a collection of common functions, classes and tools.
 * See the README file for copyright information and how to reach the author.
 ******************************************************************************/
#include <repfunc.h>
#include <array>  // std::array
#include <cstdio> // popen(), feof(), fread()


int SystemExecute(const std::string& process, std::string& message) {
  auto f = popen(process.c_str(), "r");
  if (f == nullptr) {
     message = "Cannot open pipe.";
     return -1;
     }

  std::array<char, 256> buffer;
  size_t bytes;
  while(not std::feof(f)) {
     bytes = std::fread(buffer.data(), 1, buffer.size(), f);
     message.append(buffer.data(), bytes);
     }

  return pclose(f);
}