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 asprintf (ptr: &buf, fmt: "cp %s %n%s-copy", argv[0], &off, argv[0]);
29 if (buf == NULL)
30 {
31 puts (s: "asprintf failed");
32 exit (1);
33 }
34 if (system (command: buf) != 0)
35 {
36 puts (s: "system failed");
37 exit (1);
38 }
39
40 /* Make it not executable. */
41 copy = buf + off;
42 if (chmod (file: copy, mode: 0666) != 0)
43 {
44 puts (s: "chmod failed");
45 exit (1);
46 }
47
48 add_temp_file (name: copy);
49}
50
51
52static int
53do_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 (name: dirname (path: bindir));
59 if (bindir == NULL)
60 {
61 puts (s: "canonicalize_file_name failed");
62 return 1;
63 }
64 char *path;
65 asprintf (ptr: &path, fmt: "%s:../libio:../elf", bindir);
66 if (path == NULL)
67 {
68 puts (s: "asprintf failed");
69 return 1;
70 }
71
72 setenv (name: "PATH", value: path, replace: 1);
73
74 char *argv[] = { basename (copy), NULL };
75 errno = 0;
76 EXECVP (argv[0], argv);
77
78 if (errno != EACCES)
79 {
80 printf (format: "errno = %d (%m), expected EACCES\n", errno);
81 return 1;
82 }
83
84 return 0;
85}
86

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