X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=util%2Ftpath.c;h=e2053218c2764ec955a97f7e912aee233e363c94;hb=bec71e151d306edbc5f58e19c7c139a196e867e9;hp=58774b0bedc0aab2c4095273939203a9f0fd2c46;hpb=684d09483b9c10997bc71503d36bc98451e34e93;p=yaz-moved-to-github.git diff --git a/util/tpath.c b/util/tpath.c index 58774b0..e205321 100644 --- a/util/tpath.c +++ b/util/tpath.c @@ -1,41 +1,91 @@ /* - * Copyright (c) 1995, Index Data. + * Copyright (c) 1995-2002, Index Data. * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Log: tpath.c,v $ - * Revision 1.2 1996-10-29 13:36:26 adam - * Added header. - * - * Revision 1.1 1995/11/01 16:35:00 quinn - * Making data1 look for tables in data1_tabpath - * - * + * $Id: tpath.c,v 1.8 2002-04-05 12:49:13 adam Exp $ */ +#if HAVE_CONFIG_H +#include +#endif + #include #include -#include +#include +#include +#include FILE *yaz_path_fopen(const char *path, const char *name, const char *mode) { - char spath[512] = ""; + return yaz_fopen (path, name, mode, 0); +} - if (!path) - return fopen(name, mode); +int yaz_fclose (FILE *f) +{ + return fclose (f); +} - do +FILE *yaz_fopen(const char *path, const char *name, const char *mode, + const char *base) +{ + char spath[1024]; + + for(;;) { FILE *f; - if (sscanf(path, "%511[^:]", spath) < 1) - return 0; - sprintf(spath + strlen(spath), "/%s", name); + const char *path_sep = 0; + size_t len = 0; + size_t slen = 0; + + *spath = '\0'; + if (path) + { + /* somewhat dirty since we have to consider Windows + * drive letters.. + */ + if (strchr ("/\\.", *path)) + { + path_sep = strchr (path+1, ':'); + } + else if (path[0] && path[1]) + path_sep = strchr (path+2, ':'); + if (path_sep) + len = path_sep - path; + else + len = strlen(path); + if (!strchr ("/\\", *path) && base) + { + strcpy (spath, base); + slen = strlen(spath); + spath[slen++] = '/'; + } + memcpy (spath+slen, path, len); + slen += len; + if (!strchr("/\\", spath[slen-1])) + spath[slen++] = '/'; + } + strcpy (spath+slen, name); if ((f = fopen(spath, mode))) return f; - if ((path = strchr(path, ':'))) - path++; + + if (!path_sep) + break; + path = path_sep+1; } - while (path); + return 0; +} + +int yaz_is_abspath (const char *p) +{ + if (*p == '/') + return 1; +#ifdef WIN32 + if (*p == '\\') + return 1; + if (*p && p[1] == ':' && isalpha(*p)) + return 1; +#endif return 0; }