-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathport.c
279 lines (245 loc) · 5.31 KB
/
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
#include "std.h"
#include "port.h"
#include "lint.h"
#include "file_incl.h"
#include "network_incl.h"
#if defined(WIN32) || defined(LATTICE)
int dos_style_link P2(char *, x, char *, y) {
char link_cmd[100];
sprintf(link_cmd, "copy %s %s", x, y);
return system(link_cmd);
}
#endif
/* for get_cpu_times() */
#ifdef GET_PROCESS_STATS
#include <sys/procstats.h>
#endif
#ifdef RUSAGE
#include <sys/resource.h>
#endif
#ifdef sun
time_t time PROT((time_t *));
#endif
/*
* Return a pseudo-random number in the range 0 .. n-1
*/
int random_number P1(int, n)
{
#if defined(RAND) || defined(DRAND48)
static char called = 0;
if (!called) {
time_t tim;
time(&tim);
# ifdef RAND
srand(tim);
# else
srand48(tim);
# endif
called = 1;
} /* endif */
# ifdef RAND
return rand() % n;
# else
return (int)(drand48() * n);
# endif
#else
# ifdef RANDOM
return random() % n;
# else /* RANDOM */
return current_time % n; /* You really don't want to use this method */
# endif /* RANDOM */
#endif /* RAND */
}
/*
* The function time() can't really be trusted to return an integer.
* But MudOS uses the 'current_time', which is an integer number
* of seconds. To make this more portable, the following functions
* should be defined in such a way as to return the number of seconds since
* some chosen year. The old behaviour of time(), is to return the number
* of seconds since 1970.
*/
int get_current_time()
{
return (int) time(0l); /* Just use the old time() for now */
}
char *time_string P1(time_t, t)
{
return ctime(&t);
}
/*
* Initialize the microsecond clock.
*/
void init_usec_clock()
{
#ifdef _SEQUENT_
usclk_init();
#endif
}
/*
* Get a microsecond clock sample.
*/
void
get_usec_clock P2(long *, sec, long *, usec)
{
#ifdef HAS_GETTIMEOFDAY
struct timeval tv;
gettimeofday(&tv, NULL);
*sec = tv.tv_sec;
*usec = tv.tv_usec;
#else
#ifdef _SEQUENT_
*sec = 0;
*usec = GETUSCLK();
#else
#ifdef LATTICE
unsigned int clock[2];
if (timer(clock)) {
*sec = time(0);
*usec = 0;
} else {
*sec = clock[0];
*usec = clock[1];
}
#else
*sec = time(0);
*usec = 0;
#endif
#endif
#endif
}
#ifdef USE_POSIX_SIGNALS
int
port_sigblock P1(sigset_t, mask)
{
sigset_t omask;
sigprocmask(SIG_BLOCK, &mask, &omask);
return (omask);
}
int
port_sigmask P1(int, sig)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, sig);
return (set);
}
void
(*port_signal(sig, func)) ()
int sig;
void (*func) ();
{
struct sigaction act, oact;
act.sa_handler = func;
act.sa_mask = 0;
act.sa_flags = 0;
if (sigaction(sig, &act, &oact) == -1)
return ((void (*) ()) -1);
return (oact.sa_handler);
}
int
port_sigsetmask P1(sigset_t, mask)
{
sigset_t omask;
sigprocmask(SIG_SETMASK, &mask, &omask);
return (omask);
}
#endif
int
get_cpu_times P2(unsigned long *, secs, unsigned long *, usecs)
{
#ifdef RUSAGE
struct rusage rus;
#endif
#if defined(TIMES) && !defined(RUSAGE)
struct tms t;
unsigned long total;
#endif
#ifdef GET_PROCESS_STATS
struct process_stats ps;
#endif
#ifdef RUSAGE /* start RUSAGE */
if (getrusage(RUSAGE_SELF, &rus) < 0) {
return 0;
}
*secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
*usecs = rus.ru_utime.tv_usec + rus.ru_stime.tv_usec;
return 1;
#else /* end then RUSAGE */
#ifdef GET_PROCESS_STATS /* start GET_PROCESS_STATS */
if (get_process_stats(NULL, PS_SELF, &ps, NULL) == -1) {
return 0;
}
*secs = ps.ps_utime.tv_sec + ps.ps_stime.tv_sec;
*usecs = ps.ps_utime.tv_usec + ps.ps_stime.tv_usec;
return 1;
#else /* end then GET_PROCESS_STATS */
#ifdef TIMES /* start TIMES */
times(&t);
*secs = (total = t.tms_utime + t.tms_stime) / CLK_TCK;
*usecs = ((total - (*secs * CLK_TCK)) * 1000000) / CLK_TCK;
return 1;
#else /* end then TIMES */
#ifdef LATTICE /* start LATTICE */
unsigned int clock[2];
if (timer(clock))
return 0;
*secs = clock[0];
*usecs = clock[1];
return 1;
#else
return 0;
#endif /* end LATTICE */
#endif /* end TIMES */
#endif /* end else GET_PROCESS_STATS */
#endif /* end else RUSAGE */
}
/* return the current working directory */
char *
get_current_dir P2(char *, buf, int, limit)
{
#ifdef HAS_GETCWD
return getcwd(buf, limit); /* POSIX */
#else
return getwd(buf); /* BSD */
#endif
}
#ifndef HAS_STRERROR
/* for those systems without strerror() but with sys_errlist, sys_nerr */
/* Warning: Sun has a prototype for strerror, but no definition for it,
so we can't use that name */
extern char *sys_errlist[];
extern int sys_nerr;
char *port_strerror P1(int, which)
{
if ((which < 0) || (which >= sys_nerr)) {
return "unknown error";
} else {
return sys_errlist[which];
}
}
#endif /* STRERROR */
#ifdef MEMMOVE_MISSING
/* for those without memmove() and a working bcopy() that can handle overlaps */
INLINE char *memmove P3(register char *, b, register char *, a, register int, s)
{
char *r = b;
if (b < a) {
while (s--)
*(b++) = *(a++);
} else if (b > a) {
b += s;
a += s;
while (s--)
*(--b) = *(--a);
}
return r;
}
#endif
#ifdef WIN32
char *WinStrError(int err) {
static char buf[30];
if (errno < 10000) return strerror(err);
sprintf(buf, "error #%d", err);
return buf;
}
#endif