summaryrefslogtreecommitdiffstats
path: root/c_pgms/sparse/sparse_file_alt.c
blob: 4afaa377c07a9fc39065ba00ee190ea5b975c736 (plain)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#define BLOCK_SIZE 4096

void *buf;

int main(int argc, char **argv)
{
        buf = malloc (512);
        memset (buf,"a",512);
        int fd = 0;
        int i;
        int sector_per_block = BLOCK_SIZE/512;
        int block_count = 4;

        if(argc != 2) {
                printf ("Wrong usage\n USAGE: program absolute_path_to_write\n");
                _exit (-1);
        }

        fd = open (argv[1],O_RDWR | O_CREAT,0666);
        if (fd <= 0) {
                printf ("file open failed\n");
                _exit (0);
        }

        while (block_count > 0) {
                lseek (fd, BLOCK_SIZE, SEEK_CUR);
                block_count--;

                for(i = 0; i < sector_per_block; i++)
                        write (fd, buf, 512);

                block_count--;
        }

        close(fd);

        return 0;
}