-
Notifications
You must be signed in to change notification settings - Fork 4
/
mmap_ex_fstat.c
152 lines (123 loc) · 2.82 KB
/
mmap_ex_fstat.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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
/**
* Write to a memory mapped file
*
* @param fd The file descriptor
* @param buf The file contents
*
* @return The number of bytes written.
* -1 on error
*/
int m_write (int fd, const char *buf)
{
const int error = -1;
const char *m_ptr = buf;
const int f_size =
(strlen (buf) * sizeof (char)) + 1; /* buffer size */
char *map_ptr;
char *i;
int written = 0; /* bytes written */
/* Stretch the file size to the size of the input file */
/* When not applied `file' reports no magic and the size */
/* report of `du', `ls', etc. is wrong */
if (lseek (fd, f_size - 1, SEEK_SET) == -1)
{
perror ("lseek");
return error;
}
/* Something needs to be written at the end of the file to */
/* have the file actually have the new size. If not done,
a SIGBUS signal error is generated */
if (write (fd, "", 1) != 1)
{
perror ("write");
return error;
}
/* Now the file is ready to be mmapped. */
if ((map_ptr = mmap (0, f_size, PROT_WRITE, MAP_SHARED,
fd, 0)) == MAP_FAILED)
{
perror ("mmap");
return error;
}
/* write the contents of the input file
to the mmapped file */
i = map_ptr;
while (++written && (*i++ = *m_ptr++)) ;
/* free the mmapped memory */
if (munmap (map_ptr, f_size) == -1)
{
perror ("munmap");
return error;
}
return written;
}
/**
* Get the size of a file using
* fstat
*
* @param fd The file descriptor
*
* @return The size in bytes. -1
* on error
*/
int get_fsize (int fd)
{
const int error = -1;
struct stat f_stat;
if (fstat (fd, &f_stat) == -1)
{
perror ("fstat");
return error;
}
return f_stat.st_size;
}
int main (void)
{
/* const char i_path[] = "/etc/motd"; */
const char message[] = "the file contents\n";
const char *m_ptr = message;
const char o_path[] = "file1";
const int mode = 0664;
/* int i_fd; */
int o_fd;
int f_size;
int i;
/* if ((i_fd = open (i_path, O_RDONLY)) == -1) */
/* { */
/* perror ("open"); */
/* goto error; */
/* } */
if ((o_fd = open (o_path, O_RDWR | O_CREAT,
(mode_t)mode)) == -1)
{
perror ("open");
goto error;
}
/* f_size = (strlen (message) * sizeof (char)) + 1; */
/* if ((f_size = get_fsize (i_fd)) == -1) */
/* { */
/* perror ("fstat"); */
/* goto error; */
/* } */
/* printf ("size: \t%d bytes\n", f_size); */
if ((i = m_write (o_fd, message)) == -1)
{
perror ("m_write");
goto error;
}
printf ("mapped: \t%d bytes\n", i);
/* close (i_fd); */
close (o_fd);
return 0;
error:
/* close (i_fd); */
close (o_fd);
return 1;
}