cpp

Coverage Report

Created: 2022-07-20 01:16

/home/uke/oil/cpp/leaky_libc.h
Line
Count
Source (jump to first uncovered line)
1
// leaky_libc.h: Replacement for native/libc.c
2
3
#ifndef LIBC_H
4
#define LIBC_H
5
6
#include <fnmatch.h>
7
#include <limits.h>
8
#include <stdlib.h>
9
#include <unistd.h>  // gethostname()
10
11
#include "mycpp/mylib_leaky.h"
12
13
namespace libc {
14
15
0
inline Str* realpath(Str* path) {
16
0
  assert(path->IsNulTerminated());
17
0
  char* rp = ::realpath(path->data_, 0);
18
0
  return new Str(rp);
19
0
}
20
21
1
inline Str* gethostname() {
22
1
  char* buf = static_cast<char*>(malloc(HOST_NAME_MAX + 1));
23
1
  int result = ::gethostname(buf, PATH_MAX);
24
1
  if (result != 0) {
25
    // TODO: print errno, e.g. ENAMETOOLONG (glibc)
26
0
    throw new RuntimeError(new Str("Couldn't get working directory"));
27
0
  }
28
1
  return new Str(buf);
29
1
}
30
31
4
inline bool fnmatch(Str* pat, Str* str) {
32
  // copy into NUL-terminated buffers
33
4
  mylib::Str0 pat0(pat);
34
4
  mylib::Str0 str0(str);
35
4
  int flags = FNM_EXTMATCH;
36
4
  bool result = ::fnmatch(pat0.Get(), str0.Get(), flags) == 0;
37
4
  return result;
38
4
}
39
40
List<Str*>* glob(Str* pat);
41
42
List<Str*>* regex_match(Str* pattern, Str* str);
43
44
Tuple2<int, int>* regex_first_group_match(Str* pattern, Str* str, int pos);
45
46
0
inline void print_time(double real_time, double user_time, double system_time) {
47
0
  // TODO(Jesse): How to we report CPU load? .. Do we need to?
48
0
  printf("%1.2fs user %1.2fs system BUG cpu %1.3f total", user_time,
49
0
         system_time, real_time);  // 0.05s user 0.03s system 2% cpu 3.186 total
50
0
}
51
52
}  // namespace libc
53
54
#endif  // LIBC_H