FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
SystemHelpersX11.cpp
1 /**********************************************************\
2  Original Author: Max Amanshauser
3 
4  Created: Dec 02, 2011
5  License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12  Copyright 2011 Max Amanshauser, Firebreath development team
13  \**********************************************************/
14 
15 #include "../SystemHelpers.h"
16 #include "logging.h"
17 #include <sys/types.h>
18 #include <pwd.h>
19 #include <unistd.h>
20 #include <stdexcept>
21 #include <stdlib.h>
22 
23 // Thread-safe
24 std::string FB::System::getHomeDirPath()
25 {
26  int bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
27  if(bufsize == -1)
28  bufsize = 16384; // Should be more than enough
29 
30  char* buf = (char*) malloc(bufsize);
31  if (buf == NULL) {
32  FBLOG_ERROR("SystemHelpersX11", "malloc failed.");
33  throw new std::runtime_error("malloc failed.");
34  }
35 
36  struct passwd pwd;
37  struct passwd* result;
38 
39  int s = getpwuid_r(getuid(), &pwd, buf, bufsize, &result);
40  if (result == NULL) {
41  free(buf);
42 
43  if (s == 0) {
44  FBLOG_ERROR("SystemHelpersX11", "User not found.");
45  throw new std::runtime_error("User not found.");
46  }else{
47  std::string exs ("getpwuid_r error: ");
48  exs += s;
49 
50  FBLOG_ERROR("SystemHelpersX11", exs);
51  throw new std::runtime_error(exs);
52  }
53  }
54 
55  std::string x (result->pw_dir);
56  free(buf);
57 
58  return x;
59 }
60 
61 std::string FB::System::getAppDataPath(const std::string& appName)
62 {
63  std::string s = getHomeDirPath();
64 
65  return s + "/." + appName;
66 }
67 
68 std::string FB::System::getLocalAppDataPath(const std::string& appName)
69 {
70  return getAppDataPath(appName);
71 }
72 
73 std::string FB::System::getTempPath()
74 {
75  char* c = getenv("TMPDIR");
76 
77  if(c == NULL)
78  return std::string("/tmp");
79 
80  return std::string (c);
81 }