mirror of
https://github.com/djhackersdev/saltytools.git
synced 2026-03-21 17:54:11 -05:00
feat(util): add str_dup()
This commit is contained in:
parent
c3da699f84
commit
1bb847c204
24
util/str.c
24
util/str.c
|
|
@ -1,10 +1,34 @@
|
|||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util/str.h"
|
||||
|
||||
int str_dup(char **out, const char *src) {
|
||||
size_t len;
|
||||
char *dest;
|
||||
|
||||
assert(out != NULL);
|
||||
assert(src != NULL);
|
||||
|
||||
*out = NULL;
|
||||
|
||||
len = strlen(src) + 1;
|
||||
dest = malloc(len);
|
||||
|
||||
if (dest == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
memcpy(dest, src, len);
|
||||
*out = dest;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool str_eq(const char *lhs, const char *rhs) {
|
||||
if (lhs == NULL || rhs == NULL) {
|
||||
return lhs == rhs;
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ struct strbuf {
|
|||
size_t pos;
|
||||
};
|
||||
|
||||
int str_dup(char **dest, const char *src);
|
||||
bool str_eq(const char *lhs, const char *rhs);
|
||||
|
||||
int str_printf(char **out, const char *fmt, ...)
|
||||
gcc_attribute((format(printf, 2, 3)));
|
||||
int str_vprintf(char **out, const char *fmt, va_list ap);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user