-
Notifications
You must be signed in to change notification settings - Fork 9
/
Sys.fx
136 lines (119 loc) · 3.38 KB
/
Sys.fx
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
/*
This file is a part of ficus language project.
See ficus/LICENSE for the licensing terms
*/
// Various system services
import File
ccode {
#include <limits.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#ifndef PATH_MAX
#define PATH_MAX 8192
#endif
}
val argv =
{
pure nothrow fun argc(): int = ccode { return fx_argc() }
pure fun argv(i: int): string = ccode { return fx_cstr2str(fx_argv(i), -1, fx_result) }
[: for i <- 0:argc() {argv(i)} :]
}
val win32 : bool = ccode {
#if defined _WIN32 || defined WINCE
true
#else
false
#endif
}
val unix : bool = ccode {
#if defined __linux__ || defined __unix__ || defined __MACH__ || \
defined __APPLE__ || defined BSD || defined __hpux || \
defined _AIX || defined __sun
true
#else
false
#endif
}
fun osname_() {
var osname = ""
var osinfo = ""
fun(get_version: bool) {
if !get_version && win32 { "Windows" }
else {
if osinfo == "" {
var str = ""
val p = File.popen(if win32 {"ver"} else {"uname -msr"},
if win32 {"rt"} else {"r"})
while true {
str = p.readln()
if str.empty() { break }
str = str.strip()
if !str.empty() { break }
}
p.close()
osinfo = if !str.empty() { str } else if win32 { "Windows" } else { "Unix" }
val sp = osinfo.find(" ")
osname = if sp >= 0 {osinfo[:sp]} else {osinfo}
}
if get_version {osinfo} else {osname}
}
}
}
val osname = osname_()
pure fun cc_version(): string = ccode { return fx_cc_version(fx_result) }
fun appname() = List.hd(argv)
fun arguments() = List.tl(argv)
pure nothrow fun getTickCount(): int64 = ccode { return fx_tickcount() }
pure nothrow fun getTickFrequency(): double = ccode { return fx_tickfreq() }
fun remove(name: string): void = ccode
{
fx_cstr_t name_;
int fx_status = fx_str2cstr(name, &name_, 0, 0);
if (fx_status >= 0) {
if(remove(name_.data) != 0)
fx_status = FX_SET_EXN_FAST(FX_EXN_IOError);
fx_free_cstr(&name_);
}
return fx_status;
}
fun rename(name: string, new_name: string): bool = ccode
{
fx_cstr_t name_, new_name_;
int fx_status = fx_str2cstr(name, &name_, 0, 0);
if (fx_status >= 0) {
fx_status = fx_str2cstr(new_name, &new_name_, 0, 0);
if (fx_status >= 0) {
if(rename(name_.data, new_name_.data) != 0)
fx_status = FX_SET_EXN_FAST(FX_EXN_IOError);
fx_free_cstr(&new_name_);
}
fx_free_cstr(&name_);
}
return fx_status;
}
fun file_exists(name: string): bool = ccode
{
fx_cstr_t name_;
int fx_status = fx_str2cstr(name, &name_, 0, 0);
if (fx_status >= 0) {
struct stat s;
*fx_result = stat(name_.data, &s) == 0;
fx_free_cstr(&name_);
}
return fx_status;
}
fun getcwd(): string = ccode {
char buf[PATH_MAX+16];
char* p = getcwd(buf, PATH_MAX);
return fx_cstr2str(p, p ? -1 : 0, fx_result);
}
fun command(cmd: string): int = ccode {
fx_cstr_t cmd_;
int fx_status = fx_str2cstr(cmd, &cmd_, 0, 0);
if (fx_status >= 0) {
*fx_result = system(cmd_.data);
fx_free_cstr(&cmd_);
}
return fx_status;
}