1 /*
2 Copyright (c) 1994 - 2010, Lawrence Livermore National Security, LLC.
3 LLNL-CODE-425250.
4 All rights reserved.
5 
6 This file is part of Silo. For details, see silo.llnl.gov.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11 
12    * Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the disclaimer below.
14    * Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the disclaimer (as noted
16      below) in the documentation and/or other materials provided with
17      the distribution.
18    * Neither the name of the LLNS/LLNL nor the names of its
19      contributors may be used to endorse or promote products derived
20      from this software without specific prior written permission.
21 
22 THIS SOFTWARE  IS PROVIDED BY  THE COPYRIGHT HOLDERS  AND CONTRIBUTORS
23 "AS  IS" AND  ANY EXPRESS  OR IMPLIED  WARRANTIES, INCLUDING,  BUT NOT
24 LIMITED TO, THE IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A  PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN  NO  EVENT SHALL  LAWRENCE
26 LIVERMORE  NATIONAL SECURITY, LLC,  THE U.S.  DEPARTMENT OF  ENERGY OR
27 CONTRIBUTORS BE LIABLE FOR  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR  CONSEQUENTIAL DAMAGES  (INCLUDING, BUT NOT  LIMITED TO,
29 PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS  OF USE,  DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER  IN CONTRACT, STRICT LIABILITY,  OR TORT (INCLUDING
32 NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT  OF THE USE  OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35 This work was produced at Lawrence Livermore National Laboratory under
36 Contract No.  DE-AC52-07NA27344 with the DOE.
37 
38 Neither the  United States Government nor  Lawrence Livermore National
39 Security, LLC nor any of  their employees, makes any warranty, express
40 or  implied,  or  assumes  any  liability or  responsibility  for  the
41 accuracy, completeness,  or usefulness of  any information, apparatus,
42 product, or  process disclosed, or  represents that its use  would not
43 infringe privately-owned rights.
44 
45 Any reference herein to  any specific commercial products, process, or
46 services by trade name,  trademark, manufacturer or otherwise does not
47 necessarily  constitute or imply  its endorsement,  recommendation, or
48 favoring  by  the  United  States  Government  or  Lawrence  Livermore
49 National Security,  LLC. The views  and opinions of  authors expressed
50 herein do not necessarily state  or reflect those of the United States
51 Government or Lawrence Livermore National Security, LLC, and shall not
52 be used for advertising or product endorsement purposes.
53 */
54 #include "config.h" /* For HAVE_MEMMOVE test. */
55 /*
56  * SCCTL.C - some core routines used by many packages
57  *
58  * Source Version: 2.0
59  * Software Release #92-0043
60  *
61  */
62 
63 #include "config.h" /* For the pointer checks. */
64 #include <time.h>
65 #include "score.h"
66 
67 /* Prototypes for other external functions. */
68 #ifndef WIN32
69 extern char *ctime(const time_t *);
70 #endif
71 
72 int	*lite_LAST ;
73 int	lite_SC_c_sp_alloc ;
74 int	lite_SC_c_sp_diff ;
75 int	lite_SC_c_sp_free ;
76 int	lite_SC_c_sp_max ;
77 char	*lite_SC_CHAR_S = "char";
78 
79 /* These prototypes are only included if we can't get them any other place.
80  * We need them for their pointers, below.
81  */
82 #ifndef HAVE_FCLOSE_POINTER
83 extern int fclose(FILE*);
84 #endif
85 #ifndef HAVE_FFLUSH_POINTER
86 extern int fflush(FILE*);
87 #endif
88 #ifndef HAVE_FOPEN_POINTER
89 extern FILE* fopen(const char*, const char*);
90 #endif
91 #ifndef HAVE_FPRINTF_POINTER
92 extern int fprintf(FILE*, const char*, ...);
93 #endif
94 #ifndef HAVE_FREAD_POINTER
95 extern size_t fread(void*, size_t, size_t, FILE*);
96 #endif
97 #ifndef HAVE_FSEEK_POINTER
98 extern int fseek(FILE*, long, int);
99 #endif
100 #ifndef HAVE_SETVBUF_POINTER
101 extern int setvbuf(FILE*, char*, int, size_t);
102 #endif
103 #ifndef HAVE_FTELL_POINTER
104 extern long ftell(FILE*);
105 #endif
106 #ifndef HAVE_FWRITE_POINTER
107 extern size_t fwrite(const void*, size_t, size_t, FILE*);
108 #endif
109 
110 /* declare the IO hooks */
111 PFfclose  lite_io_close_hook   = (PFfclose)  fclose;
112 PFfflush  lite_io_flush_hook   = (PFfflush)  fflush;
113 PFfopen   lite_io_open_hook    = (PFfopen)   fopen;
114 PFfprintf lite_io_printf_hook  = (PFfprintf) fprintf;
115 PFfread   lite_io_read_hook    = (PFfread)   fread;
116 PFfseek   lite_io_seek_hook    = (PFfseek)   fseek;
117 PFsetvbuf lite_io_setvbuf_hook = (PFsetvbuf) setvbuf;
118 PFftell   lite_io_tell_hook    = (PFftell)   ftell;
119 PFfwrite  lite_io_write_hook   = (PFfwrite)  fwrite;
120 
121 
122 
123 /*-------------------------------------------------------------------------
124  * Function:	lite_SC_regx_match
125  *
126  * Purpose:	Returns TRUE iff the first string arg matched the
127  *		regular expression defined by the second string arg.
128  *
129  * 		Regular expression specifiers (so far)
130  *
131  * 		   `*'	Matches any number of characters.
132  * 		   `?'  Matches any single character.
133  *
134  * Return:	Success:
135  *
136  *		Failure:
137  *
138  * Programmer:	Adapted from PACT SCORE
139  *		Mar 12, 1996
140  *
141  * Modifications:
142  *
143  *-------------------------------------------------------------------------
144  */
145 int
lite_SC_regx_match(char * s,char * patt)146 lite_SC_regx_match (char *s, char *patt) {
147 
148    int c;
149    char *ps, *pp;
150 
151    if (patt == NULL) return(TRUE);
152    if (s == NULL) return(FALSE);
153 
154    ps = s;
155    pp = patt;
156    while ((c = *pp++) != '\0') {
157       switch (c) {
158       case '*' :
159 	 while (*pp == '*') pp++;
160 	 c = *pp;
161 	 while ((ps = strchr(ps, c)) != NULL) {
162 	    if (lite_SC_regx_match(ps, pp)) return(TRUE);
163 	    ps++;
164 	 }
165 	 return(FALSE);
166 
167       case '?' :
168 	 return(lite_SC_regx_match(++ps, pp));
169 
170       case '\\' :
171 	 c = *pp++;
172 
173       default :
174 	 if (*ps++ != c) return(FALSE);
175       }
176 
177       if (((*ps == '\0') && (*pp != '\0') && (*pp != '*')) ||
178 	  ((*ps != '\0') && (*pp == '\0'))) {
179 	 return(FALSE);
180       }
181    }
182 
183    if (*ps == '\0') return(TRUE);
184    else return(FALSE);
185 }
186 
187 
188 /*-------------------------------------------------------------------------
189  * Function:	lite_SC_date
190  *
191  * Purpose:	Return a string with the time and date defined by the
192  *		ANSI function ctime.
193  *
194  * Return:	Success:
195  *
196  *		Failure:
197  *
198  * Programmer:	Robb Matzke
199  *		robb@callisto.matzke.cioe.com
200  *		Apr 17, 1996
201  *
202  * Modifications:
203  *
204  *-------------------------------------------------------------------------
205  */
206 char *
lite_SC_date(void)207 lite_SC_date (void) {
208 
209    time_t tm;
210    char t[MAXLINE];
211 
212    tm = time(NULL);
213    strcpy(t, ctime(&tm));
214    return lite_SC_strsavef (strtok(t, "\n"), "char*:SC_DATE:time");
215 }
216 
217