openrc/src/fstabinfo.c

150 lines
3.3 KiB
C
Raw Normal View History

/*
fstabinfo.c
Gets information about /etc/fstab.
Copyright 2007 Gentoo Foundation
*/
#include <errno.h>
2007-04-13 20:50:10 +05:30
#include <getopt.h>
#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>
#define GET_ENT getmntent (fp)
#define GET_ENT_FILE(_name) getmntfile (fp, _name)
#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>
#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
#include "einfo.h"
#ifdef HAVE_GETMNTENT
static struct mntent *getmntfile (FILE *fp, const char *file)
{
2007-04-11 18:14:47 +05:30
struct mntent *ent;
2007-04-11 18:14:47 +05:30
while ((ent = getmntent (fp)))
if (strcmp (file, ent->mnt_dir) == 0)
return (ent);
2007-04-11 18:14:47 +05:30
return (NULL);
}
#endif
int main (int argc, char **argv)
{
#ifdef HAVE_GETMNTENT
2007-04-11 18:14:47 +05:30
FILE *fp;
struct mntent *ent;
#else
2007-04-11 18:14:47 +05:30
struct fstab *ent;
#endif
2007-04-11 18:14:47 +05:30
int result = EXIT_FAILURE;
char *token;
int n = 0;
2007-04-13 20:50:10 +05:30
char c;
static struct option longopts[] = {
{ "fstype", 1, NULL, 'f'},
{ "mountcmd", 1, NULL, 'm'},
{ "opts", 1, NULL, 'o'},
{ "passno", 1, NULL, 'p'},
{ NULL, 0, NULL, 0}
};
while ((c = getopt_long (argc, argv, "f:m:o:p:",
longopts, (int *) 0)) != -1)
{
#ifdef HAVE_GETMNTENT
2007-04-11 18:14:47 +05:30
fp = setmntent ("/etc/fstab", "r");
#endif
2007-04-13 20:50:10 +05:30
switch (c) {
case 'f':
while ((token = strsep (&optarg, ",")))
while ((ent = GET_ENT))
if (strcmp (token, ENT_TYPE (ent)) == 0)
printf ("%s\n", ENT_FILE (ent));
result = EXIT_SUCCESS;
break;
2007-04-13 20:50:10 +05:30
case 'm':
if ((ent = GET_ENT_FILE (optarg))) {
printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
ENT_DEVICE (ent), ENT_FILE (ent));
result = EXIT_SUCCESS;
}
break;
2007-04-11 18:14:47 +05:30
2007-04-13 20:50:10 +05:30
case 'o':
if ((ent = GET_ENT_FILE (optarg))) {
printf ("%s\n", ENT_OPTS (ent));
result = EXIT_SUCCESS;
}
break;
2007-04-11 18:14:47 +05:30
2007-04-13 20:50:10 +05:30
case 'p':
switch (optarg[0]) {
2007-04-11 18:14:47 +05:30
case '=':
case '<':
case '>':
2007-04-13 20:50:10 +05:30
if (sscanf (optarg + 1, "%d", &n) != 1)
eerrorx ("%s: invalid passno %s", argv[0], optarg + 1);
2007-04-11 18:14:47 +05:30
while ((ent = GET_ENT)) {
2007-04-13 20:50:10 +05:30
if (((optarg[0] == '=' && n == ENT_PASS (ent)) ||
(optarg[0] == '<' && n > ENT_PASS (ent)) ||
(optarg[0] == '>' && n < ENT_PASS (ent))) &&
2007-04-11 18:14:47 +05:30
strcmp (ENT_FILE (ent), "none") != 0)
2007-04-13 20:50:10 +05:30
{
2007-04-11 18:14:47 +05:30
printf ("%s\n", ENT_FILE (ent));
2007-04-13 20:50:10 +05:30
result = EXIT_SUCCESS;
}
2007-04-11 18:14:47 +05:30
}
2007-04-13 20:50:10 +05:30
break;
2007-04-11 18:14:47 +05:30
default:
2007-04-13 20:50:10 +05:30
if ((ent = GET_ENT_FILE (optarg))) {
printf ("%d\n", ENT_PASS (ent));
result = EXIT_SUCCESS;
}
break;
2007-04-11 18:14:47 +05:30
}
2007-04-13 20:50:10 +05:30
default:
END_ENT;
exit (EXIT_FAILURE);
2007-04-11 18:14:47 +05:30
}
END_ENT;
2007-04-13 20:50:10 +05:30
if (result != EXIT_SUCCESS)
2007-04-11 18:14:47 +05:30
break;
}
exit (result);
}