Back to index

glibc  2.9
tst-execvp2.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <libgen.h>
00003 #undef basename
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008 #include <sys/stat.h>
00009 
00010 
00011 static void prepare (int argc, char *argv[]);
00012 static int do_test (void);
00013 #define PREPARE(argc, argv) prepare (argc, argv)
00014 #define TEST_FUNCTION do_test ()
00015 #include "../test-skeleton.c"
00016 
00017 
00018 static char *copy;
00019 
00020 static void
00021 prepare (int argc, char *argv[])
00022 {
00023   char *buf;
00024   int off;
00025   asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]);
00026   if (buf == NULL)
00027     {
00028       puts ("asprintf  failed");
00029       exit (1);
00030     }
00031   if (system (buf) != 0)
00032     {
00033       puts ("system  failed");
00034       exit (1);
00035     }
00036 
00037   /* Make it not executable.  */
00038   copy = buf + off;
00039   if (chmod (copy, 0666) != 0)
00040     {
00041       puts ("chmod  failed");
00042       exit (1);
00043     }
00044 
00045   add_temp_file (copy);
00046 }
00047 
00048 
00049 static int
00050 do_test (void)
00051 {
00052   /* Make sure we do not find a binary with the name we are going to
00053      use.  */
00054   char *bindir = strdupa (copy);
00055   bindir = canonicalize_file_name (dirname (bindir));
00056   if (bindir == NULL)
00057     {
00058       puts ("canonicalize_file_name failed");
00059       return 1;
00060     }
00061   char *path;
00062   asprintf (&path, "%s:../libio:../elf", bindir);
00063   if (path == NULL)
00064     {
00065       puts ("asprintf  failed");
00066       return 1;
00067     }
00068 
00069   setenv ("PATH", path, 1);
00070 
00071   char *argv[] = { basename (copy), NULL };
00072   errno = 0;
00073   execvp (argv[0], argv);
00074 
00075   if (errno != EACCES)
00076     {
00077       printf ("errno = %d (%m), expected EACCES\n", errno);
00078       return 1;
00079     }
00080 
00081   return 0;
00082 }