2021-10-04 22:35:09 +02:00
|
|
|
// makeCWrapper /path/to/executable \
|
|
|
|
--argv0 my-wrapper \
|
|
|
|
--set-default MESSAGE HELLO \
|
|
|
|
--prefix PATH : /usr/bin/ \
|
|
|
|
--suffix PATH : /usr/local/bin/ \
|
|
|
|
--add-flags "-x -y -z" \
|
|
|
|
--set MESSAGE2 WORLD
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2021-11-09 02:55:26 +01:00
|
|
|
#include <assert.h>
|
2021-12-01 22:49:20 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define assert_success(e) do { if ((e) < 0) { perror(#e); exit(1); } } while (0)
|
2021-10-04 22:35:09 +02:00
|
|
|
|
|
|
|
char *concat3(char *x, char *y, char *z) {
|
|
|
|
int xn = strlen(x);
|
|
|
|
int yn = strlen(y);
|
|
|
|
int zn = strlen(z);
|
|
|
|
char *res = malloc(sizeof(*res)*(xn + yn + zn + 1));
|
2021-11-09 02:55:26 +01:00
|
|
|
assert(res != NULL);
|
2021-10-04 22:35:09 +02:00
|
|
|
strncpy(res, x, xn);
|
|
|
|
strncpy(res + xn, y, yn);
|
|
|
|
strncpy(res + xn + yn, z, zn);
|
|
|
|
res[xn + yn + zn] = '\0';
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_env_prefix(char *env, char *sep, char *val) {
|
|
|
|
char *existing = getenv(env);
|
|
|
|
if (existing) val = concat3(val, sep, existing);
|
2021-12-01 22:49:20 +01:00
|
|
|
assert_success(setenv(env, val, 1));
|
2021-10-04 22:35:09 +02:00
|
|
|
if (existing) free(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_env_suffix(char *env, char *sep, char *val) {
|
|
|
|
char *existing = getenv(env);
|
|
|
|
if (existing) val = concat3(existing, sep, val);
|
2021-12-01 22:49:20 +01:00
|
|
|
assert_success(setenv(env, val, 1));
|
2021-10-04 22:35:09 +02:00
|
|
|
if (existing) free(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2021-12-01 22:49:20 +01:00
|
|
|
assert_success(setenv("MESSAGE", "HELLO", 0));
|
2021-10-04 22:35:09 +02:00
|
|
|
set_env_prefix("PATH", ":", "/usr/bin/");
|
|
|
|
set_env_suffix("PATH", ":", "/usr/local/bin/");
|
|
|
|
putenv("MESSAGE2=WORLD");
|
|
|
|
|
|
|
|
char **argv_tmp = malloc(sizeof(*argv_tmp) * (4 + argc));
|
2021-11-09 02:55:26 +01:00
|
|
|
assert(argv_tmp != NULL);
|
2021-10-04 22:35:09 +02:00
|
|
|
argv_tmp[0] = argv[0];
|
|
|
|
argv_tmp[1] = "-x";
|
|
|
|
argv_tmp[2] = "-y";
|
|
|
|
argv_tmp[3] = "-z";
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
argv_tmp[3 + i] = argv[i];
|
|
|
|
}
|
|
|
|
argv_tmp[3 + argc] = NULL;
|
|
|
|
argv = argv_tmp;
|
|
|
|
|
|
|
|
argv[0] = "my-wrapper";
|
|
|
|
return execv("/path/to/executable", argv);
|
|
|
|
}
|