newly created files for use in recording appear in a .stubs folder, and are moved...
[ardour2.git] / libs / pbd / mountpoint.cc
blob3cc94638a335023019d829dad5b1523d61573acb
1 /*
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.
18 $Id$
21 #include <cstdio>
22 #include <cstring>
23 #include <string>
24 #include <cstring>
25 #include <limits.h>
27 #include "pbd/mountpoint.h"
29 using std::string;
31 #ifdef WAF_BUILD
32 #include "libpbd-config.h"
33 #endif
35 #ifdef HAVE_GETMNTENT
36 #include <mntent.h>
38 struct mntent_sorter {
39 bool operator() (const mntent *a, const mntent *b) {
40 return strcmp (a->mnt_dir, b->mnt_dir);
44 string
45 mountpoint (string path)
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];
54 if ((mntf = setmntent ("/etc/mtab", "r")) == 0) {
55 return "";
58 best[0] = '\0';
60 while ((mnt = getmntent (mntf))) {
61 unsigned int n;
63 n = 0;
64 matchlen = 0;
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]) {
72 break;
74 matchlen++;
75 n++;
78 if (cpath[matchlen] == '\0') {
80 endmntent (mntf);
81 return mnt->mnt_dir;
83 } else {
85 if (matchlen > maxmatch) {
86 snprintf (best, sizeof(best), "%s", mnt->mnt_dir);
87 maxmatch = matchlen;
92 endmntent (mntf);
94 return best;
97 #else // !HAVE_GETMNTENT
99 #include <sys/param.h>
100 #include <sys/ucred.h>
101 #include <sys/mount.h>
103 string
104 mountpoint (string path)
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];
113 if ((count = getmntinfo(&mntbufp, MNT_NOWAIT)) == 0) {
114 free(mntbufp);
115 return "\0";
118 best[0] = '\0';
120 for (int i = 0; i < count; ++i) {
121 unsigned int n = 0;
122 matchlen = 0;
124 /* note: strcmp's semantics are not
125 strict enough to use for this.
128 while (cpath[n] && mntbufp[i].f_mntonname[n]) {
129 if (cpath[n] != mntbufp[i].f_mntonname[n]) {
130 break;
132 matchlen++;
133 n++;
136 if (cpath[matchlen] == '\0') {
137 snprintf(best, sizeof(best), "%s", mntbufp[i].f_mntonname);
138 free(mntbufp);
139 return best;
141 } else {
143 if (matchlen > maxmatch) {
144 snprintf (best, sizeof(best), "%s", mntbufp[i].f_mntonname);
145 maxmatch = matchlen;
150 free(mntbufp);
152 return best;
154 #endif // HAVE_GETMNTENT
156 #ifdef TEST_MOUNTPOINT
158 main (int argc, char *argv[])
160 printf ("mp of %s = %s\n", argv[1], mountpoint (argv[1]).c_str());
161 exit (0);
164 #endif // TEST_MOUNTPOINT