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.
25 #include <pbd/mountpoint.h>
32 struct mntent_sorter {
33 bool operator() (const mntent *a, const mntent *b) {
34 return strcmp (a->mnt_dir, b->mnt_dir);
39 mountpoint (string path)
43 unsigned int maxmatch = 0;
44 unsigned int matchlen;
45 const char *cpath = path.c_str();
46 char best[PATH_MAX+1];
48 if ((mntf = setmntent ("/etc/mtab", "r")) == 0) {
54 while ((mnt = getmntent (mntf))) {
60 /* note: strcmp's semantics are not
61 strict enough to use for this.
64 while (cpath[n] && mnt->mnt_dir[n]) {
65 if (cpath[n] != mnt->mnt_dir[n]) {
72 if (cpath[matchlen] == '\0') {
79 if (matchlen > maxmatch) {
80 snprintf (best, sizeof(best), "%s", mnt->mnt_dir);
91 #else // !HAVE_GETMNTENT
93 #include <sys/param.h>
94 #include <sys/ucred.h>
95 #include <sys/mount.h>
98 mountpoint (string path)
100 struct statfs *mntbufp = 0;
102 unsigned int maxmatch = 0;
103 unsigned int matchlen;
104 const char *cpath = path.c_str();
105 char best[PATH_MAX+1];
107 if ((count = getmntinfo(&mntbufp, MNT_NOWAIT)) == 0) {
114 for (int i = 0; i < count; ++i) {
118 /* note: strcmp's semantics are not
119 strict enough to use for this.
122 while (cpath[n] && mntbufp[i].f_mntonname[n]) {
123 if (cpath[n] != mntbufp[i].f_mntonname[n]) {
130 if (cpath[matchlen] == '\0') {
131 snprintf(best, sizeof(best), "%s", mntbufp[i].f_mntonname);
137 if (matchlen > maxmatch) {
138 snprintf (best, sizeof(best), "%s", mntbufp[i].f_mntonname);
148 #endif // HAVE_GETMNTENT
150 #ifdef TEST_MOUNTPOINT
152 main (int argc, char *argv[])
154 printf ("mp of %s = %s\n", argv[1], mountpoint (argv[1]).c_str());
158 #endif // TEST_MOUNTPOINT