1 #ifndef DINIT_UTIL_H_INCLUDED
2 #define DINIT_UTIL_H_INCLUDED 1
12 #include "baseproc-sys.h"
14 // Complete read - read the specified size until end-of-file or error; continue read if
15 // interrupted by signal.
16 inline ssize_t complete_read(int fd, void * buf, size_t n)
18 char * cbuf = static_cast<char *>(buf);
20 while ((size_t)r < n) {
21 ssize_t res = bp_sys::read(fd, cbuf + r, n - r);
30 // If any other error, and we have successfully read some, return it:
43 // Combine two paths to produce a path. If the second path is absolute, it is returned unmodified;
44 // otherwise, it is appended to the first path (with a slash separator added if needed).
45 inline std::string combine_paths(const std::string &p1, const char * p2)
47 if (*p2 == 0) return p1;
48 if (p1.empty()) return std::string(p2);
50 if (p2[0] == '/') return p2;
52 if (*(p1.rbegin()) == '/') return p1 + p2;
56 // Find the parent path of a given path, which should refer to a named file or directory (not . or ..).
57 // If the path contains no directory, returns the empty string.
58 inline std::string parent_path(const std::string &p)
60 auto spos = p.rfind('/');
61 if (spos == std::string::npos) {
62 return std::string {};
65 return p.substr(0, spos + 1);
68 // Find the base name of a path (the name after the final '/').
69 inline const char * base_name(const char *path)
71 const char * basen = path;
72 const char * s = path;
74 if (*s == '/') basen = s + 1;
80 // Check if one string starts with another
81 inline bool starts_with(std::string s, const char *prefix)
83 const char * sp = s.c_str();
84 while (*sp != 0 && *prefix != 0) {
85 if (*sp != *prefix) return false;