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

Contents of /trunk/src/fstabinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2577 - (show annotations) (download) (as text)
Wed Apr 11 12:44:47 2007 UTC (12 years ago) by uberlord
File MIME type: text/x-csrc
File size: 3215 byte(s)
Cuddle up to LKML C style
1 /*
2 fstabinfo.c
3 Gets information about /etc/fstab.
4
5 Copyright 2007 Gentoo Foundation
6 */
7
8 #include <errno.h>
9 #include <libgen.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 /* Yay for linux and it's non liking of POSIX functions.
15 Okay, we could use getfsent but the man page says use getmntent instead
16 AND we don't have getfsent on uclibc or dietlibc for some odd reason. */
17 #ifdef __linux__
18 #define HAVE_GETMNTENT
19 #include <mntent.h>
20 #define GET_ENT getmntent (fp)
21 #define GET_ENT_FILE(_name) getmntfile (fp, _name)
22 #define END_ENT endmntent (fp)
23 #define ENT_DEVICE(_ent) ent->mnt_fsname
24 #define ENT_FILE(_ent) ent->mnt_dir
25 #define ENT_TYPE(_ent) ent->mnt_type
26 #define ENT_OPTS(_ent) ent->mnt_opts
27 #define ENT_PASS(_ent) ent->mnt_passno
28 #else
29 #define HAVE_GETFSENT
30 #include <fstab.h>
31 #define GET_ENT getfsent ()
32 #define GET_ENT_FILE(_name) getfsfile (_name)
33 #define END_ENT endfsent ()
34 #define ENT_DEVICE(_ent) ent->fs_spec
35 #define ENT_TYPE(_ent) ent->fs_vfstype
36 #define ENT_FILE(_ent) ent->fs_file
37 #define ENT_OPTS(_ent) ent->fs_mntops
38 #define ENT_PASS(_ent) ent->fs_passno
39 #endif
40
41 #include "einfo.h"
42
43 #ifdef HAVE_GETMNTENT
44 static struct mntent *getmntfile (FILE *fp, const char *file)
45 {
46 struct mntent *ent;
47
48 while ((ent = getmntent (fp)))
49 if (strcmp (file, ent->mnt_dir) == 0)
50 return (ent);
51
52 return (NULL);
53 }
54 #endif
55
56 int main (int argc, char **argv)
57 {
58 int i;
59 #ifdef HAVE_GETMNTENT
60 FILE *fp;
61 struct mntent *ent;
62 #else
63 struct fstab *ent;
64 #endif
65 int result = EXIT_FAILURE;
66 char *p;
67 char *token;
68 int n = 0;
69
70 for (i = 1; i < argc; i++) {
71 #ifdef HAVE_GETMNTENT
72 fp = setmntent ("/etc/fstab", "r");
73 #endif
74
75 if (strcmp (argv[i], "--fstype") == 0 && i + 1 < argc) {
76 i++;
77 p = argv[i];
78 while ((token = strsep (&p, ",")))
79 while ((ent = GET_ENT))
80 if (strcmp (token, ENT_TYPE (ent)) == 0)
81 printf ("%s\n", ENT_FILE (ent));
82 result = EXIT_SUCCESS;
83 }
84
85 if (strcmp (argv[i], "--mount-cmd") == 0 && i + 1 < argc) {
86 i++;
87 if ((ent = GET_ENT_FILE (argv[i])) == NULL)
88 continue;
89 printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
90 ENT_DEVICE (ent), ENT_FILE (ent));
91 result = EXIT_SUCCESS;
92 }
93
94 if (strcmp (argv[i], "--opts") == 0 && i + 1 < argc) {
95 i++;
96 if ((ent = GET_ENT_FILE (argv[i])) == NULL)
97 continue;
98 printf ("%s\n", ENT_OPTS (ent));
99 result = EXIT_SUCCESS;
100 }
101
102 if (strcmp (argv[i], "--passno") == 0 && i + 1 < argc) {
103 i++;
104 switch (argv[i][0]) {
105 case '=':
106 case '<':
107 case '>':
108 if (sscanf (argv[i] + 1, "%d", &n) != 1)
109 eerrorx ("%s: invalid passno %s", argv[0], argv[i] + 1);
110
111 while ((ent = GET_ENT)) {
112 if (((argv[i][0] == '=' && n == ENT_PASS (ent)) ||
113 (argv[i][0] == '<' && n > ENT_PASS (ent)) ||
114 (argv[i][0] == '>' && n < ENT_PASS (ent))) &&
115 strcmp (ENT_FILE (ent), "none") != 0)
116 printf ("%s\n", ENT_FILE (ent));
117 }
118
119 default:
120 if ((ent = GET_ENT_FILE (argv[i])) == NULL)
121 continue;
122 printf ("%d\n", ENT_PASS (ent));
123 result = EXIT_SUCCESS;
124 }
125 }
126
127 END_ENT;
128
129 if (result != EXIT_SUCCESS) {
130 eerror ("%s: unknown option `%s'", basename (argv[0]), argv[i]);
131 break;
132 }
133
134 }
135
136 exit (result);
137 }
138

  ViewVC Help
Powered by ViewVC 1.1.20