cpp

Coverage Report

Created: 2022-07-20 01:16

/home/uke/oil/cpp/leaky_stdlib.h
Line
Count
Source (jump to first uncovered line)
1
// leaky_stdlib.h: Replacement for native/posixmodule.c
2
3
#ifndef LEAKY_STDLIB_H
4
#define LEAKY_STDLIB_H
5
6
#include <unistd.h>
7
8
#include "mycpp/mylib_leaky.h"
9
10
namespace fcntl_ {
11
12
// for F_GETFD
13
int fcntl(int fd, int cmd);
14
int fcntl(int fd, int cmd, int arg);
15
16
}  // namespace fcntl_
17
18
namespace posix {
19
20
int umask(int mask);
21
22
0
inline int access(Str* pathname, int mode) {
23
0
  // Are there any errno I care about?
24
0
  mylib::Str0 pathname0(pathname);
25
0
  return ::access(pathname0.Get(), mode) == 0;
26
0
}
27
28
1
inline Str* getcwd() {
29
1
  char* buf = static_cast<char*>(malloc(PATH_MAX + 1));
30
31
1
  char* result = ::getcwd(buf, PATH_MAX + 1);
32
1
  if (result == nullptr) {
33
    // TODO: print errno, e.g. ENAMETOOLONG
34
0
    throw new RuntimeError(new Str("Couldn't get working directory"));
35
0
  }
36
37
1
  return new Str(buf);
38
1
}
39
40
0
inline int getegid() {
41
0
  return ::getegid();
42
0
}
43
44
0
inline int geteuid() {
45
0
  return ::geteuid();
46
0
}
47
48
0
inline int getpid() {
49
0
  return ::getpid();
50
0
}
51
52
0
inline int getppid() {
53
0
  return ::getppid();
54
0
}
55
56
0
inline int getuid() {
57
0
  return ::getuid();
58
0
}
59
60
0
inline bool isatty(int fd) {
61
0
  return ::isatty(fd);
62
0
}
63
64
1
inline Str* strerror(int err_num) {
65
1
  return new Str(::strerror(err_num));
66
1
}
67
68
0
inline Tuple2<int, int> pipe() {
69
0
  int fd[2];
70
0
  if (::pipe(fd) < 0) {
71
0
    // TODO: handle errno
72
0
    assert(0);
73
0
  }
74
0
  return Tuple2<int, int>(fd[0], fd[1]);
75
0
}
76
77
0
inline int close(int fd) {
78
0
  // TODO: handle errno.  Although I'm not sure if it happens!
79
0
  return ::close(fd);
80
0
}
81
82
void putenv(Str* name, Str* value);
83
84
0
inline int fork() {
85
0
  return ::fork();
86
0
}
87
88
0
inline void _exit(int status) {
89
0
  exit(status);
90
0
}
91
92
0
inline void write(int fd, Str* s) {
93
0
  ::write(fd, s->data_, s->len_);
94
0
}
95
96
// Can we use fcntl instead?
97
void dup2(int oldfd, int newfd);
98
99
int open(Str* path, int flags, int perms);
100
101
mylib::LineReader* fdopen(int fd, Str* c_mode);
102
103
void execve(Str* argv0, List<Str*>* argv, Dict<Str*, Str*>* environ);
104
105
}  // namespace posix
106
107
namespace time_ {
108
109
void tzset();
110
time_t time();
111
time_t localtime(time_t ts);
112
Str* strftime(Str* s, time_t ts);
113
114
}  // namespace time_
115
116
#endif  // LEAKY_STDLIB_H