8 /* function prototypes */
9 void diep(const char *s
);
11 int main(int argc
, char *argv
[])
13 const char *message
= "this shall be written to file\n";
17 /* check argument count */
19 fprintf(stderr
, "Usage: %s\n", argv
[0]);
24 if ((fd
= open(argv
[1], O_RDWR
| O_CREAT
| O_TRUNC
, (mode_t
)0600)) == -1)
27 /* stretch the file size to the size of the (mmapped) array of chars */
28 if (lseek(fd
, strlen(message
) - 1, SEEK_SET
) == -1) {
33 /* something needs to be written at the end of the file to
34 have the file actually have the new size */
35 if (write(fd
, "", 1) != 1) {
41 if ((map
= (char *)mmap(0, strlen(message
), PROT_WRITE
, MAP_SHARED
, fd
, 0)) == MAP_FAILED
) {
46 /* copy message to mmapped() memory */
47 memcpy(map
, message
, strlen(message
));
49 /* free the mmaped() memory */
50 if (munmap(map
, strlen(message
)) == -1) {
61 void diep(const char *s
)