Fix a bunch of issues identified by Coverity scan
[oweals/dinit.git] / src / includes / dinit-util.h
1 #ifndef DINIT_UTIL_H_INCLUDED
2 #define DINIT_UTIL_H_INCLUDED 1
3
4 #include <string>
5
6 #include <cstddef>
7 #include <cerrno>
8
9 #include <sys/types.h>
10 #include <unistd.h>
11
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)
15 {
16     char * cbuf = static_cast<char *>(buf);
17     ssize_t r = 0;
18     while ((size_t)r < n) {
19         ssize_t res = read(fd, cbuf + r, n - r);
20         if (res == 0) {
21             return r;
22         }
23         if (res < 0) {
24             if (errno == EINTR) {
25                 continue;
26             }
27
28             // If any other error, and we have successfully read some, return it:
29             if (r == 0) {
30                 return -1;
31             }
32             else {
33                 return r;
34             }
35         }
36         r += res;
37     }
38     return n;
39 }
40
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)
44 {
45     if (*p2 == 0) return p1;
46     if (p1.empty()) return std::string(p2);
47
48     if (p2[0] == '/') return p2;
49
50     if (*(p1.rbegin()) == '/') return p1 + p2;
51     return p1 + '/' + p2;
52 }
53
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)
57 {
58     auto spos = p.rfind('/');
59     if (spos == std::string::npos) {
60         return std::string {};
61     }
62
63     return p.substr(0, spos + 1);
64 }
65
66 // Find the base name of a path (the name after the final '/').
67 inline const char * base_name(const char *path)
68 {
69     const char * basen = path;
70     const char * s = path;
71     while (*s != 0) {
72         if (*s == '/') basen = s + 1;
73         s++;
74     }
75     return basen;
76 }
77
78 #endif