/home/andy/git/oilshell/oil/cpp/stdlib.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // stdlib.cc: Replacement for standard library modules |
2 | | // and native/posixmodule.c |
3 | | |
4 | | #include "stdlib.h" |
5 | | |
6 | | #include <dirent.h> // closedir(), opendir(), readdir() |
7 | | #include <errno.h> |
8 | | #include <fcntl.h> // open |
9 | | #include <signal.h> // kill |
10 | | #include <sys/stat.h> // umask |
11 | | #include <sys/types.h> // umask |
12 | | #include <sys/wait.h> // WUNTRACED |
13 | | #include <time.h> |
14 | | #include <unistd.h> |
15 | | |
16 | | #include "mycpp/runtime.h" |
17 | | // To avoid circular dependency with e_die() |
18 | | #include "prebuilt/core/error.mycpp.h" |
19 | | |
20 | | using pyerror::e_die; |
21 | | |
22 | | namespace fcntl_ { |
23 | | |
24 | 0 | int fcntl(int fd, int cmd) { |
25 | 0 | int result = ::fcntl(fd, cmd); |
26 | 0 | if (result < 0) { |
27 | 0 | throw Alloc<IOError>(errno); |
28 | 0 | } |
29 | 0 | return result; |
30 | 0 | } |
31 | | |
32 | 0 | int fcntl(int fd, int cmd, int arg) { |
33 | 0 | int result = ::fcntl(fd, cmd, arg); |
34 | 0 | if (result < 0) { |
35 | 0 | throw Alloc<IOError>(errno); |
36 | 0 | } |
37 | 0 | return result; |
38 | 0 | } |
39 | | |
40 | | } // namespace fcntl_ |
41 | | |
42 | | namespace posix { |
43 | | |
44 | 0 | mode_t umask(mode_t mask) { |
45 | | // No error case: always succeeds |
46 | 0 | return ::umask(mask); |
47 | 0 | } |
48 | | |
49 | 1 | int open(Str* path, int flags, int perms) { |
50 | 1 | int result = ::open(path->data_, flags, perms); |
51 | 1 | if (result < 0) { |
52 | 1 | throw Alloc<OSError>(errno); |
53 | 1 | } |
54 | 0 | return result; |
55 | 1 | } |
56 | | |
57 | 0 | void dup2(int oldfd, int newfd) { |
58 | 0 | if (::dup2(oldfd, newfd) < 0) { |
59 | 0 | throw Alloc<OSError>(errno); |
60 | 0 | } |
61 | 0 | } |
62 | 1 | void putenv(Str* name, Str* value) { |
63 | 1 | int overwrite = 1; |
64 | 1 | int ret = ::setenv(name->data_, value->data_, overwrite); |
65 | 1 | if (ret < 0) { |
66 | 0 | throw Alloc<IOError>(errno); |
67 | 0 | } |
68 | 1 | } |
69 | | |
70 | 0 | mylib::LineReader* fdopen(int fd, Str* c_mode) { |
71 | 0 | FILE* f = ::fdopen(fd, c_mode->data_); |
72 | | |
73 | | // TODO: raise exception |
74 | 0 | assert(f); |
75 | | |
76 | 0 | return Alloc<mylib::CFileLineReader>(f); |
77 | 0 | } |
78 | | |
79 | 0 | void execve(Str* argv0, List<Str*>* argv, Dict<Str*, Str*>* environ) { |
80 | 0 | int n_args = len(argv); |
81 | | // never deallocated |
82 | 0 | char** _argv = static_cast<char**>(malloc((n_args + 1) * sizeof(char*))); |
83 | | |
84 | | // Annoying const_cast |
85 | | // https://stackoverflow.com/questions/190184/execv-and-const-ness |
86 | 0 | for (int i = 0; i < n_args; ++i) { |
87 | 0 | _argv[i] = const_cast<char*>(argv->index_(i)->data_); |
88 | 0 | } |
89 | 0 | _argv[n_args] = nullptr; |
90 | | |
91 | | // Convert environ into an array of pointers to strings of the form: "k=v". |
92 | 0 | int n_env = len(environ); |
93 | 0 | char** envp = static_cast<char**>(malloc((n_env + 1) * sizeof(char*))); |
94 | |
|
95 | 0 | int env_index = 0; |
96 | 0 | for (DictIter<Str*, Str*> it(environ); !it.Done(); it.Next()) { |
97 | 0 | Str* k = it.Key(); |
98 | 0 | Str* v = it.Value(); |
99 | |
|
100 | 0 | int joined_len = len(k) + len(v) + 1; |
101 | 0 | char* buf = static_cast<char*>(malloc(joined_len + 1)); |
102 | 0 | memcpy(buf, k->data_, len(k)); |
103 | 0 | buf[len(k)] = '='; |
104 | 0 | memcpy(buf + len(k) + 1, v->data_, len(v)); |
105 | 0 | buf[joined_len] = '\0'; |
106 | |
|
107 | 0 | envp[env_index++] = buf; |
108 | 0 | } |
109 | 0 | envp[n_env] = nullptr; |
110 | |
|
111 | 0 | int ret = ::execve(argv0->data_, _argv, envp); |
112 | 0 | if (ret == -1) { |
113 | 0 | throw Alloc<OSError>(errno); |
114 | 0 | } |
115 | | |
116 | | // ::execve() never returns on succcess |
117 | 0 | FAIL(kShouldNotGetHere); |
118 | 0 | } |
119 | | |
120 | 0 | void kill(int pid, int sig) { |
121 | 0 | if (::kill(pid, sig) != 0) { |
122 | 0 | throw Alloc<OSError>(errno); |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 2 | List<Str*>* listdir(Str* path) { |
127 | 2 | DIR* dirp = opendir(path->data()); |
128 | 2 | if (dirp == NULL) { |
129 | 1 | throw Alloc<OSError>(errno); |
130 | 1 | } |
131 | | |
132 | 1 | auto* ret = Alloc<List<Str*>>(); |
133 | 34 | while (true) { |
134 | 34 | errno = 0; |
135 | 34 | struct dirent* ep = readdir(dirp); |
136 | 34 | if (ep == NULL) { |
137 | 1 | if (errno != 0) { |
138 | 0 | closedir(dirp); |
139 | 0 | throw Alloc<OSError>(errno); |
140 | 0 | } |
141 | 1 | break; // no more files |
142 | 1 | } |
143 | | // Skip . and .. |
144 | 33 | int name_len = strlen(ep->d_name); |
145 | 33 | if (ep->d_name[0] == '.' && |
146 | 33 | (name_len == 1 || (ep->d_name[1] == '.' && name_len == 2))) { |
147 | 2 | continue; |
148 | 2 | } |
149 | 31 | ret->append(StrFromC(ep->d_name, name_len)); |
150 | 31 | } |
151 | | |
152 | 1 | closedir(dirp); |
153 | | |
154 | 1 | return ret; |
155 | 1 | } |
156 | | |
157 | | } // namespace posix |
158 | | |
159 | | namespace time_ { |
160 | | |
161 | 1 | void tzset() { |
162 | | // No error case: no return value |
163 | 1 | ::tzset(); |
164 | 1 | } |
165 | | |
166 | 1 | time_t time() { |
167 | 1 | time_t result = ::time(nullptr); |
168 | 1 | if (result < 0) { |
169 | 0 | throw Alloc<IOError>(errno); |
170 | 0 | } |
171 | 1 | return result; |
172 | 1 | } |
173 | | |
174 | | // NOTE(Jesse): time_t is specified to be an arithmetic type by C++. On most |
175 | | // systems it's a 64-bit integer. 64 bits is used because 32 will overflow in |
176 | | // 2038. Someone on a comittee somewhere thought of that when moving to |
177 | | // 64-bit architectures to prevent breaking ABI again; on 32-bit systems it's |
178 | | // usually 32 bits. Point being, using anything but the time_t typedef here |
179 | | // could (unlikely, but possible) produce weird behavior. |
180 | 0 | time_t localtime(time_t ts) { |
181 | | // localtime returns a pointer to a static buffer |
182 | 0 | tm* loc_time = ::localtime(&ts); |
183 | |
|
184 | 0 | time_t result = mktime(loc_time); |
185 | 0 | if (result < 0) { |
186 | 0 | throw Alloc<IOError>(errno); |
187 | 0 | } |
188 | 0 | return result; |
189 | 0 | } |
190 | | |
191 | 3 | Str* strftime(Str* s, time_t ts) { |
192 | 3 | tm* loc_time = ::localtime(&ts); |
193 | | |
194 | 3 | const int max_len = 1024; |
195 | 3 | Str* result = OverAllocatedStr(max_len); |
196 | 3 | int n = strftime(result->data(), max_len, s->data_, loc_time); |
197 | 3 | if (n == 0) { |
198 | | // bash silently truncates on large format string like |
199 | | // printf '%(%Y)T' |
200 | | // Oil doesn't mask errors |
201 | | // Leaving out location info points to 'printf' builtin |
202 | |
|
203 | 0 | e_die(StrFromC("strftime() result exceeds 1024 bytes")); |
204 | 0 | } |
205 | 3 | result->MaybeShrink(n); |
206 | 3 | return result; |
207 | 3 | } |
208 | | |
209 | 1 | void sleep(int seconds) { |
210 | 1 | ::sleep(seconds); |
211 | 1 | } |
212 | | |
213 | | } // namespace time_ |