/home/andy/git/oilshell/oil/cpp/leaky_stdlib.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // leaky_stdlib.cc: Replacement for standard library modules |
2 | | // and native/posixmodule.c |
3 | | |
4 | | // clang-format off |
5 | | #include "mycpp/myerror.h" // for OSError; must come first |
6 | | // clang-format on |
7 | | |
8 | | #include "leaky_stdlib.h" |
9 | | |
10 | | #include <errno.h> |
11 | | #include <fcntl.h> // open |
12 | | #include <sys/stat.h> // umask |
13 | | #include <sys/types.h> // umask |
14 | | #include <sys/wait.h> // WUNTRACED |
15 | | #include <time.h> |
16 | | #include <unistd.h> |
17 | | |
18 | | #include "cpp/leaky_core_error.h" |
19 | | #include "cpp/leaky_core_pyerror.h" |
20 | | #include "mycpp/mylib_old.h" |
21 | | using mylib::OverAllocatedStr; |
22 | | using mylib::StrFromC; |
23 | | |
24 | | namespace fcntl_ { |
25 | | |
26 | 0 | int fcntl(int fd, int cmd) { |
27 | 0 | int result = ::fcntl(fd, cmd); |
28 | 0 | if (result < 0) { |
29 | 0 | throw new IOError(errno); |
30 | 0 | } |
31 | 0 | return result; |
32 | 0 | } |
33 | | |
34 | 0 | int fcntl(int fd, int cmd, int arg) { |
35 | 0 | int result = ::fcntl(fd, cmd, arg); |
36 | 0 | if (result < 0) { |
37 | 0 | throw new IOError(errno); |
38 | 0 | } |
39 | 0 | return result; |
40 | 0 | } |
41 | | |
42 | | } // namespace fcntl_ |
43 | | |
44 | | namespace posix { |
45 | | |
46 | 0 | int umask(int mask) { |
47 | | // note: assuming mode_t fits in an int |
48 | 0 | return ::umask(mask); |
49 | 0 | } |
50 | | |
51 | 0 | int open(Str* path, int flags, int perms) { |
52 | 0 | return ::open(path->data_, flags, perms); |
53 | 0 | } |
54 | | |
55 | 0 | void dup2(int oldfd, int newfd) { |
56 | 0 | if (::dup2(oldfd, newfd) < 0) { |
57 | 0 | throw new OSError(errno); |
58 | 0 | } |
59 | 0 | } |
60 | 1 | void putenv(Str* name, Str* value) { |
61 | 1 | assert(name->IsNulTerminated()); |
62 | 0 | assert(value->IsNulTerminated()); |
63 | 0 | int overwrite = 1; |
64 | 1 | int ret = ::setenv(name->data_, value->data_, overwrite); |
65 | 1 | if (ret < 0) { |
66 | 0 | throw new 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 new 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 | 0 | int i = 0; |
95 | 0 | for (const auto& kv : environ->items_) { |
96 | 0 | Str* k = kv.first; |
97 | 0 | Str* v = kv.second; |
98 | 0 | int joined_len = k->len_ + v->len_ + 1; |
99 | 0 | char* buf = static_cast<char*>(malloc(joined_len + 1)); |
100 | 0 | memcpy(buf, k->data_, k->len_); |
101 | 0 | buf[k->len_] = '='; |
102 | 0 | memcpy(buf + k->len_ + 1, v->data_, v->len_); |
103 | 0 | buf[joined_len] = '\0'; |
104 | 0 | envp[i++] = buf; |
105 | 0 | } |
106 | 0 | envp[n_env] = nullptr; |
107 | |
|
108 | 0 | int ret = ::execve(argv0->data_, _argv, envp); |
109 | 0 | if (ret == -1) { |
110 | 0 | throw new OSError(errno); |
111 | 0 | } |
112 | | |
113 | | // NOTE(Jesse): ::execve() is specified to never return on success. If we |
114 | | // hit this assertion, it returned successfully (or at least something other |
115 | | // than -1) but should have overwritten our address space with the invoked |
116 | | // process' |
117 | 0 | InvalidCodePath(); |
118 | 0 | } |
119 | | |
120 | | } // namespace posix |
121 | | |
122 | | namespace time_ { |
123 | | |
124 | 0 | void tzset() { |
125 | 0 | ::tzset(); |
126 | 0 | } |
127 | | |
128 | 1 | time_t time() { |
129 | 1 | return ::time(nullptr); |
130 | 1 | } |
131 | | |
132 | | // NOTE(Jesse): time_t is specified to be an arithmetic type by C++. On most |
133 | | // systems it's a 64-bit integer. 64 bits is used because 32 will overflow in |
134 | | // 2038. Someone on a comittee somewhere thought of that when moving to 64-bit |
135 | | // architectures to prevent breaking ABI again; on 32-bit systems it's usually |
136 | | // 32 bits. Point being, using anything but the time_t typedef here could |
137 | | // (unlikely, but possible) produce weird behavior. |
138 | 0 | time_t localtime(time_t ts) { |
139 | 0 | tm* loc_time = ::localtime(&ts); |
140 | 0 | time_t result = mktime(loc_time); |
141 | 0 | return result; |
142 | 0 | } |
143 | | |
144 | 0 | Str* strftime(Str* s, time_t ts) { |
145 | | // TODO: may not work with mylib_old.h |
146 | | // https://github.com/oilshell/oil/issues/1221 |
147 | 0 | assert(s->IsNulTerminated()); |
148 | | |
149 | 0 | tm* loc_time = ::localtime(&ts); |
150 | |
|
151 | 0 | const int max_len = 1024; |
152 | 0 | Str* result = OverAllocatedStr(max_len); |
153 | 0 | int n = strftime(result->data(), max_len, s->data_, loc_time); |
154 | 0 | if (n == 0) { |
155 | | // bash silently truncates on large format string like |
156 | | // printf '%(%Y)T' |
157 | | // Oil doesn't mask errors |
158 | | // No error location info, but leaving it out points reliably to 'printf' |
159 | 0 | e_die(StrFromC("strftime() result exceeds 1024 bytes")); |
160 | 0 | } |
161 | 0 | result->SetObjLenFromStrLen(n); |
162 | 0 | return result; |
163 | 0 | } |
164 | | |
165 | | } // namespace time_ |