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