projects
/
yaz-moved-to-github.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fixed bug #627: addInfo not propagated by srw_bend_fetch() when
[yaz-moved-to-github.git]
/
src
/
tpath.c
diff --git
a/src/tpath.c
b/src/tpath.c
index
58ac639
..
9dd8637
100644
(file)
--- a/
src/tpath.c
+++ b/
src/tpath.c
@@
-1,10
+1,14
@@
/*
/*
- * Copyright (c) 1995-2003, Index Data.
+ * Copyright (C) 1995-2006, Index Data ApS
* See the file LICENSE for details.
* See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
*
*
- * $Id: tpath.c,v 1.1 2003-10-27 12:21:35 adam Exp $
+ * $Id: tpath.c,v 1.11 2006-06-08 20:55:38 adam Exp $
*/
*/
+/**
+ * \file tpath.c
+ * \brief File Path utilities
+ */
+
#if HAVE_CONFIG_H
#include <config.h>
#endif
#if HAVE_CONFIG_H
#include <config.h>
#endif
@@
-15,6
+19,11
@@
#include <ctype.h>
#include <yaz/tpath.h>
#include <yaz/log.h>
#include <ctype.h>
#include <yaz/tpath.h>
#include <yaz/log.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
{
FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
{
@@
-26,49
+35,51
@@
int yaz_fclose (FILE *f)
return fclose (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(;;)
{
for(;;)
{
- FILE *f;
-
+ struct stat stat_buf;
const char *path_sep = 0;
size_t len = 0;
size_t slen = 0;
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 (path)
{
/* somewhat dirty since we have to consider Windows
* drive letters..
*/
- if (strchr ("/\\.", *path))
- {
+ if (path[0] && strchr ("/\\.", *path))
path_sep = strchr (path+1, ':');
path_sep = strchr (path+1, ':');
- }
else if (path[0] && path[1])
path_sep = strchr (path+2, ':');
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);
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)
{
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;
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;
if (!path_sep)
break;
@@
-77,6
+88,16
@@
FILE *yaz_fopen(const char *path, const char *name, const char *mode,
return 0;
}
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 == '/')
int yaz_is_abspath (const char *p)
{
if (*p == '/')
@@
-89,3
+110,11
@@
int yaz_is_abspath (const char *p)
#endif
return 0;
}
#endif
return 0;
}
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+