4 * Copyright (C) 2006-2007 Red Hat, Inc. All rights reserved.
6 * This file is part of the device-mapper userspace tools.
8 * This copyrighted material is made available to anyone wishing to use,
9 * modify, copy, or redistribute it subject to the terms and conditions
10 * of the GNU Lesser General Public License v.2.1.
12 * You should have received a copy of the GNU Lesser General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "libdevmapper.h"
23 * consume characters while they match the predicate function.
25 static char *_consume(char *buffer
, int (*fn
) (int))
27 while (*buffer
&& fn(*buffer
))
33 static int _isword(int c
)
39 * Split buffer into NULL-separated words in argv.
40 * Returns number of words.
42 int dm_split_words(char *buffer
, unsigned max
,
43 unsigned ignore_comments
__attribute((unused
)),
48 for (arg
= 0; arg
< max
; arg
++) {
49 buffer
= _consume(buffer
, isspace
);
54 buffer
= _consume(buffer
, _isword
);
66 * Remove hyphen quoting from a component of a name.
67 * NULL-terminates the component and returns start of next component.
69 static char *_unquote(char *component
)
95 int dm_split_lvm_name(struct dm_pool
*mem
, const char *dmname
,
96 char **vgname
, char **lvname
, char **layer
)
98 if (mem
&& !(*vgname
= dm_pool_strdup(mem
, dmname
)))
101 _unquote(*layer
= _unquote(*lvname
= _unquote(*vgname
)));
107 * On error, up to glibc 2.0.6, snprintf returned -1 if buffer was too small;
108 * From glibc 2.1 it returns number of chars (excl. trailing null) that would
109 * have been written had there been room.
111 * dm_snprintf reverts to the old behaviour.
113 int dm_snprintf(char *buf
, size_t bufsize
, const char *format
, ...)
118 va_start(ap
, format
);
119 n
= vsnprintf(buf
, bufsize
, format
, ap
);
122 if (n
< 0 || ((unsigned) n
+ 1 > bufsize
))
128 char *dm_basename(const char *path
)
130 char *p
= strrchr(path
, '/');
132 return p
? p
+ 1 : (char *) path
;
135 int dm_asprintf(char **result
, const char *format
, ...)
137 int n
, ok
= 0, size
= 32;
139 char *buf
= dm_malloc(size
);
147 va_start(ap
, format
);
148 n
= vsnprintf(buf
, size
, format
, ap
);
149 if (0 <= n
&& n
< size
)
154 buf
= dm_malloc(size
);
161 *result
= dm_strdup(buf
);