gerbv  2.6A
gerb_file.c
Go to the documentation of this file.
1 /*
2  * gEDA - GNU Electronic Design Automation
3  * This file is a part of gerbv.
4  *
5  * Copyright (C) 2000-2002 Stefan Petersen (spe@stacken.kth.se)
6  *
7  * $Id$
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22  */
23 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_MMAN_H
43 #include <sys/mman.h>
44 #endif
45 #include <errno.h>
46 #include <glib/gstdio.h>
47 
48 #include "common.h"
49 #include "gerbv.h"
50 #include "gerb_file.h"
51 
52 /* DEBUG printing. #define DEBUG 1 in config.h to use this fcn. */
53 #define dprintf if(DEBUG) printf
54 
55 gerb_file_t *
56 gerb_fopen(char const * filename)
57 {
58  gerb_file_t *fd;
59  struct stat statinfo;
60 
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));
64  if (fd == NULL) {
65  return NULL;
66  }
67 
68  dprintf(" Doing fopen\n");
69  fd->fd = fopen(filename, "r");
70  if (fd->fd == NULL) {
71  g_free(fd);
72  return NULL;
73  }
74 
75  dprintf(" Doing fstat\n");
76  fd->ptr = 0;
77  fd->fileno = fileno(fd->fd);
78  if (fstat(fd->fileno, &statinfo) < 0) {
79  fclose(fd->fd);
80  g_free(fd);
81  return NULL;
82  }
83 
84  dprintf(" Checking S_ISREG\n");
85  if (!S_ISREG(statinfo.st_mode)) {
86  fclose(fd->fd);
87  g_free(fd);
88  errno = EISDIR;
89  return NULL;
90  }
91 
92  dprintf(" Checking statinfo.st_size\n");
93  if ((int)statinfo.st_size == 0) {
94  fclose(fd->fd);
95  g_free(fd);
96  errno = EIO; /* More compatible with the world outside Linux */
97  return NULL;
98  }
99 
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,
103  fd->fileno, 0);
104  if(fd->data == MAP_FAILED) {
105  fclose(fd->fd);
106  g_free(fd);
107  fd = NULL;
108  }
109 #else
110  /* all systems without mmap, not only MINGW32 */
111  fd = (gerb_file_t *)g_malloc(sizeof(gerb_file_t));
112  if (fd == NULL) {
113  return NULL;
114  }
115 
116  /* fopen() can't open files with non ASCII filenames on windows */
117  fd->fd = g_fopen(filename, "rb");
118  if (fd->fd == NULL) {
119  g_free(fd);
120  return NULL;
121  }
122  fd->ptr = 0;
123  fd->fileno = fileno(fd->fd);
124  if (fstat(fd->fileno, &statinfo) < 0) {
125  fclose(fd->fd);
126  g_free(fd);
127  return NULL;
128  }
129  if (!S_ISREG(statinfo.st_mode)) {
130  fclose(fd->fd);
131  g_free(fd);
132  errno = EISDIR;
133  return NULL;
134  }
135  if ((int)statinfo.st_size == 0) {
136  fclose(fd->fd);
137  g_free(fd);
138  errno = EIO; /* More compatible with the world outside Linux */
139  return NULL;
140  }
141  fd->datalen = (int)statinfo.st_size;
142  fd->data = calloc(1, statinfo.st_size + 1);
143  if (fd->data == NULL) {
144  fclose(fd->fd);
145  g_free(fd);
146  return NULL;
147  }
148  if (fread((void*)fd->data, 1, statinfo.st_size, fd->fd) != statinfo.st_size) {
149  fclose(fd->fd);
150  g_free(fd->data);
151  g_free(fd);
152  return NULL;
153  }
154  rewind (fd->fd);
155 #endif
156 
157  dprintf("<--- Leaving gerb_fopen\n");
158  return fd;
159 } /* gerb_fopen */
160 
161 
162 int
163 gerb_fgetc(gerb_file_t *fd)
164 {
165 
166  if (fd->ptr >= fd->datalen)
167  return EOF;
168 
169  return (int) fd->data[fd->ptr++];
170 } /* gerb_fgetc */
171 
172 
173 int
174 gerb_fgetint(gerb_file_t *fd, int *len)
175 {
176  long int result;
177  char *end;
178 
179  errno = 0;
180  result = strtol(fd->data + fd->ptr, &end, 10);
181  if (errno) {
182  GERB_COMPILE_ERROR(_("Failed to read integer"));
183  return 0;
184  }
185 
186  if (len) {
187  *len = end - (fd->data + fd->ptr);
188  }
189 
190  fd->ptr = end - fd->data;
191 
192  if (len && (result < 0))
193  *len -= 1;
194 
195  return (int)result;
196 } /* gerb_fgetint */
197 
198 
199 double
200 gerb_fgetdouble(gerb_file_t *fd)
201 {
202  double result;
203  char *end;
204 
205  errno = 0;
206  result = strtod(fd->data + fd->ptr, &end);
207  if (errno) {
208  GERB_COMPILE_ERROR(_("Failed to read double"));
209  return 0.0;
210  }
211 
212  fd->ptr = end - fd->data;
213 
214  return result;
215 } /* gerb_fgetdouble */
216 
217 
218 char *
219 gerb_fgetstring(gerb_file_t *fd, char term)
220 {
221  char *strend = NULL;
222  char *newstr;
223  char *i, *iend;
224  int len;
225 
226  iend = fd->data + fd->datalen;
227  for (i = fd->data + fd->ptr; i < iend; i++) {
228  if (*i == term) {
229  strend = i;
230  break;
231  }
232  }
233 
234  if (strend == NULL)
235  return NULL;
236 
237  len = strend - (fd->data + fd->ptr);
238 
239  newstr = (char *)g_malloc(len + 1);
240  if (newstr == NULL)
241  return NULL;
242  strncpy(newstr, fd->data + fd->ptr, len);
243  newstr[len] = '\0';
244  fd->ptr += len;
245 
246  return newstr;
247 } /* gerb_fgetstring */
248 
249 
250 void
251 gerb_ungetc(gerb_file_t *fd)
252 {
253  if (fd->ptr)
254  fd->ptr--;
255 
256  return;
257 } /* gerb_ungetc */
258 
259 
260 void
261 gerb_fclose(gerb_file_t *fd)
262 {
263  if (fd) {
264 #ifdef HAVE_SYS_MMAN_H
265  if (munmap(fd->data, fd->datalen) < 0)
266  GERB_FATAL_ERROR("munmap: %s", strerror(errno));
267 #else
268  g_free(fd->data);
269 #endif
270  if (fclose(fd->fd) == EOF)
271  GERB_FATAL_ERROR("fclose: %s", strerror(errno));
272  g_free(fd);
273  }
274 
275  return;
276 } /* gerb_fclose */
277 
278 
279 char *
280 gerb_find_file(char const * filename, char **paths)
281 {
282  char *curr_path = NULL;
283  char *complete_path = NULL;
284  int i;
285 
286 #ifdef DEBUG
287  if( DEBUG > 0 ) {
288  for (i = 0; paths[i] != NULL; i++) {
289  printf("%s(): paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
290  }
291  }
292 #endif
293 
294  for (i = 0; paths[i] != NULL; i++) {
295  dprintf("%s(): Try paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
296 
297  /*
298  * Environment variables start with a $ sign
299  */
300  if (paths[i][0] == '$') {
301  char *env_name, *env_value, *tmp;
302  int len;
303 
304  /* Extract environment name. Remember we start with a $ */
305 
306  tmp = strchr(paths[i], G_DIR_SEPARATOR);
307  if (tmp == NULL)
308  len = strlen(paths[i]) - 1;
309  else
310  len = tmp - paths[i] - 1;
311  env_name = (char *)g_malloc(len + 1);
312  if (env_name == NULL)
313  return NULL;
314  strncpy(env_name, (char *)(paths[i] + 1), len);
315  env_name[len] = '\0';
316 
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);
321 
322  if (env_value == NULL) {
323  curr_path = NULL;
324  } else {
325  curr_path = (char *)g_malloc(strlen(env_value) + strlen(&paths[i][len + 1]) + 1);
326  if (curr_path == NULL)
327  return NULL;
328  strcpy(curr_path, env_value);
329  strcat(curr_path, &paths[i][len + 1]);
330  g_free(env_name);
331  }
332  } else {
333  curr_path = paths[i];
334  }
335 
336  if (curr_path != NULL) {
337  /*
338  * Build complete path (inc. filename) and check if file exists.
339  */
340  complete_path = (char *)g_malloc(strlen(curr_path) + strlen(filename) + 2);
341  if (complete_path == NULL)
342  return 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));
347 
348  if (paths[i][0] == '$') {
349  g_free(curr_path);
350  curr_path = NULL;
351  }
352 
353  dprintf("%s(): Tring to access \"%s\"\n", __FUNCTION__,
354  complete_path);
355 
356  if (access(complete_path, R_OK) != -1)
357  break;
358 
359  g_free(complete_path);
360  complete_path = NULL;
361  }
362  }
363 
364  if (complete_path == NULL)
365  errno = ENOENT;
366 
367  dprintf("%s(): returning complete_path = \"%s\"\n", __FUNCTION__,
368  complete_path == NULL ? "(null)" : complete_path);
369 
370  return complete_path;
371 } /* gerb_find_file */