Add snprintf for sysfs buffer writing

This commit is contained in:
Mark
2020-01-27 12:59:02 +02:00
parent c28da05558
commit bfe2999b8a
7 changed files with 314 additions and 1063 deletions
+21
View File
@@ -1,6 +1,27 @@
#pragma once
#include "sys/types.h"
#define sysfs_buf_puts(buf, lim, s) \
{ \
if (lim > 0) { \
size_t l = MIN(strlen(s), (lim) - 1); \
strncpy(buf, s, l); \
buf += l; \
*buf = 0; \
lim -= l; \
} \
}
#define sysfs_buf_printf(buf, lim, fmt, ...) \
{ \
int res = snprintf(buf, lim, fmt, ##__VA_ARGS__); \
if (res >= (int) lim) { \
lim = 0; \
} else { \
buf += res; \
lim -= res; \
} \
}
typedef int (*cfg_read_func_t)(void *ctx, char *buf, size_t lim);
typedef int (*cfg_write_func_t)(void *ctx, const char *value);
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#include <stdarg.h>
#include "sys/types.h"
int snprintf(char *buf, size_t lim, const char *fmt, ...);
int vsnprintf(char *buf, size_t lim, const char *fmt, va_list args);