-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappend.c
More file actions
40 lines (36 loc) · 733 Bytes
/
Copy pathappend.c
File metadata and controls
40 lines (36 loc) · 733 Bytes
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
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <aio.h>
main(argc, argv)
int argc;
char *argv[];
{
/* append [dest] [src] */
int src, dst;
struct stat xsrc, xdst;
off_t newsz;
off_t off_out;
ssize_t rc;
if (argc != 3)
exit(-1);
dst = open(argv[1], O_RDWR);
src = open(argv[2], O_RDONLY);
if (dst == -1 || src == -1)
exit(-1);
fstat(dst, &xdst);
fstat(src, &xsrc);
newsz = xdst.st_size + xsrc.st_size;
if (newsz < xdst.st_size)
exit(-1);
ftruncate(dst, newsz);
off_out = xdst.st_size;
rc = copy_file_range(src, NULL, dst, &off_out, xsrc.st_size, 0);
if (rc < xsrc.st_size) {
printf("error %ld\n", rc);
}
close(dst);
close(src);
}