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

source code of glibc/posix/tst-execvp2.c