add usefun info on SIGINT handling peculiarities
[oweals/busybox.git] / libbb / mtab.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <mntent.h>
16 #include "libbb.h"
17
18 #define MTAB_MAX_ENTRIES 40
19
20 #ifdef CONFIG_FEATURE_MTAB_SUPPORT
21 void erase_mtab(const char *name)
22 {
23         struct mntent entries[MTAB_MAX_ENTRIES];
24         int count = 0;
25         FILE *mountTable = setmntent(bb_path_mtab_file, "r");
26         struct mntent *m;
27
28         /* Check if reading the mtab file failed */
29         if (mountTable == 0
30                         /* Bummer.  fall back on trying the /proc filesystem */
31                         && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
32                 bb_perror_msg(bb_path_mtab_file);
33                 return;
34         }
35
36         while (((m = getmntent(mountTable)) != 0) && (count < MTAB_MAX_ENTRIES))
37         {
38                 entries[count].mnt_fsname = strdup(m->mnt_fsname);
39                 entries[count].mnt_dir = strdup(m->mnt_dir);
40                 entries[count].mnt_type = strdup(m->mnt_type);
41                 entries[count].mnt_opts = strdup(m->mnt_opts);
42                 entries[count].mnt_freq = m->mnt_freq;
43                 entries[count].mnt_passno = m->mnt_passno;
44                 count++;
45         }
46         endmntent(mountTable);
47         if ((mountTable = setmntent(bb_path_mtab_file, "w"))) {
48                 int i;
49
50                 for (i = 0; i < count; i++) {
51                         int result = (strcmp(entries[i].mnt_fsname, name) == 0
52                                                   || strcmp(entries[i].mnt_dir, name) == 0);
53
54                         if (result)
55                                 continue;
56                         else
57                                 addmntent(mountTable, &entries[i]);
58                 }
59                 endmntent(mountTable);
60         } else if (errno != EROFS)
61                 bb_perror_msg(bb_path_mtab_file);
62 }
63 #endif