LCOV - code coverage report
Current view: top level - lib/util - strv.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 67 89 75.3 %
Date: 2024-02-29 22:57:05 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /*
       2             :  * String Vector functions modeled after glibc argv_* functions
       3             :  *
       4             :  * Copyright Volker Lendecke <vl@samba.org> 2014
       5             :  *
       6             :  * This program is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * This program is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include "replace.h"
      21             : #include "strv.h"
      22             : #include "talloc.h"
      23             : #include <string.h>
      24             : 
      25       39678 : static int _strv_append(TALLOC_CTX *mem_ctx, char **dst, const char *src,
      26             :                         size_t srclen)
      27             : {
      28       39678 :         size_t dstlen = talloc_array_length(*dst);
      29       39678 :         size_t newlen = dstlen + srclen;
      30         235 :         char *new_dst;
      31             : 
      32       39678 :         if ((newlen < srclen) || (newlen < dstlen)) {
      33           0 :                 return ERANGE;
      34             :         }
      35             : 
      36       39678 :         new_dst = talloc_realloc(mem_ctx, *dst, char, newlen);
      37       39678 :         if (new_dst == NULL) {
      38           0 :                 return ENOMEM;
      39             :         }
      40       39678 :         memcpy(&new_dst[dstlen], src, srclen);
      41             : 
      42       39678 :         *dst = new_dst;
      43       39678 :         return 0;
      44             : }
      45             : 
      46       39631 : int strv_add(TALLOC_CTX *mem_ctx, char **strv, const char *string)
      47             : {
      48       39631 :         return _strv_append(mem_ctx, strv, string, strlen(string)+1);
      49             : }
      50             : 
      51          11 : int strv_addn(TALLOC_CTX *mem_ctx, char **strv, const char *string, size_t n)
      52          11 : {
      53          11 :         char t[n+1];
      54             : 
      55          11 :         memcpy(t, string, n);
      56          11 :         t[n] = '\0';
      57          11 :         return _strv_append(mem_ctx, strv, t, n+1);
      58             : }
      59             : 
      60          36 : int strv_append(TALLOC_CTX *mem_ctx, char **strv, const char *src)
      61             : {
      62          36 :         return _strv_append(mem_ctx, strv, src, talloc_array_length(src));
      63             : }
      64             : 
      65     3054992 : static bool strv_valid_entry(const char *strv, size_t strv_len,
      66             :                              const char *entry, size_t *entry_len)
      67             : {
      68     3037297 :         if (strv_len == 0) {
      69      528100 :                 return false;
      70             :         }
      71     2509686 :         if (strv[strv_len-1] != '\0') {
      72           0 :                 return false;
      73             :         }
      74             : 
      75     2509073 :         if (entry < strv) {
      76           0 :                 return false;
      77             :         }
      78     2509686 :         if (entry >= (strv+strv_len)) {
      79           0 :                 return false;
      80             :         }
      81             : 
      82     2509073 :         if (entry_len != NULL) {
      83     2412230 :                 *entry_len = strlen(entry);
      84             :         }
      85             : 
      86     2486916 :         return true;
      87             : }
      88             : 
      89     2992690 : const char *strv_len_next(const char *strv, size_t strv_len,
      90             :                           const char *entry)
      91             : {
      92       39643 :         size_t entry_len;
      93             : 
      94     2992690 :         if (entry == NULL) {
      95      612192 :                 if (strv_valid_entry(strv, strv_len, strv, NULL)) {
      96       96843 :                         return strv;
      97             :                 }
      98      514736 :                 return NULL;
      99             :         }
     100             : 
     101     2380498 :         if (!strv_valid_entry(strv, strv_len, entry, &entry_len)) {
     102       30446 :                 return NULL;
     103             :         }
     104             : 
     105     2349928 :         entry += entry_len+1;
     106             : 
     107     2349928 :         if (entry >= (strv + strv_len)) {
     108      985004 :                 return NULL;
     109             :         }
     110     1364066 :         return entry;
     111             : }
     112             : 
     113     2992185 : char *strv_next(char *strv, const char *entry)
     114             : {
     115     2992185 :         size_t len = talloc_array_length(strv);
     116       39626 :         const char *result;
     117             : 
     118     2992185 :         result = strv_len_next(strv, len, entry);
     119     2992185 :         return discard_const_p(char, result);
     120             : }
     121             : 
     122       54605 : size_t strv_count(char *strv)
     123             : {
     124        1551 :         char *entry;
     125       54605 :         size_t count = 0;
     126             : 
     127      114379 :         for (entry = strv; entry != NULL; entry = strv_next(strv, entry)) {
     128       59774 :                 count += 1;
     129             :         }
     130             : 
     131       54605 :         return count;
     132             : }
     133             : 
     134       96932 : char *strv_find(char *strv, const char *entry)
     135             : {
     136       96932 :         char *e = NULL;
     137             : 
     138      181960 :         while ((e = strv_next(strv, e)) != NULL) {
     139      148134 :                 if (strcmp(e, entry) == 0) {
     140       63106 :                         return e;
     141             :                 }
     142             :         }
     143             : 
     144       33618 :         return NULL;
     145             : }
     146             : 
     147       62302 : void strv_delete(char **strv, char *entry)
     148             : {
     149       62302 :         size_t len = talloc_array_length(*strv);
     150         333 :         size_t entry_len;
     151             : 
     152       62302 :         if (entry == NULL) {
     153           0 :                 return;
     154             :         }
     155             : 
     156       62302 :         if (!strv_valid_entry(*strv, len, entry, &entry_len)) {
     157           0 :                 return;
     158             :         }
     159       62302 :         entry_len += 1;
     160             : 
     161       62302 :         memmove(entry, entry+entry_len,
     162       62302 :                 len - entry_len - (entry - *strv));
     163             : 
     164       62302 :         *strv = talloc_realloc(NULL, *strv, char, len - entry_len);
     165             : }
     166             : 
     167           0 : char * const *strv_to_env(TALLOC_CTX *mem_ctx, char *strv)
     168             : {
     169           0 :        char **data;
     170           0 :        char *next = NULL;
     171           0 :        size_t i;
     172           0 :        size_t count = strv_count(strv);
     173             : 
     174           0 :        if (strv == NULL) {
     175           0 :                return NULL;
     176             :        }
     177             : 
     178           0 :        data = talloc_array(mem_ctx, char *, count + 1);
     179             : 
     180           0 :        if (data == NULL) {
     181           0 :                return NULL;
     182             :        }
     183             : 
     184           0 :        for(i = 0; i < count; i++) {
     185           0 :                next = strv_next(strv, next);
     186           0 :                data[i] = next;
     187             :        }
     188           0 :        data[count] = NULL;
     189             : 
     190           0 :        return data;
     191             : }

Generated by: LCOV version 1.14