37 #include <sys/types.h>
42 #ifdef HAVE_SYS_MMAN_H
46 #include <glib/gstdio.h>
54 #define DPRINTF(...) do { if (DEBUG) printf(__VA_ARGS__); } while (0)
57 gerb_fopen(
char const * filename)
62 DPRINTF(
"---> Entering gerb_fopen, filename = %s\n", filename);
65 fd = g_new(gerb_file_t, 1);
67 DPRINTF(
" Doing fopen\n");
69 fd->fd = g_fopen(filename,
"rb");
71 int saved_errno = errno;
77 DPRINTF(
" Doing fstat\n");
79 fd->fileno = fileno(fd->fd);
80 if (fstat(fd->fileno, &statinfo) < 0) {
81 int saved_errno = errno;
88 DPRINTF(
" Checking S_ISREG\n");
89 if (!S_ISREG(statinfo.st_mode)) {
96 DPRINTF(
" Checking statinfo.st_size\n");
97 if ((
int)statinfo.st_size == 0) {
104 #ifdef HAVE_SYS_MMAN_H
106 DPRINTF(
" Doing mmap\n");
107 fd->datalen = (int)statinfo.st_size;
108 fd->data = (
char *)mmap(0, statinfo.st_size, PROT_READ, MAP_PRIVATE,
110 if(fd->data == MAP_FAILED) {
111 int saved_errno = errno;
122 char *buf = (
char *)g_malloc(fd->datalen + 1);
123 memcpy(buf, fd->data, fd->datalen);
124 buf[fd->datalen] =
'\0';
125 munmap(fd->data, fd->datalen);
132 DPRINTF(
" Doing calloc\n");
133 fd->datalen = (int)statinfo.st_size;
134 fd->data = calloc(1, statinfo.st_size + 1);
135 if (fd->data == NULL) {
136 int saved_errno = errno;
142 if (fread((
void*)fd->data, 1, statinfo.st_size, fd->fd) != statinfo.st_size) {
143 int saved_errno = errno;
154 DPRINTF(
" Setting filename\n");
155 fd->filename = g_strdup(filename);
157 DPRINTF(
"<--- Leaving gerb_fopen\n");
163 gerb_fgetc(gerb_file_t *fd)
166 if (fd->ptr >= fd->datalen)
169 return (
int) fd->data[fd->ptr++];
174 gerb_fgetint(gerb_file_t *fd,
int *len)
179 if (fd->ptr >= fd->datalen) {
186 result = strtol(fd->data + fd->ptr, &end, 10);
188 GERB_COMPILE_ERROR(_(
"Failed to read integer"));
193 *len = end - (fd->data + fd->ptr);
196 fd->ptr = end - fd->data;
198 if (len && (result < 0))
206 gerb_fgetdouble(gerb_file_t *fd)
213 if (fd->ptr >= fd->datalen)
216 start = fd->data + fd->ptr;
217 remaining = fd->datalen - fd->ptr;
224 && start[0] ==
'0' && (start[1] ==
'x' || start[1] ==
'X')) {
230 result = strtod(start, &end);
232 GERB_COMPILE_ERROR(_(
"Failed to read double"));
236 fd->ptr = end - fd->data;
243 gerb_fgetstring(gerb_file_t *fd,
char term)
250 iend = fd->data + fd->datalen;
251 for (i = fd->data + fd->ptr; i < iend; i++) {
261 len = strend - (fd->data + fd->ptr);
264 newstr = (
char *)g_malloc(len + 1);
265 strncpy(newstr, fd->data + fd->ptr, len);
274 gerb_ungetc(gerb_file_t *fd)
284 gerb_fclose(gerb_file_t *fd)
287 g_free(fd->filename);
293 if (fclose(fd->fd) == EOF)
294 GERB_FATAL_ERROR(
"fclose: %s", strerror(errno));
305 char *curr_path = NULL;
306 char *complete_path = NULL;
311 for (i = 0; paths[i] != NULL; i++) {
312 printf(
"%s(): paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
317 for (i = 0; paths[i] != NULL; i++) {
318 DPRINTF(
"%s(): Try paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
323 if (paths[i][0] ==
'$') {
324 char *env_name, *env_value, *tmp;
329 tmp = strchr(paths[i], G_DIR_SEPARATOR);
331 len = strlen(paths[i]) - 1;
333 len = tmp - paths[i] - 1;
336 env_name = (
char *)g_malloc(len + 1);
337 strncpy(env_name, (
char *)(paths[i] + 1), len);
338 env_name[len] =
'\0';
340 env_value = getenv(env_name);
341 DPRINTF(
"%s(): Trying \"%s\" = \"%s\" from the environment\n",
342 __FUNCTION__, env_name,
343 env_value == NULL ?
"(null)" : env_value);
345 if (env_value == NULL) {
348 curr_path = (
char *)g_malloc(strlen(env_value) + strlen(&paths[i][len + 1]) + 1);
349 strcpy(curr_path, env_value);
350 strcat(curr_path, &paths[i][len + 1]);
354 curr_path = paths[i];
357 if (curr_path != NULL) {
363 complete_path = g_build_filename(curr_path, filename, NULL);
365 if (paths[i][0] ==
'$') {
370 DPRINTF(
"%s(): Tring to access \"%s\"\n", __FUNCTION__,
373 if (access(complete_path, R_OK) != -1)
376 g_free(complete_path);
377 complete_path = NULL;
381 if (complete_path == NULL)
384 DPRINTF(
"%s(): returning complete_path = \"%s\"\n", __FUNCTION__,
385 complete_path == NULL ?
"(null)" : complete_path);
387 return complete_path;
char * gerb_find_file(char const *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.