Merge remote-tracking branch 'remotes/origin/cairocanvas' into windows
[ardour.git] / libs / pbd / 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$
19 */
20 #ifndef  COMPILER_MSVC
21 #include <cstdio>
22 #include <cstring>
23 #include <string>
24 #include <cstring>
25 #include <limits.h>
26
27 #include "pbd/mountpoint.h"
28
29 using std::string;
30
31 #ifdef WAF_BUILD
32 #include "libpbd-config.h"
33 #endif
34
35 #ifdef 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 #elif defined(PLATFORM_WINDOWS)
98
99 string
100 mountpoint (string path)
101 {
102         // TODO ... if needed
103 }
104
105 #else // !HAVE_GETMNTENT
106
107 #include <sys/param.h>
108 #include <sys/ucred.h>
109 #include <sys/mount.h>
110
111 string
112 mountpoint (string path)
113 {
114         struct statfs *mntbufp = 0;
115         int count;
116         unsigned int maxmatch = 0;
117         unsigned int matchlen;
118         const char *cpath = path.c_str();
119         char best[PATH_MAX+1];
120         
121         /* From the manpage, under "BUGS" : "The memory allocated by getmntinfo() cannot be free(3)'d by the 
122            application."
123            
124            Thus: we do NOT try to free memory allocated by getmntinfo()
125         */
126
127         if ((count = getmntinfo(&mntbufp, MNT_NOWAIT)) == 0) {
128                 return "\0";
129         }
130
131         best[0] = '\0';
132
133         for (int i = 0; i < count; ++i) {
134                 unsigned int n = 0;
135                 matchlen = 0;
136
137                 /* note: strcmp's semantics are not 
138                    strict enough to use for this.
139                 */
140
141                 while (cpath[n] && mntbufp[i].f_mntonname[n]) {
142                         if (cpath[n] != mntbufp[i].f_mntonname[n]) {
143                                 break;
144                         }
145                         matchlen++;
146                         n++;
147                 }
148
149                 if (cpath[matchlen] == '\0') {
150                         snprintf(best, sizeof(best), "%s", mntbufp[i].f_mntonname);
151                         return best;
152
153                 } else {
154
155                         if (matchlen > maxmatch) {
156                                 snprintf (best, sizeof(best), "%s", mntbufp[i].f_mntonname);
157                                 maxmatch = matchlen;
158                         }
159                 }
160         }
161         
162         return best;
163 }
164 #endif // HAVE_GETMNTENT
165
166 #ifdef TEST_MOUNTPOINT
167                 
168 main (int argc, char *argv[])
169 {
170         printf ("mp of %s = %s\n", argv[1], mountpoint (argv[1]).c_str());
171         exit (0);
172 }
173
174 #endif // TEST_MOUNTPOINT
175
176 #else  // COMPILER_MSVC
177         const char* pbd_mountpoint = "pbd/msvc/mountpoint.cc takes precedence over this file";
178 #endif // COMPILER_MSVC