| 1 |
/* |
| 2 |
fstabinfo.c |
| 3 |
Gets information about /etc/fstab. |
| 4 |
|
| 5 |
Copyright 2007 Gentoo Foundation |
| 6 |
*/ |
| 7 |
|
| 8 |
#define APPLET "fstabinfo" |
| 9 |
|
| 10 |
#include <errno.h> |
| 11 |
#include <getopt.h> |
| 12 |
#include <libgen.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
/* Yay for linux and it's non liking of POSIX functions. |
| 18 |
Okay, we could use getfsent but the man page says use getmntent instead |
| 19 |
AND we don't have getfsent on uclibc or dietlibc for some odd reason. */ |
| 20 |
#ifdef __linux__ |
| 21 |
#define HAVE_GETMNTENT |
| 22 |
#include <mntent.h> |
| 23 |
#define GET_ENT getmntent (fp) |
| 24 |
#define GET_ENT_FILE(_name) getmntfile (fp, _name) |
| 25 |
#define END_ENT endmntent (fp) |
| 26 |
#define ENT_DEVICE(_ent) ent->mnt_fsname |
| 27 |
#define ENT_FILE(_ent) ent->mnt_dir |
| 28 |
#define ENT_TYPE(_ent) ent->mnt_type |
| 29 |
#define ENT_OPTS(_ent) ent->mnt_opts |
| 30 |
#define ENT_PASS(_ent) ent->mnt_passno |
| 31 |
#else |
| 32 |
#define HAVE_GETFSENT |
| 33 |
#include <fstab.h> |
| 34 |
#define GET_ENT getfsent () |
| 35 |
#define GET_ENT_FILE(_name) getfsfile (_name) |
| 36 |
#define END_ENT endfsent () |
| 37 |
#define ENT_DEVICE(_ent) ent->fs_spec |
| 38 |
#define ENT_TYPE(_ent) ent->fs_vfstype |
| 39 |
#define ENT_FILE(_ent) ent->fs_file |
| 40 |
#define ENT_OPTS(_ent) ent->fs_mntops |
| 41 |
#define ENT_PASS(_ent) ent->fs_passno |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "builtins.h" |
| 45 |
#include "einfo.h" |
| 46 |
|
| 47 |
#ifdef HAVE_GETMNTENT |
| 48 |
static struct mntent *getmntfile (FILE *fp, const char *file) |
| 49 |
{ |
| 50 |
struct mntent *ent; |
| 51 |
|
| 52 |
while ((ent = getmntent (fp))) |
| 53 |
if (strcmp (file, ent->mnt_dir) == 0) |
| 54 |
return (ent); |
| 55 |
|
| 56 |
return (NULL); |
| 57 |
} |
| 58 |
#endif |
| 59 |
|
| 60 |
#include "_usage.h" |
| 61 |
#define getoptstring "f:m:o:p:" getoptstring_COMMON |
| 62 |
static struct option longopts[] = { |
| 63 |
{ "fstype", 1, NULL, 'f'}, |
| 64 |
{ "mountcmd", 1, NULL, 'm'}, |
| 65 |
{ "options", 1, NULL, 'o'}, |
| 66 |
{ "passno", 1, NULL, 'p'}, |
| 67 |
longopts_COMMON |
| 68 |
{ NULL, 0, NULL, 0} |
| 69 |
}; |
| 70 |
#include "_usage.c" |
| 71 |
|
| 72 |
int fstabinfo (int argc, char **argv) |
| 73 |
{ |
| 74 |
#ifdef HAVE_GETMNTENT |
| 75 |
FILE *fp; |
| 76 |
struct mntent *ent; |
| 77 |
#else |
| 78 |
struct fstab *ent; |
| 79 |
#endif |
| 80 |
int result = EXIT_FAILURE; |
| 81 |
char *token; |
| 82 |
int n = 0; |
| 83 |
int opt; |
| 84 |
|
| 85 |
while ((opt = getopt_long (argc, argv, getoptstring, |
| 86 |
longopts, (int *) 0)) != -1) |
| 87 |
{ |
| 88 |
#ifdef HAVE_GETMNTENT |
| 89 |
fp = setmntent ("/etc/fstab", "r"); |
| 90 |
#endif |
| 91 |
switch (opt) { |
| 92 |
case 'f': |
| 93 |
while ((token = strsep (&optarg, ","))) |
| 94 |
while ((ent = GET_ENT)) |
| 95 |
if (strcmp (token, ENT_TYPE (ent)) == 0) |
| 96 |
printf ("%s\n", ENT_FILE (ent)); |
| 97 |
result = EXIT_SUCCESS; |
| 98 |
break; |
| 99 |
|
| 100 |
case 'm': |
| 101 |
if ((ent = GET_ENT_FILE (optarg))) { |
| 102 |
printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent), |
| 103 |
ENT_DEVICE (ent), ENT_FILE (ent)); |
| 104 |
result = EXIT_SUCCESS; |
| 105 |
} |
| 106 |
break; |
| 107 |
|
| 108 |
case 'o': |
| 109 |
if ((ent = GET_ENT_FILE (optarg))) { |
| 110 |
printf ("%s\n", ENT_OPTS (ent)); |
| 111 |
result = EXIT_SUCCESS; |
| 112 |
} |
| 113 |
break; |
| 114 |
|
| 115 |
case 'p': |
| 116 |
switch (optarg[0]) { |
| 117 |
case '=': |
| 118 |
case '<': |
| 119 |
case '>': |
| 120 |
if (sscanf (optarg + 1, "%d", &n) != 1) |
| 121 |
eerrorx ("%s: invalid passno %s", argv[0], optarg + 1); |
| 122 |
|
| 123 |
while ((ent = GET_ENT)) { |
| 124 |
if (((optarg[0] == '=' && n == ENT_PASS (ent)) || |
| 125 |
(optarg[0] == '<' && n > ENT_PASS (ent)) || |
| 126 |
(optarg[0] == '>' && n < ENT_PASS (ent))) && |
| 127 |
strcmp (ENT_FILE (ent), "none") != 0) |
| 128 |
{ |
| 129 |
printf ("%s\n", ENT_FILE (ent)); |
| 130 |
result = EXIT_SUCCESS; |
| 131 |
} |
| 132 |
} |
| 133 |
break; |
| 134 |
|
| 135 |
default: |
| 136 |
if ((ent = GET_ENT_FILE (optarg))) { |
| 137 |
printf ("%d\n", ENT_PASS (ent)); |
| 138 |
result = EXIT_SUCCESS; |
| 139 |
} |
| 140 |
break; |
| 141 |
} |
| 142 |
break; |
| 143 |
|
| 144 |
case_RC_COMMON_GETOPT |
| 145 |
} |
| 146 |
|
| 147 |
END_ENT; |
| 148 |
|
| 149 |
if (result != EXIT_SUCCESS) |
| 150 |
break; |
| 151 |
} |
| 152 |
|
| 153 |
if (result != EXIT_SUCCESS && argc == optind) |
| 154 |
fprintf (stderr, "%s: no arguments specified\n", argv[0]); |
| 155 |
|
| 156 |
exit (result); |
| 157 |
} |