Added exec functions
This commit is contained in:
parent
1a5c23fd35
commit
3ba59aa2ab
|
@ -1,9 +1,17 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
. ../scripts/flags.sh
|
. ../scripts/flags.sh
|
||||||
|
BUILDFLAGS="$CFLAGS -Iinclude"
|
||||||
|
LINTCHECKS="-checks=-*,clang-analyzer-*,bugprone-*,portability-*,cert-*"
|
||||||
|
SOURCE="src/*.c"
|
||||||
|
|
||||||
function build() {
|
function build() {
|
||||||
cc $CFLAGS -Iinclude -shared -o "lib/libXmd.so" src/*.c || \
|
cc $SOURCE -o "lib/libXmd.so" -shared $BUILDFLAGS || \
|
||||||
|
echo "XXX FAIL!"
|
||||||
|
}
|
||||||
|
|
||||||
|
function lint() {
|
||||||
|
clang-tidy $SOURCE $LINTCHECKS -- $BUILDFLAGS || \
|
||||||
echo "XXX FAIL!"
|
echo "XXX FAIL!"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +29,9 @@ install)
|
||||||
clean)
|
clean)
|
||||||
clean
|
clean
|
||||||
;;
|
;;
|
||||||
|
lint)
|
||||||
|
lint
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
build
|
build
|
||||||
esac
|
esac
|
||||||
|
|
|
@ -13,18 +13,20 @@ typedef struct _XmdBuffer XmdBuffer;
|
||||||
#define XmdBufferNew(type) _XmdBufferNew(sizeof(type));
|
#define XmdBufferNew(type) _XmdBufferNew(sizeof(type));
|
||||||
XmdBuffer *_XmdBufferNew (Cardinal size);
|
XmdBuffer *_XmdBufferNew (Cardinal size);
|
||||||
|
|
||||||
/* XmdBufferPush adds a new element to the buffer. */
|
/* XmdBufferPush adds a new element to the buffer. If element is NULL, the new
|
||||||
|
element in the buffer is set to all zeros. */
|
||||||
void XmdBufferPush (XmdBuffer *buffer, void *element);
|
void XmdBufferPush (XmdBuffer *buffer, void *element);
|
||||||
|
|
||||||
/* XmdBufferPop copies the last element into element, and removes it from the
|
/* XmdBufferPop copies the last element into element, and removes it from the
|
||||||
buffer. */
|
buffer. If element is NULL, it removes the element without copying it. */
|
||||||
void XmdBufferPop (XmdBuffer *buffer, void *element);
|
void XmdBufferPop (XmdBuffer *buffer, void *element);
|
||||||
|
|
||||||
/* XmdBufferPoke sets a single element of the buffer. */
|
/* XmdBufferPoke sets a single element of the buffer. If element is NULL, the
|
||||||
|
element in the buffer is instead set to all zeros. */
|
||||||
void XmdBufferPoke (XmdBuffer *buffer, Cardinal index, void *element);
|
void XmdBufferPoke (XmdBuffer *buffer, Cardinal index, void *element);
|
||||||
|
|
||||||
/* XmdBufferPeek copies the value of a single element of the buffer into
|
/* XmdBufferPeek copies the value of a single element of the buffer into
|
||||||
element. */
|
element. If element is NULL, this function does nothing. */
|
||||||
void XmdBufferPeek (XmdBuffer *buffer, Cardinal index, void *element);
|
void XmdBufferPeek (XmdBuffer *buffer, Cardinal index, void *element);
|
||||||
|
|
||||||
/* XmdBufferBreak frees the buffer without freeing its data. Its data is
|
/* XmdBufferBreak frees the buffer without freeing its data. Its data is
|
||||||
|
|
12
libXmd/include/Xmd/Exec.h
Normal file
12
libXmd/include/Xmd/Exec.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef _XmdExec_h
|
||||||
|
#define _XmdExec_h
|
||||||
|
|
||||||
|
#include <X11/Intrinsic.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
FILE *XmdVaPipedExecPath (const String file, pid_t *child, String mode, ...);
|
||||||
|
|
||||||
|
FILE *XmdPipedExecPath (const String file, pid_t *child, String mode, String const argv[]);
|
||||||
|
|
||||||
|
#endif
|
|
@ -40,17 +40,21 @@ void XmdBufferResize (XmdBuffer *buffer, Cardinal length) {
|
||||||
|
|
||||||
void XmdBufferFit (XmdBuffer *buffer) {
|
void XmdBufferFit (XmdBuffer *buffer) {
|
||||||
if (buffer->length > buffer->capacity) {
|
if (buffer->length > buffer->capacity) {
|
||||||
puts("fitting");
|
|
||||||
buffer->capacity *= XmdBUFFER_GROWTH_FACTOR;
|
buffer->capacity *= XmdBUFFER_GROWTH_FACTOR;
|
||||||
buffer->data = XtReallocArray (
|
void *data = XtReallocArray (
|
||||||
buffer->data,
|
buffer->data,
|
||||||
buffer->capacity,
|
buffer->capacity,
|
||||||
buffer->size);
|
buffer->size);
|
||||||
|
if (data == NULL) {
|
||||||
|
/* TODO return error */
|
||||||
|
}
|
||||||
|
buffer->data = data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void *XmdBufferOffset (XmdBuffer *buffer, Cardinal index) {
|
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) {
|
void XmdBufferPush (XmdBuffer *buffer, void *element) {
|
||||||
|
@ -67,14 +71,19 @@ void XmdBufferPop (XmdBuffer *buffer, void *element) {
|
||||||
|
|
||||||
void XmdBufferPoke (XmdBuffer *buffer, Cardinal index, void *element) {
|
void XmdBufferPoke (XmdBuffer *buffer, Cardinal index, void *element) {
|
||||||
if (index > buffer->length) return;
|
if (index > buffer->length) return;
|
||||||
memcpy (
|
char *destination = XmdBufferOffset(buffer, index);
|
||||||
XmdBufferOffset(buffer, index),
|
char *source = element;
|
||||||
element,
|
|
||||||
buffer->size);
|
if (source == NULL) {
|
||||||
|
memset(destination, 0, buffer->size);
|
||||||
|
} else {
|
||||||
|
memcpy(destination, source, buffer->size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void XmdBufferPeek (XmdBuffer *buffer, Cardinal index, void *element) {
|
void XmdBufferPeek (XmdBuffer *buffer, Cardinal index, void *element) {
|
||||||
if (index > buffer->length) return;
|
if (index > buffer->length) return;
|
||||||
|
if (element == NULL) return;
|
||||||
memcpy (
|
memcpy (
|
||||||
element,
|
element,
|
||||||
XmdBufferOffset(buffer, index),
|
XmdBufferOffset(buffer, index),
|
||||||
|
|
49
libXmd/src/Exec.c
Normal file
49
libXmd/src/Exec.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user