1
/* check_mount() checks whether DEVICE is a mounted file system.
2
Returns 0 if the DEVICE is *not* mounted (which we consider a
3
successful outcome), and -1 if DEVICE is mounted or if the mount
4
status cannot be determined.
6
Derived from e2fsprogs/lib/ext2fs/ismounted.c
7
Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o,
12
#endif /* HAVE_CONFIG_H */
18
#endif /* HAVE_STDLIB_H */
22
#endif /* HAVE_STRING_H */
30
#define MOUNTS "/etc/mtab"
31
#define LINE_BUFFER_SIZE 256 /* Line buffer size for reading mtab */
33
int check_mount(const char *device)
38
dev_t file_dev = 0, file_rdev = 0;
41
f = setmntent(MOUNTS, "r");
43
fprintf(stderr, "Error: cannot open %s!", MOUNTS);
47
if (stat(device, &st_buf) == 0) {
48
if (S_ISBLK(st_buf.st_mode)) {
49
file_rdev = st_buf.st_rdev;
51
file_dev = st_buf.st_dev;
52
file_ino = st_buf.st_ino;
56
while ((mnt = getmntent(f)) != NULL) {
57
if (mnt->mnt_fsname[0] != '/')
59
if (strcmp(device, mnt->mnt_fsname) == 0)
61
if (stat(mnt->mnt_fsname, &st_buf) == 0) {
62
if (S_ISBLK(st_buf.st_mode)) {
63
if (file_rdev && (file_rdev == st_buf.st_rdev))
66
if (file_dev && ((file_dev == st_buf.st_dev) &&
67
(file_ino == st_buf.st_ino)))
74
return (mnt == NULL) ? 0 : -1;