skalibs

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

sadirname.c (528B)


      1 /* ISC license. */
      2 
      3 #include <string.h>
      4 #include <skalibs/bytestr.h>
      5 #include <skalibs/stralloc.h>
      6 #include <skalibs/djbunix.h>
      7 
      8 int sadirname (stralloc *sa, char const *s, size_t len)
      9 {
     10   if (!len) return stralloc_catb(sa, ".", 1) ;
     11   while (len && (s[len-1] == '/')) len-- ;
     12   if (!len) return stralloc_catb(sa, "/", 1) ;
     13   {
     14     size_t i = byte_rchr(s, len, '/') ;
     15     return (i == len) ? stralloc_catb(sa, ".", 1) :
     16            (i == 0)   ? stralloc_catb(sa, "/", 1) :
     17                         stralloc_catb(sa, s, i) ;
     18   }
     19 }