00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include "runprocess.h"
00023 #include "stringutils.h"
00024
00025 #include <stdio.h>
00026 #include <string>
00027
00028 #include "safesyswait.h"
00029
00030 #ifdef _MSC_VER
00031 # define popen _popen
00032 # define pclose _pclose
00033 #endif
00034
00035 using namespace std;
00036
00037 string
00038 stdout_to_string(const string &cmd)
00039 {
00040 string out;
00041 FILE * fh = popen(cmd.c_str(), "r");
00042 if (fh == NULL) throw ReadError();
00043 while (!feof(fh)) {
00044 char buf[4096];
00045 size_t len = fread(buf, 1, 4096, fh);
00046 if (ferror(fh)) {
00047 (void)pclose(fh);
00048 throw ReadError();
00049 }
00050 out.append(buf, len);
00051 }
00052 int status = pclose(fh);
00053
00054 if (status != 0) {
00055 if (WIFEXITED(status) && WEXITSTATUS(status) == 127) {
00056 throw NoSuchProgram();
00057 }
00058 throw ReadError();
00059 }
00060 while (out.size() > 0 && C_isspace(out[out.size() - 1])) {
00061 out.resize(out.size() - 1);
00062 }
00063 return out;
00064 }