struct json *json_object_from_file(const char *filename) { struct json *obj; int fd; if ((fd = open(filename, O_RDONLY)) < 0) { elog("json_object_from_file: error opening file %s: %s\n", filename, strerror(errno)); return NULL; } obj = json_object_from_fd(fd); close(fd); return obj; }
static void test_read_closed() { // Test reading from a closed fd int d = open("/dev/null", O_RDONLY, 0); close(d); json_object *jso = json_object_from_fd(d); if (jso != NULL) { printf("FAIL: read from closed fd returning non-NULL: %p\n", jso); fflush(stdout); printf(" jso=%s\n", json_object_to_json_string(jso)); json_object_put(jso); return; } printf("OK: json_object_from_fd(closed_fd), expecting NULL, EBADF, got:%p, %s\n", jso, json_util_get_last_err()); }
static void test_read_valid_with_fd(const char *testdir) { const char *filename = "./valid.json"; int d = open(filename, O_RDONLY, 0); if (d < 0) { fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno)); exit(1); } json_object *jso = json_object_from_fd(d); if (jso != NULL) { printf("OK: json_object_from_fd(%s)=%s\n", filename, json_object_to_json_string(jso)); json_object_put(jso); } else { fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename, json_util_get_last_err()); } close(d); }