X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Ftpath.c;h=9dd863727b2b96878e47b57848e9dc9e2e2af7d5;hp=a525a916f8c083aa6dd18f1e27c8010ff3161570;hb=3ee7ef4088b265faffbdcabe3eb13d5f04a3b832;hpb=05c274ef315384faafcc5900c17468f0ea2474e6 diff --git a/src/tpath.c b/src/tpath.c index a525a91..9dd8637 100644 --- a/src/tpath.c +++ b/src/tpath.c @@ -1,12 +1,12 @@ /* - * Copyright (c) 1995-2004, Index Data. + * Copyright (C) 1995-2006, Index Data ApS * See the file LICENSE for details. * - * $Id: tpath.c,v 1.2 2004-10-15 00:19:01 adam Exp $ + * $Id: tpath.c,v 1.11 2006-06-08 20:55:38 adam Exp $ */ /** * \file tpath.c - * \brief Implements path fopen + * \brief File Path utilities */ #if HAVE_CONFIG_H @@ -19,6 +19,11 @@ #include #include #include +#include +#include +#if HAVE_UNISTD_H +#include +#endif FILE *yaz_path_fopen(const char *path, const char *name, const char *mode) { @@ -30,49 +35,51 @@ int yaz_fclose (FILE *f) return fclose (f); } -FILE *yaz_fopen(const char *path, const char *name, const char *mode, - const char *base) -{ - char spath[1024]; +char *yaz_filepath_resolve(const char *fname, const char *path, + const char *base, char *fullpath) +{ for(;;) { - FILE *f; - + struct stat stat_buf; const char *path_sep = 0; size_t len = 0; size_t slen = 0; - - *spath = '\0'; + + *fullpath = '\0'; if (path) { /* somewhat dirty since we have to consider Windows * drive letters.. */ - if (strchr ("/\\.", *path)) - { + if (path[0] && strchr ("/\\.", *path)) path_sep = strchr (path+1, ':'); - } else if (path[0] && path[1]) path_sep = strchr (path+2, ':'); + else + path_sep = 0; + if (path_sep) len = path_sep - path; else len = strlen(path); + /* is path is relative and base is to be used */ if (!strchr ("/\\", *path) && base) { - strcpy (spath, base); - slen = strlen(spath); - spath[slen++] = '/'; + /* yes: make base the first part */ + strcpy (fullpath, base); + slen = strlen(fullpath); + fullpath[slen++] = '/'; } - memcpy (spath+slen, path, len); + if (len) + memcpy (fullpath+slen, path, len); slen += len; - if (!strchr("/\\", spath[slen-1])) - spath[slen++] = '/'; + if (slen > 0 && !strchr("/\\", fullpath[slen-1])) + fullpath[slen++] = '/'; } - strcpy (spath+slen, name); - if ((f = fopen(spath, mode))) - return f; + strcpy (fullpath+slen, fname); + if (stat(fullpath, &stat_buf) == 0) + return fullpath; if (!path_sep) break; @@ -81,6 +88,16 @@ FILE *yaz_fopen(const char *path, const char *name, const char *mode, return 0; } +FILE *yaz_fopen(const char *path, const char *fname, const char *mode, + const char *base) +{ + char fullpath[1024]; + + if (!yaz_filepath_resolve(fname, path, base, fullpath)) + return 0; /* failure */ + return fopen(fullpath, mode); +} + int yaz_is_abspath (const char *p) { if (*p == '/') @@ -93,3 +110,11 @@ int yaz_is_abspath (const char *p) #endif return 0; } +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +