-
Notifications
You must be signed in to change notification settings - Fork 32
/
user.c
49 lines (41 loc) · 1012 Bytes
/
user.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
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define KSORT_DEV "/dev/sort"
int main()
{
int fd = open(KSORT_DEV, O_RDWR);
if (fd < 0) {
perror("Failed to open character device");
goto error;
}
size_t n_elements = 1000;
size_t size = n_elements * sizeof(int);
int *inbuf = malloc(size);
if (!inbuf)
goto error;
for (size_t i = 0; i < n_elements; i++)
inbuf[i] = rand() % n_elements;
ssize_t r_sz = read(fd, inbuf, size);
if (r_sz != size) {
perror("Failed to read character device");
goto error;
}
bool pass = true;
int ret = 0;
/* Verify the result of sorting */
for (size_t i = 1; i < n_elements; i++) {
if (inbuf[i] < inbuf[i - 1]) {
pass = false;
break;
}
}
printf("Sorting %s!\n", pass ? "succeeded" : "failed");
error:
free(inbuf);
if (fd > 0)
close(fd);
return ret;
}