1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
30 #include "tst_preload_FILE.h"
32 struct tst_fail_FILE
*failures
= NULL
;
34 void tst_fail_FILE_register(struct tst_fail_FILE
*self
)
39 static struct tst_fail_FILE
*failure_by_path(const char *path
,
40 enum tst_file_call call
)
47 for (i
= 0; failures
[i
].path
!= NULL
; i
++)
48 if (failures
[i
].call
== call
&&
49 !strcmp(path
, failures
[i
].path
))
55 void failures_init_FILE(const char *path
, FILE *f
)
62 //TODO: warn on f not NULL
63 for (i
= 0; failures
[i
].path
!= NULL
; i
++)
64 if (!strcmp(path
, failures
[i
].path
))
68 static struct tst_fail_FILE
*failure_by_FILE(FILE *f
, enum tst_file_call call
)
75 for (i
= 0; failures
[i
].path
!= NULL
; i
++)
76 if (failures
[i
].call
== call
&& f
== failures
[i
].f
)
82 FILE *fopen(const char *path
, const char *mode
)
84 static FILE *(*real_fopen
)(const char *, const char *);
87 real_fopen
= dlsym(RTLD_NEXT
, "fopen");
89 struct tst_fail_FILE
*failure
= failure_by_path(path
, TST_FAIL_FOPEN
);
99 FILE *f
= real_fopen(path
, mode
);
101 failures_init_FILE(path
, f
);
108 static int (*real_fclose
)(FILE *);
111 real_fclose
= dlsym(RTLD_NEXT
, "fclose");
113 struct tst_fail_FILE
*failure
= failure_by_FILE(fp
, TST_FAIL_FCLOSE
);
116 * We close the file here correctly, we can because when fclose() has
117 * failed any further access results in undefined behavior.
123 errno
= failure
->err
;
128 return real_fclose(fp
);