-
Notifications
You must be signed in to change notification settings - Fork 14
/
Task 31 (sort file.bin inplace).c
87 lines (73 loc) · 1.58 KB
/
Task 31 (sort file.bin inplace).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
#include <stdlib.h>
#include <err.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char **argv){
if(argc != 2){
errx(1, "Invalid number of arguments. Usage: %s <file.bin>", argv[0]);
}
char *file = argv[1];
struct stat st;
if(stat(file, &st) == -1){
err(2, "could not stat file %s", file);
}
if(!(st.st_mode & S_IRUSR) || !(st.st_mode & S_IWUSR)){
errx(3, "file %s not readable or not writable", file);
}
/*
if(st.st_size > UINT64_MAX){
warnx("small chance of overfloating");
}
*/
uint64_t counting[256] = { 0 };
uint16_t level[256];
for (uint16_t j = 0; j< 256; ++j){
level[j] = 1;
}
ssize_t fd = open(file, O_RDWR);
if(fd == -1){
err(4, "error while opening file %s", file);
}
ssize_t read_sz;
uint8_t b;
while((read_sz = read(fd, &b, sizeof(b))) == sizeof(b)){
++counting[b];
if(counting[b] == UINT64_MAX){
counting[b] = 0;
++level[b];
}
}
if(read_sz == -1){
const int olderrno = errno;
close(fd);
errno = olderrno;
err(5, "error while reading file %s", file);
}
if(lseek(fd, 0, SEEK_SET) == -1){
const int olderrno = errno;
close(fd);
errno = olderrno;
err(6, "error while lseek file %s", file);
}
uint16_t i = 0;
for(; i < 256; ++i){
while(level[i]--){
b = i;
while(counting[i]--){
if(write(fd, &b, sizeof(b)) != sizeof(b)){
const int olderrno = errno;
close(fd);
errno = olderrno;
err(7, "error while writing into file %s", file);
}
}
counting[i] = UINT64_MAX;
}
}
close(fd);
exit(0);
}