LCOV - code coverage report
Current view: top level - lib/util - strv.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 67 89 75.3 %
Date: 2024-04-13 12:30:31 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       39445 : static int _strv_append(TALLOC_CTX *mem_ctx, char **dst, const char *src,
      26             :                         size_t srclen)
      27             : {
      28       39445 :         size_t dstlen = talloc_array_length(*dst);
      29       39445 :         size_t newlen = dstlen + srclen;
      30         235 :         char *new_dst;
      31             : 
      32       39445 :         if ((newlen < srclen) || (newlen < dstlen)) {
      33           0 :                 return ERANGE;
      34             :         }
      35             : 
      36       39445 :         new_dst = talloc_realloc(mem_ctx, *dst, char, newlen);
      37       39445 :         if (new_dst == NULL) {
      38           0 :                 return ENOMEM;
      39             :         }
      40       39445 :         memcpy(&new_dst[dstlen], src, srclen);
      41             : 
      42       39445 :         *dst = new_dst;
      43       39445 :         return 0;
      44             : }
      45             : 
      46       39398 : int strv_add(TALLOC_CTX *mem_ctx, char **strv, const char *string)
      47             : {
      48       39398 :         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     3051163 : static bool strv_valid_entry(const char *strv, size_t strv_len,
      66             :                              const char *entry, size_t *entry_len)
      67             : {
      68     3033392 :         if (strv_len == 0) {
      69      529513 :                 return false;
      70             :         }
      71     2504370 :         if (strv[strv_len-1] != '\0') {
      72           0 :                 return false;
      73             :         }
      74             : 
      75     2503755 :         if (entry < strv) {
      76           0 :                 return false;
      77             :         }
      78     2504370 :         if (entry >= (strv+strv_len)) {
      79           0 :                 return false;
      80             :         }
      81             : 
      82     2503755 :         if (entry_len != NULL) {
      83     2407166 :                 *entry_len = strlen(entry);
      84             :         }
      85             : 
      86     2481705 :         return true;
      87             : }
      88             : 
      89     2988915 : const char *strv_len_next(const char *strv, size_t strv_len,
      90             :                           const char *entry)
      91             : {
      92       39612 :         size_t entry_len;
      93             : 
      94     2988915 :         if (entry == NULL) {
      95      613454 :                 if (strv_valid_entry(strv, strv_len, strv, NULL)) {
      96       96589 :                         return strv;
      97             :                 }
      98      516250 :                 return NULL;
      99             :         }
     100             : 
     101     2375461 :         if (!strv_valid_entry(strv, strv_len, entry, &entry_len)) {
     102       30419 :                 return NULL;
     103             :         }
     104             : 
     105     2344918 :         entry += entry_len+1;
     106             : 
     107     2344918 :         if (entry >= (strv + strv_len)) {
     108      980593 :                 return NULL;
     109             :         }
     110     1363560 :         return entry;
     111             : }
     112             : 
     113     2988410 : char *strv_next(char *strv, const char *entry)
     114             : {
     115     2988410 :         size_t len = talloc_array_length(strv);
     116       39595 :         const char *result;
     117             : 
     118     2988410 :         result = strv_len_next(strv, len, entry);
     119     2988410 :         return discard_const_p(char, result);
     120             : }
     121             : 
     122       54397 : size_t strv_count(char *strv)
     123             : {
     124        1543 :         char *entry;
     125       54397 :         size_t count = 0;
     126             : 
     127      113753 :         for (entry = strv; entry != NULL; entry = strv_next(strv, entry)) {
     128       59356 :                 count += 1;
     129             :         }
     130             : 
     131       54397 :         return count;
     132             : }
     133             : 
     134       96673 : char *strv_find(char *strv, const char *entry)
     135             : {
     136       96673 :         char *e = NULL;
     137             : 
     138      181471 :         while ((e = strv_next(strv, e)) != NULL) {
     139      147850 :                 if (strcmp(e, entry) == 0) {
     140       63052 :                         return e;
     141             :                 }
     142             :         }
     143             : 
     144       33411 :         return NULL;
     145             : }
     146             : 
     147       62248 : void strv_delete(char **strv, char *entry)
     148             : {
     149       62248 :         size_t len = talloc_array_length(*strv);
     150         333 :         size_t entry_len;
     151             : 
     152       62248 :         if (entry == NULL) {
     153           0 :                 return;
     154             :         }
     155             : 
     156       62248 :         if (!strv_valid_entry(*strv, len, entry, &entry_len)) {
     157           0 :                 return;
     158             :         }
     159       62248 :         entry_len += 1;
     160             : 
     161       62248 :         memmove(entry, entry+entry_len,
     162       62248 :                 len - entry_len - (entry - *strv));
     163             : 
     164       62248 :         *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