#include <linux/kernel.h>
#include <linux/syscalls.h>
#include <linux/fs.h>
#include <linux/string.h>

asmlinkage long sys_copyfile(const char *src_name, const char *tgt_name ) {
  char swap[1024];
  int fp_src, fp_tgt;// fp source and fp target;
  int j = 1024;//read 1024 bytes one time;
  int flag_name_check;
  mm_segment_t old_fs =get_fs();
  set_fs (KERNEL_DS);
  flag_name_check = strcmp(src_name, tgt_name);
  if (flag_name_check == 0) {
    printk("Error! your source flie and target file have the same name.\n");
    return 0;
}
  printk("copy processing...\n");
  fp_src = sys_open(src_name, O_RDONLY, 0);
  fp_tgt = sys_open(tgt_name, O_WRONLY|O_CREAT|O_TRUNC, 0600);
  if (!fp_src) {
    printk("error open source file\n");
    return 0;
  }
  if (!fp_tgt) {
    printk("error open target file\n");
    return 0;
  }
  while ( j == 1024) {//read uncomplete
    j = sys_read(fp_src, swap, 1024);
    sys_write(fp_tgt, swap, j);
  }
  sys_close(fp_src);
  sys_close(fp_tgt);

  printk("copy finished!\n");
  set_fs (old_fs);
  return 0;
}
