Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
Eric Andersen | 08ff8a4 | 2001-03-23 17:02:05 +0000 | [diff] [blame] | 11 | #include <string.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
| 13 | |
| 14 | |
| 15 | #include <mntent.h> |
| 16 | /* |
| 17 | * Given a block device, find the mount table entry if that block device |
| 18 | * is mounted. |
| 19 | * |
| 20 | * Given any other file (or directory), find the mount table entry for its |
| 21 | * filesystem. |
| 22 | */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 23 | struct mntent *find_mount_point(const char *name, const char *table) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 24 | { |
| 25 | struct stat s; |
| 26 | dev_t mountDevice; |
| 27 | FILE *mountTable; |
| 28 | struct mntent *mountEntry; |
| 29 | |
| 30 | if (stat(name, &s) != 0) |
| 31 | return 0; |
| 32 | |
| 33 | if ((s.st_mode & S_IFMT) == S_IFBLK) |
| 34 | mountDevice = s.st_rdev; |
| 35 | else |
| 36 | mountDevice = s.st_dev; |
| 37 | |
| 38 | |
Bernhard Reutner-Fischer | 7ca61b6 | 2006-01-15 14:04:57 +0000 | [diff] [blame] | 39 | if ((mountTable = setmntent(table ? table : bb_path_mtab_file, "r")) == 0) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 40 | return 0; |
| 41 | |
| 42 | while ((mountEntry = getmntent(mountTable)) != 0) { |
Bernhard Reutner-Fischer | 7ca61b6 | 2006-01-15 14:04:57 +0000 | [diff] [blame] | 43 | |
| 44 | if(strcmp(name, mountEntry->mnt_dir) == 0 |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 45 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ |
| 46 | break; |
| 47 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ |
| 48 | break; |
| 49 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ |
| 50 | break; |
| 51 | } |
| 52 | endmntent(mountTable); |
| 53 | return mountEntry; |
| 54 | } |