blob: fa4958c263bd4b7e1fccd899791317a555d6f8ad [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 *
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
20 */
21
Eric Andersenbc341901999-11-06 04:56:00 +000022#include <stdlib.h>
23#include <unistd.h>
24#include <errno.h>
25#include <string.h>
26#include <stdio.h>
27#include <mntent.h>
Eric Andersenc4cef5a2001-04-01 16:01:11 +000028#include "libbb.h"
Eric Andersenbc341901999-11-06 04:56:00 +000029
Eric Andersena62665b2004-10-08 08:57:35 +000030#define MTAB_MAX_ENTRIES 40
Eric Andersenbc341901999-11-06 04:56:00 +000031
Rob Landley6a6798b2005-08-10 20:35:54 +000032#ifdef CONFIG_FEATURE_MTAB_SUPPORT
Erik Andersene49d5ec2000-02-08 19:58:47 +000033void erase_mtab(const char *name)
Eric Andersenbc341901999-11-06 04:56:00 +000034{
Eric Andersena62665b2004-10-08 08:57:35 +000035 struct mntent entries[MTAB_MAX_ENTRIES];
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 int count = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 struct mntent *m;
Eric Andersenbc341901999-11-06 04:56:00 +000039
Eric Andersen0ecb54a1999-12-05 23:24:55 +000040 /* Check if reading the mtab file failed */
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 if (mountTable == 0
Eric Andersenaf4ac772001-02-01 22:43:49 +000042 /* Bummer. fall back on trying the /proc filesystem */
43 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 bb_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000045 return;
46 }
47
Eric Andersena62665b2004-10-08 08:57:35 +000048 while (((m = getmntent(mountTable)) != 0) && (count < MTAB_MAX_ENTRIES))
49 {
Erik Andersenfac10d72000-02-07 05:29:42 +000050 entries[count].mnt_fsname = strdup(m->mnt_fsname);
51 entries[count].mnt_dir = strdup(m->mnt_dir);
52 entries[count].mnt_type = strdup(m->mnt_type);
53 entries[count].mnt_opts = strdup(m->mnt_opts);
Eric Andersenbc341901999-11-06 04:56:00 +000054 entries[count].mnt_freq = m->mnt_freq;
55 entries[count].mnt_passno = m->mnt_passno;
56 count++;
57 }
58 endmntent(mountTable);
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 if ((mountTable = setmntent(bb_path_mtab_file, "w"))) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 int i;
Eric Andersenbc341901999-11-06 04:56:00 +000061
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 for (i = 0; i < count; i++) {
63 int result = (strcmp(entries[i].mnt_fsname, name) == 0
64 || strcmp(entries[i].mnt_dir, name) == 0);
65
66 if (result)
Eric Andersenbc341901999-11-06 04:56:00 +000067 continue;
68 else
69 addmntent(mountTable, &entries[i]);
70 }
71 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 } else if (errno != EROFS)
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 bb_perror_msg(bb_path_mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000074}
Rob Landley6a6798b2005-08-10 20:35:54 +000075#endif