Removed use of fgets() to read files and gerb_file approved file operations.
[geda-gerbv/spe.git] / src / gerb_file.c
blob848f0710383e4c4d86d3f4cc0470d80ffb64f5ad
1 /*
2 * gEDA - GNU Electronic Design Automation
3 * This file is a part of gerbv.
5 * Copyright (C) 2000-2002 Stefan Petersen (spe@stacken.kth.se)
7 * $Id$
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.
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.
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
24 /** \file gerb_file.c
25 \brief File parsing support functions
26 \ingroup libgerbv
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
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>
47 #include "common.h"
48 #include "gerbv.h"
49 #include "gerb_file.h"
51 #define GERB_FILE_ZFILENAME_LEN_MAX 255
53 /* DEBUG printing. #define DEBUG 1 in config.h to use this fcn. */
54 #define dprintf if(DEBUG) printf
56 gerb_file_t *
57 gerb_fopen(char const * filename, char const *zip)
59 gerb_file_t *fd;
60 struct stat statinfo;
61 unz_file_info zfileinfo;
62 int r;
64 dprintf("---> Entering gerb_fopen, filename = %s\n", filename);
66 fd = (gerb_file_t *)g_malloc(sizeof(gerb_file_t));
67 if (fd == NULL) {
68 return NULL;
71 memset(fd, 0, sizeof(gerb_file_t));
73 if (zip) {
74 fd->fd = 0;
75 fd->zfd = (FILE *)unzOpen(zip);
76 if (fd->zfd == NULL) {
77 g_free(fd);
78 return NULL;
80 if (unzLocateFile(fd->zfd, filename, 0) != UNZ_OK) {
81 unzClose(fd->zfd);
82 g_free(fd);
83 return NULL;
85 if (unzGetCurrentFileInfo(fd->zfd,
86 &zfileinfo,
87 NULL, 0,
88 NULL, 0,
89 NULL, 0) != UNZ_OK) {
90 unzClose(fd->zfd);
91 g_free(fd);
92 return NULL;
95 fd->data = g_malloc(zfileinfo.uncompressed_size);
96 if (fd->data == NULL) {
97 unzClose(fd->zfd);
98 g_free(fd);
99 return NULL;
102 if (unzOpenCurrentFile(fd->zfd) != UNZ_OK) {
103 unzClose(fd->zfd);
104 g_free(fd);
105 return NULL;
107 r = unzReadCurrentFile(fd->zfd, fd->data, zfileinfo.uncompressed_size);
108 if (r != zfileinfo.uncompressed_size) {
109 fprintf(stderr, "Read out wrong %d\n", r);
111 fd->datalen = (int)zfileinfo.uncompressed_size;
112 if (unzCloseCurrentFile(fd->zfd) != UNZ_OK) {
113 unzClose(fd->zfd);
114 g_free(fd);
115 return NULL;
117 } else {
118 fd->zfd = 0;
119 #ifdef HAVE_SYS_MMAN_H
120 dprintf(" Doing fopen\n");
121 fd->fd = fopen(filename, "r");
122 if (fd->fd == NULL) {
123 g_free(fd);
124 return NULL;
127 dprintf(" Doing fstat\n");
128 fd->fileno = fileno(fd->fd);
129 if (fstat(fd->fileno, &statinfo) < 0) {
130 fclose(fd->fd);
131 g_free(fd);
132 return NULL;
135 dprintf(" Checking S_ISREG\n");
136 if (!S_ISREG(statinfo.st_mode)) {
137 fclose(fd->fd);
138 g_free(fd);
139 errno = EISDIR;
140 return NULL;
143 dprintf(" Checking statinfo.st_size\n");
144 if ((int)statinfo.st_size == 0) {
145 fclose(fd->fd);
146 g_free(fd);
147 errno = EIO; /* More compatible with the world outside Linux */
148 return NULL;
151 dprintf(" Doing mmap\n");
152 fd->datalen = (int)statinfo.st_size;
153 fd->data = (char *)mmap(0, statinfo.st_size, PROT_READ, MAP_PRIVATE,
154 fd->fileno, 0);
155 if (fd->data == MAP_FAILED) {
156 fclose(fd->fd);
157 g_free(fd);
158 fd = NULL;
160 #else
161 /* all systems without mmap, not only MINGW32 */
163 if (stat(filename, &statinfo) < 0) {
164 fclose(fd->fd);
165 g_free(fd);
166 return NULL;
169 fd->fd = fopen(filename, "rb");
170 fd->fileno = fileno(fd->fd);
171 if (fstat(fd->fileno, &statinfo) < 0) {
172 fclose(fd->fd);
173 g_free(fd);
174 return NULL;
176 if (!S_ISREG(statinfo.st_mode)) {
177 fclose(fd->fd);
178 g_free(fd);
179 errno = EISDIR;
180 return NULL;
182 if ((int)statinfo.st_size == 0) {
183 fclose(fd->fd);
184 g_free(fd);
185 errno = EIO; /* More compatible with the world outside Linux */
186 return NULL;
188 fd->datalen = (int)statinfo.st_size;
189 fd->data = calloc(1, statinfo.st_size + 1);
190 if (fd->data == NULL) {
191 fclose(fd->fd);
192 g_free(fd);
193 return NULL;
195 if (fread((void*)fd->data, 1, statinfo.st_size, fd->fd) != statinfo.st_size) {
196 fclose(fd->fd);
197 g_free(fd->data);
198 g_free(fd);
199 return NULL;
201 rewind (fd->fd);
202 #endif
205 dprintf("<--- Leaving gerb_fopen\n");
206 return fd;
207 } /* gerb_fopen */
211 gerb_fgetc(gerb_file_t *fd)
214 if (fd->ptr >= fd->datalen)
215 return EOF;
217 return (int) fd->data[fd->ptr++];
218 } /* gerb_fgetc */
222 gerb_fgetint(gerb_file_t *fd, int *len)
224 long int result;
225 char *end;
227 errno = 0;
228 result = strtol(fd->data + fd->ptr, &end, 10);
229 if (errno) {
230 GERB_COMPILE_ERROR("Failed to read integer");
231 return 0;
234 if (len) {
235 *len = end - (fd->data + fd->ptr);
238 fd->ptr = end - fd->data;
240 if (len && (result < 0))
241 *len -= 1;
243 return (int)result;
244 } /* gerb_fgetint */
247 double
248 gerb_fgetdouble(gerb_file_t *fd)
250 double result;
251 char *end;
253 errno = 0;
254 result = strtod(fd->data + fd->ptr, &end);
255 if (errno) {
256 GERB_COMPILE_ERROR("Failed to read double");
257 return 0.0;
260 fd->ptr = end - fd->data;
262 return result;
263 } /* gerb_fgetdouble */
266 char *
267 gerb_fgetstring(gerb_file_t *fd, char term)
269 char *strend = NULL;
270 char *newstr;
271 char *i, *iend;
272 int len;
274 iend = fd->data + fd->datalen;
275 for (i = fd->data + fd->ptr; i < iend; i++) {
276 if (*i == term) {
277 strend = i;
278 break;
282 if (strend == NULL)
283 return NULL;
285 len = strend - (fd->data + fd->ptr);
287 newstr = (char *)g_malloc(len + 1);
288 if (newstr == NULL)
289 return NULL;
290 strncpy(newstr, fd->data + fd->ptr, len);
291 newstr[len] = '\0';
292 fd->ptr += len;
294 return newstr;
295 } /* gerb_fgetstring */
298 void
299 gerb_ungetc(gerb_file_t *fd)
301 if (fd->ptr)
302 fd->ptr--;
304 return;
305 } /* gerb_ungetc */
308 void
309 gerb_fclose(gerb_file_t *fd)
312 if (fd) {
313 if (fd->zfd) {
314 unzClose(fd->zfd);
315 g_free(fd->data);
317 if (fd->fd) {
318 #ifdef HAVE_SYS_MMAN_H
319 if (munmap(fd->data, fd->datalen) < 0)
320 GERB_FATAL_ERROR("munmap:%s", strerror(errno));
321 #else
322 g_free(fd->data);
323 #endif
324 if (fclose(fd->fd) == EOF)
325 GERB_FATAL_ERROR("fclose:%s", strerror(errno));
327 g_free(fd);
330 return;
331 } /* gerb_fclose */
334 void
335 gerb_freset(gerb_file_t *fd)
337 fd->ptr = 0;
338 #ifndef HAVE_SYS_MMAN_H
339 rewind(fd->fd);
340 #endif
342 } /* gerb_reset */
344 char *
345 gerb_find_file(char const * filename, char **paths)
347 char *curr_path = NULL;
348 char *complete_path = NULL;
349 int i;
351 #ifdef DEBUG
352 if( DEBUG > 0 ) {
353 for (i = 0; paths[i] != NULL; i++) {
354 printf("%s(): paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
357 #endif
359 for (i = 0; paths[i] != NULL; i++) {
360 dprintf("%s(): Try paths[%d] = \"%s\"\n", __FUNCTION__, i, paths[i]);
363 * Environment variables start with a $ sign
365 if (paths[i][0] == '$') {
366 char *env_name, *env_value, *tmp;
367 int len;
369 /* Extract environment name. Remember we start with a $ */
371 tmp = strchr(paths[i], G_DIR_SEPARATOR);
372 if (tmp == NULL)
373 len = strlen(paths[i]) - 1;
374 else
375 len = tmp - paths[i] - 1;
376 env_name = (char *)g_malloc(len + 1);
377 if (env_name == NULL)
378 return NULL;
379 strncpy(env_name, (char *)(paths[i] + 1), len);
380 env_name[len] = '\0';
382 env_value = getenv(env_name);
383 dprintf("%s(): Trying \"%s\" = \"%s\" from the environment\n",
384 __FUNCTION__, env_name,
385 env_value == NULL ? "(null)" : env_value);
387 if (env_value == NULL) {
388 curr_path = NULL;
389 } else {
390 curr_path = (char *)g_malloc(strlen(env_value) + strlen(&paths[i][len + 1]) + 1);
391 if (curr_path == NULL)
392 return NULL;
393 strcpy(curr_path, env_value);
394 strcat(curr_path, &paths[i][len + 1]);
395 g_free(env_name);
397 } else {
398 curr_path = paths[i];
401 if (curr_path != NULL) {
403 * Build complete path (inc. filename) and check if file exists.
405 complete_path = (char *)g_malloc(strlen(curr_path) + strlen(filename) + 2);
406 if (complete_path == NULL)
407 return NULL;
408 strcpy(complete_path, curr_path);
409 complete_path[strlen(curr_path)] = G_DIR_SEPARATOR;
410 complete_path[strlen(curr_path) + 1] = '\0';
411 strncat(complete_path, filename, strlen(filename));
413 if (paths[i][0] == '$') {
414 g_free(curr_path);
415 curr_path = NULL;
418 dprintf("%s(): Tring to access \"%s\"\n", __FUNCTION__,
419 complete_path);
421 if (access(complete_path, R_OK) != -1)
422 break;
424 g_free(complete_path);
425 complete_path = NULL;
429 if (complete_path == NULL)
430 errno = ENOENT;
432 dprintf("%s(): returning complete_path = \"%s\"\n", __FUNCTION__,
433 complete_path == NULL ? "(null)" : complete_path);
435 return complete_path;
436 } /* gerb_find_file */
442 * Extract the file names in a zip archive and return them in a GSList.
444 int
445 gerb_filenames_in_zip(char *zipname, GSList **filenames)
447 unzFile zfd;
448 int status;
449 int idx = 0;
450 gchar *zfilename;
451 GSList *fn = NULL;
453 /* Now try to open the zip file given and determine which files are in it*/
454 zfd = unzOpen(zipname);
455 if (zfd == NULL) {
456 GERB_FATAL_ERROR("unzOpen failed on %s\n", zipname);
457 return -1;
460 if (unzGoToFirstFile(zfd) != UNZ_OK) {
461 GERB_FATAL_ERROR("Failed to go to first file in %s\n", zipname);
462 unzClose(zfd);
463 return -1;
466 /* Insert all filenames in zip to a GSList */
467 zfilename = g_malloc(GERB_FILE_ZFILENAME_LEN_MAX);
468 while (1) {
469 memset(zfilename, 0, GERB_FILE_ZFILENAME_LEN_MAX);
470 unzGetCurrentFileInfo(zfd,
471 NULL,
472 zfilename,
473 255,
474 NULL,
476 NULL,
478 fn = g_slist_append(fn, (gpointer)g_strdup(zfilename));
479 idx++;
480 dprintf("%s(): Found file \"%s\" in the zip file\n",
481 __FUNCTION__, zfilename);
483 status = unzGoToNextFile(zfd);
484 if (status == UNZ_END_OF_LIST_OF_FILE) {
485 break;
487 if (status != UNZ_OK) {
488 GERB_FATAL_ERROR("Failed to go to next file.\n");
489 unzClose(zfd);
490 g_free(zfilename);
491 return -1;
494 g_free(zfilename);
496 *filenames = fn;
498 unzClose(zfd);
500 return idx;
501 } /* gerb_filenames_in_zip */