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