1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief File Path utilities
18 #include <yaz/tpath.h>
21 #include <sys/types.h>
34 FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
36 return yaz_fopen (path, name, mode, 0);
39 int yaz_fclose (FILE *f)
45 size_t yaz_filepath_comp(const char **path_p, const char **comp)
47 const char *path = *path_p;
51 /* somewhat dirty since we have to consider Windows
54 if (path[0] && strchr ("/\\.", path[0]))
55 path_sep = strchr (path+1, ':');
56 else if (path[0] && path[1])
57 path_sep = strchr (path+2, ':');
63 len = path_sep - path;
64 *path_p = path + len + 1;
75 char *yaz_filepath_resolve(const char *fname, const char *path,
76 const char *base, char *fullpath)
89 len = yaz_filepath_comp(&path, &comp);
93 if (!strchr ("/\\", *comp) && base)
95 /* yes: make base the first part */
96 strcpy (fullpath, base);
97 slen = strlen(fullpath);
98 fullpath[slen++] = '/';
100 memcpy (fullpath+slen, comp, len);
102 if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
103 fullpath[slen++] = '/';
105 strcpy (fullpath+slen, fname);
106 if (stat(fullpath, &stat_buf) == 0)
114 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
119 if (!yaz_filepath_resolve(fname, path, base, fullpath))
120 return 0; /* failure */
121 return fopen(fullpath, mode);
124 int yaz_is_abspath (const char *p)
131 if (*p && p[1] == ':' && isalpha(*p))
139 * c-file-style: "Stroustrup"
140 * indent-tabs-mode: nil
142 * vim: shiftwidth=4 tabstop=8 expandtab