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
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)
88 len = yaz_filepath_comp(&path, &comp);
92 if (!strchr ("/\\", *comp) && base)
94 /* yes: make base the first part */
95 strcpy (fullpath, base);
96 slen = strlen(fullpath);
97 fullpath[slen++] = '/';
99 memcpy (fullpath+slen, comp, len);
101 if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
102 fullpath[slen++] = '/';
104 strcpy (fullpath+slen, fname);
105 if (stat(fullpath, &stat_buf) == 0)
113 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
118 if (!yaz_filepath_resolve(fname, path, base, fullpath))
119 return 0; /* failure */
120 return fopen(fullpath, mode);
123 int yaz_is_abspath (const char *p)
130 if (*p && p[1] == ':' && isalpha(*p))
138 * c-file-style: "Stroustrup"
139 * indent-tabs-mode: nil
141 * vim: shiftwidth=4 tabstop=8 expandtab