morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

mountpoint.c (1480B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/stat.h>
      3 #include <sys/types.h>
      4 
      5 #include <mntent.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <unistd.h>
     10 
     11 #include "util.h"
     12 
     13 static void
     14 usage(void)
     15 {
     16 	eprintf("usage: %s [-dqx] target\n", argv0);
     17 }
     18 
     19 int
     20 main(int argc, char *argv[])
     21 {
     22 	int qflag = 0, dflag = 0, xflag = 0;
     23 	struct mntent *me = NULL;
     24 	FILE *fp;
     25 	int ret = 0;
     26 	struct stat st1, st2;
     27 
     28 	ARGBEGIN {
     29 	case 'q':
     30 		qflag = 1;
     31 		break;
     32 	case 'd':
     33 		dflag = 1;
     34 		break;
     35 	case 'x':
     36 		xflag = 1;
     37 		break;
     38 	default:
     39 		usage();
     40 	} ARGEND;
     41 
     42 	if (argc < 1)
     43 		usage();
     44 
     45 	if (stat(argv[0], &st1) < 0)
     46 		eprintf("stat %s:", argv[0]);
     47 
     48 	if (xflag) {
     49 		if (!S_ISBLK(st1.st_mode))
     50 			eprintf("stat: %s: not a block device\n",
     51 				argv[0]);
     52 		printf("%u:%u\n", major(st1.st_rdev),
     53 		       minor(st1.st_rdev));
     54 		return 0;
     55 	}
     56 
     57 	if (!S_ISDIR(st1.st_mode))
     58 		eprintf("stat %s: not a directory\n", argv[0]);
     59 
     60 	if (dflag) {
     61 		printf("%u:%u\n", major(st1.st_dev),
     62 		       minor(st1.st_dev));
     63 		return 0;
     64 	}
     65 
     66 	fp = setmntent("/proc/mounts", "r");
     67 	if (!fp)
     68 		eprintf("setmntent %s:", "/proc/mounts");
     69 	while ((me = getmntent(fp)) != NULL) {
     70 		if (stat(me->mnt_dir, &st2) < 0)
     71 			eprintf("stat %s:", me->mnt_dir);
     72 		if (st1.st_dev == st2.st_dev &&
     73 		    st1.st_ino == st2.st_ino)
     74 			break;
     75 	}
     76 	endmntent(fp);
     77 
     78 	if (me == NULL)
     79 		ret = 1;
     80 
     81 	if (!qflag)
     82 		printf("%s %s a mountpoint\n", argv[0],
     83 		       !ret ? "is" : "is not");
     84 
     85 	return ret;
     86 }