/[baselayout]/trunk/src/fstabinfo.c
Gentoo

Contents of /trunk/src/fstabinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (show annotations) (download) (as text)
Thu Jun 28 15:44:14 2007 UTC (7 years, 6 months ago) by uberlord
File MIME type: text/x-csrc
File size: 3478 byte(s)
Add --nocolor to more programs
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 "einfo.h"
45
46 #ifdef HAVE_GETMNTENT
47 static struct mntent *getmntfile (FILE *fp, const char *file)
48 {
49 struct mntent *ent;
50
51 while ((ent = getmntent (fp)))
52 if (strcmp (file, ent->mnt_dir) == 0)
53 return (ent);
54
55 return (NULL);
56 }
57 #endif
58
59 #include "_usage.h"
60 #define getoptstring "f:m:o:p:" getoptstring_COMMON
61 static struct option longopts[] = {
62 { "fstype", 1, NULL, 'f'},
63 { "mountcmd", 1, NULL, 'm'},
64 { "opts", 1, NULL, 'o'},
65 { "passno", 1, NULL, 'p'},
66 longopts_COMMON
67 { NULL, 0, NULL, 0}
68 };
69 #include "_usage.c"
70
71 int main (int argc, char **argv)
72 {
73 #ifdef HAVE_GETMNTENT
74 FILE *fp;
75 struct mntent *ent;
76 #else
77 struct fstab *ent;
78 #endif
79 int result = EXIT_FAILURE;
80 char *token;
81 int n = 0;
82 int opt;
83
84 while ((opt = getopt_long (argc, argv, getoptstring,
85 longopts, (int *) 0)) != -1)
86 {
87 #ifdef HAVE_GETMNTENT
88 fp = setmntent ("/etc/fstab", "r");
89 #endif
90 switch (opt) {
91 case 'f':
92 while ((token = strsep (&optarg, ",")))
93 while ((ent = GET_ENT))
94 if (strcmp (token, ENT_TYPE (ent)) == 0)
95 printf ("%s\n", ENT_FILE (ent));
96 result = EXIT_SUCCESS;
97 break;
98
99 case 'm':
100 if ((ent = GET_ENT_FILE (optarg))) {
101 printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
102 ENT_DEVICE (ent), ENT_FILE (ent));
103 result = EXIT_SUCCESS;
104 }
105 break;
106
107 case 'o':
108 if ((ent = GET_ENT_FILE (optarg))) {
109 printf ("%s\n", ENT_OPTS (ent));
110 result = EXIT_SUCCESS;
111 }
112 break;
113
114 case 'p':
115 switch (optarg[0]) {
116 case '=':
117 case '<':
118 case '>':
119 if (sscanf (optarg + 1, "%d", &n) != 1)
120 eerrorx ("%s: invalid passno %s", argv[0], optarg + 1);
121
122 while ((ent = GET_ENT)) {
123 if (((optarg[0] == '=' && n == ENT_PASS (ent)) ||
124 (optarg[0] == '<' && n > ENT_PASS (ent)) ||
125 (optarg[0] == '>' && n < ENT_PASS (ent))) &&
126 strcmp (ENT_FILE (ent), "none") != 0)
127 {
128 printf ("%s\n", ENT_FILE (ent));
129 result = EXIT_SUCCESS;
130 }
131 }
132 break;
133
134 default:
135 if ((ent = GET_ENT_FILE (optarg))) {
136 printf ("%d\n", ENT_PASS (ent));
137 result = EXIT_SUCCESS;
138 }
139 break;
140 }
141 break;
142
143 case_RC_COMMON_GETOPT
144 }
145
146 END_ENT;
147
148 if (result != EXIT_SUCCESS)
149 break;
150 }
151
152 exit (result);
153 }

  ViewVC Help
Powered by ViewVC 1.1.20