forked from sysprog21/kcalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.c
268 lines (232 loc) · 7.09 KB
/
calc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/string.h> /* for memset() and memcpy() */
MODULE_LICENSE("Dual MIT/GPL");
MODULE_AUTHOR("National Cheng Kung University, Taiwan");
MODULE_DESCRIPTION("Math expression evaluation");
MODULE_VERSION("0.1");
#define DEVICE_NAME "calc"
#define CLASS_NAME "calc"
#define BUFF_SIZE 256
static int major;
static char message[BUFF_SIZE] = {0};
static short size_of_message;
static struct class *char_class = NULL;
static struct device *char_dev = NULL;
static char *msg_ptr = NULL;
static int result = 0;
typedef enum { ADD = 0, SUB, MULT, DIV, POW, NONE } operation_t;
/* The prototype functions for the character driver */
static int dev_open(struct inode *, struct file *);
static int dev_release(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
static void prepare_calc(void);
static void calc(int first, int second, operation_t op);
static struct file_operations fops = {
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};
static int dev_open(struct inode *inodep, struct file *filep)
{
msg_ptr = message;
return 0;
}
static ssize_t dev_read(struct file *filep,
char *buffer,
size_t len,
loff_t *offset)
{
int error_count = 0;
if (*msg_ptr == 0) {
return 0;
}
memset(message, 0, sizeof(char) * BUFF_SIZE);
snprintf(message, 64, "%d\n", result);
size_of_message = strlen(message);
error_count = copy_to_user(buffer, message, size_of_message);
if (error_count == 0) {
printk(KERN_INFO "CALC: size: %d result: %d\n", size_of_message,
result);
while (len && *msg_ptr) {
error_count = put_user(*(msg_ptr++), buffer++);
len--;
}
if (error_count == 0) {
return (size_of_message);
} else {
return -EFAULT;
}
} else {
printk(KERN_INFO "CALC: Failed to send %d characters to the user\n",
error_count);
return -EFAULT;
}
}
static ssize_t dev_write(struct file *filep,
const char *buffer,
size_t len,
loff_t *offset)
{
size_of_message = 0;
memset(message, 0, sizeof(char) * BUFF_SIZE);
snprintf(message, BUFF_SIZE, "%s", buffer);
size_of_message = strlen(message);
printk(KERN_INFO "CALC: Received %d -> %s\n", size_of_message, message);
if (size_of_message >= 3) {
prepare_calc();
}
return len;
}
static void calc(int first, int second, operation_t op)
{
switch (op) {
case ADD: {
result = first + second;
break;
}
case SUB: {
result = first - second;
break;
}
case MULT: {
result = first * second;
break;
}
case DIV: {
result = first / second;
break;
}
case POW: {
result = first ^ second;
break;
}
default:
result = 0;
break;
}
}
static void handle_case(int *first_num_stop, int *sec_num_start, int i)
{
if (first_num_stop && *first_num_stop == 0) {
*first_num_stop = i - 1;
}
if (sec_num_start && *sec_num_start == 0) {
*sec_num_start = i + 1;
}
}
static void prepare_calc(void)
{
int first_num_stop = 0;
int sec_num_start = 0;
int op_idx = 0;
operation_t op = NONE;
for (int i = 0; i < BUFF_SIZE; i++) {
if (message[i] == '\0') {
break;
}
switch (message[i]) {
case '*': {
handle_case(&first_num_stop, &sec_num_start, i);
op_idx = i;
op = MULT;
break;
}
case '/': {
handle_case(&first_num_stop, &sec_num_start, i);
op_idx = i;
op = DIV;
break;
}
case '+': {
handle_case(&first_num_stop, &sec_num_start, i);
op_idx = i;
op = ADD;
break;
}
case '-': {
handle_case(&first_num_stop, &sec_num_start, i);
op_idx = i;
op = SUB;
break;
}
case '^': {
handle_case(&first_num_stop, &sec_num_start, i);
op_idx = i;
op = POW;
break;
}
case ' ': {
handle_case(&first_num_stop, &sec_num_start, i);
op_idx = i;
break;
}
default:
break;
}
}
if (sec_num_start > 0 && op_idx > 0) {
char **endp = NULL;
int first_num = simple_strtol(message, endp, 10);
int sec_num = simple_strtol((message + sec_num_start), endp, 10);
printk(KERN_INFO "input is valid !\n");
calc(first_num, sec_num, op);
printk(KERN_INFO "first num: %d\n", first_num);
printk(KERN_INFO "sec num: %d\n", sec_num);
}
}
static int dev_release(struct inode *inodep, struct file *filep)
{
printk(KERN_INFO "CALC: Device successfully closed\n");
return 0;
}
static int __init calc_init(void)
{
printk(KERN_INFO "CALC: Initializing the CALC LKM\n");
// Try to dynamically allocate a major number for the device -- more
// difficult but worth it
major = register_chrdev(0, DEVICE_NAME, &fops);
if (major < 0) {
printk(KERN_ALERT "CALC failed to register a major number\n");
return major;
}
printk(KERN_INFO "CALC: registered correctly with major number %d\n",
major);
// Register the device class
char_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(char_class)) { // Check for error and clean up if there is
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_ALERT "Failed to register device class\n");
return PTR_ERR(
char_class); // Correct way to return an error on a pointer
}
printk(KERN_INFO "CALC: device class registered correctly\n");
// Register the device driver
char_dev =
device_create(char_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
if (IS_ERR(char_dev)) { // Clean up if there is an error
class_destroy(char_class); // Repeated code but the alternative is
// goto statements
unregister_chrdev(major, DEVICE_NAME);
printk(KERN_ALERT "Failed to create the device\n");
return PTR_ERR(char_dev);
}
printk(KERN_INFO "CALC: device class created correctly\n");
return 0;
}
static void __exit calc_exit(void)
{
device_destroy(char_class, MKDEV(major, 0)); // remove the device
class_unregister(char_class); // unregister the device class
class_destroy(char_class); // remove the device class
unregister_chrdev(major, DEVICE_NAME); // unregister the major number
printk(KERN_INFO "CALC: Goodbye!\n");
}
module_init(calc_init);
module_exit(calc_exit);