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 #ifndef EXECVP
18 # define EXECVP(file, argv) execvp (file, argv)
19 #endif
20
21 static char *copy;
22
23 static void
prepare(int argc,char * argv[])24 prepare (int argc, char *argv[])
25 {
26 char *buf;
27 int off;
28 asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]);
29 if (buf == NULL)
30 {
31 puts ("asprintf failed");
32 exit (1);
33 }
34 if (system (buf) != 0)
35 {
36 puts ("system failed");
37 exit (1);
38 }
39
40 /* Make it not executable. */
41 copy = buf + off;
42 if (chmod (copy, 0666) != 0)
43 {
44 puts ("chmod failed");
45 exit (1);
46 }
47
48 add_temp_file (copy);
49 }
50
51
52 static int
do_test(void)53 do_test (void)
54 {
55 /* Make sure we do not find a binary with the name we are going to
56 use. */
57 char *bindir = strdupa (copy);
58 bindir = canonicalize_file_name (dirname (bindir));
59 if (bindir == NULL)
60 {
61 puts ("canonicalize_file_name failed");
62 return 1;
63 }
64 char *path;
65 asprintf (&path, "%s:../libio:../elf", bindir);
66 if (path == NULL)
67 {
68 puts ("asprintf failed");
69 return 1;
70 }
71
72 setenv ("PATH", path, 1);
73
74 char *argv[] = { basename (copy), NULL };
75 errno = 0;
76 EXECVP (argv[0], argv);
77
78 if (errno != EACCES)
79 {
80 printf ("errno = %d (%m), expected EACCES\n", errno);
81 return 1;
82 }
83
84 return 0;
85 }
86