2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tpath.c,v 1.13 2007-01-03 08:42:15 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 size_t yaz_filepath_comp(const char **path_p, const char **comp)
41 const char *path = *path_p;
45 /* somewhat dirty since we have to consider Windows
48 if (path[0] && strchr ("/\\.", path[0]))
49 path_sep = strchr (path+1, ':');
50 else if (path[0] && path[1])
51 path_sep = strchr (path+2, ':');
57 len = path_sep - path;
58 *path_p = path + len + 1;
69 char *yaz_filepath_resolve(const char *fname, const char *path,
70 const char *base, char *fullpath)
83 len = yaz_filepath_comp(&path, &comp);
87 if (!strchr ("/\\", *comp) && base)
89 /* yes: make base the first part */
90 strcpy (fullpath, base);
91 slen = strlen(fullpath);
92 fullpath[slen++] = '/';
94 memcpy (fullpath+slen, comp, len);
96 if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
97 fullpath[slen++] = '/';
99 strcpy (fullpath+slen, fname);
100 if (stat(fullpath, &stat_buf) == 0)
108 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
113 if (!yaz_filepath_resolve(fname, path, base, fullpath))
114 return 0; /* failure */
115 return fopen(fullpath, mode);
118 int yaz_is_abspath (const char *p)
125 if (*p && p[1] == ':' && isalpha(*p))
133 * indent-tabs-mode: nil
135 * vim: shiftwidth=4 tabstop=8 expandtab