diff options
author | Michael Brown | 2012-10-25 05:41:39 +0200 |
---|---|---|
committer | Michael Brown | 2012-10-25 05:42:42 +0200 |
commit | c86790df5cd50b47b8a6b69a3c2367bd28f6cf3c (patch) | |
tree | b07012afb92966917e9bdbe1ec1493bc451dc205 /src/core/settings.c | |
parent | [netdevice] Do not force a poll on net_tx() (diff) | |
download | ipxe-c86790df5cd50b47b8a6b69a3c2367bd28f6cf3c.tar.gz ipxe-c86790df5cd50b47b8a6b69a3c2367bd28f6cf3c.tar.xz ipxe-c86790df5cd50b47b8a6b69a3c2367bd28f6cf3c.zip |
[settings] Add fetchf_named_setting_copy()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/settings.c')
-rw-r--r-- | src/core/settings.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/core/settings.c b/src/core/settings.c index 1b19c8f6..656ae19f 100644 --- a/src/core/settings.c +++ b/src/core/settings.c @@ -1332,6 +1332,45 @@ int fetchf_named_setting ( const char *name, return len; } +/** + * Fetch and format copy of value of named setting + * + * @v name Name of setting + * @v data Buffer to allocate and fill with formatted value + * @ret len Length of formatted value, or negative error + * + * The caller is responsible for eventually freeing the allocated + * buffer. + * + * To allow the caller to distinguish between a non-existent setting + * and an error in allocating memory for the copy, this function will + * return success (and a NULL buffer pointer) for a non-existent + * setting. + */ +int fetchf_named_setting_copy ( const char *name, char **data ) { + int len; + int check_len; + + /* Avoid returning uninitialised data on error */ + *data = NULL; + + /* Fetch formatted value length, and return success if non-existent */ + len = fetchf_named_setting ( name, NULL, 0, NULL, 0 ); + if ( len < 0 ) + return 0; + + /* Allocate buffer */ + *data = malloc ( len + 1 /* NUL */ ); + if ( ! *data ) + return -ENOMEM; + + /* Fetch formatted value */ + check_len = fetchf_named_setting ( name, NULL, 0, *data, + ( len + 1 /* NUL */ ) ); + assert ( check_len == len ); + return len; +} + /****************************************************************************** * * Setting types |