From b459f6bd9493013dd8f933ef5db66e47218a72df Mon Sep 17 00:00:00 2001 From: rlar Date: Thu, 13 Oct 2016 20:21:11 +0200 Subject: [PATCH] table2D, table3D, swallow warnings and use fstat() --- src/xspice/icm/table/table2D/cfunc.mod | 32 ++++++++++++++------------ src/xspice/icm/table/table3D/cfunc.mod | 32 ++++++++++++++------------ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/xspice/icm/table/table2D/cfunc.mod b/src/xspice/icm/table/table2D/cfunc.mod index ddcc10cb0..1f64cbccf 100644 --- a/src/xspice/icm/table/table2D/cfunc.mod +++ b/src/xspice/icm/table/table2D/cfunc.mod @@ -68,6 +68,9 @@ NON-STANDARD FEATURES #include #include +#include +#include + #include "mada/eno2.h" /*=== CONSTANTS ========================*/ @@ -369,12 +372,12 @@ cm_table2D(ARGS) /* structure holding parms, inputs, outputs, etc. */ double tmp; char *cFile, *cThisPtr, *cThisLine, *cThisLinePtr; int isNewline; /* Boolean indicating we've read a CR or LF */ - long lFileLen; /* Length of file */ - long lFileRead; /* Length of file read in */ + size_t lFileLen; /* Length of file */ + size_t lFileRead; /* Length of file read in */ long lIndex; /* Index into cThisLine array */ int lLineCount; /* Current line number */ - long lStartPos; /* Offset of start of current line */ - long lTotalChars; /* Total characters read */ + size_t lStartPos; /* Offset of start of current line */ + size_t lTotalChars; /* Total characters read */ int interporder; /* order of interpolation for eno */ /* allocate static storage for *loc */ @@ -398,22 +401,21 @@ cm_table2D(ARGS) /* structure holding parms, inputs, outputs, etc. */ loc->state->fp = fopen(p, "r"); free(p); } - if (!loc->state->fp) { - cm_message_printf("cannot open file %s", PARAM(file)); - loc->state->atend = 1; - loc->init_err = 1; - return; - } + } + struct stat st; + if (!loc->state->fp || fstat(fileno(loc->state->fp), &st)) { + cm_message_printf("cannot open file %s", PARAM(file)); + loc->state->atend = 1; + loc->init_err = 1; + return; } /* get file length */ - fseek(loc->state->fp, 0L, SEEK_END); /* Position to end of file */ - lFileLen = ftell(loc->state->fp); /* Get file length */ - rewind(loc->state->fp); /* Back to start of file */ + lFileLen = (size_t) st.st_size; /* create string to hold the whole file */ - cFile = calloc((size_t) lFileLen + 1, sizeof(char)); + cFile = calloc(lFileLen + 1, sizeof(char)); /* create another string long enough for file manipulation */ - cThisLine = calloc((size_t) lFileLen + 1, sizeof(char)); + cThisLine = calloc(lFileLen + 1, sizeof(char)); if (cFile == NULL || cThisLine == NULL) { cm_message_printf("Insufficient memory to read file %s", PARAM(file)); loc->state->atend = 1; diff --git a/src/xspice/icm/table/table3D/cfunc.mod b/src/xspice/icm/table/table3D/cfunc.mod index c8daba003..08c6da435 100644 --- a/src/xspice/icm/table/table3D/cfunc.mod +++ b/src/xspice/icm/table/table3D/cfunc.mod @@ -68,6 +68,9 @@ NON-STANDARD FEATURES #include #include +#include +#include + #include "mada/eno2.h" #include "mada/eno3.h" @@ -377,12 +380,12 @@ cm_table3D(ARGS) /* structure holding parms, inputs, outputs, etc. */ double tmp; char *cFile, *cThisPtr, *cThisLine, *cThisLinePtr; int isNewline; /* Boolean indicating we've read a CR or LF */ - long lFileLen; /* Length of file */ - long lFileRead; /* Length of file read in */ + size_t lFileLen; /* Length of file */ + size_t lFileRead; /* Length of file read in */ long lIndex; /* Index into cThisLine array */ int lLineCount; /* Current line number */ - long lStartPos; /* Offset of start of current line */ - long lTotalChars; /* Total characters read */ + size_t lStartPos; /* Offset of start of current line */ + size_t lTotalChars; /* Total characters read */ int lTableCount; /* Number of tables */ int interporder; /* order of interpolation for eno */ @@ -408,22 +411,21 @@ cm_table3D(ARGS) /* structure holding parms, inputs, outputs, etc. */ loc->state->fp = fopen(pp, "r"); free(pp); } - if (!loc->state->fp) { - cm_message_printf("cannot open file %s", PARAM(file)); - loc->state->atend = 1; - loc->init_err = 1; - return; - } + } + struct stat st; + if (!loc->state->fp || fstat(fileno(loc->state->fp), &st)) { + cm_message_printf("cannot open file %s", PARAM(file)); + loc->state->atend = 1; + loc->init_err = 1; + return; } /* get file length */ - fseek(loc->state->fp, 0L, SEEK_END); /* Position to end of file */ - lFileLen = ftell(loc->state->fp); /* Get file length */ - rewind(loc->state->fp); /* Back to start of file */ + lFileLen = (size_t) st.st_size; /* create string to hold the whole file */ - cFile = calloc((size_t) lFileLen + 1, sizeof(char)); + cFile = calloc(lFileLen + 1, sizeof(char)); /* create another string long enough for file manipulation */ - cThisLine = calloc((size_t) lFileLen + 1, sizeof(char)); + cThisLine = calloc(lFileLen + 1, sizeof(char)); if (cFile == NULL || cThisLine == NULL) { cm_message_printf("Insufficient memory to read file %s", PARAM(file)); loc->state->atend = 1;