blob: 18386efb5acb578ba2f6d734b0e52ee116c4d152 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbdfd0d72001-10-24 05:00:29 +00002/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenbdfd0d72001-10-24 05:00:29 +00006 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 */
9
Eric Andersenbc341901999-11-06 04:56:00 +000010#include <mntent.h>
Eric Andersenc4cef5a2001-04-01 16:01:11 +000011#include "libbb.h"
Eric Andersenbc341901999-11-06 04:56:00 +000012
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000013#if ENABLE_FEATURE_MTAB_SUPPORT
Erik Andersene49d5ec2000-02-08 19:58:47 +000014void erase_mtab(const char *name)
Eric Andersenbc341901999-11-06 04:56:00 +000015{
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000016 struct mntent *entries = NULL;
17 int i, count = 0;
18 FILE *mountTable;
Erik Andersene49d5ec2000-02-08 19:58:47 +000019 struct mntent *m;
Eric Andersenbc341901999-11-06 04:56:00 +000020
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000021 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 cad53642003-03-19 09:13:01 +000025 bb_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000026 return;
27 }
28
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000029 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 Andersenbc341901999-11-06 04:56:00 +000038 }
39 endmntent(mountTable);
Eric Andersenbc341901999-11-06 04:56:00 +000040
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000041 mountTable = setmntent(bb_path_mtab_file, "w");
42 if (mountTable) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000043 for (i = 0; i < count; i++) {
Denis Vlasenkof1a6c6b2006-11-27 17:29:09 +000044 if (strcmp(entries[i].mnt_fsname, name) != 0
45 && strcmp(entries[i].mnt_dir, name) != 0)
Eric Andersenbc341901999-11-06 04:56:00 +000046 addmntent(mountTable, &entries[i]);
47 }
48 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 } else if (errno != EROFS)
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 bb_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000051}
Rob Landley6a6798b2005-08-10 20:35:54 +000052#endif