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