1 #ifndef DINIT_UTIL_H_INCLUDED
2 #define DINIT_UTIL_H_INCLUDED 1
12 // Complete read - read the specified size until end-of-file or error; continue read if
13 // interrupted by signal.
14 inline ssize_t complete_read(int fd, void * buf, size_t n)
16 char * cbuf = static_cast<char *>(buf);
18 while ((size_t)r < n) {
19 ssize_t res = read(fd, cbuf + r, n - r);
28 // If any other error, and we have successfully read some, return it:
41 // Combine two paths to produce a path. If the second path is absolute, it is returned unmodified;
42 // otherwise, it is appended to the first path (with a slash separator added if needed).
43 inline std::string combine_paths(const std::string &p1, const char * p2)
45 if (*p2 == 0) return p1;
46 if (p1.empty()) return std::string(p2);
48 if (p2[0] == '/') return p2;
50 if (*(p1.rbegin()) == '/') return p1 + p2;
54 // Find the parent path of a given path, which should refer to a named file or directory (not . or ..).
55 // If the path contains no directory, returns the empty string.
56 inline std::string parent_path(const std::string &p)
58 auto spos = p.rfind('/');
59 if (spos == std::string::npos) {
60 return std::string {};
63 return p.substr(0, spos + 1);
66 // Find the base name of a path (the name after the final '/').
67 inline const char * base_name(const char *path)
69 const char * basen = path;
70 const char * s = path;
72 if (*s == '/') basen = s + 1;