diff options
Diffstat (limited to 'libbb/find_mount_point.c')
-rw-r--r-- | libbb/find_mount_point.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/libbb/find_mount_point.c b/libbb/find_mount_point.c new file mode 100644 index 000000000..cb00b9806 --- /dev/null +++ b/libbb/find_mount_point.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | * | ||
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | #include "libbb.h" | ||
11 | #include <mntent.h> | ||
12 | |||
13 | /* | ||
14 | * Given a block device, find the mount table entry if that block device | ||
15 | * is mounted. | ||
16 | * | ||
17 | * Given any other file (or directory), find the mount table entry for its | ||
18 | * filesystem. | ||
19 | */ | ||
20 | struct mntent *find_mount_point(const char *name, const char *table) | ||
21 | { | ||
22 | struct stat s; | ||
23 | dev_t mountDevice; | ||
24 | FILE *mountTable; | ||
25 | struct mntent *mountEntry; | ||
26 | |||
27 | if (stat(name, &s) != 0) | ||
28 | return 0; | ||
29 | |||
30 | if ((s.st_mode & S_IFMT) == S_IFBLK) | ||
31 | mountDevice = s.st_rdev; | ||
32 | else | ||
33 | mountDevice = s.st_dev; | ||
34 | |||
35 | |||
36 | mountTable = setmntent(table ? table : bb_path_mtab_file, "r"); | ||
37 | if (!mountTable) | ||
38 | return 0; | ||
39 | |||
40 | while ((mountEntry = getmntent(mountTable)) != 0) { | ||
41 | if (strcmp(name, mountEntry->mnt_dir) == 0 | ||
42 | || strcmp(name, mountEntry->mnt_fsname) == 0 | ||
43 | ) { /* String match. */ | ||
44 | break; | ||
45 | } | ||
46 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ | ||
47 | break; | ||
48 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ | ||
49 | break; | ||
50 | } | ||
51 | endmntent(mountTable); | ||
52 | return mountEntry; | ||
53 | } | ||