- 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

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;
}
////////////////////////////////////////////////////////////////////////////////