envalloc_uniq.c (779B)
1 /* ISC license. */ 2 3 #include <string.h> 4 #include <errno.h> 5 #include <skalibs/bytestr.h> 6 #include <skalibs/genalloc.h> 7 #include <skalibs/envalloc.h> 8 9 int envalloc_uniq (genalloc *v, char delim) 10 { 11 unsigned int m = 0 ; 12 size_t i = 0 ; 13 for (; i < genalloc_len(char const *, v) ; i++) 14 { 15 size_t j = i+1 ; 16 char const *s = genalloc_s(char const *, v)[i] ; 17 size_t n = str_chr(s, delim) ; 18 if (delim && !s[n]) return (errno = EINVAL, -1) ; 19 for (; j < genalloc_len(char const *, v) ; j++) 20 { 21 char const **p = genalloc_s(char const *, v) ; 22 if (!strncmp(s, p[j], n)) 23 { 24 size_t len = genalloc_len(char const *, v) - 1 ; 25 genalloc_setlen(char const *, v, len) ; 26 p[j] = p[len] ; 27 m++ ; 28 } 29 } 30 } 31 return m ; 32 }