37 #include <sys/types.h>
42 #ifdef HAVE_SYS_MMAN_H
46 #include <glib/gstdio.h>
53 #define dprintf if(DEBUG) printf
56 gerb_fopen(
char const * filename)
61 dprintf(
"---> Entering gerb_fopen, filename = %s\n", filename);
62 #ifdef HAVE_SYS_MMAN_H
63 fd = (gerb_file_t *)g_malloc(
sizeof(gerb_file_t));
68 dprintf(
" Doing fopen\n");
69 fd->fd = fopen(filename,
"r");
75 dprintf(
" Doing fstat\n");
77 fd->fileno = fileno(fd->fd);
78 if (fstat(fd->fileno, &statinfo) < 0) {
84 dprintf(
" Checking S_ISREG\n");
85 if (!S_ISREG(statinfo.st_mode)) {
92 dprintf(
" Checking statinfo.st_size\n");
93 if ((
int)statinfo.st_size == 0) {
100 dprintf(
" Doing mmap\n");
101 fd->datalen = (int)statinfo.st_size;
102 fd->data = (
char *)mmap(0, statinfo.st_size, PROT_READ, MAP_PRIVATE,
104 if(fd->data == MAP_FAILED) {
111 fd = (gerb_file_t *)g_malloc(
sizeof(gerb_file_t));
117 fd->fd = g_fopen(filename,
"rb");
118 if (fd->fd == NULL) {
123 fd->fileno = fileno(fd->fd);
124 if (fstat(fd->fileno, &statinfo) < 0) {
129 if (!S_ISREG(statinfo.st_mode)) {
135 if ((
int)statinfo.st_size == 0) {
141 fd->datalen = (int)statinfo.st_size;
142 fd->data = calloc(1, statinfo.st_size + 1);
143 if (fd->data == NULL) {
148 if (fread((
void*)fd->data, 1, statinfo.st_size, fd->fd) != statinfo.st_size) {
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)
180 result = strtol(fd->data + fd->ptr, &end, 10);
182 GERB_COMPILE_ERROR(_(
"Failed to read integer"));
187 *len = end - (fd->data + fd->ptr);
190 fd->ptr = end - fd->data;
192 if (len && (result < 0))
200 gerb_fgetdouble(gerb_file_t *fd)
206 result = strtod(fd->data + fd->ptr, &end);
208 GERB_COMPILE_ERROR(_(
"Failed to read double"));
212 fd->ptr = end - fd->data;
219 gerb_fgetstring(gerb_file_t *fd,
char term)
226 iend = fd->data + fd->datalen;
227 for (i = fd->data + fd->ptr; i < iend; i++) {
237 len = strend - (fd->data + fd->ptr);
239 newstr = (
char *)g_malloc(len + 1);
242 strncpy(newstr, fd->data + fd->ptr, len);
251 gerb_ungetc(gerb_file_t *fd)
261 gerb_fclose(gerb_file_t *fd)
264 #ifdef HAVE_SYS_MMAN_H
265 if (munmap(fd->data, fd->datalen) < 0)
266 GERB_FATAL_ERROR(
"munmap: %s", strerror(errno));
270 if (fclose(fd->fd) == EOF)
271 GERB_FATAL_ERROR(
"fclose: %s", strerror(errno));
280 gerb_find_file(
char const * filename,
char **paths)
282 char *curr_path = NULL;
283 char *complete_path = NULL;
288 for (i = 0; paths[i] != NULL; i++) {
289 printf(
"%s(): paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
294 for (i = 0; paths[i] != NULL; i++) {
295 dprintf(
"%s(): Try paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
300 if (paths[i][0] ==
'$') {
301 char *env_name, *env_value, *tmp;
306 tmp = strchr(paths[i], G_DIR_SEPARATOR);
308 len = strlen(paths[i]) - 1;
310 len = tmp - paths[i] - 1;
311 env_name = (
char *)g_malloc(len + 1);
312 if (env_name == NULL)
314 strncpy(env_name, (
char *)(paths[i] + 1), len);
315 env_name[len] =
'\0';
317 env_value = getenv(env_name);
318 dprintf(
"%s(): Trying \"%s\" = \"%s\" from the environment\n",
319 __FUNCTION__, env_name,
320 env_value == NULL ?
"(null)" : env_value);
322 if (env_value == NULL) {
325 curr_path = (
char *)g_malloc(strlen(env_value) + strlen(&paths[i][len + 1]) + 1);
326 if (curr_path == NULL)
328 strcpy(curr_path, env_value);
329 strcat(curr_path, &paths[i][len + 1]);
333 curr_path = paths[i];
336 if (curr_path != NULL) {
340 complete_path = (
char *)g_malloc(strlen(curr_path) + strlen(filename) + 2);
341 if (complete_path == NULL)
343 strcpy(complete_path, curr_path);
344 complete_path[strlen(curr_path)] = G_DIR_SEPARATOR;
345 complete_path[strlen(curr_path) + 1] =
'\0';
346 strncat(complete_path, filename, strlen(filename));
348 if (paths[i][0] ==
'$') {
353 dprintf(
"%s(): Tring to access \"%s\"\n", __FUNCTION__,
356 if (access(complete_path, R_OK) != -1)
359 g_free(complete_path);
360 complete_path = NULL;
364 if (complete_path == NULL)
367 dprintf(
"%s(): returning complete_path = \"%s\"\n", __FUNCTION__,
368 complete_path == NULL ?
"(null)" : complete_path);
370 return complete_path;