1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * \brief File Path utilities
18 #include <yaz/tpath.h>
20 #include <sys/types.h>
26 FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
28 return yaz_fopen (path, name, mode, 0);
31 int yaz_fclose (FILE *f)
37 size_t yaz_filepath_comp(const char **path_p, const char **comp)
39 const char *path = *path_p;
43 /* somewhat dirty since we have to consider Windows
46 if (path[0] && strchr ("/\\.", path[0]))
47 path_sep = strchr (path+1, ':');
48 else if (path[0] && path[1])
49 path_sep = strchr (path+2, ':');
55 len = path_sep - path;
56 *path_p = path + len + 1;
67 char *yaz_filepath_resolve(const char *fname, const char *path,
68 const char *base, char *fullpath)
81 len = yaz_filepath_comp(&path, &comp);
85 if (!strchr ("/\\", *comp) && base)
87 /* yes: make base the first part */
88 strcpy (fullpath, base);
89 slen = strlen(fullpath);
90 fullpath[slen++] = '/';
92 memcpy (fullpath+slen, comp, len);
94 if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
95 fullpath[slen++] = '/';
97 strcpy (fullpath+slen, fname);
98 if (stat(fullpath, &stat_buf) == 0)
106 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
111 if (!yaz_filepath_resolve(fname, path, base, fullpath))
112 return 0; /* failure */
113 return fopen(fullpath, mode);
116 int yaz_is_abspath (const char *p)
123 if (*p && p[1] == ':' && isalpha(*p))
131 * indent-tabs-mode: nil
133 * vim: shiftwidth=4 tabstop=8 expandtab