root/lang/c/blockdiff/trunk/blockdiff_merge.c @ 10798

Revision 10798, 0.9 kB (checked in by kazuho, 5 years ago)

initial import

Line 
1#include <fcntl.h>
2#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
5#include <openssl/crypto.h>
6
7#define BLOCK_SIZE (16384)
8
9
10/* read patch data from STDIN and apply to argv[0] */
11
12int main(int argc, char **argv)
13{
14  int fd;
15  unsigned char block[BLOCK_SIZE];
16  size_t block_size;
17  long long offset;
18 
19  if (argc != 2) {
20    fprintf(stderr, "Usage: %s <file>\n", argv[0]);
21    exit(99);
22  }
23 
24  if ((fd = open(argv[1], O_RDWR | O_CREAT, 0666)) == -1) {
25    perror("failed to open file");
26    exit(1);
27  }
28 
29  while (fread(&offset, 1, sizeof(offset), stdin) == sizeof(offset)) {
30    if ((block_size = fread(block, 1, sizeof(block), stdin)) <= 0) {
31      perror("unexpected eof");
32      exit(2);
33    }
34    if (lseek(fd, offset, SEEK_SET) == -1) {
35      perror("seek failed");
36      exit(3);
37    }
38    if (write(fd, block, block_size) != block_size) {
39      perror("write failed");
40      exit(3);
41    }
42  }
43 
44  close(fd);
45 
46  return 0;
47}
Note: See TracBrowser for help on using the browser.