From 9e063b223e31fccb5075e31ccdf051a91131e1be Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Mon, 21 Aug 2006 12:52:39 +0000 Subject: [PATCH] Add dm_snprintf --- WHATS_NEW_DM | 2 +- libdm/.exported_symbols | 1 + libdm/libdevmapper.h | 6 ++++++ libdm/libdm-string.c | 22 ++++++++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM index 01d95e60c..1932c2cd4 100644 --- a/WHATS_NEW_DM +++ b/WHATS_NEW_DM @@ -1,6 +1,6 @@ Version 1.02.10 - ============================== - Add dm_split_words() and dm_split_lvm_name() to libdevmapper. + Add dm_snprintf(), dm_split_words() and dm_split_lvm_name() to libdevmapper. Reorder mm bounds_check code to reduce window for a dmeventd race. Version 1.02.09 - 15 Aug 2006 diff --git a/libdm/.exported_symbols b/libdm/.exported_symbols index 263d2ad61..43cbf51f7 100644 --- a/libdm/.exported_symbols +++ b/libdm/.exported_symbols @@ -111,3 +111,4 @@ dm_set_selinux_context dm_task_set_geometry dm_split_lvm_name dm_split_words +dm_snprintf diff --git a/libdm/libdevmapper.h b/libdm/libdevmapper.h index c979e8728..209177f68 100644 --- a/libdm/libdevmapper.h +++ b/libdm/libdevmapper.h @@ -17,6 +17,7 @@ #define LIB_DEVICE_MAPPER_H #include +#include #include #ifdef linux @@ -601,4 +602,9 @@ int dm_split_words(char *buffer, unsigned max, unsigned ignore_comments, /* Not implemented */ char **argv); +/* + * Returns -1 if buffer too small + */ +int dm_snprintf(char *buf, size_t bufsize, const char *format, ...); + #endif /* LIB_DEVICE_MAPPER_H */ diff --git a/libdm/libdm-string.c b/libdm/libdm-string.c index 4ce660807..f990d95bc 100644 --- a/libdm/libdm-string.c +++ b/libdm/libdm-string.c @@ -99,3 +99,25 @@ int dm_split_lvm_name(struct dm_pool *mem, const char *dmname, return 1; } + +/* + * On error, up to glibc 2.0.6, snprintf returned -1 if buffer was too small; + * From glibc 2.1 it returns number of chars (excl. trailing null) that would + * have been written had there been room. + * + * dm_snprintf reverts to the old behaviour. + */ +int dm_snprintf(char *buf, size_t bufsize, const char *format, ...) +{ + int n; + va_list ap; + + va_start(ap, format); + n = vsnprintf(buf, bufsize, format, ap); + va_end(ap); + + if (n < 0 || (n > bufsize - 1)) + return -1; + + return n; +}