2 * Copyright (C) 2006-2007 Red Hat, Inc. All rights reserved.
4 * This file is part of the device-mapper userspace tools.
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.
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
16 #include "libdevmapper.h"
21 * consume characters while they match the predicate function.
23 static char *_consume(char *buffer, int (*fn) (int))
25 while (*buffer && fn(*buffer))
31 static int _isword(int c)
37 * Split buffer into NULL-separated words in argv.
38 * Returns number of words.
40 int dm_split_words(char *buffer, unsigned max,
41 unsigned ignore_comments __attribute__((unused)),
46 for (arg = 0; arg < max; arg++) {
47 buffer = _consume(buffer, isspace);
52 buffer = _consume(buffer, _isword);
64 * Remove hyphen quoting from a component of a name.
65 * NULL-terminates the component and returns start of next component.
67 static char *_unquote(char *component)
93 int dm_split_lvm_name(struct dm_pool *mem, const char *dmname,
94 char **vgname, char **lvname, char **layer)
96 if (mem && !(*vgname = dm_pool_strdup(mem, dmname)))
99 _unquote(*layer = _unquote(*lvname = _unquote(*vgname)));
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.
109 * dm_snprintf reverts to the old behaviour.
111 int dm_snprintf(char *buf, size_t bufsize, const char *format, ...)
116 va_start(ap, format);
117 n = vsnprintf(buf, bufsize, format, ap);
120 if (n < 0 || ((unsigned) n + 1 > bufsize))
126 const char *dm_basename(const char *path)
128 const char *p = strrchr(path, '/');
130 return p ? p + 1 : path;
133 int dm_asprintf(char **result, const char *format, ...)
135 int n, ok = 0, size = 32;
137 char *buf = dm_malloc(size);
145 va_start(ap, format);
146 n = vsnprintf(buf, size, format, ap);
149 if (0 <= n && n < size)
154 buf = dm_malloc(size);
160 *result = dm_strdup(buf);