cpp

Coverage Report

Created: 2022-11-10 11:34

/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 <errno.h>
7
#include <sys/types.h>  // mode_t
8
#include <unistd.h>
9
10
#include "mycpp/runtime.h"
11
12
namespace fcntl_ {
13
14
// for F_GETFD
15
int fcntl(int fd, int cmd);
16
int fcntl(int fd, int cmd, int arg);
17
18
}  // namespace fcntl_
19
20
namespace posix {
21
22
mode_t umask(mode_t mask);
23
24
0
inline int access(Str* pathname, int mode) {
25
0
  return ::access(pathname->data_, mode) == 0;
26
0
}
27
28
2
inline Str* getcwd() {
29
2
  Str* result = OverAllocatedStr(PATH_MAX);
30
2
  char* p = ::getcwd(result->data_, PATH_MAX);
31
2
  if (p == nullptr) {
32
0
    throw Alloc<OSError>(errno);
33
0
  }
34
  // Important: set the length of the string!
35
2
  result->SetObjLenFromC();
36
2
  return result;
37
2
}
38
39
0
inline int getegid() {
40
0
  return ::getegid();
41
0
}
42
43
0
inline int geteuid() {
44
0
  return ::geteuid();
45
0
}
46
47
0
inline int getpid() {
48
0
  return ::getpid();
49
0
}
50
51
0
inline int getppid() {
52
0
  return ::getppid();
53
0
}
54
55
0
inline int getuid() {
56
0
  return ::getuid();
57
0
}
58
59
0
inline bool isatty(int fd) {
60
0
  return ::isatty(fd);
61
0
}
62
63
1
inline Str* strerror(int err_num) {
64
1
  return StrFromC(::strerror(err_num));
65
1
}
66
67
0
inline Tuple2<int, int> pipe() {
68
0
  int fd[2];
69
0
  if (::pipe(fd) < 0) {
70
0
    // TODO: handle errno
71
0
    assert(0);
72
0
  }
73
0
  return Tuple2<int, int>(fd[0], fd[1]);
74
0
}
75
76
0
inline int close(int fd) {
77
0
  // TODO: handle errno.  Although I'm not sure if it happens!
78
0
  return ::close(fd);
79
0
}
80
81
void putenv(Str* name, Str* value);
82
83
0
inline int fork() {
84
0
  return ::fork();
85
0
}
86
87
0
inline void _exit(int status) {
88
0
  ::_exit(status);
89
0
}
90
91
0
inline void write(int fd, Str* s) {
92
0
  ::write(fd, s->data_, len(s));
93
0
}
94
95
// Can we use fcntl instead?
96
void dup2(int oldfd, int newfd);
97
98
int open(Str* path, int flags, int perms);
99
100
mylib::LineReader* fdopen(int fd, Str* c_mode);
101
102
void execve(Str* argv0, List<Str*>* argv, Dict<Str*, Str*>* environ);
103
104
void kill(int pid, int sig);
105
106
}  // namespace posix
107
108
namespace time_ {
109
110
void tzset();
111
time_t time();
112
time_t localtime(time_t ts);
113
Str* strftime(Str* s, time_t ts);
114
115
}  // namespace time_
116
117
#endif  // LEAKY_STDLIB_H