]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[settings] Add fetchf_named_setting_copy()
authorMichael Brown <mcb30@ipxe.org>
Thu, 25 Oct 2012 03:41:39 +0000 (20:41 -0700)
committerMichael Brown <mcb30@ipxe.org>
Thu, 25 Oct 2012 03:42:42 +0000 (20:42 -0700)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/settings.c
src/include/ipxe/settings.h

index 1b19c8f62cc94e0d0c9882c1e172e246a2cff371..656ae19fe642b55914f9a80888d9c65fc7f1ea2b 100644 (file)
@@ -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
index 68987bdd63ea903e146e3974add08d77921b1a36..3f8876020a5ffb1fb1d3c8e72d5e32f361fc865f 100644 (file)
@@ -291,6 +291,7 @@ extern int storef_named_setting ( const char *name,
 extern int fetchf_named_setting ( const char *name, char *name_buf,
                                  size_t name_len, char *value_buf,
                                  size_t value_len );
+extern int fetchf_named_setting_copy ( const char *name, char **data );
 extern char * expand_settings ( const char *string );
 
 extern struct setting_type setting_type_string __setting_type;