LCOV - code coverage report
Current view: top level - misc/kabi/lvm2.git/libdm - libdm-string.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 22 67 32.8 %
Date: 2010-04-13 Functions: 4 8 50.0 %
Branches: 9 40 22.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2006-2007 Red Hat, Inc. All rights reserved.
       3                 :            :  *
       4                 :            :  * This file is part of the device-mapper userspace tools.
       5                 :            :  *
       6                 :            :  * This copyrighted material is made available to anyone wishing to use,
       7                 :            :  * modify, copy, or redistribute it subject to the terms and conditions
       8                 :            :  * of the GNU Lesser General Public License v.2.1.
       9                 :            :  *
      10                 :            :  * You should have received a copy of the GNU Lesser General Public License
      11                 :            :  * along with this program; if not, write to the Free Software Foundation,
      12                 :            :  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      13                 :            :  */
      14                 :            : 
      15                 :            : #include "dmlib.h"
      16                 :            : #include "libdevmapper.h"
      17                 :            : 
      18                 :            : #include <ctype.h>
      19                 :            : 
      20                 :            : /*
      21                 :            :  * consume characters while they match the predicate function.
      22                 :            :  */
      23                 :        120 : static char *_consume(char *buffer, int (*fn) (int))
      24                 :            : {
      25 [ +  - ][ +  + ]:        750 :         while (*buffer && fn(*buffer))
      26                 :        630 :                 buffer++;
      27                 :            : 
      28                 :        120 :         return buffer;
      29                 :            : }
      30                 :            : 
      31                 :        690 : static int _isword(int c)
      32                 :            : {
      33                 :        690 :         return !isspace(c);
      34                 :            : }
      35                 :            : 
      36                 :            : /*
      37                 :            :  * Split buffer into NULL-separated words in argv.
      38                 :            :  * Returns number of words.
      39                 :            :  */
      40                 :         15 : int dm_split_words(char *buffer, unsigned max,
      41                 :            :                    unsigned ignore_comments __attribute((unused)),
      42                 :            :                    char **argv)
      43                 :            : {
      44                 :            :         unsigned arg;
      45                 :            : 
      46         [ +  + ]:         75 :         for (arg = 0; arg < max; arg++) {
      47                 :         60 :                 buffer = _consume(buffer, isspace);
      48         [ -  + ]:         60 :                 if (!*buffer)
      49                 :          0 :                         break;
      50                 :            : 
      51                 :         60 :                 argv[arg] = buffer;
      52                 :         60 :                 buffer = _consume(buffer, _isword);
      53                 :            : 
      54         [ +  - ]:         60 :                 if (*buffer) {
      55                 :         60 :                         *buffer = '\0';
      56                 :         60 :                         buffer++;
      57                 :            :                 }
      58                 :            :         }
      59                 :            : 
      60                 :         15 :         return arg;
      61                 :            : }
      62                 :            : 
      63                 :            : /*
      64                 :            :  * Remove hyphen quoting from a component of a name.
      65                 :            :  * NULL-terminates the component and returns start of next component.
      66                 :            :  */
      67                 :          0 : static char *_unquote(char *component)
      68                 :            : {
      69                 :          0 :         char *c = component;
      70                 :          0 :         char *o = c;
      71                 :            :         char *r;
      72                 :            : 
      73         [ #  # ]:          0 :         while (*c) {
      74         [ #  # ]:          0 :                 if (*(c + 1)) {
      75         [ #  # ]:          0 :                         if (*c == '-') {
      76         [ #  # ]:          0 :                                 if (*(c + 1) == '-')
      77                 :          0 :                                         c++;
      78                 :            :                                 else
      79                 :          0 :                                         break;
      80                 :            :                         }
      81                 :            :                 }
      82                 :          0 :                 *o = *c;
      83                 :          0 :                 o++;
      84                 :          0 :                 c++;
      85                 :            :         }
      86                 :            : 
      87         [ #  # ]:          0 :         r = (*c) ? c + 1 : c;
      88                 :          0 :         *o = '\0';
      89                 :            : 
      90                 :          0 :         return r;
      91                 :            : }
      92                 :            : 
      93                 :          0 : int dm_split_lvm_name(struct dm_pool *mem, const char *dmname,
      94                 :            :                       char **vgname, char **lvname, char **layer)
      95                 :            : {
      96 [ #  # ][ #  # ]:          0 :         if (mem && !(*vgname = dm_pool_strdup(mem, dmname)))
      97                 :          0 :                 return 0;
      98                 :            : 
      99                 :          0 :         _unquote(*layer = _unquote(*lvname = _unquote(*vgname)));
     100                 :            : 
     101                 :          0 :         return 1;
     102                 :            : }
     103                 :            : 
     104                 :            : /*
     105                 :            :  * On error, up to glibc 2.0.6, snprintf returned -1 if buffer was too small;
     106                 :            :  * From glibc 2.1 it returns number of chars (excl. trailing null) that would 
     107                 :            :  * have been written had there been room.
     108                 :            :  *
     109                 :            :  * dm_snprintf reverts to the old behaviour.
     110                 :            :  */
     111                 :        106 : int dm_snprintf(char *buf, size_t bufsize, const char *format, ...)
     112                 :            : {
     113                 :            :         int n;
     114                 :            :         va_list ap;
     115                 :            : 
     116                 :        106 :         va_start(ap, format);
     117                 :        106 :         n = vsnprintf(buf, bufsize, format, ap);
     118                 :        106 :         va_end(ap);
     119                 :            : 
     120   [ +  -  -  + ]:        106 :         if (n < 0 || ((unsigned) n + 1 > bufsize))
     121                 :          0 :                 return -1;
     122                 :            : 
     123                 :        106 :         return n;
     124                 :            : }
     125                 :            : 
     126                 :          0 : char *dm_basename(const char *path)
     127                 :            : {
     128                 :          0 :         char *p = strrchr(path, '/');
     129                 :            : 
     130         [ #  # ]:          0 :         return p ? p + 1 : (char *) path;
     131                 :            : }
     132                 :            : 
     133                 :          0 : int dm_asprintf(char **result, const char *format, ...)
     134                 :            : {
     135                 :          0 :         int n, ok = 0, size = 32;
     136                 :            :         va_list ap;
     137                 :          0 :         char *buf = dm_malloc(size);
     138                 :            : 
     139                 :          0 :         *result = 0;
     140                 :            : 
     141         [ #  # ]:          0 :         if (!buf)
     142                 :          0 :                 return -1;
     143                 :            : 
     144         [ #  # ]:          0 :         while (!ok) {
     145                 :          0 :                 va_start(ap, format);
     146                 :          0 :                 n = vsnprintf(buf, size, format, ap);
     147   [ #  #  #  # ]:          0 :                 if (0 <= n && n < size)
     148                 :          0 :                         ok = 1;
     149                 :            :                 else {
     150                 :          0 :                         dm_free(buf);
     151                 :          0 :                         size *= 2;
     152                 :          0 :                         buf = dm_malloc(size);
     153         [ #  # ]:          0 :                         if (!buf)
     154                 :          0 :                                 return -1;
     155                 :            :                 };
     156                 :          0 :                 va_end(ap);
     157                 :            :         }
     158                 :            : 
     159                 :          0 :         *result = dm_strdup(buf);
     160                 :          0 :         dm_free(buf);
     161                 :          0 :         return n + 1;
     162                 :            : }

Generated by: LCOV version 1.8