/
sys.ops
282 lines (192 loc) · 6.21 KB
/
sys.ops
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
/*
** sys.ops
*/
=head1 NAME
sys.ops - System Interaction Dynops
=head1 DESCRIPTION
Operations that allow the user to interact with the system.
=over 4
=cut
BEGIN_OPS_PREAMBLE
#ifdef PARROT_HAS_HEADER_SYSUTSNAME
# include <sys/utsname.h>
#endif
END_OPS_PREAMBLE
########################################
=item B<gmtime>(out STR, in INT)
Take the integer, which represents GMT epoch-seconds, and turn it into a
formatted string.
=item B<localtime>(out STR, in INT)
Take the integer, which represents GMT epoch-seconds, and turn it into a
formatted string. The time is adjusted to localtime before formatting.
=item B<decodetime>(out PMC, in INT)
Take the integer, which represents GMT epoch-seconds, and return an Array
PMC which represents the decoded time. The array elements are
(include "tm.pasm" for the constants):
=over 4
=item 0, .TM_SEC
Seconds (0-60)
=item 1, .TM_MIN
Minutes (0-59)
=item 2, .TM_HOUR
Hours (0-23)
=item 3, .TM_MDAY
Day of month (1-31)
=item 4, .TM_MON
Month of year (1-12) (Note, B<actual> month, not month - 1)
=item 5, .TM_YEAR
Year (note, B<actual> year. The year 2004 comes back as 2004, not 104)
=item 6, .TM_WDAY
Day of week. Sunday is day 0
=item 7, .TM_YDAY
Day of year (0-365)
=item 8, .TM_ISDST
Daylight savings/summer time in effect
=back
Timezone name will be GMT and offset will be 0, since the time is taken and
decoded as GMT.
=item B<decodelocaltime>(out PMC, in INT)
Take the passed in integer, which represents GMT epoch-seconds, and return
an array as in decodetime. The time is first shifted to localtime before
decoding.
=cut
op gmtime(out STR, in INT) {
struct tm tm;
const time_t t = (time_t) $2;
char resultbuffer[26];
Parrot_gmtime_r(&t, &tm);
Parrot_asctime_r(&tm, resultbuffer);
$1 = Parrot_str_new(interp, resultbuffer, 25);
}
op localtime(out STR, in INT) {
struct tm tm;
const time_t t = (time_t) $2;
char resultbuffer[26];
Parrot_localtime_r(&t, &tm);
Parrot_asctime_r(&tm, resultbuffer);
$1 = Parrot_str_new(interp, resultbuffer, 25);
}
op decodetime(out PMC, in INT) {
struct tm tm;
const time_t t = (time_t) $2;
Parrot_gmtime_r(&t, &tm);
$1 = Parrot_util_tm_to_array(interp, &tm);
}
op decodelocaltime(out PMC, in INT) {
struct tm tm;
const time_t t = (time_t) $2;
Parrot_localtime_r(&t, &tm);
$1 = Parrot_util_tm_to_array(interp, &tm);
}
########################################
=item B<sysinfo>(out STR, in INT)
Return system information. The values it returns depends on what you
ask. They are:
=over 4
=item C<PARROT_INTSIZE> The size of a platform integer
=item C<PARROT_FLOATSIZE> The size of a platform float
=item C<PARROT_POINTERSIZE> The size of a platform pointer (Largest possible data pointer)
=item C<PARROT_INTMAX> The maximum value of a platform integer
=item C<PARROT_INTMIN> The minimum value of a platform integer
=back
=item B<sysinfo>(out INT, in INT)
Return system information. The values it returns depends on what you
ask. They are:
=over 4
=item C<PARROT_OS> The OS name
This is something like "OS X", "MacOS", "Windows", or "VMS".
=item C<PARROT_OS_VERSION> The OS version string
This is the current (hopefully) OS Version. For OSes that have plain
version numbers it will be the version number. For OSes that name
their versions, it will be the version name.
On HP/UX, this may be something like 11.2, while on Windows it may be
"95", "98", "2000", "NT", "ME", or "XP".
=item C<PARROT_OS_VERSION_NUMBER> The OS version number string
On OSes that don't name themselves, this will be the same as what's
returned in the OS version string. On OSes that I<do> name themselves,
this will be a more specific version number. In all cases it is OS
dependent.
=item C<CPU_ARCH> The CPU architecture
The name of the CPU family, such as "VAX", "Alpha", "PPC", or "x86".
=item C<CPU_TYPE> The CPU model
The model name of the current system's CPU. This is something like
"EV68", "Athlon", or "Pentium III".
=back
Note that some of these values may be set at Parrot compile time,
rather than at runtime, if the system has no reliable way to fetch the
data dynamically, or if there is no way the value can change. Also,
the value returned may be somewhat misleading--Parrot running under a
VirtualPC window will report "Windows" as its OS and "x86" as its CPU,
even if it's really running on an OS X system.
=cut
inline op sysinfo(out STR, in INT) {
switch ($2) {
case PARROT_OS:
$1 = Parrot_str_new_constant(interp, BUILD_OS_NAME);
break;
#ifdef PARROT_HAS_HEADER_SYSUTSNAME
case PARROT_OS_VERSION:
{
struct utsname info;
if (uname(&info) == 0) {
$1 = Parrot_str_new_init(interp, info.version, strlen(info.version),
Parrot_ascii_encoding_ptr, 0);
}
}
break;
case PARROT_OS_VERSION_NUMBER:
{
struct utsname info;
if (uname(&info) == 0) {
$1 = Parrot_str_new_init(interp, info.release, strlen(info.version),
Parrot_ascii_encoding_ptr, 0);
}
}
break;
#endif
case CPU_ARCH:
$1 = Parrot_str_new_init(interp, PARROT_CPU_ARCH, sizeof (PARROT_CPU_ARCH) - 1,
Parrot_ascii_encoding_ptr, 0);
break;
case CPU_TYPE:
default:
$1 = string_from_literal(interp, "");
break;
}
}
inline op sysinfo(out INT, in INT) {
switch ($2) {
case PARROT_INTSIZE:
$1 = sizeof (INTVAL);
break;
case PARROT_FLOATSIZE:
$1 = sizeof (FLOATVAL);
break;
case PARROT_POINTERSIZE:
$1 = sizeof (void *);
break;
case PARROT_INTMIN:
$1 = PARROT_INTVAL_MIN;
break;
case PARROT_INTMAX:
$1 = PARROT_INTVAL_MAX;
break;
default:
$1 = -1;
break;
}
}
###############################################################################
=back
=head1 COPYRIGHT
Copyright (C) 2010, Parrot Foundation.
=head1 LICENSE
This program is free software. It is subject to the same license
as the Parrot interp itself.
=cut
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/