Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix null pointer dereference.
[simgrid.git] / src / xbt / xbt_os_file.cpp
1 /* xbt_os_file.cpp -- portable interface to file-related functions          */
2
3 /* Copyright (c) 2017-2019. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "src/internal_config.h"
9 #include "xbt/asserts.h"
10 #include "xbt/file.hpp" /* this module */
11
12 #ifdef _WIN32
13 #include <windows.h>
14 #endif
15
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19
20 #include <cstring>
21 #include <errno.h>
22 #include <libgen.h> /* POSIX dirname */
23
24 simgrid::xbt::Path::Path()
25 {
26 #if HAVE_UNISTD_H
27   char buffer[2048];
28   char* ret = getcwd(buffer, 2048);
29   xbt_assert(ret == buffer, "Error during getcwd: %s", strerror(errno));
30   path_ = std::string(buffer);
31 #else
32   path_ = std::string(".");
33 #endif
34 }
35
36 std::string simgrid::xbt::Path::get_dir_name()
37 {
38   std::string p(path_);
39   char *res = dirname(&p[0]);
40   return std::string(res, strlen(res));
41 }
42
43 std::string simgrid::xbt::Path::get_base_name()
44 {
45   std::string p(path_);
46   char *res = basename(&p[0]);
47   return std::string(res, strlen(res));
48 }