1 | #include <errno.h> |
2 | #include <libgen.h> |
3 | #undef basename |
4 | #include <stdio.h> |
5 | #include <stdlib.h> |
6 | #include <string.h> |
7 | #include <unistd.h> |
8 | #include <sys/stat.h> |
9 | |
10 | |
11 | static void prepare (int argc, char *argv[]); |
12 | static int do_test (void); |
13 | #define PREPARE(argc, argv) prepare (argc, argv) |
14 | #define TEST_FUNCTION do_test () |
15 | #include "../test-skeleton.c" |
16 | |
17 | |
18 | static char *copy; |
19 | |
20 | static void |
21 | prepare (int argc, char *argv[]) |
22 | { |
23 | char *buf; |
24 | int off; |
25 | asprintf (ptr: &buf, fmt: "cp %s %n%s-copy" , argv[0], &off, argv[0]); |
26 | if (buf == NULL) |
27 | { |
28 | puts (s: "asprintf failed" ); |
29 | exit (1); |
30 | } |
31 | if (system (command: buf) != 0) |
32 | { |
33 | puts (s: "system failed" ); |
34 | exit (1); |
35 | } |
36 | |
37 | /* Make it not executable. */ |
38 | copy = buf + off; |
39 | if (chmod (file: copy, mode: 0666) != 0) |
40 | { |
41 | puts (s: "chmod failed" ); |
42 | exit (1); |
43 | } |
44 | |
45 | add_temp_file (name: copy); |
46 | } |
47 | |
48 | |
49 | static int |
50 | do_test (void) |
51 | { |
52 | /* Make sure we do not find a binary with the name we are going to |
53 | use. */ |
54 | char *bindir = strdupa (copy); |
55 | bindir = canonicalize_file_name (name: dirname (path: bindir)); |
56 | if (bindir == NULL) |
57 | { |
58 | puts (s: "canonicalize_file_name failed" ); |
59 | return 1; |
60 | } |
61 | char *path; |
62 | asprintf (ptr: &path, fmt: "%s:../libio:../elf" , bindir); |
63 | if (path == NULL) |
64 | { |
65 | puts (s: "asprintf failed" ); |
66 | return 1; |
67 | } |
68 | |
69 | setenv (name: "PATH" , value: path, replace: 1); |
70 | |
71 | char *prog = basename (copy); |
72 | errno = 0; |
73 | execlp (prog, prog, NULL); |
74 | |
75 | if (errno != EACCES) |
76 | { |
77 | printf (format: "errno = %d (%m), expected EACCES\n" , errno); |
78 | return 1; |
79 | } |
80 | |
81 | return 0; |
82 | } |
83 | |