driver: add dev_name inline
[barebox-mini2440.git] / lib / copy_file.c
blob0ff0435f11c4b686724b28de2c3251f420410365
1 #include <common.h>
2 #include <fs.h>
3 #include <fcntl.h>
4 #include <errno.h>
5 #include <malloc.h>
6 #define RW_BUF_SIZE (ulong)4096
8 /**
9 * @param[in] src FIXME
10 * @param[out] dst FIXME
12 int copy_file(const char *src, const char *dst)
14 char *rw_buf = NULL;
15 int srcfd = 0, dstfd = 0;
16 int r, w;
17 int ret = 1;
19 rw_buf = xmalloc(RW_BUF_SIZE);
21 srcfd = open(src, O_RDONLY);
22 if (srcfd < 0) {
23 printf("could not open %s: %s\n", src, errno_str());
24 goto out;
27 dstfd = open(dst, O_WRONLY | O_CREAT);
28 if (dstfd < 0) {
29 printf("could not open %s: %s\n", dst, errno_str());
30 goto out;
33 while(1) {
34 r = read(srcfd, rw_buf, RW_BUF_SIZE);
35 if (r < 0) {
36 perror("read");
37 goto out;
39 if (!r)
40 break;
41 w = write(dstfd, rw_buf, r);
42 if (w < 0) {
43 perror("write");
44 goto out;
48 ret = 0;
49 out:
50 free(rw_buf);
51 if (srcfd > 0)
52 close(srcfd);
53 if (dstfd > 0)
54 close(dstfd);
56 return ret;