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

Contents of /trunk/src/fstabinfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2698 - (show annotations) (download) (as text)
Mon May 14 12:24:18 2007 UTC (7 years, 3 months ago) by uberlord
File MIME type: text/x-csrc
File size: 3536 byte(s)
    Allow time for pidfiles to be created, #178274.
    Use int instead of char for getopt, #178084 thanks to drizztbsd.
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 #define getoptstring "f:m:o:p:h"
60 static struct option longopts[] = {
61 { "fstype", 1, NULL, 'f'},
62 { "mountcmd", 1, NULL, 'm'},
63 { "opts", 1, NULL, 'o'},
64 { "passno", 1, NULL, 'p'},
65 { "help", 0, NULL, 'h'},
66 { NULL, 0, NULL, 0}
67 };
68 #include "_usage.c"
69
70 int main (int argc, char **argv)
71 {
72 #ifdef HAVE_GETMNTENT
73 FILE *fp;
74 struct mntent *ent;
75 #else
76 struct fstab *ent;
77 #endif
78 int result = EXIT_FAILURE;
79 char *token;
80 int n = 0;
81 int opt;
82
83 while ((opt = getopt_long (argc, argv, getoptstring,
84 longopts, (int *) 0)) != -1)
85 {
86 #ifdef HAVE_GETMNTENT
87 fp = setmntent ("/etc/fstab", "r");
88 #endif
89 switch (opt) {
90 case 'f':
91 while ((token = strsep (&optarg, ",")))
92 while ((ent = GET_ENT))
93 if (strcmp (token, ENT_TYPE (ent)) == 0)
94 printf ("%s\n", ENT_FILE (ent));
95 result = EXIT_SUCCESS;
96 break;
97
98 case 'm':
99 if ((ent = GET_ENT_FILE (optarg))) {
100 printf ("-o %s -t %s %s %s\n", ENT_OPTS (ent), ENT_TYPE (ent),
101 ENT_DEVICE (ent), ENT_FILE (ent));
102 result = EXIT_SUCCESS;
103 }
104 break;
105
106 case 'o':
107 if ((ent = GET_ENT_FILE (optarg))) {
108 printf ("%s\n", ENT_OPTS (ent));
109 result = EXIT_SUCCESS;
110 }
111 break;
112
113 case 'p':
114 switch (optarg[0]) {
115 case '=':
116 case '<':
117 case '>':
118 if (sscanf (optarg + 1, "%d", &n) != 1)
119 eerrorx ("%s: invalid passno %s", argv[0], optarg + 1);
120
121 while ((ent = GET_ENT)) {
122 if (((optarg[0] == '=' && n == ENT_PASS (ent)) ||
123 (optarg[0] == '<' && n > ENT_PASS (ent)) ||
124 (optarg[0] == '>' && n < ENT_PASS (ent))) &&
125 strcmp (ENT_FILE (ent), "none") != 0)
126 {
127 printf ("%s\n", ENT_FILE (ent));
128 result = EXIT_SUCCESS;
129 }
130 }
131 break;
132
133 default:
134 if ((ent = GET_ENT_FILE (optarg))) {
135 printf ("%d\n", ENT_PASS (ent));
136 result = EXIT_SUCCESS;
137 }
138 break;
139 }
140 break;
141
142 case 'h':
143 END_ENT;
144 usage (EXIT_SUCCESS);
145 default:
146 END_ENT;
147 usage (EXIT_FAILURE);
148 }
149
150 END_ENT;
151
152 if (result != EXIT_SUCCESS)
153 break;
154 }
155
156 exit (result);
157 }

  ViewVC Help
Powered by ViewVC 1.1.20