Refactor some code in libm-test-driver.c.
[glibc.git] / posix / tst-execlp2.c
blob81a723dda4f8bef54c3eb83853a0d5eccb87ef3e
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>
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"
18 static char *copy;
20 static void
21 prepare (int argc, char *argv[])
23 char *buf;
24 int off;
25 asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]);
26 if (buf == NULL)
28 puts ("asprintf failed");
29 exit (1);
31 if (system (buf) != 0)
33 puts ("system failed");
34 exit (1);
37 /* Make it not executable. */
38 copy = buf + off;
39 if (chmod (copy, 0666) != 0)
41 puts ("chmod failed");
42 exit (1);
45 add_temp_file (copy);
49 static int
50 do_test (void)
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 (dirname (bindir));
56 if (bindir == NULL)
58 puts ("canonicalize_file_name failed");
59 return 1;
61 char *path;
62 asprintf (&path, "%s:../libio:../elf", bindir);
63 if (path == NULL)
65 puts ("asprintf failed");
66 return 1;
69 setenv ("PATH", path, 1);
71 char *prog = basename (copy);
72 errno = 0;
73 execlp (prog, prog, NULL);
75 if (errno != EACCES)
77 printf ("errno = %d (%m), expected EACCES\n", errno);
78 return 1;
81 return 0;