2 Copyright (C) 2002 Paul Davis
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.
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.
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.
27 #include "pbd/mountpoint.h"
32 #include "libpbd-config.h"
38 struct mntent_sorter {
39 bool operator() (const mntent *a, const mntent *b) {
40 return strcmp (a->mnt_dir, b->mnt_dir);
45 mountpoint (string path)
49 unsigned int maxmatch = 0;
50 unsigned int matchlen;
51 const char *cpath = path.c_str();
52 char best[PATH_MAX+1];
54 if ((mntf = setmntent ("/etc/mtab", "r")) == 0) {
60 while ((mnt = getmntent (mntf))) {
66 /* note: strcmp's semantics are not
67 strict enough to use for this.
70 while (cpath[n] && mnt->mnt_dir[n]) {
71 if (cpath[n] != mnt->mnt_dir[n]) {
78 if (cpath[matchlen] == '\0') {
85 if (matchlen > maxmatch) {
86 snprintf (best, sizeof(best), "%s", mnt->mnt_dir);
97 #elif defined(PLATFORM_WINDOWS)
100 mountpoint (string path)
102 /* this function is currently only called from 'old_peak_path()'
103 * via find_broken_peakfile() - only relevant for loading pre
104 * libsndfile Ardour 2.0 sessions.
107 return ""; // TODO ... if needed
110 #else // !HAVE_GETMNTENT
112 #include <sys/param.h>
113 #include <sys/ucred.h>
114 #include <sys/mount.h>
117 mountpoint (string path)
119 #if defined(__NetBSD__)
120 struct statvfs *mntbufp = 0;
122 struct statfs *mntbufp = 0;
125 unsigned int maxmatch = 0;
126 unsigned int matchlen;
127 const char *cpath = path.c_str();
128 char best[PATH_MAX+1];
130 /* From the manpage, under "BUGS" : "The memory allocated by getmntinfo() cannot be free(3)'d by the
133 Thus: we do NOT try to free memory allocated by getmntinfo()
136 if ((count = getmntinfo(&mntbufp, MNT_NOWAIT)) == 0) {
142 for (int i = 0; i < count; ++i) {
146 /* note: strcmp's semantics are not
147 strict enough to use for this.
150 while (cpath[n] && mntbufp[i].f_mntonname[n]) {
151 if (cpath[n] != mntbufp[i].f_mntonname[n]) {
158 if (cpath[matchlen] == '\0') {
159 snprintf(best, sizeof(best), "%s", mntbufp[i].f_mntonname);
164 if (matchlen > maxmatch) {
165 snprintf (best, sizeof(best), "%s", mntbufp[i].f_mntonname);
173 #endif // HAVE_GETMNTENT
175 #ifdef TEST_MOUNTPOINT
177 main (int argc, char *argv[])
179 printf ("mp of %s = %s\n", argv[1], mountpoint (argv[1]).c_str());
183 #endif // TEST_MOUNTPOINT
185 #else // COMPILER_MSVC
186 const char* pbd_mountpoint = "pbd/msvc/mountpoint.cc takes precedence over this file";
187 #endif // COMPILER_MSVC