79152586

Date: 2024-11-03 11:26:13
Score: 1
Natty:
Report link
// Pass std::string to execvp() example:

const size_t BUFFER_SIZE = 1024;

int run(const std::string &s) {

char buf[BUFFER_SIZE];
char* p = buf;

char* argp[BUFFER_SIZE/4];
char** pp = argp;
*(pp++) = p;

for (const auto c:s) {
  char next_char = c;
  if (c == ' ') {
    next_char = '\0';
    *(pp++) = (p + 1);     
  }
  *p++ = next_char;
  *p = '\0';
}
*pp = nullptr;

return(execvp(*argp, (char**)argp));

}

Reasons:
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: iz56