ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / hfsplus / options.c
1 /*
2  *  linux/fs/hfsplus/options.c
3  *
4  * Copyright (C) 2001
5  * Brad Boyer (flar@allandria.com)
6  * (C) 2003 Ardis Technologies <roman@ardistech.com>
7  *
8  * Option parsing
9  */
10
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include "hfsplus_fs.h"
15
16 /* Initialize an options object to reasonable defaults */
17 void fill_defaults(struct hfsplus_sb_info *opts)
18 {
19         if (!opts)
20                 return;
21
22         opts->creator = HFSPLUS_DEF_CR_TYPE;
23         opts->type = HFSPLUS_DEF_CR_TYPE;
24         opts->umask = current->fs->umask;
25         opts->uid = current->uid;
26         opts->gid = current->gid;
27         opts->part = -1;
28         opts->session = -1;
29 }
30
31 /* convert a "four byte character" to a 32 bit int with error checks */
32 static int fill_fourchar(u32 *result, char *input)
33 {
34         u32 out;
35         int i;
36
37         if (!result || !input || !*input || (strlen(input) != 4))
38                 return 0;
39
40         for (out = 0, i = 0; i < 4; i++) {
41                 out <<= 8;
42                 out |= ((int)(input[i])) & 0xFF;
43         }
44         *result = out;
45         return 1;
46 }
47
48 /* convert a string to int with error checks */
49 static int fill_int(int *result, char *input, int base)
50 {
51         char *tmp = input;
52         int intval;
53
54         if (!result || !input || !*input)
55                 return 0;
56
57         intval = simple_strtoul(tmp, &tmp, base);
58         if (*tmp)
59                 return 0;
60
61         *result = intval;
62         return 1;
63 }
64
65 /* Parse options from mount. Returns 0 on failure */
66 /* input is the options passed to mount() as a string */
67 int parse_options(char *input, struct hfsplus_sb_info *results)
68 {
69         char *curropt, *value;
70         int tmp;
71
72         if (!input)
73                 return 1;
74
75         while ((curropt = strsep(&input,",")) != NULL) {
76                 if (!*curropt)
77                         continue;
78
79                 if ((value = strchr(curropt, '=')) != NULL)
80                         *value++ = '\0';
81
82                 if (!strcmp(curropt, "creator")) {
83                         if (!fill_fourchar(&(results->creator), value)) {
84                                 printk("HFS+-fs: creator requires a 4 character value\n");
85                                 return 0;
86                         }
87                 } else if (!strcmp(curropt, "type")) {
88                         if (!fill_fourchar(&(results->type), value)) {
89                                 printk("HFS+-fs: type requires a 4 character value\n");
90                                 return 0;
91                         }
92                 } else if (!strcmp(curropt, "umask")) {
93                         if (!fill_int(&tmp, value, 8)) {
94                                 printk("HFS+-fs: umask requires a value\n");
95                                 return 0;
96                         }
97                         results->umask = (umode_t)tmp;
98                 } else if (!strcmp(curropt, "uid")) {
99                         if (!fill_int(&tmp, value, 0)) {
100                                 printk("HFS+-fs: uid requires an argument\n");
101                                 return 0;
102                         }
103                         results->uid = (uid_t)tmp;
104                 } else if (!strcmp(curropt, "gid")) {
105                         if (!fill_int(&tmp, value, 0)) {
106                                 printk("HFS+-fs: gid requires an argument\n");
107                                 return 0;
108                         }
109                         results->gid = (gid_t)tmp;
110                 } else if (!strcmp(curropt, "part")) {
111                         if (!fill_int(&results->part, value, 0)) {
112                                 printk("HFS+-fs: part requires an argument\n");
113                                 return 0;
114                         }
115                 } else if (!strcmp(curropt, "session")) {
116                         if (!fill_int(&results->session, value, 0)) {
117                                 printk("HFS+-fs: session requires an argument\n");
118                                 return 0;
119                         }
120                 } else {
121                         printk("HFS+-fs: unknown option %s\n", curropt);
122                         return 0;
123                 }
124         }
125
126         return 1;
127 }