0fdf529a359882ff70e4f5d48a60525399c3138b
[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.10 2006-06-08 10:26:10 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 (strchr ("/\\.", *path))
56             {
57                 path_sep = strchr (path+1, ':');
58             }
59             else if (path[0] && path[1])
60                 path_sep = strchr (path+2, ':');
61             if (path_sep)
62                 len = path_sep - path;
63             else
64                 len = strlen(path);
65             /* is path is relative and base is to be used */
66             if (!strchr ("/\\", *path) && base)
67             {
68                 /* yes: make base the first part */
69                 strcpy (fullpath, base);
70                 slen = strlen(fullpath);
71                 fullpath[slen++] = '/';
72             }
73             memcpy (fullpath+slen, path, len);
74             slen += len;
75             if (slen > 0 && !strchr("/\\", fullpath[slen-1]))
76                 fullpath[slen++] = '/';
77         }
78         strcpy (fullpath+slen, fname);
79
80         if (stat(fullpath, &stat_buf) == 0)
81             return fullpath;
82         
83         if (!path_sep)
84             break;
85         path = path_sep+1;
86     }
87     return 0;
88 }
89
90 FILE *yaz_fopen(const char *path, const char *fname, const char *mode,
91                 const char *base)
92 {
93     char fullpath[1024];
94
95     if (!yaz_filepath_resolve(fname, path, base, fullpath))
96         return 0; /* failure */
97     return fopen(fullpath, mode);
98 }
99
100 int yaz_is_abspath (const char *p)
101 {
102     if (*p == '/')
103         return 1;
104 #ifdef WIN32
105     if (*p == '\\')
106         return 1;
107     if (*p && p[1] == ':' && isalpha(*p))
108         return 1;
109 #endif
110     return 0;
111 }
112 /*
113  * Local variables:
114  * c-basic-offset: 4
115  * indent-tabs-mode: nil
116  * End:
117  * vim: shiftwidth=4 tabstop=8 expandtab
118  */
119