skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

access_at.c (1036B)


      1 /* ISC license. */
      2 
      3 #include <skalibs/sysdeps.h>
      4 
      5 #ifdef SKALIBS_HASLINKAT
      6 
      7 #ifndef _ATFILE_SOURCE
      8 #define _ATFILE_SOURCE
      9 #endif
     10 
     11 #include <skalibs/nonposix.h>
     12 
     13 #include <unistd.h>
     14 
     15 #include <skalibs/fcntl.h>
     16 #include <skalibs/unix-transactional.h>
     17 
     18 int access_at (int dirfd, char const *file, int amode, unsigned int flag)
     19 {
     20   return faccessat(dirfd, file, amode, flag ? AT_EACCESS : 0) ;
     21 }
     22 
     23 #else
     24 
     25 #include <errno.h>
     26 #include <unistd.h>
     27 
     28 #include <skalibs/djbunix.h>
     29 #include <skalibs/unix-transactional.h>
     30 
     31 int access_at (int dirfd, char const *file, int amode, unsigned int flag)
     32 {
     33   int fdhere ;
     34   if (getuid() != geteuid() || getgid() != getegid())
     35     return (errno = ENOSYS, -1) ;
     36   (void)flag ;
     37   fdhere = openc_read(".") ;
     38   if (fdhere < 0) return -1 ;
     39   if (fd_chdir(dirfd) < 0)
     40   {
     41     fd_close(fdhere) ;
     42     return -1 ;
     43   }
     44   if (access(file, amode) < 0)
     45   {
     46     int e = errno ;
     47     fd_chdir(fdhere) ;
     48     fd_close(fdhere) ;
     49     return (errno = e, -1) ;
     50   }
     51   fd_chdir(fdhere) ;
     52   fd_close(fdhere) ;
     53   return 0 ;
     54 }
     55 
     56 #endif