2 * mountpoint(1) - see if a directory is a mountpoint
4 * This is libmount based reimplementation of the mountpoit(1)
5 * from sysvinit project.
8 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
9 * Written by Karel Zak <kzak@redhat.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it would be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
43 static char *dir_to_device(const char *spec
)
45 struct libmnt_table
*tb
= mnt_new_table_from_file("/proc/self/mountinfo");
52 fs
= mnt_table_find_target(tb
, spec
, MNT_ITER_BACKWARD
);
53 if (fs
&& mnt_fs_get_target(fs
))
54 res
= xstrdup(mnt_fs_get_source(fs
));
60 static int print_devno(const char *devname
, struct stat
*st
)
64 if (!st
&& stat(devname
, &stbuf
) == 0)
68 if (!S_ISBLK(st
->st_mode
)) {
70 warnx(_("%s: not a block device"), devname
);
73 printf("%u:%u\n", major(st
->st_rdev
), minor(st
->st_rdev
));
77 static void __attribute__((__noreturn__
)) usage(FILE *out
)
79 fputs(_("\nUsage:\n"), out
);
81 _(" %1$s [-qd] /path/to/directory\n"
82 " %1$s -x /dev/device\n"), program_invocation_short_name
);
84 fputs(_("\nOptions:\n"), out
);
85 fputs(_(" -q, --quiet quiet mode - don't print anything\n"
86 " -d, --fs-devno print maj:min device number of the filesystem\n"
87 " -x, --devno print maj:min device number of the block device\n"
88 " -h, --help this help\n"), out
);
90 fprintf(out
, _("\nFor more information see mountpoint(1).\n"));
92 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
95 int main(int argc
, char **argv
)
97 int c
, fs_devno
= 0, dev_devno
= 0, rc
= 0;
101 static const struct option longopts
[] = {
102 { "quiet", 0, 0, 'q' },
103 { "fs-devno", 0, 0, 'd' },
104 { "devno", 0, 0, 'x' },
105 { "help", 0, 0, 'h' },
109 setlocale(LC_ALL
, "");
110 bindtextdomain(PACKAGE
, LOCALEDIR
);
115 while ((c
= getopt_long(argc
, argv
, "qdxh", longopts
, NULL
)) != -1) {
136 if (optind
+ 1 != argc
)
139 spec
= argv
[optind
++];
141 if (stat(spec
, &st
)) {
143 err(EXIT_FAILURE
, "%s", spec
);
147 rc
= print_devno(spec
, &st
);
151 if (!S_ISDIR(st
.st_mode
)) {
153 errx(EXIT_FAILURE
, _("%s: not a directory"), spec
);
156 src
= dir_to_device(spec
);
159 printf(_("%s is not a mountpoint\n"), spec
);
163 rc
= print_devno(src
, NULL
);
165 printf(_("%s is a mountpoint\n"), spec
);
169 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;