cpp

Coverage Report

Created: 2022-09-21 22:22

/home/andy/git/oilshell/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/runtime.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
  return ::access(pathname->data_, mode) == 0;
24
0
}
25
26
1
inline Str* getcwd() {
27
1
  char* buf = static_cast<char*>(malloc(PATH_MAX + 1));
28
29
1
  char* result = ::getcwd(buf, PATH_MAX + 1);
30
1
  if (result == nullptr) {
31
    // TODO: print errno, e.g. ENAMETOOLONG
32
0
    throw Alloc<RuntimeError>(StrFromC("Couldn't get working directory"));
33
0
  }
34
35
1
  return CopyBufferIntoNewStr(buf);
36
1
}
37
38
0
inline int getegid() {
39
0
  return ::getegid();
40
0
}
41
42
0
inline int geteuid() {
43
0
  return ::geteuid();
44
0
}
45
46
0
inline int getpid() {
47
0
  return ::getpid();
48
0
}
49
50
0
inline int getppid() {
51
0
  return ::getppid();
52
0
}
53
54
0
inline int getuid() {
55
0
  return ::getuid();
56
0
}
57
58
0
inline bool isatty(int fd) {
59
0
  return ::isatty(fd);
60
0
}
61
62
1
inline Str* strerror(int err_num) {
63
1
  return CopyBufferIntoNewStr(::strerror(err_num));
64
1
}
65
66
0
inline Tuple2<int, int> pipe() {
67
0
  int fd[2];
68
0
  if (::pipe(fd) < 0) {
69
0
    // TODO: handle errno
70
0
    assert(0);
71
0
  }
72
0
  return Tuple2<int, int>(fd[0], fd[1]);
73
0
}
74
75
0
inline int close(int fd) {
76
0
  // TODO: handle errno.  Although I'm not sure if it happens!
77
0
  return ::close(fd);
78
0
}
79
80
void putenv(Str* name, Str* value);
81
82
0
inline int fork() {
83
0
  return ::fork();
84
0
}
85
86
0
inline void _exit(int status) {
87
0
  ::_exit(status);
88
0
}
89
90
0
inline void write(int fd, Str* s) {
91
0
  ::write(fd, s->data_, len(s));
92
0
}
93
94
// Can we use fcntl instead?
95
void dup2(int oldfd, int newfd);
96
97
int open(Str* path, int flags, int perms);
98
99
mylib::LineReader* fdopen(int fd, Str* c_mode);
100
101
void execve(Str* argv0, List<Str*>* argv, Dict<Str*, Str*>* environ);
102
103
}  // namespace posix
104
105
namespace time_ {
106
107
void tzset();
108
time_t time();
109
time_t localtime(time_t ts);
110
Str* strftime(Str* s, time_t ts);
111
112
}  // namespace time_
113
114
#endif  // LEAKY_STDLIB_H