1 #define _GNU_SOURCE /* See feature_test_macros(7) */
7 #define assert_success(e) do { if ((e) < 0) { perror(#e); abort(); } } while (0)
9 void set_env_prefix(char *env
, char *sep
, char *prefix
) {
10 char *existing
= getenv(env
);
13 assert_success(asprintf(&val
, "%s%s%s", prefix
, sep
, existing
));
14 assert_success(setenv(env
, val
, 1));
17 assert_success(setenv(env
, prefix
, 1));
21 void set_env_suffix(char *env
, char *sep
, char *suffix
) {
22 char *existing
= getenv(env
);
25 assert_success(asprintf(&val
, "%s%s%s", existing
, sep
, suffix
));
26 assert_success(setenv(env
, val
, 1));
29 assert_success(setenv(env
, suffix
, 1));
33 int main(int argc
, char **argv
) {
34 assert_success(setenv("MESSAGE", "HELLO", 0));
35 set_env_prefix("PATH", ":", "/usr/bin/");
36 set_env_suffix("PATH", ":", "/usr/local/bin/");
37 putenv("MESSAGE2=WORLD");
39 char **argv_tmp
= calloc(3 + argc
+ 0 + 1, sizeof(*argv_tmp
));
40 assert(argv_tmp
!= NULL
);
41 argv_tmp
[0] = argv
[0];
45 for (int i
= 1; i
< argc
; ++i
) {
46 argv_tmp
[3 + i
] = argv
[i
];
48 argv_tmp
[3 + argc
+ 0] = NULL
;
51 argv
[0] = "my-wrapper";
52 return execv("/send/me/flags", argv
);