-/* $OpenBSD: json.c,v 1.5 2023/04/26 19:14:54 claudio Exp $ */
+/* $OpenBSD: json.c,v 1.6 2023/04/26 20:53:17 claudio Exp $ */
/*
* Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
static char indent[JSON_MAX_STACK + 1];
static int level;
+static int eb;
static FILE *jsonfh;
static void
do_comma_indent(void)
{
if (stack[level].count++ > 0)
- fprintf(jsonfh, ",\n");
- fprintf(jsonfh, "\t%.*s", level, indent);
+ if (!eb)
+ eb = fprintf(jsonfh, ",\n") == -1;
+ if (!eb)
+ eb = fprintf(jsonfh, "\t%.*s", level, indent) == -1;
}
static void
{
if (stack[level].type == ARRAY)
return;
- fprintf(jsonfh, "\"%s\": ", name);
+ if (!eb)
+ eb = fprintf(jsonfh, "\"%s\": ", name) == -1;
}
static int
level = 0;
stack[level].type = START;
jsonfh = fh;
+ eb = 0;
- fprintf(jsonfh, "{\n");
+ eb = fprintf(jsonfh, "{\n") == -1;
}
-void
+int
json_do_finish(void)
{
while (level > 0)
json_do_end();
- fprintf(jsonfh, "\n}\n");
+ if (!eb)
+ eb = fprintf(jsonfh, "\n}\n") == -1;
+
+ return -eb;
}
void
do_comma_indent();
do_name(name);
- fprintf(jsonfh, "[\n");
+ if (!eb)
+ eb = fprintf(jsonfh, "[\n") == -1;
if (++level >= JSON_MAX_STACK)
errx(1, "json stack too deep");
do_comma_indent();
do_name(name);
- fprintf(jsonfh, "{\n");
+ if (!eb)
+ eb = fprintf(jsonfh, "{\n") == -1;
if (++level >= JSON_MAX_STACK)
errx(1, "json stack too deep");
void
json_do_end(void)
{
- if (stack[level].type == ARRAY)
- fprintf(jsonfh, "\n%.*s]", level, indent);
- else if (stack[level].type == OBJECT)
- fprintf(jsonfh, "\n%.*s}", level, indent);
- else
+ if (stack[level].type == ARRAY) {
+ if (!eb)
+ eb = fprintf(jsonfh, "\n%.*s]", level, indent) == -1;
+ } else if (stack[level].type == OBJECT) {
+ if (!eb)
+ eb = fprintf(jsonfh, "\n%.*s}", level, indent) == -1;
+ } else {
errx(1, "json bad stack state");
-
+ }
stack[level].name = NULL;
stack[level].type = NONE;
stack[level].count = 0;
do_comma_indent();
do_name(name);
- fprintf(jsonfh, "\"");
+ if (!eb)
+ eb = fprintf(jsonfh, "\"") == -1;
va_start(ap, fmt);
- vfprintf(jsonfh, fmt, ap);
+ if (!eb)
+ eb = vfprintf(jsonfh, fmt, ap) == -1;
va_end(ap);
- fprintf(jsonfh, "\"");
+ if (!eb)
+ eb = fprintf(jsonfh, "\"") == -1;
}
void
do_comma_indent();
do_name(name);
- fprintf(jsonfh, "\"");
+ if (!eb)
+ eb = fprintf(jsonfh, "\"") == -1;
for (i = 0; i < len; i++)
- fprintf(jsonfh, "%02x", *(data + i));
- fprintf(jsonfh, "\"");
+ if (!eb)
+ eb = fprintf(jsonfh, "%02x", *(data + i)) == -1;
+ if (!eb)
+ eb = fprintf(jsonfh, "\"") == -1;
}
void
{
do_comma_indent();
do_name(name);
- if (v)
- fprintf(jsonfh, "true");
- else
- fprintf(jsonfh, "false");
+ if (v) {
+ if (!eb)
+ eb = fprintf(jsonfh, "true") == -1;
+ } else {
+ if (!eb)
+ eb = fprintf(jsonfh, "false") == -1;
+ }
}
void
{
do_comma_indent();
do_name(name);
- fprintf(jsonfh, "%llu", v);
+ if (!eb)
+ eb = fprintf(jsonfh, "%llu", v) == -1;
}
void
{
do_comma_indent();
do_name(name);
- fprintf(jsonfh, "%lld", v);
+ if (!eb)
+ eb = fprintf(jsonfh, "%lld", v) == -1;
}
void
{
do_comma_indent();
do_name(name);
- fprintf(jsonfh, "%f", v);
+ if (!eb)
+ eb = fprintf(jsonfh, "%f", v) == -1;
}
-/* $OpenBSD: json.h,v 1.5 2023/04/26 19:14:54 claudio Exp $ */
+/* $OpenBSD: json.h,v 1.6 2023/04/26 20:53:17 claudio Exp $ */
/*
* Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
#include <stdarg.h>
#include <stdio.h>
-void json_do_start(FILE *);
-void json_do_finish(void);
-void json_do_array(const char *);
-void json_do_object(const char *);
-void json_do_end(void);
-void json_do_printf(const char *, const char *, ...)
- __attribute__((__format__ (printf, 2, 3)));
-void json_do_hexdump(const char *, void *, size_t);
-void json_do_bool(const char *, int);
-void json_do_uint(const char *, unsigned long long);
-void json_do_int(const char *, long long);
-void json_do_double(const char *, double);
+void json_do_start(FILE *);
+int json_do_finish(void);
+void json_do_array(const char *);
+void json_do_object(const char *);
+void json_do_end(void);
+void json_do_printf(const char *, const char *, ...)
+ __attribute__((__format__ (printf, 2, 3)));
+void json_do_hexdump(const char *, void *, size_t);
+void json_do_bool(const char *, int);
+void json_do_uint(const char *, unsigned long long);
+void json_do_int(const char *, long long);
+void json_do_double(const char *, double);