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  bool ok = yarp::os::impl::NameConfig::createPath(p, ignoreLevels);
46  return ok ? 0 : 1;
47 }
48 
49 int yarp::os::rmdir(const char *p)
50 {
51  return yarp::os::impl::rmdir(p);
52 }
53 
54 int yarp::os::rename(const char *oldname, const char *newname)
55 {
56  return std::rename(oldname, newname);
57 }
58 
59 int yarp::os::stat(const char *path)
60 {
62  return yarp::os::impl::stat(path, &dummy);
63 }
64 
66 {
67  pid_t pid = yarp::os::impl::getpid();
68  return pid;
69 }
70 
71 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
72 void yarp::os::setprogname(const char *progname)
73 {
74 #ifdef YARP_HAS_ACE
75  ACE_OS::setprogname(ACE::basename(progname));
76 #else
77  // not available
78  YARP_UNUSED(progname);
79 #endif
80 }
81 
82 
83 void yarp::os::getprogname(char* progname, size_t size)
84 {
85 #ifdef YARP_HAS_ACE
86  const char* tmp = ACE_OS::getprogname();
87  if (std::strlen(tmp)==0) {
88  std::strncpy(progname, "no_progname", size);
89  } else {
90  std::strncpy(progname, tmp, size);
91  }
92 #else
93  // not available
94  *progname = '\0';
95  YARP_UNUSED(size);
96 #endif
97 }
98 #endif // YARP_NO_DEPRECATED
99 
100 
101 void yarp::os::gethostname(char* hostname, size_t size)
102 {
103  yarp::os::impl::gethostname(hostname, size);
104  if (std::strlen(hostname)==0) {
105  std::strncpy(hostname, "no_hostname", size);
106  }
107 }
108 
109 char* yarp::os::getcwd(char *buf, size_t size)
110 {
111  return yarp::os::impl::getcwd(buf, size);
112 }
113 
115 {
116 #if defined(__APPLE__)
117  static void* handle = 0;
118  if (!enabled && !handle) {
119  handle = disableAppNap();
120  } else {
121  restoreAppNap(handle);
122  }
123 
124 #endif
125 }
126 
127 
128 
129 #ifndef YARP_NO_DEPRECATED // Since YARP 2.3.70
131 {
132  switch (signum) {
134  return yarp::os::impl::signal(SIGINT, sighandler);
136  return yarp::os::impl::signal(SIGTERM, sighandler);
137  default:
138  return nullptr; //signal not implemented yet
139  }
140 }
141 #endif // YARP_NO_DEPRECATED
142 
143 #ifndef YARP_NO_DEPRECATED // Since YARP 2.3.70
144 void yarp::os::exit(int exit_code)
145 {
146  std::exit(exit_code); //may cause crash... exit is not recommended in processes with multi thread, see http://www.cplusplus.com/reference/cstdlib/exit/
147 }
148 #endif // YARP_NO_DEPRECATED
149 
150 #ifndef YARP_NO_DEPRECATED // Since YARP 2.3.70
152 {
153 #if defined(_WIN32)
154  if (verbose==false) {
155  //to suppress windows dialog message
156  _set_abort_behavior(0, _WRITE_ABORT_MSG);
157  _set_abort_behavior(0, _CALL_REPORTFAULT);
158  }
159 #else
160  YARP_UNUSED(verbose);
161 #endif
162  std::abort(); // exit is not recommended in processes with multi thread, see http://www.cplusplus.com/reference/cstdlib/exit/ and http://www.cplusplus.com/reference/cstdlib/abort/
163 }
164 #endif // YARP_NO_DEPRECATED
165 
166 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
168 {
169 #if defined(YARP_HAS_ACE)
170  pid_t pid = ACE_OS::fork();
171 #elif defined(__unix__)
172  pid_t pid = ::fork();
173 #else
174 YARP_COMPILER_ERROR(Cannot implement fork on this platform)
175 #endif
176  return pid;
177 }
178 #endif // YARP_NO_DEPRECATED
static bool createPath(const std::string &fileName, int ignoreLevel=1)
Definition: NameConfig.cpp:138
char * getcwd(char *buf, size_t size)
Portable wrapper for the getcwd() function.
Definition: Os.cpp:109
void setprogname(const char *progname)
Portable wrapper for the setprogname() function.
Definition: Os.cpp:72
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:59
void exit(int exit_code)
Portable wrapper for the exit() function.
Definition: Os.cpp:144
YarpSignalHandler signal(int signum, YarpSignalHandler sighandler)
Portable wrapper for the signal() function.
Definition: Os.cpp:130
bool verbose
Definition: yarpros.cpp:21
int rename(const char *oldname, const char *newname)
Portable wrapper for the rename() function.
Definition: Os.cpp:54
int getpid()
Portable wrapper for the getppid() function.
Definition: Os.cpp:65
void(* YarpSignalHandler)(int)
Definition: Os.h:170
void setEnergySavingModeState(bool enabled)
Toggle the OS energy saving feature.
Definition: Os.cpp:114
int fork(void)
Portable wrapper for the fork() function.
Definition: Os.cpp:167
void getprogname(char *progname, size_t size)
Portable wrapper for the getprogname() function.
Definition: Os.cpp:83
void gethostname(char *hostname, size_t size)
Portable wrapper for the gethostname() function.
Definition: Os.cpp:101
int rmdir(const char *p)
Portable wrapper for the rmdir() function.
Definition: Os.cpp:49
int mkdir(const char *p)
Portable wrapper for the mkdir() function.
Definition: Os.cpp:39
void abort(bool verbose=false)
Portable wrapper for the abort() function.
Definition: Os.cpp:151
void * disableAppNap()
int mkdir_p(const char *p, int ignoreLevels=0)
Create a directory and all parent directories needed.
Definition: Os.cpp:44