lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | |
| 18 | static char *copy; |
| 19 | |
| 20 | static void |
| 21 | prepare (int argc, char *argv[]) |
| 22 | { |
| 23 | char *buf; |
| 24 | int off; |
| 25 | asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]); |
| 26 | if (buf == NULL) |
| 27 | { |
| 28 | puts ("asprintf failed"); |
| 29 | exit (1); |
| 30 | } |
| 31 | if (system (buf) != 0) |
| 32 | { |
| 33 | puts ("system failed"); |
| 34 | exit (1); |
| 35 | } |
| 36 | |
| 37 | /* Make it not executable. */ |
| 38 | copy = buf + off; |
| 39 | if (chmod (copy, 0666) != 0) |
| 40 | { |
| 41 | puts ("chmod failed"); |
| 42 | exit (1); |
| 43 | } |
| 44 | |
| 45 | add_temp_file (copy); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | static int |
| 50 | do_test (void) |
| 51 | { |
| 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) |
| 57 | { |
| 58 | puts ("canonicalize_file_name failed"); |
| 59 | return 1; |
| 60 | } |
| 61 | char *path; |
| 62 | asprintf (&path, "%s:../libio:../elf", bindir); |
| 63 | if (path == NULL) |
| 64 | { |
| 65 | puts ("asprintf failed"); |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | setenv ("PATH", path, 1); |
| 70 | |
| 71 | char *prog = basename (copy); |
| 72 | errno = 0; |
| 73 | execlp (prog, prog, NULL); |
| 74 | |
| 75 | if (errno != EACCES) |
| 76 | { |
| 77 | printf ("errno = %d (%m), expected EACCES\n", errno); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |