YARP
Yet Another Robot Platform
Os.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <yarp/os/Os.h>
13 
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cstring>
17 
18 #ifdef YARP_HAS_ACE
19 # include <ace/ACE.h>
20 // In one the ACE headers there is a definition of "main" for WIN32
21 # ifdef main
22 # undef main
23 # endif
24 #endif
25 
26 #if defined(__APPLE__)
28 #endif
29 
30 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4.0
31 const char* yarp::os::getenv(const char* var)
32 {
33  return std::getenv(var);
34 }
35 #endif
36 
37 int yarp::os::mkdir(const char* p)
38 {
39  return yarp::os::impl::mkdir(p, 0755);
40 }
41 
42 int yarp::os::mkdir_p(const char* p, int ignoreLevels)
43 {
44  std::string fileName(p);
45 
46  size_t index = fileName.rfind('/');
47  if (index == std::string::npos) {
48 #if defined(_WIN32)
49  index = fileName.rfind('\\');
50  if (index == std::string::npos) {
51  return 1;
52  }
53 #else
54  return 1;
55 #endif
56  }
57  std::string base = fileName.substr(0, index);
58  if (yarp::os::stat(const_cast<char*>(base.c_str())) < 0) {
59  int result = yarp::os::mkdir_p(base.c_str(), ignoreLevels - 1);
60  if (result != 0) {
61  return 1;
62  }
63  }
64  if (ignoreLevels <= 0) {
65  if (yarp::os::stat(fileName.c_str()) < 0) {
66  if (yarp::os::mkdir(fileName.c_str()) >= 0) {
67  return 0;
68  }
69  return 1;
70  }
71  }
72  return 0;
73 }
74 
75 int yarp::os::rmdir(const char* p)
76 {
77  return yarp::os::impl::rmdir(p);
78 }
79 
80 int yarp::os::rename(const char* oldname, const char* newname)
81 {
82  return std::rename(oldname, newname);
83 }
84 
85 int yarp::os::stat(const char* path)
86 {
88  return yarp::os::impl::stat(path, &dummy);
89 }
90 
92 {
93  pid_t pid = yarp::os::impl::getpid();
94  return pid;
95 }
96 
97 void yarp::os::gethostname(char* hostname, size_t size)
98 {
99  yarp::os::impl::gethostname(hostname, size);
100  if (std::strlen(hostname) == 0) {
101  std::strncpy(hostname, "no_hostname", size);
102  }
103 }
104 
106 {
107  char hostname[HOST_NAME_MAX];
108  yarp::os::gethostname(hostname, HOST_NAME_MAX);
109  return {hostname};
110 }
111 
112 char* yarp::os::getcwd(char* buf, size_t size)
113 {
114  return yarp::os::impl::getcwd(buf, size);
115 }
116 
118 {
119 #if defined(__APPLE__)
120  static void* handle = 0;
121  if (!enabled && !handle) {
122  handle = disableAppNap();
123  } else {
124  restoreAppNap(handle);
125  }
126 
127 #endif
128 }
129 
130 
131 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
132 
133 void yarp::os::setprogname(const char* progname)
134 {
135 # ifdef YARP_HAS_ACE
136  ACE_OS::setprogname(ACE::basename(progname));
137 # else
138  // not available
139  YARP_UNUSED(progname);
140 # endif
141 }
142 
143 void yarp::os::getprogname(char* progname, size_t size)
144 {
145 # ifdef YARP_HAS_ACE
146  const char* tmp = ACE_OS::getprogname();
147  if (std::strlen(tmp) == 0) {
148  std::strncpy(progname, "no_progname", size);
149  } else {
150  std::strncpy(progname, tmp, size);
151  }
152 # else
153  // not available
154  *progname = '\0';
155  YARP_UNUSED(size);
156 # endif
157 }
158 
160 {
161 # if defined(YARP_HAS_ACE)
162  pid_t pid = ACE_OS::fork();
163 # elif defined(__unix__)
164  pid_t pid = ::fork();
165 # else
166  YARP_COMPILER_ERROR(Cannot implement fork on this platform)
167 # endif
168  return pid;
169 }
170 
171 #endif // YARP_NO_DEPRECATED
void restoreAppNap(void *activityInfo)
void * disableAppNap()
struct ::stat YARP_stat
int mkdir(const char *p)
Portable wrapper for the mkdir() function.
Definition: Os.cpp:37
int rename(const char *oldname, const char *newname)
Portable wrapper for the rename() function.
Definition: Os.cpp:80
void setEnergySavingModeState(bool enabled)
Toggle the OS energy saving feature.
Definition: Os.cpp:117
char * getcwd(char *buf, size_t size)
Portable wrapper for the getcwd() function.
Definition: Os.cpp:112
int stat(const char *path)
Portable wrapper for the stat() function.
Definition: Os.cpp:85
int getpid()
Portable wrapper for the getppid() function.
Definition: Os.cpp:91
const char * getenv(const char *var)
Portable wrapper for the getenv() function.
Definition: Os.cpp:31
int fork()
Portable wrapper for the fork() function.
Definition: Os.cpp:159
int rmdir(const char *p)
Portable wrapper for the rmdir() function.
Definition: Os.cpp:75
void setprogname(const char *progname)
Portable wrapper for the setprogname() function.
Definition: Os.cpp:133
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
Definition: Os.cpp:97
void getprogname(char *progname, size_t size)
Portable wrapper for the getprogname() function.
Definition: Os.cpp:143
int mkdir_p(const char *p, int ignoreLevels=0)
Create a directory and all parent directories needed.
Definition: Os.cpp:42
#define YARP_COMPILER_ERROR(x)
Generate an error at build time on supported compilers.
Definition: system.h:110
#define YARP_UNUSED(var)
Definition: api.h:162