Skip to content

Commit

Permalink
Fix warnings on Windows and reimplement err and errx as functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmens committed Oct 29, 2022
2 parents a641454 + 03da38a commit 93ce3c3
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions jo.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,35 @@
static JsonNode *pile; /* pile of nested objects/arrays */

#if defined(_WIN32) || defined(_AIX)
# define err(n, s) { fprintf(stderr, s); exit(n); }
# define errx(n, f, a) { fprintf(stderr, f, a); exit(n); }
#include <errno.h>
#include <stdarg.h>
static inline void err(int eval, const char *fmt, ...) {
int errnum = errno;

va_list ap;
va_start(ap, fmt);

fprintf(stderr, "jo: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, ": %s\n", strerror(errnum));

va_end(ap);
exit(eval);
}
static inline void errx(int eval, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);

fprintf(stderr, "jo: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");

va_end(ap);
exit(eval);
}
#endif

#ifdef _WIN32
#if defined(_WIN32) && !defined(fseeko)
# define fseeko fseek
# define ftello ftell
#endif
Expand Down Expand Up @@ -167,7 +191,7 @@ char *slurp_file(const char* filename, size_t *out_len, bool fold_newlines)
}

buf = slurp(fp, buffer_len, EOF, out_len, fold_newlines);
if (*out_len < 0) {
if (*out_len == (size_t)-1) {
errx(1, "File %s is too large to be read into memory", filename);
}
if (!use_stdin) fclose(fp);
Expand All @@ -179,7 +203,7 @@ char *slurp_line(FILE *fp, size_t *out_len)
char *buf;

buf = slurp(fp, SLURP_BLOCK_SIZE, '\n', out_len, false);
if (*out_len < 0) {
if (*out_len == (size_t)-1) {
errx(1, "Line too large to be read into memory");
}
return buf;
Expand Down Expand Up @@ -545,7 +569,7 @@ char* utf8_from_locale(const char *str, size_t len)
if (len == 0) {
return strdup("");
}
if (len == -1) {
if (len == (size_t)-1) {
len = strlen(str);
}
wcssize = MultiByteToWideChar(GetACP(), 0, str, len, NULL, 0);
Expand Down Expand Up @@ -578,7 +602,7 @@ char* locale_from_utf8(const char *utf8, size_t len)
if (len == 0) {
return strdup("");
}
if (len == -1) {
if (len == (size_t)-1) {
len = strlen(utf8);
}
wcssize = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
Expand Down

0 comments on commit 93ce3c3

Please sign in to comment.