skalibs

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

sabasename.c (453B)


      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 sabasename (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     i = (i == len) ? 0 : i+1 ;
     16     return stralloc_catb(sa, s + i, len - i) ;
     17   }
     18 }