Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #161 from youpong/rewrite-split
Browse files Browse the repository at this point in the history
rewrite split
  • Loading branch information
TanmayPatil105 authored Aug 10, 2024
2 parents b2b660d + ac52b91 commit ad2a719
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,14 @@ class Command
lines = 0;
}

static char **split(string cmd)
static void split(vector<char *> &v, string cmd)
{
vector<string> v = {};

istringstream ss{cmd};
for (string arg{}; getline(ss, arg, ' '); )
{
if (arg != "")
v.push_back(arg);
}

char **argv = new char*[v.size() + 1]; // +1 for the terminating NULL pointer
char **p = argv;
for (auto &s : v)
{
*p++ = std::strcpy(new char[s.length() + 1], s.c_str());
}
*p = (char *)0; // terminated by a NULL pointer
v.push_back(strdup(arg.c_str()));

return argv;
v.push_back((char *)0);
}

public:
Expand Down Expand Up @@ -316,7 +304,9 @@ class Command
throw runtime_error("dup2 failed");
}

char **argv = split(cmd);
vector<char *>v{};
split(v, cmd);
auto argv = v.data();
execvp(argv[0], argv);

// If execvp() returns, an error have occured.
Expand Down

0 comments on commit ad2a719

Please sign in to comment.