3599ddcdeb8c28bda675785a9fac9d9a6ebf3352
[yaz-moved-to-github.git] / src / tpath.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file tpath.c
7  * \brief File Path utilities
8  */
9
10 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <yaz/tpath.h>
19 #include <yaz/log.h>
20 #if HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif
23 #if HAVE_SYS_STAT_H
24 #include <sys/stat.h>
25 #endif
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
31 {
32     return yaz_fopen (path, name, mode, 0);
33 }
34
35 int yaz_fclose (FILE *f)
36 {
37     return fclose (f);
38 }
39
40
41 size_t yaz_filepath_comp(const char **path_p, const char **comp)
42 {
43     const char *path = *path_p;
44     size_t len;
45     const char *path_sep;
46
47     /* somewhat dirty since we have to consider Windows
48      * drive letters..
49      */
50     if (path[0] && strchr ("/\\.", path[0]))
51         path_sep = strchr (path+1, ':');
52     else if (path[0] && path[1])
53         path_sep = strchr (path+2, ':');
54     else
55         path_sep = 0;
56     
57     if (path_sep)
58     {
59         len = path_sep - path;
60         *path_p = path + len + 1;
61     }
62     else
63     {
64         len = strlen(path);
65         *path_p = path + len;
66     }
67     *comp = path;
68     return len;
69 }
70
71 char *yaz_filepath_resolve(const char *fname, const char *path,
72                            const char *base, char *fullpath)
73 {
74     for(;;)
75     {
76         struct stat stat_buf;
77         size_t slen = 0;
78        
79         *fullpath = '\0';
80         if (path)
81         {
82             const char *comp;
83             size_t len = 0;
84
85             len = yaz_filepath_comp(&path, &comp);
86             if (!len)
87                 break;
88
89             if (!strchr ("/\\", *comp) && base)
90             {
91                 /* yes: make base the first part */
92                 strcpy (fullpath, base);
93                 slen = strlen(fullpath);
94                 fullpath[slen++] = '/';
95             }
96             memcpy (fullpath+slen, comp, len);
97             slen += len;
98             if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
99                 fullpath[slen++] = '/';
100         }
101         strcpy (fullpath+slen, fname);
102         if (stat(fullpath, &stat_buf) == 0)
103             return fullpath;
104         if (!path)
105             break;
106     }
107     return 0;
108 }
109
110 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
111                 const char *base)
112 {
113     char fullpath[1024];
114
115     if (!yaz_filepath_resolve(fname, path, base, fullpath))
116         return 0; /* failure */
117     return fopen(fullpath, mode);
118 }
119
120 int yaz_is_abspath (const char *p)
121 {
122     if (*p == '/')
123         return 1;
124 #ifdef WIN32
125     if (*p == '\\')
126         return 1;
127     if (*p && p[1] == ':' && isalpha(*p))
128         return 1;
129 #endif
130     return 0;
131 }
132 /*
133  * Local variables:
134  * c-basic-offset: 4
135  * indent-tabs-mode: nil
136  * End:
137  * vim: shiftwidth=4 tabstop=8 expandtab
138  */
139