Index: libucdf/tester.c =================================================================== --- libucdf/tester.c (revision 35436) +++ libucdf/tester.c (revision 35437) @@ -66,6 +66,12 @@ if (argc > 1) fn = argv[1]; + if (ucdf_test_parse(fn) != 0) { + printf("%s doesn't look like a CDF file\n", fn); + return -1; + } + + if (ucdf_open(&ctx, fn) != 0) { printf("error reading %s: %d (%s)\n", fn, ctx.error, ucdf_error_str[ctx.error]); return -1; Index: libucdf/ucdf.c =================================================================== --- libucdf/ucdf.c (revision 35436) +++ libucdf/ucdf.c (revision 35437) @@ -406,7 +406,7 @@ return 0; } -int ucdf_open(ucdf_ctx_t *ctx, const char *path) +static int ucdf_open_(ucdf_ctx_t *ctx, const char *path) { ctx->f = fopen(path, "rb"); if (ctx->f == NULL) { @@ -414,9 +414,28 @@ return -1; } - if (ucdf_read_hdr(ctx) != 0) - goto error; + if (ucdf_read_hdr(ctx) != 0) { + fclose(ctx->f); + return -1; + } + return 0; +} +int ucdf_test_parse(const char *path) +{ + ucdf_ctx_t ctx; + if (ucdf_open_(&ctx, path) == 0) { + fclose(ctx.f); + return 0; + } + return -1; +} + +int ucdf_open(ucdf_ctx_t *ctx, const char *path) +{ + if (ucdf_open_(ctx, path) != 0) + return -1; + if (ucdf_read_sats(ctx) != 0) goto error; Index: libucdf/ucdf.h =================================================================== --- libucdf/ucdf.h (revision 35436) +++ libucdf/ucdf.h (revision 35437) @@ -84,6 +84,10 @@ ucdf_file_t ssd_f; }; +/* Look at a file, try to read some headers (cheap) to decide if path is a + valid CDF file. Returns 0 on no error (file is CDF), -1 on error. */ +int ucdf_test_parse(const char *path); + /* Open and map a CDF file. Returns -1 on error, error code is in ctx->error */ int ucdf_open(ucdf_ctx_t *ctx, const char *path);