summaryrefslogtreecommitdiff
path: root/src/libunixonacid/access_at.c
blob: c66f5d49d789e3ae6769bb1d4f16016b5759a180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* ISC license. */

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASOPENAT

#ifndef _ATFILE_SOURCE
#define _ATFILE_SOURCE
#endif

#include <skalibs/nonposix.h>
#include <fcntl.h>
#include <unistd.h>
#include <skalibs/unix-transactional.h>

int access_at (int dirfd, char const *file, int amode, unsigned int flag)
{
  return faccessat(dirfd, file, amode, flag ? AT_EACCESS : 0) ;
}

#else

#include <errno.h>
#include <unistd.h>
#include <skalibs/djbunix.h>
#include <skalibs/unix-transactional.h>

int access_at (int dirfd, char const *file, int amode, unsigned int flag)
{
  int fdhere ;
  if (getuid() != geteuid() || getgid() != getegid())
    return (errno = ENOSYS, -1) ;
  (void)flag ;
  fdhere = open_read(".") ;
  if (fdhere < 0) return -1 ;
  if (fd_chdir(dirfd) < 0)
  {
    fd_close(fdhere) ;
    return -1 ;
  }
  if (access(file, amode) < 0)
  {
    int e = errno ;
    fd_chdir(fdhere) ;
    fd_close(fdhere) ;
    return (errno = e, -1) ;
  }
  fd_chdir(fdhere) ;
  fd_close(fdhere) ;
  return 0 ;
}

#endif