Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 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 | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 10 | #include <mntent.h> |
Eric Andersen | c4cef5a | 2001-04-01 16:01:11 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 13 | #if ENABLE_FEATURE_MTAB_SUPPORT |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 14 | void erase_mtab(const char *name) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 15 | { |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 16 | struct mntent *entries = NULL; |
| 17 | int i, count = 0; |
| 18 | FILE *mountTable; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 19 | struct mntent *m; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 21 | mountTable = setmntent(bb_path_mtab_file, "r"); |
| 22 | /* Bummer. Fall back on trying the /proc filesystem */ |
| 23 | if (!mountTable) mountTable = setmntent("/proc/mounts", "r"); |
| 24 | if (!mountTable) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 26 | return; |
| 27 | } |
| 28 | |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 29 | while ((m = getmntent(mountTable)) != 0) { |
| 30 | i = count++; |
| 31 | entries = xrealloc(entries, count * sizeof(entries[0])); |
| 32 | entries[i].mnt_fsname = xstrdup(m->mnt_fsname); |
| 33 | entries[i].mnt_dir = xstrdup(m->mnt_dir); |
| 34 | entries[i].mnt_type = xstrdup(m->mnt_type); |
| 35 | entries[i].mnt_opts = xstrdup(m->mnt_opts); |
| 36 | entries[i].mnt_freq = m->mnt_freq; |
| 37 | entries[i].mnt_passno = m->mnt_passno; |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 38 | } |
| 39 | endmntent(mountTable); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 41 | mountTable = setmntent(bb_path_mtab_file, "w"); |
| 42 | if (mountTable) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | for (i = 0; i < count; i++) { |
Denis Vlasenko | f1a6c6b | 2006-11-27 17:29:09 +0000 | [diff] [blame] | 44 | if (strcmp(entries[i].mnt_fsname, name) != 0 |
| 45 | && strcmp(entries[i].mnt_dir, name) != 0) |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 46 | addmntent(mountTable, &entries[i]); |
| 47 | } |
| 48 | endmntent(mountTable); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | } else if (errno != EROFS) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | bb_perror_msg(bb_path_mtab_file); |
Eric Andersen | bc34190 | 1999-11-06 04:56:00 +0000 | [diff] [blame] | 51 | } |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 52 | #endif |