A Discrete-Event Network Simulator
API
system-path.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "system-path.h"
21 #include "fatal-error.h"
22 #include "assert.h"
23 #include "log.h"
24 #include "ns3/core-config.h"
25 #include <cstdlib>
26 #include <cerrno>
27 #include <cstring>
28 
29 
30 #if defined (HAVE_DIRENT_H) and defined (HAVE_SYS_TYPES_H)
31 
32 #define HAVE_OPENDIR
33 #include <sys/types.h>
34 #include <dirent.h>
35 #endif
36 #if defined (HAVE_SYS_STAT_H) and defined (HAVE_SYS_TYPES_H)
37 
38 #define HAVE_MKDIR_H
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #endif
42 #include <sstream>
43 #ifdef __APPLE__
44 #include <mach-o/dyld.h>
45 #endif /* __APPLE__ */
46 
47 #ifdef __FreeBSD__
48 #include <sys/types.h>
49 #include <sys/sysctl.h>
50 #endif
51 
52 #ifdef __linux__
53 #include <unistd.h>
54 #endif
55 
60 #if defined (__win32__)
61 #define SYSTEM_PATH_SEP "\\"
62 #else
63 #define SYSTEM_PATH_SEP "/"
64 #endif
65 
72 namespace ns3 {
73 
74 NS_LOG_COMPONENT_DEFINE ("SystemPath");
75 
76 namespace SystemPath {
77 
88 std::string Dirname (std::string path)
89 {
90  NS_LOG_FUNCTION (path);
91  std::list<std::string> elements = Split (path);
92  std::list<std::string>::const_iterator last = elements.end();
93  last--;
94  return Join (elements.begin (), last);
95 }
96 
97 std::string FindSelfDirectory (void)
98 {
109  std::string filename;
110 #if defined(__linux__)
111  {
112  ssize_t size = 1024;
113  char *buffer = (char*)malloc (size);
114  memset (buffer, 0, size);
115  int status;
116  while (true)
117  {
118  status = readlink("/proc/self/exe", buffer, size);
119  if (status != 1 || (status == -1 && errno != ENAMETOOLONG))
120  {
121  break;
122  }
123  size *= 2;
124  free (buffer);
125  buffer = (char*)malloc (size);
126  memset (buffer, 0, size);
127  }
128  if (status == -1)
129  {
130  NS_FATAL_ERROR ("Oops, could not find self directory.");
131  }
132  filename = buffer;
133  free (buffer);
134  }
135 #elif defined (__win32__)
136  {
139  DWORD size = 1024;
140  LPTSTR lpFilename = (LPTSTR) malloc (sizeof(TCHAR) * size);
141  DWORD status = GetModuleFilename (0, lpFilename, size);
142  while (status == size)
143  {
144  size = size * 2;
145  free (lpFilename);
146  lpFilename = (LPTSTR) malloc (sizeof(TCHAR) * size);
147  status = GetModuleFilename (0, lpFilename, size);
148  }
149  NS_ASSERT (status != 0);
150  filename = lpFilename;
151  free (lpFilename);
152  }
153 #elif defined (__APPLE__)
154  {
155  uint32_t bufsize = 1024;
156  char *buffer = (char *) malloc (bufsize);
157  NS_ASSERT (buffer != 0);
158  int status = _NSGetExecutablePath (buffer, &bufsize);
159  if (status == -1)
160  {
161  free (buffer);
162  buffer = (char *) malloc (bufsize);
163  status = _NSGetExecutablePath (buffer, &bufsize);
164  }
165  NS_ASSERT (status == 0);
166  filename = buffer;
167  free (buffer);
168  }
169 #elif defined (__FreeBSD__)
170  {
171  int mib[4];
172  size_t bufSize = 1024;
173  char *buf = (char *) malloc(bufSize);
174 
175  mib[0] = CTL_KERN;
176  mib[1] = KERN_PROC;
177  mib[2] = KERN_PROC_PATHNAME;
178  mib[3] = -1;
179 
180  sysctl(mib, 4, buf, &bufSize, NULL, 0);
181  filename = buf;
182  }
183 #endif
184  return Dirname (filename);
185 }
186 
187 std::string Append (std::string left, std::string right)
188 {
189  // removing trailing separators from 'left'
190  NS_LOG_FUNCTION (left << right);
191  while (true)
192  {
193  std::string::size_type lastSep = left.rfind (SYSTEM_PATH_SEP);
194  if (lastSep != left.size () - 1)
195  {
196  break;
197  }
198  left = left.substr (0, left.size () - 1);
199  }
200  std::string retval = left + SYSTEM_PATH_SEP + right;
201  return retval;
202 }
203 
204 std::list<std::string> Split (std::string path)
205 {
206  NS_LOG_FUNCTION (path);
207  std::list<std::string> retval;
208  std::string::size_type current = 0, next = 0;
209  next = path.find (SYSTEM_PATH_SEP, current);
210  while (next != std::string::npos)
211  {
212  std::string item = path.substr (current, next - current);
213  retval.push_back (item);
214  current = next + 1;
215  next = path.find (SYSTEM_PATH_SEP, current);
216  }
217  std::string item = path.substr (current, next - current);
218  retval.push_back (item);
219  return retval;
220 }
221 
222 std::string Join (std::list<std::string>::const_iterator begin,
223  std::list<std::string>::const_iterator end)
224 {
225  NS_LOG_FUNCTION (&begin << &end);
226  std::string retval = "";
227  for (std::list<std::string>::const_iterator i = begin; i != end; i++)
228  {
229  if (i == begin)
230  {
231  retval = *i;
232  }
233  else
234  {
235  retval = retval + SYSTEM_PATH_SEP + *i;
236  }
237  }
238  return retval;
239 }
240 
241 std::list<std::string> ReadFiles (std::string path)
242 {
243  NS_LOG_FUNCTION (path);
244  std::list<std::string> files;
245 #if defined HAVE_OPENDIR
246  DIR *dp = opendir (path.c_str ());
247  if (dp == NULL)
248  {
249  NS_FATAL_ERROR ("Could not open directory=" << path);
250  }
251  struct dirent *de = readdir (dp);
252  while (de != 0)
253  {
254  files.push_back (de->d_name);
255  de = readdir (dp);
256  }
257  closedir (dp);
258 #elif defined (HAVE_FIND_FIRST_FILE)
259  HANDLE hFind;
261  WIN32_FIND_DATA fileData;
262 
263  hFind = FindFirstFile (path.c_str (), &FindFileData);
264  if (hFind == INVALID_HANDLE_VALUE)
265  {
266  NS_FATAL_ERROR ("Could not open directory=" << path);
267  }
268  do
269  {
270  files.push_back (fileData.cFileName);
271  } while (FindNextFile (hFind, &fileData));
272  FindClose(hFind);
273 #else
274 #error "No support for reading a directory on this platform"
275 #endif
276  return files;
277 }
278 
279 std::string
281 {
283  char *path = NULL;
284 
285  path = getenv ("TMP");
286  if (path == NULL)
287  {
288  path = getenv ("TEMP");
289  if (path == NULL)
290  {
291  path = const_cast<char *> ("/tmp");
292  }
293  }
294 
295  //
296  // Just in case the user wants to go back and find the output, we give
297  // a hint as to which dir we created by including a time hint.
298  //
299  time_t now = time (NULL);
300  struct tm *tm_now = localtime (&now);
301  //
302  // But we also randomize the name in case there are multiple users doing
303  // this at the same time
304  //
305  srand (time (0));
306  long int n = rand ();
307 
308  //
309  // The final path to the directory is going to look something like
310  //
311  // /tmp/ns3-14.30.29.32767
312  //
313  // The first segment comes from one of the temporary directory env
314  // variables or /tmp if not found. The directory name starts with an
315  // identifier telling folks who is making all of the temp directories
316  // and then the local time (in this case 14.30.29 -- which is 2:30 and
317  // 29 seconds PM).
318  //
319  std::ostringstream oss;
320  oss << path << SYSTEM_PATH_SEP << "ns-3." << tm_now->tm_hour << "."
321  << tm_now->tm_min << "." << tm_now->tm_sec << "." << n;
322 
323  return oss.str ();
324 }
325 
326 void
327 MakeDirectories (std::string path)
328 {
329  NS_LOG_FUNCTION (path);
330 
331  // Make sure all directories on the path exist
332  std::list<std::string> elements = Split (path);
333  for (std::list<std::string>::const_iterator i = elements.begin (); i != elements.end (); ++i)
334  {
335  std::string tmp = Join (elements.begin (), i);
336 #if defined(HAVE_MKDIR_H)
337  if (mkdir (tmp.c_str (), S_IRWXU))
338  {
339  NS_LOG_ERROR ("failed creating directory " << tmp);
340  }
341 #endif
342  }
343 
344  // Make the final directory. Is this redundant with last iteration above?
345 #if defined(HAVE_MKDIR_H)
346  if (mkdir (path.c_str (), S_IRWXU))
347  {
348  NS_LOG_ERROR ("failed creating directory " << path);
349  }
350 #endif
351 
352 }
353 
354 } // namespace SystemPath
355 
356 } // namespace ns3
NS_FATAL_x macro definitions.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:145
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
Definition: system-path.cc:204
System-independent file and directory function declarations.
std::string Join(std::list< std::string >::const_iterator begin, std::list< std::string >::const_iterator end)
Join a list of file system path directories into a single file system path.
Definition: system-path.cc:222
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
std::string MakeTemporaryDirectoryName(void)
Get the name of a temporary directory.
Definition: system-path.cc:280
std::list< std::string > ReadFiles(std::string path)
Get the list of files located in a file system directory.
Definition: system-path.cc:241
std::string Dirname(std::string path)
Get the directory path for a file.
Definition: system-path.cc:88
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void MakeDirectories(std::string path)
Create all the directories leading to path.
Definition: system-path.cc:327
std::string FindSelfDirectory(void)
Get the file system path to the current executable.
Definition: system-path.cc:97
std::string Append(std::string left, std::string right)
Join two file system path elements.
Definition: system-path.cc:187
#define SYSTEM_PATH_SEP
System-specific path separator used between directory names.
Definition: system-path.cc:63
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:220
Debug message logging.