forked from xdsopl/robot36
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmap_file.c
107 lines (92 loc) · 2.02 KB
/
mmap_file.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
/*
robot36 - encode and decode images using SSTV in Robot 36 mode
Written in 2011 by <Ahmet Inan> <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "mmap_file.h"
int mmap_file_ro(void **p, char *name, size_t *size)
{
*size = 0;
int fd = open(name, O_RDONLY);
if (fd == -1) {
perror("open");
return 0;
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
perror("fstat");
close(fd);
return 0;
}
if (!S_ISREG(sb.st_mode)) {
fprintf(stderr, "%s not a file\n", name);
close(fd);
return 0;
}
*p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (*p == MAP_FAILED) {
perror("mmap");
close(fd);
return 0;
}
if (close(fd) == -1) {
perror ("close");
return 0;
}
*size = sb.st_size;
return 1;
}
int mmap_file_rw(void **p, char *name, size_t size)
{
int fd = open(name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd == -1) {
perror("open");
return 0;
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
perror("fstat");
close(fd);
return 0;
}
if (!S_ISREG(sb.st_mode)) {
fprintf(stderr, "%s not a file\n", name);
close(fd);
return 0;
}
if (lseek(fd, size - 1, SEEK_SET) == -1) {
perror("lseek");
close(fd);
return 0;
}
if (write(fd, "", 1) != 1) {
perror("write");
close(fd);
return 0;
}
*p = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (*p == MAP_FAILED) {
perror("mmap");
close(fd);
return 0;
}
if (close(fd) == -1) {
perror ("close");
return 0;
}
return 1;
}
int munmap_file(void *p, size_t size)
{
if (munmap(p, size) == -1) {
perror("munmap");
return 0;
}
return 1;
}