1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief File Path utilities
17 #include <yaz/tpath.h>
20 #include <sys/types.h>
33 FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
35 return yaz_fopen(path, name, mode, 0);
38 int yaz_fclose (FILE *f)
44 size_t yaz_filepath_comp(const char **path_p, const char **comp)
46 const char *path = *path_p;
50 /* somewhat dirty since we have to consider Windows
53 if (path[0] && strchr("/\\.", path[0]))
54 path_sep = strchr(path+1, ':');
55 else if (path[0] && path[1])
56 path_sep = strchr(path+2, ':');
62 len = path_sep - path;
63 *path_p = path + len + 1;
74 char *yaz_filepath_resolve(const char *fname, const char *path,
75 const char *base, char *fullpath)
77 if (path && *path == '\0')
79 if (strchr("/\\", *fname))
92 len = yaz_filepath_comp(&path, &comp);
96 if (!strchr("/\\", *comp) && base)
98 /* yes: make base the first part */
99 strcpy(fullpath, base);
100 slen = strlen(fullpath);
101 fullpath[slen++] = '/';
103 memcpy(fullpath+slen, comp, len);
105 if(slen > 0 && !strchr("/\\", fullpath[slen-1]))
106 fullpath[slen++] = '/';
108 strcpy(fullpath+slen, fname);
109 if (stat(fullpath, &stat_buf) == 0)
117 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
122 if (!yaz_filepath_resolve(fname, path, base, fullpath))
123 return 0; /* failure */
124 return fopen(fullpath, mode);
127 int yaz_is_abspath(const char *p)
134 if (*p && p[1] == ':' &&
135 ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')))
143 * c-file-style: "Stroustrup"
144 * indent-tabs-mode: nil
146 * vim: shiftwidth=4 tabstop=8 expandtab