Apache Mesos
getcwd.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_POSIX_GETCWD_HPP__
14 #define __STOUT_OS_POSIX_GETCWD_HPP__
15 
16 #include <string>
17 
18 #include <stout/try.hpp>
19 
20 
21 namespace os {
22 
23 inline std::string getcwd()
24 {
25  size_t size = 100;
26 
27  while (true) {
28  char* temp = new char[size];
29  if (::getcwd(temp, size) == temp) {
30  std::string result(temp);
31  delete[] temp;
32  return result;
33  } else {
34  if (errno != ERANGE) {
35  delete[] temp;
36  return std::string();
37  }
38  size *= 2;
39  delete[] temp;
40  }
41  }
42 
43  return std::string();
44 }
45 
46 } // namespace os {
47 
48 
49 #endif // __STOUT_OS_POSIX_GETCWD_HPP__
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
std::string getcwd()
Definition: getcwd.hpp:23
Definition: posix_signalhandler.hpp:23
std::string temp()
Definition: temp.hpp:27