2022-05-24 02:27:07 +02:00
|
|
|
#include <sys/wait.h>
|
2022-05-22 09:55:04 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "error.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
char *program;
|
|
|
|
|
2022-05-24 02:46:34 +02:00
|
|
|
int spawn_redirect(const void *arg)
|
|
|
|
{
|
2022-05-25 23:57:50 +02:00
|
|
|
int *fds = (int *)arg;
|
2022-05-24 02:46:34 +02:00
|
|
|
|
2022-05-25 23:57:50 +02:00
|
|
|
ERRCHK_RET(close(fds[0]) == -1, FUNCFAILED("close"), ERRNOS);
|
|
|
|
ERRCHK_RET(dup2(fds[1], fds[2]) == -1, FUNCFAILED("dup2"), ERRNOS);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spawn_wait(pid_t pid, int *exitcode)
|
|
|
|
{
|
|
|
|
int stat;
|
|
|
|
ERRCHK_RET(waitpid(pid, &stat, 0) == -1, FUNCFAILED("waitpid"), ERRNOS);
|
|
|
|
|
|
|
|
if (exitcode && WIFEXITED(stat))
|
|
|
|
*exitcode = WEXITSTATUS(stat);
|
2022-05-24 02:46:34 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-05-22 09:55:04 +02:00
|
|
|
/*
|
|
|
|
* Call command
|
|
|
|
*
|
|
|
|
* If cpid is NULL, wait for the command to finish executing;
|
|
|
|
* otherwise store pid in cpid
|
2022-05-23 03:32:09 +02:00
|
|
|
*
|
2022-05-24 02:46:34 +02:00
|
|
|
* cfunc is a function to call when child process is created
|
2022-05-22 09:55:04 +02:00
|
|
|
*/
|
2022-05-24 02:46:34 +02:00
|
|
|
int spawn(char *args[], pid_t *cpid, int *exitcode, int (*cfunc)(const void *),
|
|
|
|
const void *carg)
|
2022-05-22 09:55:04 +02:00
|
|
|
{
|
|
|
|
if (exitcode)
|
|
|
|
*exitcode = -1;
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
2022-05-24 02:27:07 +02:00
|
|
|
ERRCHK_RET(pid == -1, FUNCFAILED("fork"), ERRNOS);
|
2022-05-22 09:55:04 +02:00
|
|
|
|
|
|
|
/* Child process */
|
|
|
|
if (pid == 0) {
|
2022-05-24 02:46:34 +02:00
|
|
|
if (cfunc && (cfunc(carg) != OK))
|
|
|
|
exit(EXIT_FAILURE);
|
2022-05-23 03:32:09 +02:00
|
|
|
|
2022-05-22 09:55:04 +02:00
|
|
|
execvp(args[0], args);
|
2022-05-25 23:57:50 +02:00
|
|
|
if (errno == ENOENT)
|
|
|
|
exit(NOTEXIST_EC);
|
|
|
|
|
2022-05-24 02:27:07 +02:00
|
|
|
PRINTINTERR(FUNCFAILED("exec"), ERRNOS);
|
2022-05-22 09:55:04 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:57:50 +02:00
|
|
|
if (cpid)
|
2022-05-22 09:55:04 +02:00
|
|
|
*cpid = pid;
|
|
|
|
|
2022-05-25 23:57:50 +02:00
|
|
|
if (exitcode)
|
|
|
|
ERRCHK_RET_OK(spawn_wait(pid, exitcode));
|
2022-05-22 09:55:04 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:45:35 +02:00
|
|
|
int strcmpnull(char const *s1, char const *s2)
|
|
|
|
{
|
|
|
|
if (!s1 && !s2)
|
|
|
|
return 0;
|
|
|
|
else if (s1 && !s2)
|
|
|
|
return 1;
|
|
|
|
else if (!s1 && s2)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return strcmp(s1, s2);
|
|
|
|
}
|
|
|
|
|
2022-05-22 09:55:04 +02:00
|
|
|
CharVec char_v_new(size_t cap)
|
|
|
|
{
|
|
|
|
CharVec v;
|
|
|
|
v.buf = NULL;
|
|
|
|
v.len = 0;
|
|
|
|
v.cap = cap;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void char_v_free(CharVec *v)
|
|
|
|
{
|
|
|
|
if (v->buf) {
|
|
|
|
free(v->buf);
|
|
|
|
v->buf = NULL;
|
|
|
|
v->len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void char_v_append(CharVec *v, char c)
|
|
|
|
{
|
|
|
|
if (!v->buf) {
|
|
|
|
v->buf = malloc(v->cap * sizeof(v->buf[0]));
|
|
|
|
if (!v->buf) {
|
2022-05-24 02:27:07 +02:00
|
|
|
PRINTINTERR(FUNCFAILED("malloc"), ERRNOS);
|
2022-05-22 09:55:04 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
v->buf[0] = '\0';
|
|
|
|
v->len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->len + 1 >= v->cap) {
|
|
|
|
v->cap *= 2;
|
|
|
|
v->buf = realloc(v->buf, v->cap * sizeof(v->buf[0]));
|
|
|
|
if (!v->buf) {
|
2022-05-24 02:27:07 +02:00
|
|
|
PRINTINTERR(FUNCFAILED("realloc"), ERRNOS);
|
2022-05-22 09:55:04 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v->buf[v->len - 1] = c;
|
|
|
|
v->buf[v->len] = '\0';
|
|
|
|
v->len++;
|
|
|
|
}
|