]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - lib/e2p/feature.c
Add handling for invalid option characters in debugfs subcommands
[thirdparty/e2fsprogs.git] / lib / e2p / feature.c
CommitLineData
d7b701de
TT
1/*
2 * feature.c --- convert between features and strings
3 *
4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
5 *
6 * This file can be redistributed under the terms of the GNU Library General
7 * Public License
8 *
9 */
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
36caf25f 14#include <ctype.h>
d7b701de
TT
15#include <errno.h>
16
17#include "e2p.h"
18
d7b701de
TT
19struct feature {
20 int compat;
21 unsigned int mask;
36caf25f 22 const char *string;
d7b701de
TT
23};
24
e2207ce5 25static struct feature feature_list[] = {
d7b701de
TT
26 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_PREALLOC,
27 "dir_prealloc" },
28 { E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL,
29 "has_journal" },
30 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_IMAGIC_INODES,
31 "imagic_inodes" },
342d847d
TT
32 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXT_ATTR,
33 "ext_attr" },
49d5ddcc
TT
34 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
35 "dir_index" },
36 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
37 "resize_inode" },
d7b701de
TT
38 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
39 "sparse_super" },
40 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
41 "large_file" },
d7b701de
TT
42 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
43 "compression" },
44 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
45 "filetype" },
46 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
47 "needs_recovery" },
c2204b32
TT
48 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
49 "journal_dev" },
39dc1c45
TT
50 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
51 "extents" },
c046ac7f
TT
52 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
53 "meta_bg" },
d7b701de
TT
54 { 0, 0, 0 },
55};
56
36caf25f 57const char *e2p_feature2string(int compat, unsigned int mask)
d7b701de
TT
58{
59 struct feature *f;
60 static char buf[20];
61 char fchar;
62 int fnum;
63
64 for (f = feature_list; f->string; f++) {
65 if ((compat == f->compat) &&
66 (mask == f->mask))
67 return f->string;
68 }
69 switch (compat) {
70 case E2P_FEATURE_COMPAT:
71 fchar = 'C';
72 break;
73 case E2P_FEATURE_INCOMPAT:
74 fchar = 'I';
75 break;
76 case E2P_FEATURE_RO_INCOMPAT:
77 fchar = 'R';
78 break;
79 default:
80 fchar = '?';
81 break;
82 }
83 for (fnum = 0; mask >>= 1; fnum++);
84 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
85 return buf;
86}
87
36caf25f 88int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
d7b701de
TT
89{
90 struct feature *f;
91 char *eptr;
92 int num;
93
94 for (f = feature_list; f->string; f++) {
95 if (!strcasecmp(string, f->string)) {
36caf25f 96 *compat_type = f->compat;
d7b701de
TT
97 *mask = f->mask;
98 return 0;
99 }
100 }
101 if (strncasecmp(string, "FEATURE_", 8))
102 return 1;
103
104 switch (string[8]) {
105 case 'c':
106 case 'C':
36caf25f 107 *compat_type = E2P_FEATURE_COMPAT;
d7b701de
TT
108 break;
109 case 'i':
110 case 'I':
36caf25f 111 *compat_type = E2P_FEATURE_INCOMPAT;
d7b701de
TT
112 break;
113 case 'r':
114 case 'R':
36caf25f 115 *compat_type = E2P_FEATURE_RO_INCOMPAT;
d7b701de
TT
116 break;
117 default:
118 return 1;
119 }
120 if (string[9] == 0)
121 return 1;
122 num = strtol(string+9, &eptr, 10);
123 if (num > 32 || num < 0)
124 return 1;
125 if (*eptr)
126 return 1;
127 *mask = 1 << num;
128 return 0;
129}
130
131static char *skip_over_blanks(char *cp)
132{
133 while (*cp && isspace(*cp))
134 cp++;
135 return cp;
136}
137
36caf25f 138static char *skip_over_word(char *cp)
d7b701de 139{
944ab713 140 while (*cp && !isspace(*cp) && *cp != ',')
d7b701de
TT
141 cp++;
142 return cp;
143}
144
944ab713
TT
145/*
146 * Edit a feature set array as requested by the user. The ok_array,
147 * if set, allows the application to limit what features the user is
148 * allowed to set or clear using this function.
149 */
36caf25f 150int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
d7b701de
TT
151{
152 char *cp, *buf, *next;
153 int neg;
36caf25f
TT
154 unsigned int mask;
155 int compat_type;
d7b701de
TT
156
157 buf = malloc(strlen(str)+1);
944ab713 158 if (!buf)
d7b701de 159 return 1;
d7b701de 160 strcpy(buf, str);
9dc6ad1e 161 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
d7b701de
TT
162 neg = 0;
163 cp = skip_over_blanks(cp);
164 next = skip_over_word(cp);
9dc6ad1e 165
d7b701de
TT
166 if (*next == 0)
167 next = 0;
168 else
169 *next = 0;
9dc6ad1e
TT
170
171 if ((strcasecmp(cp, "none") == 0) ||
172 (strcasecmp(cp, "clear") == 0)) {
173 compat_array[0] = 0;
174 compat_array[1] = 0;
175 compat_array[2] = 0;
176 continue;
177 }
178
d7b701de
TT
179 switch (*cp) {
180 case '-':
181 case '^':
182 neg++;
183 case '+':
184 cp++;
185 break;
186 }
36caf25f 187 if (e2p_string2feature(cp, &compat_type, &mask))
d7b701de 188 return 1;
36caf25f 189 if (ok_array && !(ok_array[compat_type] & mask))
944ab713 190 return 1;
d7b701de 191 if (neg)
36caf25f 192 compat_array[compat_type] &= ~mask;
d7b701de 193 else
36caf25f 194 compat_array[compat_type] |= mask;
d7b701de
TT
195 }
196 return 0;
197}
198