2007-04-05 11:18:42 +00:00
|
|
|
/*
|
|
|
|
fstabinfo.c
|
|
|
|
Gets information about /etc/fstab.
|
|
|
|
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
*/
|
|
|
|
|
2007-04-17 12:44:32 +00:00
|
|
|
#define APPLET "fstabinfo"
|
|
|
|
|
2007-04-05 11:18:42 +00:00
|
|
|
#include <errno.h>
|
2007-04-13 15:20:10 +00:00
|
|
|
#include <getopt.h>
|
2007-04-05 11:18:42 +00:00
|
|
|
#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-25 22:21:28 +00:00
|
|
|
#define START_ENT fp = setmntent ("/etc/fstab", "r");
|
2007-04-05 11:18:42 +00:00
|
|
|
#define GET_ENT getmntent (fp)
|
2007-09-25 22:21:28 +00:00
|
|
|
#define GET_ENT_FILE(_name) getmntfile (_name)
|
2007-04-05 11:18:42 +00:00
|
|
|
#define END_ENT endmntent (fp)
|
|
|
|
#define ENT_DEVICE(_ent) ent->mnt_fsname
|
|
|
|
#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-25 22:21:28 +00:00
|
|
|
#define START_ENT
|
2007-04-05 11:18:42 +00:00
|
|
|
#define GET_ENT getfsent ()
|
|
|
|
#define GET_ENT_FILE(_name) getfsfile (_name)
|
|
|
|
#define END_ENT endfsent ()
|
|
|
|
#define ENT_DEVICE(_ent) ent->fs_spec
|
|
|
|
#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 16:05:56 +00:00
|
|
|
#include "builtins.h"
|
2007-04-05 11:18:42 +00:00
|
|
|
#include "einfo.h"
|
2007-09-25 22:21:28 +00:00
|
|
|
#include "rc.h"
|
|
|
|
#include "strlist.h"
|
2007-04-05 11:18:42 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_GETMNTENT
|
2007-09-25 22:21:28 +00:00
|
|
|
static struct mntent *getmntfile (const char *file)
|
2007-04-05 11:18:42 +00:00
|
|
|
{
|
2007-09-25 22:21:28 +00:00
|
|
|
struct mntent *ent = NULL;
|
|
|
|
FILE *fp;
|
2007-04-05 11:18:42 +00:00
|
|
|
|
2007-09-25 22:21:28 +00:00
|
|
|
START_ENT;
|
2007-04-11 12:44:47 +00:00
|
|
|
while ((ent = getmntent (fp)))
|
|
|
|
if (strcmp (file, ent->mnt_dir) == 0)
|
2007-09-25 22:21:28 +00:00
|
|
|
break;
|
|
|
|
END_ENT;
|
|
|
|
|
|
|
|
return (ent);
|
2007-04-05 11:18:42 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-06-28 15:44:14 +00:00
|
|
|
#include "_usage.h"
|
2007-09-25 12:22:48 +00:00
|
|
|
#define getoptstring "m:o:p:t:" getoptstring_COMMON
|
2007-04-17 12:44:32 +00:00
|
|
|
static struct option longopts[] = {
|
2007-09-25 22:21:28 +00:00
|
|
|
{ "mountcmd", 0, NULL, 'm'},
|
|
|
|
{ "options", 0, NULL, 'o'},
|
|
|
|
{ "passno", 0, NULL, 'p'},
|
2007-09-25 12:22:48 +00:00
|
|
|
{ "fstype", 1, NULL, 't'},
|
2007-06-28 15:44:14 +00:00
|
|
|
longopts_COMMON
|
2007-04-17 12:44:32 +00:00
|
|
|
{ NULL, 0, NULL, 0}
|
|
|
|
};
|
2007-09-25 16:21:38 +00:00
|
|
|
static const char * const longopts_help[] = {
|
|
|
|
"Construct the arguments to give to mount",
|
|
|
|
"Extract the options field",
|
|
|
|
"Extract the pass number field",
|
|
|
|
"Extract the file system type",
|
|
|
|
longopts_help_COMMON
|
|
|
|
};
|
2007-04-17 12:44:32 +00:00
|
|
|
#include "_usage.c"
|
|
|
|
|
2007-09-25 22:21:28 +00:00
|
|
|
#define OUTPUT_FILE (1 << 1)
|
|
|
|
#define OUTPUT_MOUNTCMD (1 << 2)
|
|
|
|
#define OUTPUT_OPTIONS (1 << 3)
|
|
|
|
#define OUTPUT_PASSNO (1 << 4)
|
|
|
|
|
2007-07-31 16:05:56 +00:00
|
|
|
int fstabinfo (int argc, char **argv)
|
2007-04-05 11:18:42 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_GETMNTENT
|
2007-04-11 12:44:47 +00:00
|
|
|
FILE *fp;
|
|
|
|
struct mntent *ent;
|
2007-04-05 11:18:42 +00:00
|
|
|
#else
|
2007-04-11 12:44:47 +00:00
|
|
|
struct fstab *ent;
|
2007-04-05 11:18:42 +00:00
|
|
|
#endif
|
2007-09-25 22:45:57 +00:00
|
|
|
int result = EXIT_SUCCESS;
|
2007-04-11 12:44:47 +00:00
|
|
|
char *token;
|
2007-09-25 22:21:28 +00:00
|
|
|
int i;
|
2007-05-14 12:24:18 +00:00
|
|
|
int opt;
|
2007-09-25 22:21:28 +00:00
|
|
|
int output = OUTPUT_FILE;
|
|
|
|
char **files = NULL;
|
|
|
|
char *file;
|
|
|
|
bool filtered = false;
|
2007-04-13 15:20:10 +00:00
|
|
|
|
2007-05-14 12:24:18 +00:00
|
|
|
while ((opt = getopt_long (argc, argv, getoptstring,
|
|
|
|
longopts, (int *) 0)) != -1)
|
2007-04-13 15:20:10 +00:00
|
|
|
{
|
2007-05-14 12:24:18 +00:00
|
|
|
switch (opt) {
|
2007-04-13 15:20:10 +00:00
|
|
|
case 'm':
|
2007-09-25 22:21:28 +00:00
|
|
|
output = OUTPUT_MOUNTCMD;
|
2007-04-13 15:20:10 +00:00
|
|
|
break;
|
2007-04-11 12:44:47 +00:00
|
|
|
|
2007-04-13 15:20:10 +00:00
|
|
|
case 'o':
|
2007-09-25 22:21:28 +00:00
|
|
|
output = OUTPUT_OPTIONS;
|
2007-04-13 15:20:10 +00:00
|
|
|
break;
|
2007-04-11 12:44:47 +00:00
|
|
|
|
2007-04-13 15:20:10 +00:00
|
|
|
case 'p':
|
2007-04-19 15:31:01 +00:00
|
|
|
switch (optarg[0]) {
|
|
|
|
case '=':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
2007-09-25 22:21:28 +00:00
|
|
|
if (sscanf (optarg + 1, "%d", &i) != 1)
|
2007-04-19 15:31:01 +00:00
|
|
|
eerrorx ("%s: invalid passno %s", argv[0], optarg + 1);
|
|
|
|
|
2007-09-25 22:21:28 +00:00
|
|
|
filtered = true;
|
|
|
|
START_ENT;
|
2007-04-19 15:31:01 +00:00
|
|
|
while ((ent = GET_ENT)) {
|
2007-09-25 22:21:28 +00:00
|
|
|
if (((optarg[0] == '=' && i == ENT_PASS (ent)) ||
|
|
|
|
(optarg[0] == '<' && i > ENT_PASS (ent)) ||
|
|
|
|
(optarg[0] == '>' && i < ENT_PASS (ent))) &&
|
2007-04-19 15:31:01 +00:00
|
|
|
strcmp (ENT_FILE (ent), "none") != 0)
|
2007-09-25 22:21:28 +00:00
|
|
|
rc_strlist_add (&files, ENT_FILE (ent));
|
2007-04-19 15:31:01 +00:00
|
|
|
}
|
2007-09-25 22:21:28 +00:00
|
|
|
END_ENT;
|
2007-04-19 15:31:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-09-25 22:21:28 +00:00
|
|
|
rc_strlist_add (&files, optarg);
|
|
|
|
output = OUTPUT_PASSNO;
|
2007-04-19 15:31:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2007-04-17 12:44:32 +00:00
|
|
|
|
2007-09-25 12:22:48 +00:00
|
|
|
case 't':
|
2007-09-25 22:21:28 +00:00
|
|
|
filtered = true;
|
|
|
|
while ((token = strsep (&optarg, ","))) {
|
|
|
|
START_ENT;
|
2007-09-25 12:22:48 +00:00
|
|
|
while ((ent = GET_ENT))
|
|
|
|
if (strcmp (token, ENT_TYPE (ent)) == 0)
|
2007-09-25 22:21:28 +00:00
|
|
|
rc_strlist_add (&files, ENT_FILE (ent));
|
|
|
|
END_ENT;
|
|
|
|
}
|
2007-09-25 12:22:48 +00:00
|
|
|
break;
|
|
|
|
|
2007-06-28 15:44:14 +00:00
|
|
|
case_RC_COMMON_GETOPT
|
2007-04-11 12:44:47 +00:00
|
|
|
}
|
2007-09-25 22:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (optind < argc)
|
|
|
|
rc_strlist_add (&files, argv[optind++]);
|
2007-04-11 12:44:47 +00:00
|
|
|
|
2007-09-25 22:45:57 +00:00
|
|
|
if (! files && ! filtered) {
|
2007-09-25 22:21:28 +00:00
|
|
|
START_ENT;
|
|
|
|
while ((ent = GET_ENT))
|
|
|
|
rc_strlist_add (&files, ENT_FILE (ent));
|
2007-04-11 12:44:47 +00:00
|
|
|
END_ENT;
|
|
|
|
|
2007-09-25 22:21:28 +00:00
|
|
|
if (! files)
|
|
|
|
eerrorx ("%s: emtpy fstab", argv[0]);
|
2007-04-11 12:44:47 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 22:21:28 +00:00
|
|
|
/* Ensure we always display something */
|
|
|
|
START_ENT;
|
|
|
|
STRLIST_FOREACH (files, file, i) {
|
|
|
|
if (! (ent = GET_ENT_FILE (file))) {
|
|
|
|
result = EXIT_FAILURE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-09-25 22:45:57 +00:00
|
|
|
/* No point in outputting if quiet */
|
|
|
|
if (rc_is_env ("RC_QUIET", "yes"))
|
|
|
|
continue;
|
|
|
|
|
2007-09-25 22:21:28 +00:00
|
|
|
switch (output) {
|
|
|
|
case OUTPUT_MOUNTCMD:
|
|
|
|
printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
|
|
|
|
ENT_DEVICE (ent), ENT_FILE (ent));
|
|
|
|
break;
|
|
|
|
|
|
|
|
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 01:47:02 +00:00
|
|
|
|
2007-09-25 22:21:28 +00:00
|
|
|
rc_strlist_free (files);
|
2007-04-11 12:44:47 +00:00
|
|
|
exit (result);
|
2007-04-05 11:18:42 +00:00
|
|
|
}
|