/* /adm/SimulEfun/absolute_path.c
* from Nightmare IV
* returns the full path of a string based on a relative path
* created by Huthar@Portals as resolv_path.c
* modifications by Pallando@Nightmare 930526
* changed to absolute_path() by Descartes of Borg 940501
* features added and speed doubled by Pallando, 940531
* Beek fixed ("/", "std") -> "//std" bug, 941004
*/
string absolute_path(string curr, string New) {
int i, len;
string *tmp;
string name, rest;
if(curr && (curr == "cwd") && this_player())
curr = (string)this_player()->query_cwd();
if(!New || New == "" || New == ".") return curr;
if( (New == "here") && this_player() )
{
return file_name(environment(this_player())) + ".c";
}
len = strlen( New );
switch( New[0..0] )
{
case "~":
if( New == "~" || New == "~/" )
New = user_path( (string)this_player()-> query_name() )[0..-2];
else if( New[1..1] == "/" )
New = user_path( (string)this_player()-> query_name() ) +
New[2..len];
else if( sscanf( New, "~%s/%s", name, rest ) == 2 )
New = user_path( name ) + rest;
else
New = user_path( New[1..len] )[0..-2];
break;
case "^":
New = "/domains/" + New[1..len];
break;
case "/":
break;
default:
if (curr != "/")
New = curr + "/" + New;
else New = "/" + New;
}
if( -1 == strsrch( New, ".." ) ) return New;
if(New[strlen(New) - 1] != '/')
New += "/";
tmp = explode(New,"/");
if (!tmp) tmp = ({"/"});
for(i = 0; i < sizeof(tmp); i++)
if(tmp[i] == "..") {
if(sizeof(tmp) > 2) {
tmp = tmp[0..(i-2)] + tmp[(i+1)..(sizeof(tmp)-1)];
i -= 2;
} else {
tmp = tmp[2 ..(sizeof(tmp)-1)];
i = 0;
}
}
New = "/" + implode(tmp,"/");
if(New == "//") New = "/";
//tc("New: "+New);
return New;
}