- Properly expands ~ characters in data.location

This commit is contained in:
Paul Beckingham 2008-07-09 22:56:39 -04:00
parent ec8da0baaa
commit 2150642f9d
5 changed files with 44 additions and 2 deletions

14
html/README Normal file
View file

@ -0,0 +1,14 @@
Documentation Restructuring
high level pages
download
previous verisons
tutorial
recurrence
priorities
subprojects
tags
need page banner
need task link off beckinghma.net

View file

@ -279,7 +279,7 @@ void handleVersion (Config& conf)
"file."
<< std::endl;
if (access (conf.get ("data.location").c_str (), X_OK))
if (access (expandPath (conf.get ("data.location")).c_str (), X_OK))
std::cout << "Configuration error: data.location contains a directory name"
" that doesn't exist, or is unreadable."
<< std::endl;

View file

@ -286,7 +286,7 @@ int main (int argc, char** argv)
}
TDB tdb;
tdb.dataDirectory (conf.get ("data.location"));
tdb.dataDirectory (expandPath (conf.get ("data.location")));
// Log commands, if desired.
if (conf.get ("command.logging") == "on")

View file

@ -119,6 +119,7 @@ std::string formatSeconds (time_t);
const std::string uuid ();
const char* optionalBlankLine (Config&);
int convertDuration (std::string&);
std::string expandPath (const std::string&);
// rules.cpp
void initializeColorRules (Config&);

View file

@ -32,6 +32,7 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include "Date.h"
#include "Table.h"
#include "task.h"
@ -304,3 +305,29 @@ int convertDuration (std::string& input)
}
////////////////////////////////////////////////////////////////////////////////
std::string expandPath (const std::string& in)
{
std::string copy = in;
unsigned int tilde;
if ((tilde = copy.find ("~/")) != std::string::npos)
{
struct passwd* pw = getpwuid (getuid ());
copy.replace (tilde, 1, pw->pw_dir);
}
else if ((tilde = copy.find ("~")) != std::string::npos)
{
unsigned int slash;
if ((slash = copy.find ("/", tilde)) != std::string::npos)
{
std::string name = copy.substr (tilde + 1, slash - tilde - 1);
struct passwd* pw = getpwnam (name.c_str ());
if (pw)
copy.replace (tilde, slash - tilde, pw->pw_dir);
}
}
return copy;
}
////////////////////////////////////////////////////////////////////////////////