#include <config.h>
+/* For execvpe(). */
+#define _GNU_SOURCE
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include "example.h"
#include "../src/compiler.h"
return 1;
}
+static int cmp(void const *a, void const *b) {
+ return strcmp(*(char * const *)a, *(char * const *)b);
+}
static void dump(char *argv[]) {
size_t i;
while (*argv) {
printf("argv[%zu] = |%s|\n", i++, *argv++);
}
+
+ /* Order of environment variables is not defined, sort them for consistent
+ * test results. */
i = 0;
char **env = environ;
+ while (*env++) {
+ i++;
+ }
+ qsort(environ, i, sizeof(*env), cmp);
+
+ i = 0;
+ env = environ;
while (*env) {
/* Skip LD_PRELOAD which contains system dependent values. */
if (strncmp(*env, "LD_PRELOAD=", 11)) {
- printf("environ[%zu] = |%s|\n", i, *env);
+ printf("environ[%zu] = |%s|\n", i++, *env);
}
- i++;
env++;
}
printf("\n");
printf("\nCHECKING COLORING.\n\n");
fflush(stdout);
- dup2(2, 3);
+ xdup2(2, 3);
char *args[] = { argv0, NULL };
char *envp[] = { ldpreload, NULL };
execve(argv[0], args, envp);
return EXIT_FAILURE;
} else if (!skip--) {
- dup2(2, 4);
+ xdup2(2, 4);
execl(argv[0], argv0, NULL);
return EXIT_FAILURE;
} else if (!skip--) {
- dup2(2, 5);
+ xdup2(2, 5);
execlp(argv[0], argv0, NULL);
return EXIT_FAILURE;
} else if (!skip--) {
- dup2(2, 6);
+ xdup2(2, 6);
char *envp[] = { ldpreload, NULL };
execle(argv[0], argv0, NULL, envp);
return EXIT_FAILURE;
} else if (!skip--) {
- dup2(2, 7);
+ xdup2(2, 7);
/* Test closing a few descriptors. */
close(5);
execv(argv[0], args);
return EXIT_FAILURE;
} else if (!skip--) {
- dup2(2, 8);
+ xdup2(2, 8);
/* And a few more. */
close(7);
execvp(argv[0], args);
return EXIT_FAILURE;
- } else {
+
+ /* Test handling of COLORED_STDERR_FDS. */
+
+ } else if (!skip--) {
+ /* And the rest. */
close(3);
close(8);
+
+ xdup2(2, 5);
+
+ char *args[] = { argv0, NULL };
+ char *envp[] = { ldpreload, "COLORED_STDERR_FDS=5,", NULL };
+
+ execve(argv[0], args, envp);
+ return EXIT_FAILURE;
+ } else if (!skip--) {
+ char *args[] = { argv0, NULL };
+ char *envp[] = { ldpreload, NULL };
+
+ xdup2(5, 6);
+ close(5);
+
+ execve(argv[0], args, envp);
+ return EXIT_FAILURE;
+ } else if (!skip--) {
+ close(6);
+
+ char *args[] = { argv0, NULL };
+ setenv("COLORED_STDERR_FDS", "2,", 1);
+
+ execv(argv[0], args);
+ return EXIT_FAILURE;
}
execvp(argv[0], args);
return EXIT_FAILURE;
+
+#ifdef HAVE_EXECVPE
+ /* execvpe(3), but not testing the p(ath) part */
+ } else if (!skip--) {
+ char *args[] = { argv0, NULL };
+ char *envp[] = { "TEST=54", ldpreload, NULL };
+
+ execvpe(argv[0], args, envp);
+ return EXIT_FAILURE;
+ } else if (!skip--) {
+ char *args[] = { argv0, "foo", "bar", NULL };
+ char *envp[] = { "TEST=55", ldpreload, NULL };
+
+ execvpe(argv[0], args, envp);
+ return EXIT_FAILURE;
+#else
+ /* Fake output to let the test pass. */
+ } else if (!skip--) {
+ puts("argv[0] = |./example_exec|");
+ puts("environ[0] = |COLORED_STDERR_PRIVATE_FDS=2,|");
+ puts("environ[1] = |TEST=54|");
+ puts("");
+ puts("argv[0] = |./example_exec|");
+ puts("argv[1] = |foo|");
+ puts("argv[2] = |bar|");
+ puts("environ[0] = |COLORED_STDERR_PRIVATE_FDS=2,|");
+ puts("environ[1] = |TEST=55|");
+ puts("");
+#endif
}
printf("Done.\n");