2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
fstabinfo.c
|
|
|
|
Gets information about /etc/fstab.
|
|
|
|
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
*/
|
|
|
|
|
2007-04-17 18:14:32 +05:30
|
|
|
#define APPLET "fstabinfo"
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <errno.h>
|
2007-04-13 20:50:10 +05:30
|
|
|
#include <getopt.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <libgen.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* Yay for linux and it's non liking of POSIX functions.
|
|
|
|
Okay, we could use getfsent but the man page says use getmntent instead
|
|
|
|
AND we don't have getfsent on uclibc or dietlibc for some odd reason. */
|
|
|
|
#ifdef __linux__
|
|
|
|
#define HAVE_GETMNTENT
|
|
|
|
#include <mntent.h>
|
2007-09-26 03:51:28 +05:30
|
|
|
#define START_ENT fp = setmntent ("/etc/fstab", "r");
|
2007-04-05 16:48:42 +05:30
|
|
|
#define GET_ENT getmntent (fp)
|
2007-09-26 03:51:28 +05:30
|
|
|
#define GET_ENT_FILE(_name) getmntfile (_name)
|
2007-04-05 16:48:42 +05:30
|
|
|
#define END_ENT endmntent (fp)
|
2007-10-11 15:19:54 +05:30
|
|
|
#define ENT_BLOCKDEVICE(_ent) ent->mnt_fsname
|
2007-04-05 16:48:42 +05:30
|
|
|
#define ENT_FILE(_ent) ent->mnt_dir
|
|
|
|
#define ENT_TYPE(_ent) ent->mnt_type
|
|
|
|
#define ENT_OPTS(_ent) ent->mnt_opts
|
|
|
|
#define ENT_PASS(_ent) ent->mnt_passno
|
|
|
|
#else
|
|
|
|
#define HAVE_GETFSENT
|
|
|
|
#include <fstab.h>
|
2007-09-26 03:51:28 +05:30
|
|
|
#define START_ENT
|
2007-04-05 16:48:42 +05:30
|
|
|
#define GET_ENT getfsent ()
|
|
|
|
#define GET_ENT_FILE(_name) getfsfile (_name)
|
|
|
|
#define END_ENT endfsent ()
|
2007-10-11 15:19:54 +05:30
|
|
|
#define ENT_BLOCKDEVICE(_ent) ent->fs_spec
|
2007-04-05 16:48:42 +05:30
|
|
|
#define ENT_TYPE(_ent) ent->fs_vfstype
|
|
|
|
#define ENT_FILE(_ent) ent->fs_file
|
|
|
|
#define ENT_OPTS(_ent) ent->fs_mntops
|
|
|
|
#define ENT_PASS(_ent) ent->fs_passno
|
|
|
|
#endif
|
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
#include "builtins.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
#include "einfo.h"
|
2007-09-26 03:51:28 +05:30
|
|
|
#include "rc.h"
|
|
|
|
#include "strlist.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
#ifdef HAVE_GETMNTENT
|
2007-09-26 03:51:28 +05:30
|
|
|
static struct mntent *getmntfile (const char *file)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-09-26 03:51:28 +05:30
|
|
|
struct mntent *ent = NULL;
|
|
|
|
FILE *fp;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-26 03:51:28 +05:30
|
|
|
START_ENT;
|
2007-04-11 18:14:47 +05:30
|
|
|
while ((ent = getmntent (fp)))
|
|
|
|
if (strcmp (file, ent->mnt_dir) == 0)
|
2007-09-26 03:51:28 +05:30
|
|
|
break;
|
|
|
|
END_ENT;
|
|
|
|
|
|
|
|
return (ent);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
#include "_usage.h"
|
2007-10-11 15:19:54 +05:30
|
|
|
#define getoptstring "bmop:t:" getoptstring_COMMON
|
2007-04-17 18:14:32 +05:30
|
|
|
static struct option longopts[] = {
|
2007-10-11 15:19:54 +05:30
|
|
|
{ "blockdevice", 0, NULL, 'b' },
|
|
|
|
{ "options", 0, NULL, 'o' },
|
|
|
|
{ "passno", 1, NULL, 'p' },
|
|
|
|
{ "fstype", 1, NULL, 't' },
|
2007-06-28 21:14:14 +05:30
|
|
|
longopts_COMMON
|
2007-04-17 18:14:32 +05:30
|
|
|
};
|
2007-09-25 21:51:38 +05:30
|
|
|
static const char * const longopts_help[] = {
|
2007-10-11 15:19:54 +05:30
|
|
|
"Extract the block device",
|
2007-09-25 21:51:38 +05:30
|
|
|
"Extract the options field",
|
2007-10-11 15:19:54 +05:30
|
|
|
"Extract or query the pass number field",
|
|
|
|
"List entries with matching file system type",
|
2007-09-25 21:51:38 +05:30
|
|
|
longopts_help_COMMON
|
|
|
|
};
|
2007-04-17 18:14:32 +05:30
|
|
|
#include "_usage.c"
|
|
|
|
|
2007-09-26 03:51:28 +05:30
|
|
|
#define OUTPUT_FILE (1 << 1)
|
|
|
|
#define OUTPUT_OPTIONS (1 << 3)
|
|
|
|
#define OUTPUT_PASSNO (1 << 4)
|
2007-10-11 15:19:54 +05:30
|
|
|
#define OUTPUT_BLOCKDEV (1 << 5)
|
2007-09-26 03:51:28 +05:30
|
|
|
|
2007-07-31 21:35:56 +05:30
|
|
|
int fstabinfo (int argc, char **argv)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
|
|
|
#ifdef HAVE_GETMNTENT
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
struct mntent *ent;
|
2007-04-05 16:48:42 +05:30
|
|
|
#else
|
2007-04-11 18:14:47 +05:30
|
|
|
struct fstab *ent;
|
2007-04-05 16:48:42 +05:30
|
|
|
#endif
|
2007-09-26 04:15:57 +05:30
|
|
|
int result = EXIT_SUCCESS;
|
2007-04-11 18:14:47 +05:30
|
|
|
char *token;
|
2007-09-26 03:51:28 +05:30
|
|
|
int i;
|
2007-05-14 17:54:18 +05:30
|
|
|
int opt;
|
2007-09-26 03:51:28 +05:30
|
|
|
int output = OUTPUT_FILE;
|
|
|
|
char **files = NULL;
|
|
|
|
char *file;
|
|
|
|
bool filtered = false;
|
2007-04-13 20:50:10 +05:30
|
|
|
|
2007-05-14 17:54:18 +05:30
|
|
|
while ((opt = getopt_long (argc, argv, getoptstring,
|
|
|
|
longopts, (int *) 0)) != -1)
|
2007-04-13 20:50:10 +05:30
|
|
|
{
|
2007-05-14 17:54:18 +05:30
|
|
|
switch (opt) {
|
2007-10-11 15:19:54 +05:30
|
|
|
case 'b':
|
|
|
|
output = OUTPUT_BLOCKDEV;
|
|
|
|
break;
|
2007-04-13 20:50:10 +05:30
|
|
|
case 'o':
|
2007-09-26 03:51:28 +05:30
|
|
|
output = OUTPUT_OPTIONS;
|
2007-04-13 20:50:10 +05:30
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-13 20:50:10 +05:30
|
|
|
case 'p':
|
2007-04-19 21:01:01 +05:30
|
|
|
switch (optarg[0]) {
|
|
|
|
case '=':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
2007-09-26 03:51:28 +05:30
|
|
|
if (sscanf (optarg + 1, "%d", &i) != 1)
|
2007-04-19 21:01:01 +05:30
|
|
|
eerrorx ("%s: invalid passno %s", argv[0], optarg + 1);
|
|
|
|
|
2007-09-26 03:51:28 +05:30
|
|
|
filtered = true;
|
|
|
|
START_ENT;
|
2007-04-19 21:01:01 +05:30
|
|
|
while ((ent = GET_ENT)) {
|
2007-09-26 03:51:28 +05:30
|
|
|
if (((optarg[0] == '=' && i == ENT_PASS (ent)) ||
|
|
|
|
(optarg[0] == '<' && i > ENT_PASS (ent)) ||
|
|
|
|
(optarg[0] == '>' && i < ENT_PASS (ent))) &&
|
2007-04-19 21:01:01 +05:30
|
|
|
strcmp (ENT_FILE (ent), "none") != 0)
|
2007-09-26 03:51:28 +05:30
|
|
|
rc_strlist_add (&files, ENT_FILE (ent));
|
2007-04-19 21:01:01 +05:30
|
|
|
}
|
2007-09-26 03:51:28 +05:30
|
|
|
END_ENT;
|
2007-04-19 21:01:01 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-09-26 03:51:28 +05:30
|
|
|
rc_strlist_add (&files, optarg);
|
|
|
|
output = OUTPUT_PASSNO;
|
2007-04-19 21:01:01 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2007-04-17 18:14:32 +05:30
|
|
|
|
2007-09-25 17:52:48 +05:30
|
|
|
case 't':
|
2007-09-26 03:51:28 +05:30
|
|
|
filtered = true;
|
|
|
|
while ((token = strsep (&optarg, ","))) {
|
|
|
|
START_ENT;
|
2007-09-25 17:52:48 +05:30
|
|
|
while ((ent = GET_ENT))
|
|
|
|
if (strcmp (token, ENT_TYPE (ent)) == 0)
|
2007-09-26 03:51:28 +05:30
|
|
|
rc_strlist_add (&files, ENT_FILE (ent));
|
|
|
|
END_ENT;
|
|
|
|
}
|
2007-09-25 17:52:48 +05:30
|
|
|
break;
|
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
case_RC_COMMON_GETOPT
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-09-26 03:51:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
while (optind < argc)
|
|
|
|
rc_strlist_add (&files, argv[optind++]);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-26 04:15:57 +05:30
|
|
|
if (! files && ! filtered) {
|
2007-09-26 03:51:28 +05:30
|
|
|
START_ENT;
|
|
|
|
while ((ent = GET_ENT))
|
|
|
|
rc_strlist_add (&files, ENT_FILE (ent));
|
2007-04-11 18:14:47 +05:30
|
|
|
END_ENT;
|
|
|
|
|
2007-09-26 03:51:28 +05:30
|
|
|
if (! files)
|
|
|
|
eerrorx ("%s: emtpy fstab", argv[0]);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2007-09-26 03:51:28 +05:30
|
|
|
/* Ensure we always display something */
|
|
|
|
START_ENT;
|
|
|
|
STRLIST_FOREACH (files, file, i) {
|
|
|
|
if (! (ent = GET_ENT_FILE (file))) {
|
|
|
|
result = EXIT_FAILURE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-26 04:15:57 +05:30
|
|
|
/* No point in outputting if quiet */
|
2007-09-26 12:35:33 +05:30
|
|
|
if (rc_env_bool ("RC_QUIET"))
|
2007-09-26 04:15:57 +05:30
|
|
|
continue;
|
|
|
|
|
2007-09-26 03:51:28 +05:30
|
|
|
switch (output) {
|
2007-10-11 15:19:54 +05:30
|
|
|
case OUTPUT_BLOCKDEV:
|
|
|
|
printf ("%s\n", ENT_BLOCKDEVICE (ent));
|
|
|
|
break;
|
2007-09-26 03:51:28 +05:30
|
|
|
case OUTPUT_OPTIONS:
|
|
|
|
printf ("%s\n", ENT_OPTS (ent));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OUTPUT_FILE:
|
|
|
|
printf ("%s\n", file);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OUTPUT_PASSNO:
|
|
|
|
printf ("%d\n", ENT_PASS (ent));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_ENT;
|
2007-09-25 07:17:02 +05:30
|
|
|
|
2007-09-26 03:51:28 +05:30
|
|
|
rc_strlist_free (files);
|
2007-04-11 18:14:47 +05:30
|
|
|
exit (result);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|