-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathefuns_port.c
338 lines (300 loc) · 9.19 KB
/
efuns_port.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
efunctions.c: this file contains the efunctions called from
inside eval_instruction() in interpret.c. Note: if you are adding
local efunctions that are specific to your driver, you would be better
off adding them to a separate source file. Doing so will make it much
easier for you to upgrade (won't have to patch this file). Be sure
to #include "lpc_incl.h" in that separate source file.
*/
#include "std.h"
#include "lpc_incl.h"
#include "file_incl.h"
#include "lint.h"
#include "include/localtime.h"
#include "port.h"
#include "crypt.h"
#include "efun_protos.h"
/* get a value for CLK_TCK for use by times() */
#if (defined(TIMES) && !defined(RUSAGE))
/* this may need #ifdef'd to handle different types of machines */
#include <limits.h>
#endif
#ifdef F_CRYPT
#define SALT_LEN 8
void
f_crypt PROT((void))
{
char *res, *p, salt[SALT_LEN + 1];
char *choice =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
if (sp->type == T_STRING && SVALUE_STRLEN(sp) >= 2) {
p = sp->u.string;
} else {
int i;
for (i = 0; i < SALT_LEN; i++)
salt[i] = choice[random_number(strlen(choice))];
salt[SALT_LEN] = 0;
p = salt;
}
res = string_copy(CRYPT((sp-1)->u.string, p), "f_crypt");
pop_stack();
free_string_svalue(sp);
sp->subtype = STRING_MALLOC;
sp->u.string = res;
}
#endif
#ifdef F_OLDCRYPT
void
f_oldcrypt PROT((void)) {
char *res, salt[3];
char *choice =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
if (sp->type == T_STRING && SVALUE_STRLEN(sp) >= 2) {
salt[0] = sp->u.string[0];
salt[1] = sp->u.string[1];
free_string_svalue(sp--);
} else {
salt[0] = choice[random_number(strlen(choice))];
salt[1] = choice[random_number(strlen(choice))];
pop_stack();
}
salt[2] = 0;
res = string_copy(OLDCRYPT(sp->u.string, salt), "f_crypt");
free_string_svalue(sp);
sp->subtype = STRING_MALLOC;
sp->u.string = res;
}
#endif
#ifdef F_LOCALTIME
/* FIXME: most of the #ifdefs here should be based on configure checks
instead. Same for rusage() */
void
f_localtime PROT((void))
{
struct tm *tm;
array_t *vec;
time_t lt;
#ifdef sequent
struct timezone tz;
#endif
lt = sp->u.number;
tm = localtime(<);
vec = allocate_empty_array(11);
vec->item[LT_SEC].type = T_NUMBER;
vec->item[LT_SEC].u.number = tm->tm_sec;
vec->item[LT_MIN].type = T_NUMBER;
vec->item[LT_MIN].u.number = tm->tm_min;
vec->item[LT_HOUR].type = T_NUMBER;
vec->item[LT_HOUR].u.number = tm->tm_hour;
vec->item[LT_MDAY].type = T_NUMBER;
vec->item[LT_MDAY].u.number = tm->tm_mday;
vec->item[LT_MON].type = T_NUMBER;
vec->item[LT_MON].u.number = tm->tm_mon;
vec->item[LT_YEAR].type = T_NUMBER;
vec->item[LT_YEAR].u.number = tm->tm_year + 1900;
vec->item[LT_WDAY].type = T_NUMBER;
vec->item[LT_WDAY].u.number = tm->tm_wday;
vec->item[LT_YDAY].type = T_NUMBER;
vec->item[LT_YDAY].u.number = tm->tm_yday;
vec->item[LT_GMTOFF].type = T_NUMBER;
vec->item[LT_ZONE].type = T_STRING;
vec->item[LT_ZONE].subtype = STRING_MALLOC;
vec->item[LT_ISDST].type = T_NUMBER;
#if defined(BSD42) || defined(apollo) || defined(_AUX_SOURCE) \
|| defined(OLD_ULTRIX)
/* 4.2 BSD doesn't seem to provide any way to get these last three values */
vec->item[LT_GMTOFF].u.number = 0;
vec->item[LT_ZONE].type = T_NUMBER;
vec->item[LT_ZONE].u.number = 0;
vec->item[LT_ISDST].u.number = -1;
#else /* BSD42 */
vec->item[LT_ISDST].u.number = tm->tm_isdst;
#if defined(sequent)
vec->item[LT_GMTOFF].u.number = 0;
gettimeofday(NULL, &tz);
vec->item[LT_GMTOFF].u.number = tz.tz_minuteswest;
vec->item[LT_ZONE].u.string =
string_copy(timezone(tz.tz_minuteswest, tm->tm_isdst), "f_localtime");
#else /* sequent */
#if (defined(hpux) || defined(_SEQUENT_) || defined(_AIX) || defined(SunOS_5) \
|| defined(SVR4) || defined(sgi) || defined(linux) || defined(cray) \
|| defined(LATTICE) || defined(SCO))
if (!tm->tm_isdst) {
vec->item[LT_GMTOFF].u.number = timezone;
vec->item[LT_ZONE].u.string = string_copy(tzname[0], "f_localtime");
} else {
#if (defined(_AIX) || defined(hpux) || defined(linux) || defined(cray) \
|| defined(LATTICE))
vec->item[LT_GMTOFF].u.number = timezone;
#else
vec->item[LT_GMTOFF].u.number = altzone;
#endif
vec->item[LT_ZONE].u.string = string_copy(tzname[1], "f_localtime");
}
#else
#ifndef WIN32
vec->item[LT_GMTOFF].u.number = tm->tm_gmtoff;
vec->item[LT_ZONE].u.string = string_copy(tm->tm_zone, "f_localtime");
#else
vec->item[LT_GMTOFF].u.number = _timezone;
vec->item[LT_ZONE].u.string = string_copy(_tzname[_daylight?1:0],"f_localtime");
#endif
#endif
#endif /* sequent */
#endif /* BSD42 */
put_array(vec);
}
#endif
#ifdef F_RUSAGE
#ifdef WIN32
void f_rusage PROT((void))
{
error("rusage() not supported under Windows.\n");
}
#else
#ifdef RUSAGE
void
f_rusage PROT((void))
{
struct rusage rus;
mapping_t *m;
long usertime, stime;
int maxrss;
if (getrusage(RUSAGE_SELF, &rus) < 0) {
m = allocate_mapping(0);
} else {
#if 1 /* Was !SunOS_5 */
usertime = rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
stime = rus.ru_stime.tv_sec * 1000 + rus.ru_stime.tv_usec / 1000;
#else
usertime = rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_nsec / 1000000;
stime = rus.ru_stime.tv_sec * 1000 + rus.ru_stime.tv_nsec / 1000000;
#endif
maxrss = rus.ru_maxrss;
#ifdef sun
maxrss *= getpagesize() / 1024;
#endif
m = allocate_mapping(16);
add_mapping_pair(m, "utime", usertime);
add_mapping_pair(m, "stime", stime);
add_mapping_pair(m, "maxrss", maxrss);
add_mapping_pair(m, "ixrss", rus.ru_ixrss);
add_mapping_pair(m, "idrss", rus.ru_idrss);
add_mapping_pair(m, "isrss", rus.ru_isrss);
add_mapping_pair(m, "minflt", rus.ru_minflt);
add_mapping_pair(m, "majflt", rus.ru_majflt);
add_mapping_pair(m, "nswap", rus.ru_nswap);
add_mapping_pair(m, "inblock", rus.ru_inblock);
add_mapping_pair(m, "oublock", rus.ru_oublock);
add_mapping_pair(m, "msgsnd", rus.ru_msgsnd);
add_mapping_pair(m, "msgrcv", rus.ru_msgrcv);
add_mapping_pair(m, "nsignals", rus.ru_nsignals);
add_mapping_pair(m, "nvcsw", rus.ru_nvcsw);
add_mapping_pair(m, "nivcsw", rus.ru_nivcsw);
}
push_refed_mapping(m);
}
#else
#ifdef GET_PROCESS_STATS
void
f_rusage PROT((void))
{
struct process_stats ps;
mapping_t *m;
int utime, stime, maxrss;
if (get_process_stats(NULL, PS_SELF, &ps, NULL) == -1)
m = allocate_mapping(0);
else {
utime = ps.ps_utime.tv_sec * 1000 + ps.ps_utime.tv_usec / 1000;
stime = ps.ps_stime.tv_sec * 1000 + ps.ps_stime.tv_usec / 1000;
maxrss = ps.ps_maxrss * getpagesize() / 1024;
m = allocate_mapping(19);
add_mapping_pair(m, "utime", utime);
add_mapping_pair(m, "stime", stime);
add_mapping_pair(m, "maxrss", maxrss);
add_mapping_pair(m, "pagein", ps.ps_pagein);
add_mapping_pair(m, "reclaim", ps.ps_reclaim);
add_mapping_pair(m, "zerofill", ps.ps_zerofill);
add_mapping_pair(m, "pffincr", ps.ps_pffincr);
add_mapping_pair(m, "pffdecr", ps.ps_pffdecr);
add_mapping_pair(m, "swap", ps.ps_swap);
add_mapping_pair(m, "syscall", ps.ps_syscall);
add_mapping_pair(m, "volcsw", ps.ps_volcsw);
add_mapping_pair(m, "involcsw", ps.ps_involcsw);
add_mapping_pair(m, "signal", ps.ps_signal);
add_mapping_pair(m, "lread", ps.ps_lread);
add_mapping_pair(m, "lwrite", ps.ps_lwrite);
add_mapping_pair(m, "bread", ps.ps_bread);
add_mapping_pair(m, "bwrite", ps.ps_bwrite);
add_mapping_pair(m, "phread", ps.ps_phread);
add_mapping_pair(m, "phwrite", ps.ps_phwrite);
}
push_refed_mapping(m);
}
#else
#ifdef TIMES /* has times() but not getrusage() */
/*
warning times are reported in processor dependent units of time.
see man pages for 'times' to figure out how long a tick is on your system.
*/
void
f_rusage PROT((void))
{
mapping_t *m;
struct tms t;
times(&t);
m = allocate_mapping(2);
add_mapping_pair(m, "utime", t.tms_utime * 1000 / CLK_TCK);
add_mapping_pair(m, "stime", t.tms_stime * 1000 / CLK_TCK);
push_refed_mapping(m);
}
#else
#ifdef LATTICE
void
f_rusage PROT((void))
{
mapping_t *m;
int i;
unsigned int clock[2];
i = timer(clock); /* returns 0 if success, -1 otherwise */
m = allocate_mapping(2);
add_mapping_pair(m, "utime", i ? 0 : clock[0] * 1000 + clock[1] / 1000);
add_mapping_pair(m, "stime", i ? 0 : clock[0] * 1000 + clock[1] / 1000);
push_refed_mapping(m);
}
#endif /* LATTICE */
#endif /* TIMES */
#endif /* GET_PROCESS_STATS */
#endif /* RUSAGE */
#endif /* WIN32 */
#endif
#ifdef F_MALLOC_CHECK
/* this efun only useful on the NeXT (func_spec.c has #ifdef NeXT). A
non-zero return value indicates that some memory corruption has occurred
at some time prior to this calling of this efun.
*/
void
f_malloc_check PROT((void))
{
push_number(NXMallocCheck());
}
#endif
#ifdef F_MALLOC_DEBUG
/* NeXT specific efun for setting the debugging level of NeXT's built-in
malloc.
*/
void
f_malloc_debug PROT((void))
{
int level;
level = sp->u.number;
if (level < 0) {
int rc;
rc = malloc_debug(0);
malloc_singlethreaded();
sp->u.number = rc;
} else {
sp->u.number = malloc_debug(level);
}
}
#endif