skalibs

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

opendir_at.c (740B)


      1 /* ISC license. */
      2 
      3 #include <skalibs/sysdeps.h>
      4 #include <skalibs/nonposix.h>
      5 #include <skalibs/direntry.h>
      6 #include <skalibs/djbunix.h>
      7 #include <skalibs/unix-transactional.h>
      8 
      9 #ifdef SKALIBS_HASFDOPENDIR
     10 
     11 DIR *opendir_at (int dfd, char const *name)
     12 {
     13   DIR *dir ;
     14   int fd = openc_readatb(dfd, name) ;
     15   if (fd == -1) return 0 ;
     16   dir = fdopendir(fd) ;
     17   if (!dir) fd_close(fd) ;
     18   return dir ;
     19 }
     20 
     21 #else
     22 
     23 #include <unistd.h>
     24 #include <stdlib.h>
     25 
     26 DIR *opendir_at (int dfd, char const *name)
     27 {
     28   DIR *dir ;
     29   int here = open_read(".") ;
     30   if (here == -1) return 0 ;
     31   if (fchdir(dfd) == -1)
     32   {
     33     fd_close(here) ;
     34     return 0 ;
     35   }
     36   dir = opendir(name) ;
     37   if (fchdir(here) == -1) abort() ;
     38   fd_close(here) ;
     39   return dir ;
     40 }
     41 
     42 #endif