Skip to content

Commit

Permalink
Add option for output file, redirect data there
Browse files Browse the repository at this point in the history
  • Loading branch information
i-ky committed Feb 25, 2023
1 parent 15102c7 commit d7ad848
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/basset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using std::cerr;
using std::cout;
using std::ifstream;
using std::ofstream;
using std::ostream;
using std::string;
using std::to_string;
Expand All @@ -33,6 +34,7 @@ int main(int argc, char *argv[]) {
};

bool verbose{false};
string output{"compile_commands.json"};

while (*argv != nullptr) {
if (*argv == "--"s) {
Expand All @@ -48,6 +50,13 @@ int main(int argc, char *argv[]) {
verbose = true;
} else if (*argv == "--no-verbose"s) {
verbose = false;
} else if (*argv == "--output"s) {
if (*++argv == nullptr) {
cerr << "--output requires a value\n";
return -1;
}

output = *argv;
} else {
cerr << "unsupported option: " << *argv << '\n';
usage(cerr);
Expand Down Expand Up @@ -95,6 +104,13 @@ int main(int argc, char *argv[]) {
return -1;
}

ofstream out(output);

if (!out) {
cerr << "cannot open '" << output << "'\n";
return -1;
}

while (auto pid = wait(&wstatus)) {
if (pid == -1) {
perror("cannot wait()");
Expand All @@ -118,7 +134,7 @@ int main(int argc, char *argv[]) {
return -1;
}

cerr << string(exe, ret) << '\n';
out << string(exe, ret) << '\n';

char cwd[PATH_MAX];
ret = readlink(("/proc/" + to_string(pid) + "/cwd").c_str(), cwd,
Expand All @@ -129,12 +145,12 @@ int main(int argc, char *argv[]) {
return -1;
}

cerr << string(cwd, ret) << '\n';
out << string(cwd, ret) << '\n';

ifstream cmdline("/proc/" + to_string(pid) + "/cmdline");

for (string arg; getline(cmdline, arg, '\0');) {
cerr << '\t' << arg.data() << '\n';
out << '\t' << arg.data() << '\n';
}

if (!cmdline.eof()) {
Expand Down

0 comments on commit d7ad848

Please sign in to comment.