helpers: Add a strdup_or_die() function

Add a helper similar to string_or_die() that performs a strdup and abort
on errors. This function have the same behaviour as string_or_die with
format set to "%s".
This commit is contained in:
Otavio Pontes
2018-07-13 16:15:41 -07:00
committed by Matthew Johnson
parent fb668f615c
commit 1eeed64dd0
2 changed files with 16 additions and 0 deletions
+15
View File
@@ -584,6 +584,21 @@ out_fds:
return ret;
}
/* Return a duplicated copy of the string using strdup().
* Abort if there's no memory to allocate the new string.
*/
char *strdup_or_die(const char *const str)
{
char *result;
result = strdup(str);
if (!result) {
abort();
}
return result;
}
void string_or_die(char **strp, const char *fmt, ...)
{
va_list ap;
+1
View File
@@ -357,6 +357,7 @@ extern void print_manifest_files(struct manifest *m);
extern void swupd_deinit(int lock_fd, struct list **subs);
extern int swupd_init(int *lock_fd);
extern void string_or_die(char **strp, const char *fmt, ...);
char *strdup_or_die(const char *const str);
extern void free_string(char **s);
void update_motd(int new_release);
void delete_motd(void);