WIN32 fixes for drive specifications.
[yaz-moved-to-github.git] / util / tpath.c
1 /*
2  * Copyright (c) 1995-2000, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: tpath.c,v $
7  * Revision 1.5  2000-12-05 19:03:19  adam
8  * WIN32 fixes for drive specifications.
9  *
10  * Revision 1.4  2000/02/29 13:44:55  adam
11  * Check for config.h (currently not generated).
12  *
13  * Revision 1.3  1999/11/30 13:47:12  adam
14  * Improved installation. Moved header files to include/yaz.
15  *
16  * Revision 1.2  1996/10/29 13:36:26  adam
17  * Added header.
18  *
19  * Revision 1.1  1995/11/01 16:35:00  quinn
20  * Making data1 look for tables in data1_tabpath
21  *
22  *
23  */
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <yaz/tpath.h>
31
32 FILE *yaz_path_fopen(const char *path, const char *name, const char *mode)
33 {
34     char spath[512];
35
36     for(;;)
37     {
38         FILE *f;
39
40         const char *path_sep = 0;
41         size_t len = 0;
42         
43         if (path)
44         {
45             /* somewhat dirty since we have to consider Windows
46              * drive letters..
47              */
48             if (strchr ("/\\.", *path))
49             {
50                 path_sep = strchr (path+1, ':');
51             }
52             else if (path[0] && path[1])
53                 path_sep = strchr (path+2, ':');
54             if (path_sep)
55                 len = path_sep - path;
56             else
57                 len = strlen(path);
58             if (len > 255)
59                 len = 255;
60             memcpy (spath, path, len);
61             if (!strchr("/\\", spath[len-1]))
62             {
63                 strcpy (spath+len, "/");
64                 len++;
65             }
66         }
67         sprintf (spath+len, "%.255s", name);
68         if ((f = fopen(spath, mode)))
69             return f;
70
71         if (!path_sep)
72             break;
73         path = path_sep+1;
74     }
75     return 0;
76 }