tests: fix warn unused result on asprintf calls
[platform/upstream/glibc.git] / posix / tst-execvp2.c
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 #ifndef EXECVP
18 # define EXECVP(file, argv)  execvp (file, argv)
19 #endif
20
21 static char *copy;
22
23 static void
24 prepare (int argc, char *argv[])
25 {
26   char *buf;
27   int off;
28
29   buf = xasprintf ("cp %s %n%s-copy", argv[0], &off, argv[0]);
30   if (system (buf) != 0)
31     {
32       puts ("system  failed");
33       exit (1);
34     }
35
36   /* Make it not executable.  */
37   copy = buf + off;
38   if (chmod (copy, 0666) != 0)
39     {
40       puts ("chmod  failed");
41       exit (1);
42     }
43
44   add_temp_file (copy);
45 }
46
47
48 static int
49 do_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 (dirname (bindir));
55   if (bindir == NULL)
56     {
57       puts ("canonicalize_file_name failed");
58       return 1;
59     }
60
61   char *path = xasprintf ("%s:../libio:../elf", bindir);
62
63   setenv ("PATH", path, 1);
64
65   char *argv[] = { basename (copy), NULL };
66   errno = 0;
67   EXECVP (argv[0], argv);
68
69   if (errno != EACCES)
70     {
71       printf ("errno = %d (%m), expected EACCES\n", errno);
72       return 1;
73     }
74
75   return 0;
76 }