Browse Source

[settings] Add fetchf_named_setting_copy()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
c86790df5c
2 changed files with 40 additions and 0 deletions
  1. 39
    0
      src/core/settings.c
  2. 1
    0
      src/include/ipxe/settings.h

+ 39
- 0
src/core/settings.c View File

@@ -1332,6 +1332,45 @@ int fetchf_named_setting ( const char *name,
1332 1332
 	return len;
1333 1333
 }
1334 1334
 
1335
+/**
1336
+ * Fetch and format copy of value of named setting
1337
+ *
1338
+ * @v name		Name of setting
1339
+ * @v data		Buffer to allocate and fill with formatted value
1340
+ * @ret len		Length of formatted value, or negative error
1341
+ *
1342
+ * The caller is responsible for eventually freeing the allocated
1343
+ * buffer.
1344
+ *
1345
+ * To allow the caller to distinguish between a non-existent setting
1346
+ * and an error in allocating memory for the copy, this function will
1347
+ * return success (and a NULL buffer pointer) for a non-existent
1348
+ * setting.
1349
+ */
1350
+int fetchf_named_setting_copy ( const char *name, char **data ) {
1351
+	int len;
1352
+	int check_len;
1353
+
1354
+	/* Avoid returning uninitialised data on error */
1355
+	*data = NULL;
1356
+
1357
+	/* Fetch formatted value length, and return success if non-existent */
1358
+	len = fetchf_named_setting ( name, NULL, 0, NULL, 0 );
1359
+	if ( len < 0 )
1360
+		return 0;
1361
+
1362
+	/* Allocate buffer */
1363
+	*data = malloc ( len + 1 /* NUL */ );
1364
+	if ( ! *data )
1365
+		return -ENOMEM;
1366
+
1367
+	/* Fetch formatted value */
1368
+	check_len = fetchf_named_setting ( name, NULL, 0, *data,
1369
+					   ( len + 1 /* NUL */ ) );
1370
+	assert ( check_len == len );
1371
+	return len;
1372
+}
1373
+
1335 1374
 /******************************************************************************
1336 1375
  *
1337 1376
  * Setting types

+ 1
- 0
src/include/ipxe/settings.h View File

@@ -291,6 +291,7 @@ extern int storef_named_setting ( const char *name,
291 291
 extern int fetchf_named_setting ( const char *name, char *name_buf,
292 292
 				  size_t name_len, char *value_buf,
293 293
 				  size_t value_len );
294
+extern int fetchf_named_setting_copy ( const char *name, char **data );
294 295
 extern char * expand_settings ( const char *string );
295 296
 
296 297
 extern struct setting_type setting_type_string __setting_type;

Loading…
Cancel
Save