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

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