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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
Eric Andersen | 08ff8a4 | 2001-03-23 17:02:05 +0000 | [diff] [blame] | 23 | #include <string.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 24 | #include "libbb.h" |
| 25 | |
| 26 | |
| 27 | #include <mntent.h> |
| 28 | /* |
| 29 | * Given a block device, find the mount table entry if that block device |
| 30 | * is mounted. |
| 31 | * |
| 32 | * Given any other file (or directory), find the mount table entry for its |
| 33 | * filesystem. |
| 34 | */ |
| 35 | extern struct mntent *find_mount_point(const char *name, const char *table) |
| 36 | { |
| 37 | struct stat s; |
| 38 | dev_t mountDevice; |
| 39 | FILE *mountTable; |
| 40 | struct mntent *mountEntry; |
| 41 | |
| 42 | if (stat(name, &s) != 0) |
| 43 | return 0; |
| 44 | |
| 45 | if ((s.st_mode & S_IFMT) == S_IFBLK) |
| 46 | mountDevice = s.st_rdev; |
| 47 | else |
| 48 | mountDevice = s.st_dev; |
| 49 | |
| 50 | |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 51 | if ((mountTable = setmntent(table ? : bb_path_mtab_file, "r")) == 0) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 52 | return 0; |
| 53 | |
| 54 | while ((mountEntry = getmntent(mountTable)) != 0) { |
| 55 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 56 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ |
| 57 | break; |
| 58 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ |
| 59 | break; |
| 60 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ |
| 61 | break; |
| 62 | } |
| 63 | endmntent(mountTable); |
| 64 | return mountEntry; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /* END CODE */ |
| 69 | /* |
| 70 | Local Variables: |
| 71 | c-file-style: "linux" |
| 72 | c-basic-offset: 4 |
| 73 | tab-width: 4 |
| 74 | End: |
| 75 | */ |