view spawn.cpp @ 3:ae3a6d7bcca6 default tip

cache the package list for better performance
author Yasuri <yasuri@charliesmp.net>
date Sun, 13 Sep 2026 19:48:34 +0000
parents 00b62a080354
children
line wrap: on
line source

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <spawn.h>
#include <cstring>
#include <string>
#include "process.h"
extern char** environ; // idk what this does
std::string spawn(char* const argv[])
{
    pid_t pid;
    int status;
    status = posix_spawnp(&pid, argv[0], NULL, NULL, const_cast<char* const*>(argv), environ);
    if (status != 0) {
        std::cerr << "posix_spawnp failed for " << argv[0] << ": " << strerror(status) << std::endl;
        return "error";
    }
    if (waitpid(pid, &status, 0) == -1) {
        perror("waitpid");
        return "error";
    }
    if (WIFEXITED(status)) {
        return std::to_string(WEXITSTATUS(status));
    }
    if (WIFSIGNALED(status)) {
        return "sig:" + std::to_string(WTERMSIG(status));
    }
    return "success";
}