Merge branch 'master' into windows
[ardour.git] / libs / pbd / msvc / mountpoint.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: mountpoint.cc 3920 2008-10-11 12:34:46Z paul $
19 */
20
21 #ifdef COMPILER_MSVC
22
23 /* TODO - Still to be implemented */
24
25 #include <cstdio>
26 #include <cstring>
27 #include <string>
28 #include <cstring>
29 #include <limits.h>
30
31 #include <pbd/mountpoint.h>
32
33 using std::string;
34
35 #if HAVE_GETMNTENT
36 #include <mntent.h>
37
38 struct mntent_sorter {
39     bool operator() (const mntent *a, const mntent *b) {
40             return strcmp (a->mnt_dir, b->mnt_dir);
41     }
42 };
43
44 string
45 mountpoint (string path)
46 {
47         FILE *mntf;
48         mntent *mnt;
49         unsigned int maxmatch = 0;
50         unsigned int matchlen;
51         const char *cpath = path.c_str();
52         char best[PATH_MAX+1];
53
54         if ((mntf = setmntent ("/etc/mtab", "r")) == 0) {
55                 return "";
56         }
57
58         best[0] = '\0';
59
60         while ((mnt = getmntent (mntf))) {
61                 unsigned int n;
62
63                 n = 0;
64                 matchlen = 0;
65
66                 /* note: strcmp's semantics are not
67                    strict enough to use for this.
68                 */
69
70                 while (cpath[n] && mnt->mnt_dir[n]) {
71                         if (cpath[n] != mnt->mnt_dir[n]) {
72                                 break;
73                         }
74                         matchlen++;
75                         n++;
76                 }
77
78                 if (cpath[matchlen] == '\0') {
79
80                         endmntent (mntf);
81                         return mnt->mnt_dir;
82
83                 } else {
84
85                         if (matchlen > maxmatch) {
86                                 snprintf (best, sizeof(best), "%s", mnt->mnt_dir);
87                                 maxmatch = matchlen;
88                         }
89                 }
90         }
91
92         endmntent (mntf);
93
94         return best;
95 }
96
97 #else // !HAVE_GETMNTENT
98
99 string
100 mountpoint (string path)
101 {
102 return "";
103
104 /*  // The rest is commented out temporarily by JE - 30-11-2009
105     // (I think this must be the implementation for MacOS).
106         struct statfs *mntbufp = 0;
107         int count;
108         unsigned int maxmatch = 0;
109         unsigned int matchlen;
110         const char *cpath = path.c_str();
111         char best[PATH_MAX+1];
112
113         if ((count = getmntinfo(&mntbufp, MNT_NOWAIT)) == 0) {
114                 free(mntbufp);
115                 return "\0";
116         }
117
118         best[0] = '\0';
119
120         for (int i = 0; i < count; ++i) {
121                 unsigned int n = 0;
122                 matchlen = 0;
123
124                 // note: strcmp's semantics are not
125                 // strict enough to use for this.
126
127                 while (cpath[n] && mntbufp[i].f_mntonname[n]) {
128                         if (cpath[n] != mntbufp[i].f_mntonname[n]) {
129                                 break;
130                         }
131                         matchlen++;
132                         n++;
133                 }
134
135                 if (cpath[matchlen] == '\0') {
136                         snprintf(best, sizeof(best), "%s", mntbufp[i].f_mntonname);
137                         free(mntbufp);
138                         return best;
139
140                 } else {
141
142                         if (matchlen > maxmatch) {
143                                 snprintf (best, sizeof(best), "%s", mntbufp[i].f_mntonname);
144                                 maxmatch = matchlen;
145                         }
146                 }
147         }
148
149         return best;
150 */
151 }
152 #endif // HAVE_GETMNTENT
153
154 #ifdef TEST_MOUNTPOINT
155
156 main (int argc, char *argv[])
157 {
158         printf ("mp of %s = %s\n", argv[1], mountpoint (argv[1]).c_str());
159         exit (0);
160 }
161
162 #endif // TEST_MOUNTPOINT
163
164 #else  // !COMPILER_MSVC
165         const char* pbd_mountpoint = "original pbd/mountpoint.cc takes precedence over this file";
166 #endif // COMPILER_MSVC