diff options
Diffstat (limited to '')
-rw-r--r-- | findmount.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/findmount.c b/findmount.c new file mode 100644 index 000000000..26e28fcd3 --- /dev/null +++ b/findmount.c | |||
@@ -0,0 +1,46 @@ | |||
1 | #include "internal.h" | ||
2 | #include <stdio.h> | ||
3 | #include <mntent.h> | ||
4 | #include <sys/stat.h> | ||
5 | |||
6 | /* | ||
7 | * Given a block device, find the mount table entry if that block device | ||
8 | * is mounted. | ||
9 | * | ||
10 | * Given any other file (or directory), find the mount table entry for its | ||
11 | * filesystem. | ||
12 | */ | ||
13 | extern struct mntent * | ||
14 | findMountPoint(const char * name, const char * table) | ||
15 | { | ||
16 | struct stat s; | ||
17 | dev_t mountDevice; | ||
18 | FILE * mountTable; | ||
19 | struct mntent * mountEntry; | ||
20 | |||
21 | if ( stat(name, &s) != 0 ) | ||
22 | return 0; | ||
23 | |||
24 | if ( (s.st_mode & S_IFMT) == S_IFBLK ) | ||
25 | mountDevice = s.st_rdev; | ||
26 | else | ||
27 | mountDevice = s.st_dev; | ||
28 | |||
29 | |||
30 | if ( (mountTable = setmntent(table, "r")) == 0 ) | ||
31 | return 0; | ||
32 | |||
33 | while ( (mountEntry = getmntent(mountTable)) != 0 ) { | ||
34 | if ( strcmp(name, mountEntry->mnt_dir) == 0 | ||
35 | || strcmp(name, mountEntry->mnt_fsname) == 0 ) /* String match. */ | ||
36 | break; | ||
37 | if ( stat(mountEntry->mnt_fsname, &s) == 0 | ||
38 | && s.st_rdev == mountDevice ) /* Match the device. */ | ||
39 | break; | ||
40 | if ( stat(mountEntry->mnt_dir, &s) == 0 | ||
41 | && s.st_dev == mountDevice ) /* Match the directory's mount point. */ | ||
42 | break; | ||
43 | } | ||
44 | endmntent(mountTable); | ||
45 | return mountEntry; | ||
46 | } | ||