2 * Copyright © 2007, 2008 Ian D. Romanick
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #if defined(HAVE_FCNTL_H) && defined(HAVE_SYS_STAT_H) && defined(HAVE_SYS_TYPES_H) && defined(HAVE_UNISTD_H)
27 # include <sys/types.h>
28 # include <sys/stat.h>
35 # define WIN32_LEAN_AND_MEAN
44 char *piglit_load_text_file(const char *file_name
, unsigned *size
)
48 #if defined(USE_STDIO)
52 errno_t err
= fopen_s(&fp
, file_name
, "r");
54 if (err
|| (fp
== NULL
)) {
58 fp
= fopen(file_name
, "r");
64 if (fseek(fp
, 0, SEEK_END
) == 0) {
65 size_t len
= (size_t) ftell(fp
);
68 text
= malloc(len
+ 1);
70 size_t total_read
= 0;
73 size_t bytes
= fread(text
+ total_read
, 1,
74 len
- total_read
, fp
);
86 } while (total_read
< len
);
89 text
[total_read
] = '\0';
102 int fd
= open(file_name
, O_RDONLY
);
108 if (fstat(fd
, & st
) == 0) {
109 ssize_t total_read
= 0;
111 text
= malloc(st
.st_size
+ 1);
114 ssize_t bytes
= read(fd
, text
+ total_read
,
115 st
.st_size
- total_read
);
127 } while (total_read
< st
.st_size
);
129 text
[total_read
] = '\0';