mirror of
https://github.com/troglobit/finit.git
synced 2026-10-16 10:39:35 +00:00
echo(): refactor and rename helper function -> fnwrite()
More often than not, the file to write to in sysfs changes rather than the value. This patch changes echo() into a fnwrite(), flipping what is vsnprintf()'ed, and adds a stupid str() function that converts any value (float/int/double/uint64_t) to a static string buffer. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
74d715243c
commit
6e24f2ff13
3 files changed
+41
-30
No files matched your search
+27
-14
@@ -56,28 +56,41 @@ char *progname(char *arg0)
|
||||
return prognm;
|
||||
}
|
||||
|
||||
int echo(char *file, int append, char *fmt, ...)
|
||||
char *str(char *fmt, ...)
|
||||
{
|
||||
static char buf[32];
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
int fnwrite(char *value, char *fmt, ...)
|
||||
{
|
||||
char path[256];
|
||||
va_list ap;
|
||||
FILE *fp;
|
||||
|
||||
if (!file)
|
||||
fp = stdout;
|
||||
else
|
||||
fp = fopen(file, append ? "a" : "w");
|
||||
if (!value) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(path, sizeof(path), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
fp = fopen(path, "w");
|
||||
if (!fp)
|
||||
return -1;
|
||||
|
||||
if (fmt) {
|
||||
va_start(ap, fmt);
|
||||
vfprintf(fp, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
/* echo(1) always adds a newline */
|
||||
fprintf(fp, "\n");
|
||||
if (file)
|
||||
fclose(fp);
|
||||
fputs(value, fp);
|
||||
fputs("\n", fp);
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user