LCOV - code coverage report
Current view: top level - lib/util - strv.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 62 82 75.6 %
Date: 2021-09-23 10:06:22 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       28810 : static int _strv_append(TALLOC_CTX *mem_ctx, char **dst, const char *src,
      26             :                         size_t srclen)
      27             : {
      28       28810 :         size_t dstlen = talloc_array_length(*dst);
      29       28810 :         size_t newlen = dstlen + srclen;
      30             :         char *new_dst;
      31             : 
      32       28810 :         if ((newlen < srclen) || (newlen < dstlen)) {
      33           0 :                 return ERANGE;
      34             :         }
      35             : 
      36       28810 :         new_dst = talloc_realloc(mem_ctx, *dst, char, newlen);
      37       28810 :         if (new_dst == NULL) {
      38           0 :                 return ENOMEM;
      39             :         }
      40       29047 :         memcpy(&new_dst[dstlen], src, srclen);
      41             : 
      42       28810 :         *dst = new_dst;
      43       28810 :         return 0;
      44             : }
      45             : 
      46       28754 : int strv_add(TALLOC_CTX *mem_ctx, char **strv, const char *string)
      47             : {
      48       28754 :         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          22 :         memcpy(t, string, n);
      56          11 :         t[n] = '\0';
      57          11 :         return _strv_append(mem_ctx, strv, t, n+1);
      58             : }
      59             : 
      60          45 : int strv_append(TALLOC_CTX *mem_ctx, char **strv, const char *src)
      61             : {
      62          45 :         return _strv_append(mem_ctx, strv, src, talloc_array_length(src));
      63             : }
      64             : 
      65      673431 : static bool strv_valid_entry(const char *strv, size_t strv_len,
      66             :                              const char *entry, size_t *entry_len)
      67             : {
      68      687484 :         if (strv_len == 0) {
      69      392289 :                 return false;
      70             :         }
      71      281628 :         if (strv[strv_len-1] != '\0') {
      72           0 :                 return false;
      73             :         }
      74             : 
      75      281018 :         if (entry < strv) {
      76           0 :                 return false;
      77             :         }
      78      281628 :         if (entry >= (strv+strv_len)) {
      79           0 :                 return false;
      80             :         }
      81             : 
      82      281018 :         if (entry_len != NULL) {
      83      211866 :                 *entry_len = strlen(entry);
      84             :         }
      85             : 
      86      277212 :         return true;
      87             : }
      88             : 
      89      642192 : const char *strv_len_next(const char *strv, size_t strv_len,
      90             :                           const char *entry)
      91             : {
      92             :         size_t entry_len;
      93             : 
      94      642192 :         if (entry == NULL) {
      95      440098 :                 if (strv_valid_entry(strv, strv_len, strv, NULL)) {
      96       69152 :                         return strv;
      97             :                 }
      98      370336 :                 return NULL;
      99             :         }
     100             : 
     101      188651 :         if (!strv_valid_entry(strv, strv_len, entry, &entry_len)) {
     102       21953 :                 return NULL;
     103             :         }
     104             : 
     105      166574 :         entry += entry_len+1;
     106             : 
     107      166574 :         if (entry >= (strv + strv_len)) {
     108      100176 :                 return NULL;
     109             :         }
     110       63711 :         return entry;
     111             : }
     112             : 
     113      642175 : char *strv_next(char *strv, const char *entry)
     114             : {
     115      642175 :         size_t len = talloc_array_length(strv);
     116             :         const char *result;
     117             : 
     118      642175 :         result = strv_len_next(strv, len, entry);
     119      642175 :         return discard_const_p(char, result);
     120             : }
     121             : 
     122       50660 : size_t strv_count(char *strv)
     123             : {
     124             :         char *entry;
     125       50660 :         size_t count = 0;
     126             : 
     127      105561 :         for (entry = strv; entry != NULL; entry = strv_next(strv, entry)) {
     128       54901 :                 count += 1;
     129             :         }
     130             : 
     131       50660 :         return count;
     132             : }
     133             : 
     134       69391 : char *strv_find(char *strv, const char *entry)
     135             : {
     136       69391 :         char *e = NULL;
     137             : 
     138      176873 :         while ((e = strv_next(strv, e)) != NULL) {
     139      101472 :                 if (strcmp(e, entry) == 0) {
     140       45699 :                         return e;
     141             :                 }
     142             :         }
     143             : 
     144       23088 :         return NULL;
     145             : }
     146             : 
     147       45292 : void strv_delete(char **strv, char *entry)
     148             : {
     149       45292 :         size_t len = talloc_array_length(*strv);
     150             :         size_t entry_len;
     151             : 
     152       45292 :         if (entry == NULL) {
     153           0 :                 return;
     154             :         }
     155             : 
     156       45292 :         if (!strv_valid_entry(*strv, len, entry, &entry_len)) {
     157           0 :                 return;
     158             :         }
     159       45292 :         entry_len += 1;
     160             : 
     161       45625 :         memmove(entry, entry+entry_len,
     162       45292 :                 len - entry_len - (entry - *strv));
     163             : 
     164       45292 :         *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             :        char **data;
     170           0 :        char *next = NULL;
     171             :        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.13