| 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #ifdef CONFIG_GETMNTENT |
| 5 | #include <mntent.h> |
| 6 | |
| 7 | #define MTAB "/etc/mtab" |
| 8 | |
| 9 | int device_is_mounted(const char *dev) |
| 10 | { |
| 11 | FILE *mtab; |
| 12 | struct mntent *mnt; |
| 13 | int ret = 0; |
| 14 | |
| 15 | mtab = setmntent(MTAB, "r"); |
| 16 | if (!mtab) |
| 17 | return 0; |
| 18 | |
| 19 | while ((mnt = getmntent(mtab)) != NULL) { |
| 20 | if (!mnt->mnt_fsname) |
| 21 | continue; |
| 22 | if (!strcmp(mnt->mnt_fsname, dev)) { |
| 23 | ret = 1; |
| 24 | break; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | endmntent(mtab); |
| 29 | return ret; |
| 30 | } |
| 31 | |
| 32 | #elif defined(CONFIG_GETMNTINFO) |
| 33 | /* for BSDs */ |
| 34 | #include <sys/param.h> |
| 35 | #include <sys/mount.h> |
| 36 | |
| 37 | int device_is_mounted(const char *dev) |
| 38 | { |
| 39 | struct statfs *st; |
| 40 | int i, ret; |
| 41 | |
| 42 | ret = getmntinfo(&st, MNT_NOWAIT); |
| 43 | if (ret <= 0) |
| 44 | return 0; |
| 45 | |
| 46 | for (i = 0; i < ret; i++) { |
| 47 | if (!strcmp(st[i].f_mntfromname, dev)) |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | #else |
| 55 | /* others */ |
| 56 | |
| 57 | int device_is_mounted(const char *dev) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | #endif |