about summary refs log tree commit diff
path: root/csrc/cp.c
blob: 5280bae87e0e94f2ee989a6d11e74bb9435aac79 (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
#include "cp.h"
#include <stdio.h>

FileStatus copy_file(char *src, char *dst) {

  FILE *source = fopen(src, "rb");
  if (!source) {
    return READ_ERROR;
  }

  FILE *destination = fopen(dst, "wb");
  if (!destination) {
    fclose(source);
    return WRITE_ERROR;
  }

  char buffer[1024];
  size_t bytes;

  while ((bytes = fread(buffer, 1, sizeof(buffer), source)) > 0) {
    fwrite(buffer, 1, bytes, destination);
  }

  fclose(source);
  fclose(destination);

  return WRITE_OK;
}