Debian Codegraph @

listings/appendix-b/lseek-huge.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <fcntl.h>
8 #include <stdlib.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 int main (int argc, char* argv[])
14 {
15 int zero = 0;
16 const int megabyte = 1024 * 1024;
17
18 char* filename = argv[1];
19 size_t length = (size_t) atoi (argv[2]) * megabyte;
20
21 /* Open a new file. */
22 int fd = open (filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
23 /* Jump to one byte short of where we want the file to end. */
24 lseek (fd, length - 1, SEEK_SET);
25 /* Write a single zero byte. */
26 write (fd, &zero, 1);
27 /* All done. */
28 close (fd);
29
30 return 0;
31 }