- MIDI "recording" - rec region creation/drawing, actual MIDI region creation/view...
[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
21 #include <cstdio>
22 #include <string>
23 #include <limits.h>
24
25 #include <pbd/mountpoint.h>
26
27 using std::string;
28
29 #if HAVE_GETMNTENT
30 #include <mntent.h>
31
32 struct mntent_sorter {
33     bool operator() (const mntent *a, const mntent *b) {
34             return strcmp (a->mnt_dir, b->mnt_dir);
35     }
36 };
37
38 string
39 mountpoint (string path)
40 {
41         FILE *mntf;
42         mntent *mnt;
43         unsigned int maxmatch = 0;
44         unsigned int matchlen;
45         const char *cpath = path.c_str();
46         char best[PATH_MAX+1];
47         
48         if ((mntf = setmntent ("/etc/mtab", "r")) == 0) {
49                 return "";
50         }
51
52         best[0] = '\0';
53
54         while ((mnt = getmntent (mntf))) {
55                 unsigned int n;
56
57                 n = 0;
58                 matchlen = 0;
59
60                 /* note: strcmp's semantics are not 
61                    strict enough to use for this.
62                 */
63
64                 while (cpath[n] && mnt->mnt_dir[n]) {
65                         if (cpath[n] != mnt->mnt_dir[n]) {
66                                 break;
67                         }
68                         matchlen++;
69                         n++;
70                 }
71
72                 if (cpath[matchlen] == '\0') {
73
74                         endmntent (mntf);
75                         return mnt->mnt_dir;
76
77                 } else {
78
79                         if (matchlen > maxmatch) {
80                                 snprintf (best, sizeof(best), "%s", mnt->mnt_dir);
81                                 maxmatch = matchlen;
82                         }
83                 }
84         }
85
86         endmntent (mntf);
87
88         return best;
89 }
90
91 #else // !HAVE_GETMNTENT
92
93 #include <sys/param.h>
94 #include <sys/ucred.h>
95 #include <sys/mount.h>
96
97 string
98 mountpoint (string path)
99 {
100         struct statfs *mntbufp = 0;
101         int count;
102         unsigned int maxmatch = 0;
103         unsigned int matchlen;
104         const char *cpath = path.c_str();
105         char best[PATH_MAX+1];
106         
107         if ((count = getmntinfo(&mntbufp, MNT_NOWAIT)) == 0) {
108                 free(mntbufp);
109                 return "\0";
110         }
111
112         best[0] = '\0';
113
114         for (int i = 0; i < count; ++i) {
115                 unsigned int n = 0;
116                 matchlen = 0;
117
118                 /* note: strcmp's semantics are not 
119                    strict enough to use for this.
120                 */
121
122                 while (cpath[n] && mntbufp[i].f_mntonname[n]) {
123                         if (cpath[n] != mntbufp[i].f_mntonname[n]) {
124                                 break;
125                         }
126                         matchlen++;
127                         n++;
128                 }
129
130                 if (cpath[matchlen] == '\0') {
131                         snprintf(best, sizeof(best), "%s", mntbufp[i].f_mntonname);
132                         free(mntbufp);
133                         return best;
134
135                 } else {
136
137                         if (matchlen > maxmatch) {
138                                 snprintf (best, sizeof(best), "%s", mntbufp[i].f_mntonname);
139                                 maxmatch = matchlen;
140                         }
141                 }
142         }
143
144         free(mntbufp);
145         
146         return best;
147 }
148 #endif // HAVE_GETMNTENT
149
150 #ifdef TEST_MOUNTPOINT
151                 
152 main (int argc, char *argv[])
153 {
154         printf ("mp of %s = %s\n", argv[1], mountpoint (argv[1]).c_str());
155         exit (0);
156 }
157
158 #endif // TEST_MOUNTPOINT