Improved path fixes for classic TOS systems.

svn path=/trunk/netsurf/; revision=11439
This commit is contained in:
Ole Loots 2011-01-22 16:27:26 +00:00
parent 7d4056ae41
commit f222ec79e9
1 changed files with 74 additions and 38 deletions

View File

@ -22,13 +22,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <windom.h>
#include "utils/log.h"
#include "utils/url.h"
#include "atari/findfile.h"
#include "atari/gui.h"
#include "atari/misc.h"
extern unsigned short gdosversion;
static void fix_path(char * path);
char *path_to_url(const char *path)
{
@ -51,49 +56,86 @@ char *url_to_path(const char *url)
char *path;
/* return the absolute path including leading / */
path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN - 1));
/* TODO: fix path seperator */
if(gdosversion > TOS4VER ) {
path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN - 1));
} else {
/* do not include / within ulr_path */
path = strdup(url_path + (FILE_SCHEME_PREFIX_LEN));
int l = strlen(path);
int i;
for( i = 0; i<l-1; i++){
if( path[i] == '/' ){
path[i] = 0x5C;
}
}
}
curl_free(url_path);
LOG(("%s", path));
return path;
}
#ifdef PLAIN_TOS
#undef realpath
#undef access
#define access(f,m) (0)
#define PATH_SEP '/'
char * realpath(const char * path, char * rpath)
/* convert nonsense getcwd path (returned by mintlib getcwd on plain TOS) */
static void fix_path(char * path)
{
char npath[PATH_MAX];
/* only apply fix to paths that contain /dev/ */
if( strlen(path) < 6 ){
return;
}
if( strncmp(path, "/dev/", 5) != 0 ) {
return;
}
strncpy((char*)&npath, path, PATH_MAX);
npath[0] = path[5];
npath[1] = ':';
npath[2] = 0;
strcat((char*)&npath, &path[6]);
strcpy(path, (char*)&npath);
}
/*
a fixed version of realpath() which returns valid
paths for TOS which have no root fs. (/ , U: )
*/
char * gdos_realpath(const char * path, char * rpath)
{
size_t l;
size_t i;
char old;
char new = PATH_SEP;
char fsep = 0x5C;
if( rpath == NULL ){
return( NULL );
}
if( gdosversion > TOS4VER ){
return( realpath(path, rpath) );
}
if( PATH_SEP == '/') {
if( fsep == '/') {
/* replace '\' with / */
old = 0x2F; /* / */
old = 0x5C; /* / */
} else {
/* replace '/' with \ */
old = '/';
}
if( path[0] == 0x2F || path[0] == '/' ){
strcpy(rpath, "U:");
strcat(rpath, path);
}
else if( path[0] == '.') {
if( path[0] != '/' && path[0] != 0x5c && path[1] != ':') {
/* it is not an absolute path */
char cwd[PATH_MAX];
getcwd((char*)&cwd, PATH_MAX);
fix_path((char*)&cwd);
strcpy(rpath, (char*)&cwd);
if( (path[1] == '/' || path[1] == 0x27 ) ) {
strcat(rpath, &path[1]);
l = strlen(rpath);
if(rpath[l-1] != 0x5C && rpath[l-1] != '/') {
rpath[l] = fsep;
rpath[l+1] = 0;
}
if( (path[1] == '/' || path[1] == 0x5C ) ) {
strcat(rpath, &path[2]);
} else {
strcat(rpath, "/");
strcat(rpath, path);
}
} else {
@ -103,12 +145,12 @@ char * realpath(const char * path, char * rpath)
l = strlen(rpath);
for( i = 0; i<l-1; i++){
if( rpath[i] == old ){
rpath[i] = new;
rpath[i] = fsep;
}
}
return( rpath );
}
#endif
/**
* Locate a shared resource file by searching known places in order.
@ -122,27 +164,26 @@ char * realpath(const char * path, char * rpath)
* environment variable),
*/
#ifndef NETSURF_GEM_RESPATH
#define NETSURF_GEM_RESPATH "./res/"
#define NETSURF_GEM_RESPATH "./res/"
#endif
char * atari_find_resource(char *buf, const char *filename, const char *def)
{
char *cdir = NULL;
char t[PATH_MAX];
LOG(("%s (def: %s)", filename, def ));
strcpy(t, NETSURF_GEM_RESPATH);
strcat(t, filename);
LOG(("checking %s", (char*)&t));
if (realpath(t, buf) != NULL) {
if (gdos_realpath(t, buf) != NULL) {
if (access(buf, R_OK) == 0) {
return buf;
}
}
strcpy(t, "./");
strcat(t, filename);
LOG(("checking %s", (char*)&t));
if (realpath(t, buf) != NULL) {
if (gdos_realpath(t, buf) != NULL) {
if (access(buf, R_OK) == 0) {
return buf;
}
@ -154,16 +195,15 @@ char * atari_find_resource(char *buf, const char *filename, const char *def)
strcat(t, "/.netsurf/");
strcat(t, filename);
LOG(("checking %s", (char*)&t));
if (realpath(t, buf) != NULL) {
if (gdos_realpath(t, buf) != NULL) {
if (access(buf, R_OK) == 0)
return buf;
}
}
cdir = getenv("NETSURFRES");
if (cdir != NULL) {
if (realpath(cdir, buf) != NULL) {
if (gdos_realpath(cdir, buf) != NULL) {
strcat(buf, "/");
strcat(buf, filename);
LOG(("checking %s", (char*)&t));
@ -171,16 +211,15 @@ char * atari_find_resource(char *buf, const char *filename, const char *def)
return buf;
}
}
if (def[0] == '~') {
snprintf(t, PATH_MAX, "%s%s", getenv("HOME"), def + 1);
LOG(("checking %s", (char*)&t));
if (realpath(t, buf) == NULL) {
if (gdos_realpath(t, buf) == NULL) {
strcpy(buf, t);
}
} else {
LOG(("checking %s", (char*)def));
if (realpath(def, buf) == NULL) {
if (gdos_realpath(def, buf) == NULL) {
strcpy(buf, def);
}
}
@ -188,11 +227,8 @@ char * atari_find_resource(char *buf, const char *filename, const char *def)
return buf;
}
/*
* Local Variables:
* c-basic-offset: 8
* End:
*/