cpp

Coverage Report

Created: 2022-09-21 22:22

/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/runtime.h"
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
int umask(int mask) {
45
  // note: assuming mode_t fits in an int
46
0
  return ::umask(mask);
47
0
}
48
49
0
int open(Str* path, int flags, int perms) {
50
0
  return ::open(path->data_, flags, perms);
51
0
}
52
53
0
void dup2(int oldfd, int newfd) {
54
0
  if (::dup2(oldfd, newfd) < 0) {
55
0
    throw Alloc<OSError>(errno);
56
0
  }
57
0
}
58
1
void putenv(Str* name, Str* value) {
59
1
  int overwrite = 1;
60
1
  int ret = ::setenv(name->data_, value->data_, overwrite);
61
1
  if (ret < 0) {
62
0
    throw Alloc<IOError>(errno);
63
0
  }
64
1
}
65
66
0
mylib::LineReader* fdopen(int fd, Str* c_mode) {
67
0
  FILE* f = ::fdopen(fd, c_mode->data_);
68
69
  // TODO: raise exception
70
0
  assert(f);
71
72
0
  return Alloc<mylib::CFileLineReader>(f);
73
0
}
74
75
0
void execve(Str* argv0, List<Str*>* argv, Dict<Str*, Str*>* environ) {
76
0
  int n_args = len(argv);
77
  // never deallocated
78
0
  char** _argv = static_cast<char**>(malloc((n_args + 1) * sizeof(char*)));
79
80
  // Annoying const_cast
81
  // https://stackoverflow.com/questions/190184/execv-and-const-ness
82
0
  for (int i = 0; i < n_args; ++i) {
83
0
    _argv[i] = const_cast<char*>(argv->index_(i)->data_);
84
0
  }
85
0
  _argv[n_args] = nullptr;
86
87
  // Convert environ into an array of pointers to strings of the form: "k=v".
88
0
  int n_env = len(environ);
89
0
  char** envp = static_cast<char**>(malloc((n_env + 1) * sizeof(char*)));
90
91
0
  int EnvIndex = 0;
92
0
  for (DictIter<Str*, Str*> it(environ); !it.Done(); it.Next()) {
93
0
    Str* k = it.Key();
94
0
    Str* v = it.Value();
95
96
0
    int joined_len = len(k) + len(v) + 1;
97
0
    char* buf = static_cast<char*>(malloc(joined_len + 1));
98
0
    memcpy(buf, k->data_, len(k));
99
0
    buf[len(k)] = '=';
100
0
    memcpy(buf + len(k) + 1, v->data_, len(v));
101
0
    buf[joined_len] = '\0';
102
103
0
    envp[EnvIndex++] = buf;
104
0
  }
105
0
  envp[n_env] = nullptr;
106
107
0
  int ret = ::execve(argv0->data_, _argv, envp);
108
0
  if (ret == -1) {
109
0
    throw Alloc<OSError>(errno);
110
0
  }
111
112
  // NOTE(Jesse): ::execve() is specified to never return on success.  If we
113
  // hit this assertion, it returned successfully (or at least something other
114
  // than -1) but should have overwritten our address space with the invoked
115
  // process'
116
0
  InvalidCodePath();
117
0
}
118
119
}  // namespace posix
120
121
namespace time_ {
122
123
0
void tzset() {
124
0
  ::tzset();
125
0
}
126
127
1
time_t time() {
128
1
  return ::time(nullptr);
129
1
}
130
131
// NOTE(Jesse): time_t is specified to be an arithmetic type by C++. On most
132
// systems it's a 64-bit integer.  64 bits is used because 32 will overflow in
133
// 2038.  Someone on a comittee somewhere thought of that when moving to 64-bit
134
// architectures to prevent breaking ABI again; on 32-bit systems it's usually
135
// 32 bits.  Point being, using anything but the time_t typedef here could
136
// (unlikely, but possible) produce weird behavior.
137
0
time_t localtime(time_t ts) {
138
0
  tm* loc_time = ::localtime(&ts);
139
0
  time_t result = mktime(loc_time);
140
0
  return result;
141
0
}
142
143
0
Str* strftime(Str* s, time_t ts) {
144
  // TODO: may not work with leaky_containers.h
145
  // https://github.com/oilshell/oil/issues/1221
146
0
  tm* loc_time = ::localtime(&ts);
147
148
0
  const int max_len = 1024;
149
0
  Str* result = OverAllocatedStr(max_len);
150
0
  int n = strftime(result->data(), max_len, s->data_, loc_time);
151
0
  if (n == 0) {
152
    // bash silently truncates on large format string like
153
    //   printf '%(%Y)T'
154
    // Oil doesn't mask errors
155
    // No error location info, but leaving it out points reliably to 'printf'
156
0
    e_die(StrFromC("strftime() result exceeds 1024 bytes"));
157
0
  }
158
0
  result->SetObjLenFromStrLen(n);
159
0
  return result;
160
0
}
161
162
}  // namespace time_