Skip to content

Commit

Permalink
param: shell: fix unused variable warning
Browse files Browse the repository at this point in the history
  • Loading branch information
flightlessmango committed Apr 8, 2024
1 parent fb0b559 commit cdd8043
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <thread>
#include <iostream>
#include <sys/wait.h>
#include <spdlog/spdlog.h>

std::string Shell::readOutput() {
std::string output;
Expand Down Expand Up @@ -37,8 +38,22 @@ std::string Shell::readOutput() {
}

Shell::Shell() {
pipe(to_shell);
pipe(from_shell);
static bool failed;
if (pipe(to_shell) == -1) {
SPDLOG_ERROR("Failed to create to_shell pipe: {}", strerror(errno));
failed = true;
}

if (pipe(from_shell) == -1) {
SPDLOG_ERROR("Failed to create from_shell pipe: {}", strerror(errno));
failed = true;
}

// if either pipe fails, there's no point in continuing.
if (failed){
SPDLOG_ERROR("Shell has failed, will not be able to use exec");
return;
}

shell_pid = fork();

Expand All @@ -58,9 +73,13 @@ Shell::Shell() {
// Set the read end of the from_shell pipe to non-blocking
setNonBlocking(from_shell[0]);
}
success = true;
}

std::string Shell::exec(std::string cmd) {
if (!success)
return "";

writeCommand(cmd);
return readOutput();
}
Expand Down
1 change: 1 addition & 0 deletions src/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Shell {
int to_shell[2];
int from_shell[2];
pid_t shell_pid;
bool success;

#ifdef __linux__
void setNonBlocking(int fd) {
Expand Down

0 comments on commit cdd8043

Please sign in to comment.