]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - lib/e2p/feature.c
Use long long to calculate the percentage
[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" },
49d5ddcc
TT
32 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
33 "dir_index" },
34 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
35 "resize_inode" },
d7b701de
TT
36 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
37 "sparse_super" },
38 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
39 "large_file" },
40 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_BTREE_DIR,
41 "btree_dir" },
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" },
d7b701de
TT
50 { 0, 0, 0 },
51};
52
36caf25f 53const char *e2p_feature2string(int compat, unsigned int mask)
d7b701de
TT
54{
55 struct feature *f;
56 static char buf[20];
57 char fchar;
58 int fnum;
59
60 for (f = feature_list; f->string; f++) {
61 if ((compat == f->compat) &&
62 (mask == f->mask))
63 return f->string;
64 }
65 switch (compat) {
66 case E2P_FEATURE_COMPAT:
67 fchar = 'C';
68 break;
69 case E2P_FEATURE_INCOMPAT:
70 fchar = 'I';
71 break;
72 case E2P_FEATURE_RO_INCOMPAT:
73 fchar = 'R';
74 break;
75 default:
76 fchar = '?';
77 break;
78 }
79 for (fnum = 0; mask >>= 1; fnum++);
80 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
81 return buf;
82}
83
36caf25f 84int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
d7b701de
TT
85{
86 struct feature *f;
87 char *eptr;
88 int num;
89
90 for (f = feature_list; f->string; f++) {
91 if (!strcasecmp(string, f->string)) {
36caf25f 92 *compat_type = f->compat;
d7b701de
TT
93 *mask = f->mask;
94 return 0;
95 }
96 }
97 if (strncasecmp(string, "FEATURE_", 8))
98 return 1;
99
100 switch (string[8]) {
101 case 'c':
102 case 'C':
36caf25f 103 *compat_type = E2P_FEATURE_COMPAT;
d7b701de
TT
104 break;
105 case 'i':
106 case 'I':
36caf25f 107 *compat_type = E2P_FEATURE_INCOMPAT;
d7b701de
TT
108 break;
109 case 'r':
110 case 'R':
36caf25f 111 *compat_type = E2P_FEATURE_RO_INCOMPAT;
d7b701de
TT
112 break;
113 default:
114 return 1;
115 }
116 if (string[9] == 0)
117 return 1;
118 num = strtol(string+9, &eptr, 10);
119 if (num > 32 || num < 0)
120 return 1;
121 if (*eptr)
122 return 1;
123 *mask = 1 << num;
124 return 0;
125}
126
127static char *skip_over_blanks(char *cp)
128{
129 while (*cp && isspace(*cp))
130 cp++;
131 return cp;
132}
133
36caf25f 134static char *skip_over_word(char *cp)
d7b701de 135{
944ab713 136 while (*cp && !isspace(*cp) && *cp != ',')
d7b701de
TT
137 cp++;
138 return cp;
139}
140
944ab713
TT
141/*
142 * Edit a feature set array as requested by the user. The ok_array,
143 * if set, allows the application to limit what features the user is
144 * allowed to set or clear using this function.
145 */
36caf25f 146int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
d7b701de
TT
147{
148 char *cp, *buf, *next;
149 int neg;
36caf25f
TT
150 unsigned int mask;
151 int compat_type;
d7b701de
TT
152
153 buf = malloc(strlen(str)+1);
944ab713 154 if (!buf)
d7b701de 155 return 1;
d7b701de
TT
156 strcpy(buf, str);
157 cp = buf;
158 while (cp && *cp) {
159 neg = 0;
160 cp = skip_over_blanks(cp);
161 next = skip_over_word(cp);
162 if (*next == 0)
163 next = 0;
164 else
165 *next = 0;
166 switch (*cp) {
167 case '-':
168 case '^':
169 neg++;
170 case '+':
171 cp++;
172 break;
173 }
36caf25f 174 if (e2p_string2feature(cp, &compat_type, &mask))
d7b701de 175 return 1;
36caf25f 176 if (ok_array && !(ok_array[compat_type] & mask))
944ab713 177 return 1;
d7b701de 178 if (neg)
36caf25f 179 compat_array[compat_type] &= ~mask;
d7b701de 180 else
36caf25f 181 compat_array[compat_type] |= mask;
d7b701de
TT
182 cp = next ? next+1 : 0;
183 }
184 return 0;
185}
186