Added exec functions

This commit is contained in:
Sasha Koshka
2023-11-09 23:23:44 -05:00
parent 1a5c23fd35
commit 3ba59aa2ab
5 changed files with 95 additions and 12 deletions

View File

@@ -40,17 +40,21 @@ void XmdBufferResize (XmdBuffer *buffer, Cardinal length) {
void XmdBufferFit (XmdBuffer *buffer) {
if (buffer->length > buffer->capacity) {
puts("fitting");
buffer->capacity *= XmdBUFFER_GROWTH_FACTOR;
buffer->data = XtReallocArray (
void *data = XtReallocArray (
buffer->data,
buffer->capacity,
buffer->size);
if (data == NULL) {
/* TODO return error */
}
buffer->data = data;
}
}
void *XmdBufferOffset (XmdBuffer *buffer, Cardinal index) {
return buffer->data + (index * buffer->size);
char *data = buffer->data;
return (void *)(data + ((size_t)(index) * buffer->size));
}
void XmdBufferPush (XmdBuffer *buffer, void *element) {
@@ -67,14 +71,19 @@ void XmdBufferPop (XmdBuffer *buffer, void *element) {
void XmdBufferPoke (XmdBuffer *buffer, Cardinal index, void *element) {
if (index > buffer->length) return;
memcpy (
XmdBufferOffset(buffer, index),
element,
buffer->size);
char *destination = XmdBufferOffset(buffer, index);
char *source = element;
if (source == NULL) {
memset(destination, 0, buffer->size);
} else {
memcpy(destination, source, buffer->size);
}
}
void XmdBufferPeek (XmdBuffer *buffer, Cardinal index, void *element) {
if (index > buffer->length) return;
if (element == NULL) return;
memcpy (
element,
XmdBufferOffset(buffer, index),

49
libXmd/src/Exec.c Normal file
View File

@@ -0,0 +1,49 @@
#define _POSIX_C_SOURCE 200112L
#include <Xmd/Exec.h>
#include <Xmd/Buffer.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
FILE *XmdVaPipedExecPath (const String file, pid_t *child, String mode, ...) {
XmdBuffer *buffer = XmdBufferNew(String);
va_list argList;
va_start(argList, mode);
while (1) {
String arg = va_arg(argList, String);
if (arg == NULL) break;
XmdBufferPush(buffer, &arg);
}
va_end(argList);
String *args = XmdBufferBreak(buffer);
FILE* pipe = XmdPipedExecPath(file, child, mode, args);
XtFree((char *)(args));
return pipe;
}
FILE *XmdPipedExecPath (const String file, pid_t *child, String mode, String const argv[]) {
int pipes[2];
pipe(pipes);
pid_t pid = fork();
switch (pid) {
case -1: /* fail */
return NULL;
case 0: /* child */
dup2(pipes[1], STDOUT_FILENO);
close(pipes[0]);
close(pipes[1]);
execvp(file, argv);
exit(EXIT_FAILURE);
return NULL;
default: /* parent */
close(pipes[1]);
*child = pid;
return fdopen(pipes[0], mode);
}
}