|
@@ -169,6 +169,17 @@ public:
|
|
|
--iter->ref;
|
|
|
}
|
|
|
|
|
|
+ constexpr int _next_fd(void) const
|
|
|
+ {
|
|
|
+ int fd = 0;
|
|
|
+
|
|
|
+ for (auto iter = arr.cbegin(); iter != arr.cend(); ++iter)
|
|
|
+ if (iter->key == fd)
|
|
|
+ ++fd;
|
|
|
+
|
|
|
+ return fd;
|
|
|
+ }
|
|
|
+
|
|
|
public:
|
|
|
constexpr filearr(const filearr&) = delete;
|
|
|
constexpr filearr& operator=(const filearr&) = delete;
|
|
@@ -179,18 +190,27 @@ public:
|
|
|
{
|
|
|
}
|
|
|
|
|
|
- constexpr int _next_fd(void) const
|
|
|
+ constexpr int dup(int old_fd)
|
|
|
{
|
|
|
- int fd = 0;
|
|
|
+ return dup2(old_fd, _next_fd());
|
|
|
+ }
|
|
|
|
|
|
- for (auto iter = arr.cbegin(); iter != arr.cend(); ++iter)
|
|
|
- if (iter->key == fd)
|
|
|
- ++fd;
|
|
|
+ // TODO: the third parameter should be int flags
|
|
|
+ // determining whether the fd should be closed
|
|
|
+ // after exec() (FD_CLOEXEC)
|
|
|
+ constexpr int dup2(int old_fd, int new_fd)
|
|
|
+ {
|
|
|
+ close(new_fd);
|
|
|
|
|
|
- return fd;
|
|
|
+ auto iter = arr.find(old_fd);
|
|
|
+ if (!iter)
|
|
|
+ return -EBADF;
|
|
|
+
|
|
|
+ this->arr.insert(types::make_pair(new_fd, iter->value));
|
|
|
+ return new_fd;
|
|
|
}
|
|
|
|
|
|
- constexpr void dup(const filearr& orig)
|
|
|
+ constexpr void dup_all(const filearr& orig)
|
|
|
{
|
|
|
for (auto iter : orig.arr) {
|
|
|
this->arr.insert(types::make_pair(iter.key, iter.value));
|