37 #include <sys/types.h>
42 #ifdef HAVE_SYS_MMAN_H
46 #include <glib/gstdio.h>
58 gerb_fopen(
const char* filename) {
62 dprintf(
"---> Entering gerb_fopen, filename = %s\n", filename);
64 fd = g_new(gerb_file_t, 1);
69 dprintf(
" Doing fopen\n");
71 fd->fd = g_fopen(filename,
"rb");
77 dprintf(
" Doing fstat\n");
79 fd->fileno = fileno(fd->fd);
80 if (fstat(fd->fileno, &statinfo) < 0) {
86 dprintf(
" Checking S_ISREG\n");
87 if (!S_ISREG(statinfo.st_mode)) {
94 dprintf(
" Checking statinfo.st_size\n");
95 if ((
int)statinfo.st_size == 0) {
102 #ifdef HAVE_SYS_MMAN_H
104 dprintf(
" Doing mmap\n");
105 fd->datalen = (int)statinfo.st_size;
106 fd->data = (
char*)mmap(0, statinfo.st_size, PROT_READ, MAP_PRIVATE, fd->fileno, 0);
107 if (fd->data == MAP_FAILED) {
116 dprintf(
" Doing calloc\n");
117 fd->datalen = (int)statinfo.st_size;
118 fd->data = calloc(1, statinfo.st_size + 1);
119 if (fd->data == NULL) {
124 if (fread((
void*)fd->data, 1, statinfo.st_size, fd->fd) != statinfo.st_size) {
134 dprintf(
" Setting filename\n");
135 fd->filename = g_strdup(filename);
137 dprintf(
"<--- Leaving gerb_fopen\n");
142 gerb_fgetc(gerb_file_t* fd) {
144 if (fd->ptr >= fd->datalen)
147 return (
int)fd->data[fd->ptr++];
151 gerb_fgetint(gerb_file_t* fd,
int* len) {
156 result = strtol(fd->data + fd->ptr, &end, 10);
158 GERB_COMPILE_ERROR(_(
"Failed to read integer"));
163 *len = end - (fd->data + fd->ptr);
166 fd->ptr = end - fd->data;
168 if (len && (result < 0))
175 gerb_fgetdouble(gerb_file_t* fd) {
180 result = strtod(fd->data + fd->ptr, &end);
182 GERB_COMPILE_ERROR(_(
"Failed to read double"));
186 fd->ptr = end - fd->data;
192 gerb_fgetstring(gerb_file_t* fd,
char term) {
198 iend = fd->data + fd->datalen;
199 for (i = fd->data + fd->ptr; i < iend; i++) {
209 len = strend - (fd->data + fd->ptr);
211 newstr = (
char*)g_malloc(len + 1);
214 strncpy(newstr, fd->data + fd->ptr, len);
222 gerb_ungetc(gerb_file_t* fd) {
230 gerb_fclose(gerb_file_t* fd) {
232 g_free(fd->filename);
234 #ifdef HAVE_SYS_MMAN_H
235 if (munmap(fd->data, fd->datalen) < 0)
236 GERB_FATAL_ERROR(
"munmap: %s", strerror(errno));
240 if (fclose(fd->fd) == EOF)
241 GERB_FATAL_ERROR(
"fclose: %s", strerror(errno));
250 char* curr_path = NULL;
251 char* complete_path = NULL;
256 for (i = 0; paths[i] != NULL; i++) {
257 printf(
"%s(): paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
262 for (i = 0; paths[i] != NULL; i++) {
263 dprintf(
"%s(): Try paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
268 if (paths[i][0] ==
'$') {
269 char *env_name, *env_value, *tmp;
274 tmp = strchr(paths[i], G_DIR_SEPARATOR);
276 len = strlen(paths[i]) - 1;
278 len = tmp - paths[i] - 1;
279 env_name = (
char*)g_malloc(len + 1);
280 if (env_name == NULL)
282 strncpy(env_name, (
char*)(paths[i] + 1), len);
283 env_name[len] =
'\0';
285 env_value = getenv(env_name);
287 "%s(): Trying \"%s\" = \"%s\" from the environment\n", __FUNCTION__, env_name,
288 env_value == NULL ?
"(null)" : env_value
291 if (env_value == NULL) {
294 curr_path = (
char*)g_malloc(strlen(env_value) + strlen(&paths[i][len + 1]) + 1);
295 if (curr_path == NULL)
297 strcpy(curr_path, env_value);
298 strcat(curr_path, &paths[i][len + 1]);
302 curr_path = paths[i];
305 if (curr_path != NULL) {
309 complete_path = g_build_filename(curr_path, filename, NULL);
310 if (complete_path == NULL)
313 if (paths[i][0] ==
'$') {
318 dprintf(
"%s(): Tring to access \"%s\"\n", __FUNCTION__, complete_path);
320 if (access(complete_path, R_OK) != -1)
323 g_free(complete_path);
324 complete_path = NULL;
328 if (complete_path == NULL)
332 "%s(): returning complete_path = \"%s\"\n", __FUNCTION__, complete_path == NULL ?
"(null)" : complete_path
335 return complete_path;
char * gerb_find_file(const char *filename, char **paths)
Search for files in directories pointed out by paths, a NULL terminated list of directories to search...
Header info for the file parsing support functions.
The main header file for the libgerbv library.