2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tpath.c,v 1.10 2006-06-08 10:26:10 adam Exp $
9 * \brief File Path utilities
20 #include <yaz/tpath.h>
22 #include <sys/types.h>
28 FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
30 return yaz_fopen (path, name, mode, 0);
33 int yaz_fclose (FILE *f)
39 char *yaz_filepath_resolve(const char *fname, const char *path,
40 const char *base, char *fullpath)
45 const char *path_sep = 0;
52 /* somewhat dirty since we have to consider Windows
55 if (strchr ("/\\.", *path))
57 path_sep = strchr (path+1, ':');
59 else if (path[0] && path[1])
60 path_sep = strchr (path+2, ':');
62 len = path_sep - path;
65 /* is path is relative and base is to be used */
66 if (!strchr ("/\\", *path) && base)
68 /* yes: make base the first part */
69 strcpy (fullpath, base);
70 slen = strlen(fullpath);
71 fullpath[slen++] = '/';
73 memcpy (fullpath+slen, path, len);
75 if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
76 fullpath[slen++] = '/';
78 strcpy (fullpath+slen, fname);
80 if (stat(fullpath, &stat_buf) == 0)
90 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
95 if (!yaz_filepath_resolve(fname, path, base, fullpath))
96 return 0; /* failure */
97 return fopen(fullpath, mode);
100 int yaz_is_abspath (const char *p)
107 if (*p && p[1] == ':' && isalpha(*p))
115 * indent-tabs-mode: nil
117 * vim: shiftwidth=4 tabstop=8 expandtab