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