-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathexecute_cmd.cpp
85 lines (74 loc) · 1.83 KB
/
execute_cmd.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* .============.
* // M A K E / \
* // C++ DEV / \
* // E A S Y / \/ \
* ++ ----------. \/\ .
* \\ \ \ /\ /
* \\ \ \ /
* \\ \ \ /
* -============'
*
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
*
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
* Use of this source code is governed by MIT license that can be found
* in the LICENSE file in the root of the source tree. All contributing
* project authors may be found in the CONTRIBUTORS.md file in the root
* of the source tree.
*/
#include "execute_cmd.h"
#include <unistd.h>
#include <fcntl.h>
#include <tbox/base/log.h>
namespace tbox {
namespace util {
bool ExecuteCmd(const std::string &cmd)
{
int ret = 0;
return ExecuteCmd(cmd, ret);
}
bool ExecuteCmd(const std::string &cmd, int &ret)
{
ret = ::system(cmd.c_str());
#if 0
LogTrace("system(\"%s\") = %d", cmd.c_str(), ret);
#endif
if (ret != 0) {
LogWarn("system(\"%s\") = %d", cmd.c_str(), ret);
return false;
}
return true;
}
bool ExecuteCmd(const std::string &cmd, std::string &result)
{
int ret = 0;
return ExecuteCmd(cmd, result, ret);
}
bool ExecuteCmd(const std::string &cmd, std::string &result, int &ret)
{
auto fp = ::popen(cmd.c_str(), "r");
if (fp == nullptr) {
LogWarn("popen(\"%s\") fail", cmd.c_str());
return false;
}
for (;;) {
char buff[1025];
auto rsize = ::fread(buff, 1, (sizeof(buff) - 1), fp);
if (rsize == 0)
break;
buff[rsize] = '\0';
result += buff;
}
ret = pclose(fp);
#if 0
LogTrace("popen(\"%s\") = %d, \"%s\"", cmd.c_str(), ret, buff);
#endif
if (ret != 0) {
LogWarn("pclose ret:%d", ret);
return false;
}
return true;
}
}
}