blob: 735260ce00e358beb717e0d9e0e78810a30a3abb [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
2 * mountopts.c --- convert between default mount options and strings
3 *
4 * Copyright (C) 2002 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>
14#include <ctype.h>
15#include <errno.h>
16
17#include "e2p.h"
18
19struct mntopt {
20 unsigned int mask;
21 const char *string;
22};
23
24static struct mntopt mntopt_list[] = {
25 { EXT2_DEFM_DEBUG, "debug" },
26 { EXT2_DEFM_BSDGROUPS, "bsdgroups" },
27 { EXT2_DEFM_XATTR_USER, "user_xattr" },
28 { EXT2_DEFM_ACL, "acl" },
29 { EXT2_DEFM_UID16, "uid16" },
30 { EXT3_DEFM_JMODE_DATA, "journal_data" },
31 { EXT3_DEFM_JMODE_ORDERED, "journal_data_ordered" },
32 { EXT3_DEFM_JMODE_WBACK, "journal_data_writeback" },
33 { 0, 0 },
34};
35
36const char *e2p_mntopt2string(unsigned int mask)
37{
38 struct mntopt *f;
39 static char buf[20];
40 int fnum;
41
42 for (f = mntopt_list; f->string; f++) {
43 if (mask == f->mask)
44 return f->string;
45 }
46 for (fnum = 0; mask >>= 1; fnum++);
47 sprintf(buf, "MNTOPT_%d", fnum);
48 return buf;
49}
50
51int e2p_string2mntopt(char *string, unsigned int *mask)
52{
53 struct mntopt *f;
54 char *eptr;
55 int num;
56
57 for (f = mntopt_list; f->string; f++) {
58 if (!strcasecmp(string, f->string)) {
59 *mask = f->mask;
60 return 0;
61 }
62 }
63 if (strncasecmp(string, "MNTOPT_", 8))
64 return 1;
65
66 if (string[8] == 0)
67 return 1;
68 num = strtol(string+8, &eptr, 10);
69 if (num > 32 || num < 0)
70 return 1;
71 if (*eptr)
72 return 1;
73 *mask = 1 << num;
74 return 0;
75}
76
77static char *skip_over_blanks(char *cp)
78{
79 while (*cp && isspace(*cp))
80 cp++;
81 return cp;
82}
83
84static char *skip_over_word(char *cp)
85{
86 while (*cp && !isspace(*cp) && *cp != ',')
87 cp++;
88 return cp;
89}
90
91/*
92 * Edit a mntopt set array as requested by the user. The ok
93 * parameter, if non-zero, allows the application to limit what
94 * mntopts the user is allowed to set or clear using this function.
95 */
96int e2p_edit_mntopts(const char *str, __u32 *mntopts, __u32 ok)
97{
98 char *cp, *buf, *next;
99 int neg;
100 unsigned int mask;
101
Mike Frysingerd1a9d572005-06-11 22:25:27 +0000102 buf = xmalloc(strlen(str)+1);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000103 strcpy(buf, str);
104 cp = buf;
105 while (cp && *cp) {
106 neg = 0;
107 cp = skip_over_blanks(cp);
108 next = skip_over_word(cp);
109 if (*next == 0)
110 next = 0;
111 else
112 *next = 0;
113 switch (*cp) {
114 case '-':
115 case '^':
116 neg++;
117 case '+':
118 cp++;
119 break;
120 }
121 if (e2p_string2mntopt(cp, &mask))
122 return 1;
123 if (ok && !(ok & mask))
124 return 1;
125 if (mask & EXT3_DEFM_JMODE)
126 *mntopts &= ~EXT3_DEFM_JMODE;
127 if (neg)
128 *mntopts &= ~mask;
129 else
130 *mntopts |= mask;
131 cp = next ? next+1 : 0;
132 }
133 return 0;
134}