Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /branches/tendra5/src/producers/common/utility/system.c – Rev 2

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "system.h"
33
#include "c_types.h"
34
#include "error.h"
35
#include "file.h"
36
#include "ustring.h"
37
#include "xalloc.h"
38
 
39
 
40
/*
41
    FILE SEEKING FLAG
42
 
43
    The flag good_fseek is true if a simple fseek on the byte count has
44
    the desired effect, and false otherwise (for example on DOS machines).
45
    Similarly binary_mode is true if there are separate text and binary
46
    file modes.  The flag good_stat is true if the st_dev and st_ino
47
    fields of struct stat are sufficient to uniquely identify a file
48
    (this may not be true for machines with small ino_t types and
49
    file systems mounted from machines with large ino_t types).
50
*/
51
 
52
int good_fseek = 1 ;
53
int good_stat = 0 ;
54
int text_mode = 0 ;
55
int binary_mode = 0 ;
56
int file_sep = '/' ;
57
int drive_sep = 0 ;
58
 
59
 
60
/*
61
    SEEK A POSITION IN A FILE
62
 
63
    This routine seeks a position n bytes from the start of the file f.
64
    It returns 0 if good_fseek is false, otherwise it returns 1 for
65
    successful seeks and -1 otherwise.
66
*/
67
 
68
int file_seek
69
    PROTO_N ( ( f, n ) )
70
    PROTO_T ( FILE *f X long n )
71
{
72
    if ( n == 0 ) {
73
	/* Can always rewind a file */
74
	rewind ( f ) ;
75
	return ( 1 ) ;
76
    }
77
    if ( good_fseek ) {
78
	/* Use fseek if it works */
79
	int s = fseek ( f, n, SEEK_SET ) ;
80
	if ( s == -1 ) return ( -1 ) ;
81
	return ( 1 ) ;
82
    }
83
    return ( 0 ) ;
84
}
85
 
86
 
87
/*
88
    CONVERT A TIME TO AN UNSIGNED LONG
89
 
90
    This macro converts the time_t value A to an unsigned long.  Since
91
    time_t may be any arithmetic type, this may not be totally portable.
92
*/
93
 
94
#define time_value( A )		( ( unsigned long ) ( A ) )
95
 
96
 
97
/*
98
    CURRENT TIME
99
 
100
    These variables are used to hold the current time as a struct tm.
101
    The date is also used to form a string which is unique for each
102
    translation unit.
103
*/
104
 
105
char uniq_string [50] ;
106
unsigned long crt_time = 0 ;
107
static int have_crt_time = 0 ;
108
static struct tm crt_time_str ;
109
 
110
 
111
/*
112
    FIND THE CURRENT TIME
113
 
114
    This routine assigns the current time to the variables above, returning
115
    a pointer to crt_time_str.  Note that the actual time is taken only the
116
    first time the function is called.
117
*/
118
 
119
static struct tm *get_crt_time
120
    PROTO_Z ()
121
{
122
    if ( !have_crt_time ) {
123
	CONST char *s1, *s2 ;
124
	time_t t = time ( NIL ( time_t ) ) ;
125
	if ( t == ( time_t ) -1 ) {
126
	    /* Use a (seemingly) random date if time fails */
127
	    struct tm *st = &crt_time_str ;
128
	    st->tm_mday = 24 ;
129
	    st->tm_mon = 6 /* July */ ;
130
	    st->tm_year = 62 ;
131
	    st->tm_hour = 23 ;
132
	    st->tm_min = 30 ;
133
	    st->tm_sec = 0 ;
134
	    error ( ERROR_INTERNAL, "Can't find current date" ) ;
135
	} else {
136
	    struct tm *st = localtime ( &t ) ;
137
	    crt_time_str = *st ;
138
	    crt_time = time_value ( t ) ;
139
	}
140
	have_crt_time = 1 ;
141
	s1 = find_time ( "%.2d%.2d%.2d" ) ;
142
	s2 = find_date ( "%s%.2d_%d" ) ;
143
	sprintf_v ( uniq_string, "%s_%s", s1, s2 ) ;
144
    }
145
    return ( &crt_time_str ) ;
146
}
147
 
148
 
149
/*
150
    PRINT THE CURRENT DATE
151
 
152
    This routine returns a string giving the current date in the form
153
    given by fmt.  This is used in the __DATE__ built-in macro.
154
*/
155
 
156
CONST char *find_date
157
    PROTO_N ( ( fmt ) )
158
    PROTO_T ( CONST char *fmt )
159
{
160
    static char buff [20] ;
161
    static CONST char *month_name [12] = {
162
	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
163
	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
164
    } ;
165
    struct tm *st = get_crt_time () ;
166
    int day = st->tm_mday ;
167
    CONST char *month = month_name [ st->tm_mon ] ;
168
    int year = 1900 + st->tm_year ;
169
    sprintf_v ( buff, fmt, month, day, year ) ;
170
    return ( buff ) ;
171
}
172
 
173
 
174
/*
175
    PRINT THE CURRENT TIME
176
 
177
    This routine returns a string giving the current time in the form
178
    given by fmt.  This is used in the __TIME__ built-in macro.
179
*/
180
 
181
CONST char *find_time
182
    PROTO_N ( ( fmt ) )
183
    PROTO_T ( CONST char *fmt )
184
{
185
    static char buff [20] ;
186
    struct tm *st = get_crt_time () ;
187
    int hour = st->tm_hour ;
188
    int min = st->tm_min ;
189
    int sec = st->tm_sec ;
190
    sprintf_v ( buff, fmt, hour, min, sec ) ;
191
    return ( buff ) ;
192
}
193
 
194
 
195
/*
196
    FIND FILE STATISTICS
197
 
198
    This routine finds the file statistics for the file with pathname nm
199
    and assigns them into fs.  It returns fs if this is successful and the
200
    null pointer otherwise.
201
*/
202
 
203
STAT_TYPE *stat_func
204
    PROTO_N ( ( nm, fs ) )
205
    PROTO_T ( char *nm X STAT_TYPE *fs )
206
{
207
#if FS_POSIX
208
    int s = stat ( nm, fs ) ;
209
    if ( s != -1 ) return ( fs ) ;
210
#endif
211
    UNUSED ( nm ) ;
212
    return ( NULL ) ;
213
}
214
 
215
 
216
/*
217
    FIND A FILE DATE
218
 
219
    This routine converts the date from the file statistics fs to an
220
    unsigned long.
221
*/
222
 
223
unsigned long stat_date
224
    PROTO_N ( ( fs ) )
225
    PROTO_T ( STAT_TYPE *fs )
226
{
227
    unsigned long date = 0 ;
228
    if ( fs ) date = time_value ( fs->st_mtime ) ;
229
    return ( date ) ;
230
}
231
 
232
 
233
/*
234
    ARE TWO FILES EQUAL?
235
 
236
    This routine checks whether the files given by the statistics fs and
237
    gs represent the same file.  Two files are the same if their device
238
    and inode numbers are equal (except on machines where good_stat is
239
    false).
240
*/
241
 
242
int stat_equal
243
    PROTO_N ( ( fs, gs ) )
244
    PROTO_T ( STAT_TYPE *fs X STAT_TYPE *gs )
245
{
246
    if ( fs && gs ) {
247
	if ( fs->st_dev == gs->st_dev && fs->st_ino == gs->st_ino ) {
248
	    return ( good_stat ) ;
249
	}
250
    }
251
    return ( 0 ) ;
252
}
253
 
254
 
255
/*
256
    FIND THE CURRENT WORKING DIRECTORY
257
 
258
    This routine finds the current working directory, returning '.' if
259
    this cannot be found.
260
*/
261
 
262
CONST char *find_cwd
263
    PROTO_Z ()
264
{
265
    static CONST char *crt_directory = NULL ;
266
    if ( crt_directory == NULL ) {
267
#if FS_POSIX
268
	char buff [1024] ;
269
	char *nm = getcwd ( buff, 1024 ) ;
270
	if ( nm ) {
271
	    /* Copy result */
272
	    string dir = xustrcpy ( ustrlit ( nm ) ) ;
273
	    dir = make_pathname ( dir ) ;
274
	    crt_directory = strlit ( dir ) ;
275
	} else {
276
	    crt_directory = "." ;
277
	}
278
#else
279
	crt_directory = "." ;
280
#endif
281
    }
282
    return ( crt_directory ) ;
283
}
284
 
285
 
286
/*
287
    FIND THE CURRENT MACHINE
288
 
289
    This routine finds the name of the machine on which the program is
290
    running, returning the empty string if this cannot be found.
291
*/
292
 
293
CONST char *find_machine
294
    PROTO_Z ()
295
{
296
    static CONST char *machine_name = NULL ;
297
    if ( machine_name == NULL ) {
298
#if FS_UTSNAME
299
	struct utsname un ;
300
	if ( uname ( &un ) != -1 ) {
301
	    string s = ustrlit ( un.nodename ) ;
302
	    s = xustrcpy ( s ) ;
303
	    machine_name = strlit ( s ) ;
304
	    return ( machine_name ) ;
305
	}
306
#endif
307
	machine_name = "" ;
308
    }
309
    return ( machine_name ) ;
310
}