-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.cpp
More file actions
390 lines (326 loc) · 10.8 KB
/
helper.cpp
File metadata and controls
390 lines (326 loc) · 10.8 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "helper.h"
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <sys/wait.h>
#include <iomanip>
#include "Commands.h"
#include <cstring>
#include <regex>
#include <cstddef>
#include <thread>
#include <fcntl.h>
#include <cmath>
#include <experimental/filesystem>
#include <sys/stat.h>
#include <memory>
using namespace std;
const std::string WHITESPACE = " \n\r\t\f\v";
#if 0
#define FUNC_ENTRY() \
cout << __PRETTY_FUNCTION__ << " --> " << endl;
#define FUNC_EXIT() \
cout << __PRETTY_FUNCTION__ << " <-- " << endl;
#else
#define FUNC_ENTRY()
#define FUNC_EXIT()
#endif
string _ltrim(const std::string &s) {
size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
string _rtrim(const std::string &s) {
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
string _trim(const std::string &s) {
return _rtrim(_ltrim(s));
}
std::size_t _parseCommandLine(const char *cmd_line, char **args) {
FUNC_ENTRY()
std::size_t i = 0;
std::istringstream iss(_trim(string(cmd_line)).c_str());
for (std::string s; iss >> s;) {
args[i] = (char *) malloc(s.length() + 1);
memset(args[i], 0, s.length() + 1);
strcpy(args[i], s.c_str());
args[++i] = NULL;
}
return i;
FUNC_EXIT()
}
std::vector<std::string> splitTokens(const std::string& s) {
std::vector<std::string> toks;
size_t i = 0, n = s.size();
while (i < n) {
// skip spaces
while (i < n && isspace(static_cast<unsigned char>(s[i]))) i++;
if (i >= n) break;
size_t j = i;
while (j < n && !isspace(static_cast<unsigned char>(s[j]))) j++;
toks.emplace_back(s.substr(i, j - i));
i = j;
}
return toks;
}
bool _isBackgroundComamnd(const char *cmd_line) {
const string str(cmd_line);
return str[str.find_last_not_of(WHITESPACE)] == '&';
}
void _removeBackgroundSign(char *cmd_line) {
const string str(cmd_line);
// find last character other than spaces
unsigned int idx = str.find_last_not_of(WHITESPACE);
// if all characters are spaces then return
if (idx == string::npos) {
return;
}
// if the command line does not end with & then return
if (cmd_line[idx] != '&') {
return;
}
// replace the & (background sign) with space and then remove all tailing spaces.
cmd_line[idx] = ' ';
// truncate the command line string up to the last non-space character
cmd_line[str.find_last_not_of(WHITESPACE, idx) + 1] = 0;
}
void charPtrArrayToStringArray(char* const src[],
std::string dest[],
std::size_t n)
{
for (std::size_t i = 0; i < n; ++i) {
dest[i] = src[i] ? src[i] : "";
}
}
std::vector<std::string> extract_env_var_names(const std::vector<char>& env_file) {
std::vector<std::string> names;
size_t i = 0;
while (i < env_file.size()) {
if (env_file[i] != '\0') {
std::string entry(&env_file[i]); // Whole "NAME=VALUE" string
size_t equal_pos = entry.find('=');
if (equal_pos != std::string::npos && equal_pos > 0) {
names.emplace_back(entry.substr(0, equal_pos)); // Only NAME part
}
i += entry.length() + 1; // Skip to next NUL-separated entry
} else {
++i; // Skip any unexpected NULs
}
}
return names;
}
std::vector<char> environ_file_to_vector() {
// build the path to /proc/<pid>/environ
const std::string path = "/proc/" + std::to_string(getpid()) + "/environ";
// read the entire file into a string (throws on error)
std::string raw;
readFileContent(path, raw);
// move its bytes into a vector<char>
return std::vector<char>(raw.begin(), raw.end());
}
void remove_env_var(const std::string& varname) {
size_t varlen = varname.length();
for (char **env = __environ; *env != nullptr; ++env) {
if (std::strncmp(*env, varname.c_str(), varlen) == 0 && (*env)[varlen] == '=') {
// Found it
char **next = env;
do {
next[0] = next[1]; // Shift all pointers left
++next;
} while (next[-1] != nullptr); // Stop when NULL pointer shifted
break; // Only remove the first occurrence
}
}
}
uint64_t get_total_cpu_time() {
std::string content;
try {
readFileContent("/proc/stat", content);
} catch (...) {
return 0;
}
auto lines = splitLines(content);
if (lines.empty()){
return 0;
}
const std::string& first = lines[0];
// Tokenize on whitespace
auto toks = splitTokens(first);
// toks[0] == "cpu", then user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice
if (toks.size() < 11) return 0;
// Sum fields 1..10
uint64_t total = 0;
for (size_t idx = 1; idx <= 10; ++idx) {
total += std::stoull(toks[idx]);
}
return total;
}
uint64_t get_process_cpu_time(pid_t pid) {
// build path to /proc/<pid>/stat
const std::string path = "/proc/" + std::to_string(pid) + "/stat";
std::string content;
// read the entire file (throws on failure)
try {
readFileContent(path, content);
}catch(...){
return 0;
}
// split into lines and take the first one
auto lines = splitLines(content);
const std::string& firstLine = lines[0];
// split the line on whitespace into tokens
auto toks = splitTokens(firstLine);
uint64_t utime = std::stoull(toks[13]);
uint64_t stime = std::stoull(toks[14]);
return utime + stime;
}
double get_mem_mb(pid_t pid) {
const std::string path = "/proc/" + std::to_string(pid) + "/statm";
std::string content;
try{
readFileContent(path, content);
}catch(...){
return 0;
}
auto toks = splitTokens(content);
if (toks.size() < 2) {
throw std::runtime_error("get_mem_mb: malformed statm file");
}
uint64_t pages_resident = std::stoull(toks[1]);
long page_size = sysconf(_SC_PAGESIZE);
double bytes = static_cast<double>(pages_resident) * page_size;
return bytes / (1024.0 * 1024.0);
}
bool isComplexCommand(std::string args_cmd[], const int numOfArgs){
for(int i = 0; i < numOfArgs; i++){
if(args_cmd[i].find_first_of("*?") != std::string::npos){
return true;
}
}
return false;
}
std::pair<std::string, std::size_t>* findSpecial(const char* cmdLine)
{
if (!cmdLine) return nullptr;
const char* p = cmdLine;
std::size_t idx = 0;
while (*p) {
/* skip ordinary characters that are not | or > */
if (*p != '|' && *p != '>') {
++p; ++idx;
continue;
}
/* ---- candidate found ---- */
if (*p == '|') {
if (*(p + 1) == '&') { // |&
return new std::pair<std::string,std::size_t>("|&", idx);
} else { // |
return new std::pair<std::string,std::size_t>("|", idx);
}
}
else if (*p == '>') {
if (*(p + 1) == '>') { // >>
return new std::pair<std::string,std::size_t>(">>", idx);
} else { // >
return new std::pair<std::string,std::size_t>(">", idx);
}
}
}
return nullptr; // nothing found
}
std::pair<std::string, std::size_t>* findAndSplitSpecial(std::string args[], std::size_t &numArgs) {
static const char* ops[] = {"|&", ">>", "|", ">"};
for (std::size_t i = 0; i < numArgs; ++i) {
const std::string &token = args[i];
for (const char* opC : ops) {
std::string op(opC);
std::size_t pos = token.find(op);
if (pos != std::string::npos) {
// Compute left and right substrings
std::string left = token.substr(0, pos);
std::string right = token.substr(pos + op.size());
// Determine how many new slots we need
std::size_t insertCount = 1 + (left.empty() ? 0 : 1) + (right.empty() ? 0 : 1);
// Shift existing elements to make room
for (std::size_t j = numArgs; j-- > i + 1; ) {
args[j + insertCount - 1] = args[j];
}
// Insert left, op, right
std::size_t idx = i;
if (!left.empty()) {
args[idx++] = left;
}
args[idx++] = op;
if (!right.empty()) {
args[idx++] = right;
}
// Update the count
numArgs += insertCount - 1;
// Compute the position of the operator
std::size_t opIndex = i + (left.empty() ? 0 : 1);
return new std::pair<std::string, std::size_t>(op, opIndex);
}
}
}
return nullptr;
}
int findPipePlacement(std::string args[], int numOfArgs) {
for (int i = 0; i < numOfArgs; i++) {
if (args[i] == "|") {
return i;
}
}
return 0;
}
std::vector<std::string> charArrayToVector(char* arr[], std::size_t length) {
std::vector<std::string> result;
result.reserve(length);
for (std::size_t i = 0; i < length; ++i) {
if (arr[i]) // skip nullptrs if any
result.emplace_back(arr[i]); // construct std::string from char*
}
return result;
}
std::string* vectorToStringArray(const std::vector<std::string>& vec) {
std::size_t n = vec.size();
std::string* arr = new std::string[n];
for (std::size_t i = 0; i < n; ++i) {
arr[i] = vec[i]; // copy each string
}
return arr;
}
void readFileContent(const std::string& path, std::string& content) {
// Open the file for reading only
int fd = open(path.c_str(), O_RDONLY);
if (fd == -1) {
perror("smash error: open failed");
return;
}
constexpr size_t BUFFER_SIZE = 4096;
char buffer[BUFFER_SIZE];
ssize_t bytesRead;
// Repeatedly read until EOF
while ((bytesRead = read(fd, buffer, BUFFER_SIZE)) > 0) {
content.append(buffer, static_cast<size_t>(bytesRead));
}
if (bytesRead == -1) {
close(fd);
perror("smash error: read failed");
return;
}
close(fd);
}
std::vector<std::string> splitLines(const std::string& s) {
std::vector<std::string> lines;
size_t start = 0;
while (start < s.size()) {
size_t pos = s.find('\n', start);
if (pos == std::string::npos) pos = s.size();
lines.emplace_back(s.substr(start, pos - start));
start = pos + 1;
}
return lines;
}