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.
26 #include <pbd/mountpoint.h>
33 struct mntent_sorter {
34 bool operator() (const mntent *a, const mntent *b) {
35 return strcmp (a->mnt_dir, b->mnt_dir);
40 mountpoint (string path)
44 unsigned int maxmatch = 0;
45 unsigned int matchlen;
46 const char *cpath = path.c_str();
47 char best[PATH_MAX+1];
49 if ((mntf = setmntent ("/etc/mtab", "r")) == 0) {
55 while ((mnt = getmntent (mntf))) {
61 /* note: strcmp's semantics are not
62 strict enough to use for this.
65 while (cpath[n] && mnt->mnt_dir[n]) {
66 if (cpath[n] != mnt->mnt_dir[n]) {
73 if (cpath[matchlen] == '\0') {
80 if (matchlen > maxmatch) {
81 snprintf (best, sizeof(best), "%s", mnt->mnt_dir);
92 #else // !HAVE_GETMNTENT
94 #include <sys/param.h>
95 #include <sys/ucred.h>
96 #include <sys/mount.h>
99 mountpoint (string path)
101 struct statfs *mntbufp = 0;
103 unsigned int maxmatch = 0;
104 unsigned int matchlen;
105 const char *cpath = path.c_str();
106 char best[PATH_MAX+1];
108 if ((count = getmntinfo(&mntbufp, MNT_NOWAIT)) == 0) {
115 for (int i = 0; i < count; ++i) {
119 /* note: strcmp's semantics are not
120 strict enough to use for this.
123 while (cpath[n] && mntbufp[i].f_mntonname[n]) {
124 if (cpath[n] != mntbufp[i].f_mntonname[n]) {
131 if (cpath[matchlen] == '\0') {
132 snprintf(best, sizeof(best), "%s", mntbufp[i].f_mntonname);
138 if (matchlen > maxmatch) {
139 snprintf (best, sizeof(best), "%s", mntbufp[i].f_mntonname);
149 #endif // HAVE_GETMNTENT
151 #ifdef TEST_MOUNTPOINT
153 main (int argc, char *argv[])
155 printf ("mp of %s = %s\n", argv[1], mountpoint (argv[1]).c_str());
159 #endif // TEST_MOUNTPOINT