More comments on path.{c,h} functions
[yaz-moved-to-github.git] / src / tpath.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tpath.c,v 1.8 2006-04-26 10:01:31 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 #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 char *yaz_filepath_resolve(const char *fname, const char *path,
42                            const char *base, char *fullpath)
43 {
44     for(;;)
45     {
46         struct stat stat_buf;
47         const char *path_sep = 0;
48         size_t len = 0;
49         size_t slen = 0;
50         
51         *fullpath = '\0';
52         if (path)
53         {
54             /* somewhat dirty since we have to consider Windows
55              * drive letters..
56              */
57             if (strchr ("/\\.", *path))
58             {
59                 path_sep = strchr (path+1, ':');
60             }
61             else if (path[0] && path[1])
62                 path_sep = strchr (path+2, ':');
63             if (path_sep)
64                 len = path_sep - path;
65             else
66                 len = strlen(path);
67             if (!strchr ("/\\", *path) && base)
68             {
69                 strcpy (fullpath, base);
70                 slen = strlen(fullpath);
71                 fullpath[slen++] = '/';
72             }
73             memcpy (fullpath+slen, path, len);
74             slen += len;
75             if (!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