1 /* mountpoint.c - Check if a directory is a mountpoint.
3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
5 USE_MOUNTPOINT(NEWTOY(mountpoint, "<1qdx[-dx]", TOYFLAG_BIN))
11 usage: mountpoint [-q] [-d] directory
12 mountpoint [-q] [-x] device
14 -q Be quiet, return zero if directory is a mountpoint
15 -d Print major/minor device number of the directory
16 -x Print major/minor device number of the block device
19 #define FOR_mountpoint
22 static void die(char *gripe)
24 if (!(toys.optflags & FLAG_q)) printf("%s: not a %s\n", *toys.optargs, gripe);
30 void mountpoint_main(void)
33 char *arg = *toys.optargs;
34 int quiet = toys.optflags & FLAG_q;
36 if (lstat(arg, &st1)) perror_exit_raw(arg);
38 if (toys.optflags & FLAG_x) {
39 if (S_ISBLK(st1.st_mode)) {
41 printf("%u:%u\n", dev_major(st1.st_rdev), dev_minor(st1.st_rdev));
48 // TODO: Ignore the fact a file can be a mountpoint for --bind mounts.
49 if (!S_ISDIR(st1.st_mode)) die("directory");
51 arg = xmprintf("%s/..", arg);
53 if (CFG_TOYBOX_FREE) free(arg);
55 // If the device is different, it's a mount point. If the device _and_
56 // inode are the same, it's probably "/". This misses --bind mounts from
57 // elsewhere in the same filesystem, but so does the other one and in the
58 // absence of a spec I guess that's the expected behavior?
59 toys.exitval = !(st1.st_dev != st2.st_dev || st1.st_ino == st2.st_ino);
60 if (toys.optflags & FLAG_d)
61 printf("%u:%u\n", dev_major(st1.st_dev), dev_minor(st1.st_dev));
63 printf("%s is %sa mountpoint\n", *toys.optargs, toys.exitval ? "not " : "");