ubase/mountpoint.c

103 lines
1.6 KiB
C
Raw Normal View History

2013-08-16 10:00:43 +00:00
/* See LICENSE file for copyright and license details. */
2014-06-30 18:03:41 +00:00
#include <sys/stat.h>
#include <sys/sysmacros.h>
2014-06-30 18:03:41 +00:00
#include <sys/types.h>
2014-02-15 18:34:06 +00:00
#include <mntent.h>
2013-08-16 10:00:43 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-06-30 18:03:41 +00:00
#include <unistd.h>
2013-08-16 10:00:43 +00:00
#include "util.h"
static void
usage(void)
{
2013-09-03 12:24:08 +00:00
eprintf("usage: %s [-dqx] target\n", argv0);
2013-08-16 10:00:43 +00:00
}
int
main(int argc, char *argv[])
{
2015-05-10 10:17:47 +00:00
int dflag = 0, qflag = 0, xflag = 0;
int ret = 0;
2014-02-15 18:34:06 +00:00
struct mntent *me = NULL;
FILE *fp;
struct stat st1, st2;
2013-08-16 10:00:43 +00:00
ARGBEGIN {
2013-09-01 17:24:34 +00:00
case 'd':
dflag = 1;
break;
2015-05-10 10:17:47 +00:00
case 'q':
qflag = 1;
break;
2013-09-03 12:24:08 +00:00
case 'x':
xflag = 1;
break;
2013-08-16 10:00:43 +00:00
default:
usage();
} ARGEND;
if (argc < 1)
usage();
2015-05-10 12:16:58 +00:00
if (stat(argv[0], &st1) < 0) {
if (qflag)
return 1;
2013-08-29 18:05:09 +00:00
eprintf("stat %s:", argv[0]);
2015-05-10 12:16:58 +00:00
}
2013-08-29 18:05:09 +00:00
2013-09-03 12:24:08 +00:00
if (xflag) {
2015-05-10 12:16:58 +00:00
if (!S_ISBLK(st1.st_mode)) {
if (qflag)
return 1;
2013-09-03 12:24:08 +00:00
eprintf("stat: %s: not a block device\n",
argv[0]);
2015-05-10 12:16:58 +00:00
}
2013-09-03 12:24:08 +00:00
printf("%u:%u\n", major(st1.st_rdev),
minor(st1.st_rdev));
2014-10-02 22:45:25 +00:00
return 0;
2013-09-03 12:24:08 +00:00
}
2015-05-10 12:16:58 +00:00
if (!S_ISDIR(st1.st_mode)) {
if (qflag)
return 1;
2013-09-03 12:24:08 +00:00
eprintf("stat %s: not a directory\n", argv[0]);
2015-05-10 12:16:58 +00:00
}
2013-08-29 18:05:09 +00:00
2013-09-01 17:24:34 +00:00
if (dflag) {
printf("%u:%u\n", major(st1.st_dev),
minor(st1.st_dev));
2014-10-02 22:45:25 +00:00
return 0;
2013-09-01 17:24:34 +00:00
}
2014-02-15 18:34:06 +00:00
fp = setmntent("/proc/mounts", "r");
2015-05-10 12:16:58 +00:00
if (!fp) {
if (qflag)
return 1;
2014-02-15 18:34:06 +00:00
eprintf("setmntent %s:", "/proc/mounts");
2015-05-10 12:16:58 +00:00
}
2014-02-15 18:34:06 +00:00
while ((me = getmntent(fp)) != NULL) {
2015-05-10 12:16:58 +00:00
if (stat(me->mnt_dir, &st2) < 0) {
if (qflag)
return 1;
2014-02-15 18:34:06 +00:00
eprintf("stat %s:", me->mnt_dir);
2015-05-10 12:16:58 +00:00
}
if (st1.st_dev == st2.st_dev &&
st1.st_ino == st2.st_ino)
2013-08-16 10:00:43 +00:00
break;
}
2014-02-15 18:34:06 +00:00
endmntent(fp);
2014-01-25 22:34:05 +00:00
2014-02-15 18:34:06 +00:00
if (me == NULL)
2013-08-16 10:00:43 +00:00
ret = 1;
if (!qflag)
printf("%s %s a mountpoint\n", argv[0],
!ret ? "is" : "is not");
2013-08-16 10:00:43 +00:00
return ret;
}