|
|
@@ -65,6 +65,8 @@ static int sscanf_is_broken = 0; |
|
|
static int sscanf_is_broken_testdone = 0; |
|
|
static int sscanf_is_broken_testdone = 0; |
|
|
static void sscanf_is_broken_test(void); |
|
|
static void sscanf_is_broken_test(void); |
|
|
|
|
|
|
|
|
|
|
|
static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename); |
|
|
|
|
|
|
|
|
static char _last_err[256] = ""; |
|
|
static char _last_err[256] = ""; |
|
|
|
|
|
|
|
|
const char *json_util_get_last_err() |
|
|
const char *json_util_get_last_err() |
|
|
@@ -126,42 +128,61 @@ struct json_object* json_object_from_file(const char *filename) |
|
|
|
|
|
|
|
|
int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags) |
|
|
int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags) |
|
|
{ |
|
|
{ |
|
|
const char *json_str; |
|
|
|
|
|
int fd, ret; |
|
|
|
|
|
unsigned int wpos, wsize; |
|
|
|
|
|
|
|
|
int fd, ret; |
|
|
|
|
|
int saved_errno; |
|
|
|
|
|
|
|
|
if(!obj) { |
|
|
|
|
|
_set_last_err("json_object_to_file: object is null\n"); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (!obj) { |
|
|
|
|
|
_set_last_err("json_object_to_file: object is null\n"); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) { |
|
|
|
|
|
_set_last_err("json_object_to_file: error opening file %s: %s\n", |
|
|
|
|
|
filename, strerror(errno)); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) { |
|
|
|
|
|
_set_last_err("json_object_to_file: error opening file %s: %s\n", |
|
|
|
|
|
filename, strerror(errno)); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
ret = _json_object_to_fd(fd, obj, flags, filename); |
|
|
|
|
|
saved_errno = errno; |
|
|
|
|
|
close(fd); |
|
|
|
|
|
errno = saved_errno; |
|
|
|
|
|
return ret; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if(!(json_str = json_object_to_json_string_ext(obj,flags))) { |
|
|
|
|
|
close(fd); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
int json_object_to_fd(int fd, struct json_object *obj, int flags) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!obj) { |
|
|
|
|
|
_set_last_err("json_object_to_fd: object is null\n"); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */ |
|
|
|
|
|
wpos = 0; |
|
|
|
|
|
while(wpos < wsize) { |
|
|
|
|
|
if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) { |
|
|
|
|
|
close(fd); |
|
|
|
|
|
_set_last_err("json_object_to_file: error writing file %s: %s\n", |
|
|
|
|
|
filename, strerror(errno)); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return _json_object_to_fd(fd, obj, flags, NULL); |
|
|
|
|
|
} |
|
|
|
|
|
static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename) |
|
|
|
|
|
{ |
|
|
|
|
|
int ret; |
|
|
|
|
|
const char *json_str; |
|
|
|
|
|
unsigned int wpos, wsize; |
|
|
|
|
|
|
|
|
/* because of the above check for ret < 0, we can safely cast and add */ |
|
|
|
|
|
wpos += (unsigned int)ret; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
filename = filename ? filename : "(fd)"; |
|
|
|
|
|
|
|
|
close(fd); |
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
if (!(json_str = json_object_to_json_string_ext(obj,flags))) { |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */ |
|
|
|
|
|
wpos = 0; |
|
|
|
|
|
while(wpos < wsize) { |
|
|
|
|
|
if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) { |
|
|
|
|
|
_set_last_err("json_object_to_file: error writing file %s: %s\n", |
|
|
|
|
|
filename, strerror(errno)); |
|
|
|
|
|
return -1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* because of the above check for ret < 0, we can safely cast and add */ |
|
|
|
|
|
wpos += (unsigned int)ret; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// backwards compatible "format and write to file" function |
|
|
// backwards compatible "format and write to file" function |
|
|
|