skalibs

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

sals.c (893B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 #include <string.h>
      5 
      6 #include <skalibs/direntry.h>
      7 #include <skalibs/stralloc.h>
      8 #include <skalibs/djbunix.h>
      9 
     10 int sals (char const *fn, stralloc *sa, size_t *x)
     11 {
     12   int n = 0 ;
     13   size_t sabase = sa->len ;
     14   size_t maxlen = 0 ;
     15   DIR *dir = opendir(fn) ;
     16   if (dir)
     17   {
     18     for (;;)
     19     {
     20       direntry *d ;
     21       size_t len ;
     22       errno = 0 ;
     23       d = readdir(dir) ;
     24       if (!d) break ;
     25       if (d->d_name[0] == '.')
     26         if (((d->d_name[1] == '.') && !d->d_name[2]) || !d->d_name[1])
     27           continue ;
     28       len = strlen(d->d_name) ;
     29       if (len > maxlen) maxlen = len ;
     30       if (!stralloc_catb(sa, d->d_name, len+1)) goto err ;
     31       n++ ;
     32     }
     33     if (errno) goto err ;
     34     dir_close(dir) ;
     35   }
     36   else if (errno != ENOENT) return -1 ;
     37   if (x) *x = maxlen ;
     38   return n ;
     39 
     40  err:
     41   dir_close(dir) ;
     42   sa->len = sabase ;
     43   return -1 ;
     44 }