summaryrefslogtreecommitdiff
path: root/src/libunixonacid/opendir_at.c
blob: d5a32216503a1d10a911cb81d2ae8c8404245c0a (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
/* ISC license. */

#include <skalibs/sysdeps.h>
#include <skalibs/nonposix.h>
#include <skalibs/direntry.h>
#include <skalibs/djbunix.h>
#include <skalibs/unix-transactional.h>

#ifdef SKALIBS_HASFDOPENDIR

DIR *opendir_at (int dfd, char const *name)
{
  DIR *dir ;
  int fd = openc_readatb(dfd, name) ;
  if (fd == -1) return 0 ;
  dir = fdopendir(fd) ;
  if (!dir) fd_close(fd) ;
  return dir ;
}

#else

#include <unistd.h>
#include <stdlib.h>

DIR *opendir_at (int dfd, char const *name)
{
  DIR *dir ;
  int here = open_read(".") ;
  if (here == -1) return 0 ;
  if (fchdir(dfd) == -1)
  {
    fd_close(here) ;
    return 0 ;
  }
  dir = opendir(name) ;
  if (fchdir(here) == -1) abort() ;
  fd_close(here) ;
  return dir ;
}

#endif