歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux下簡單cp命令的實現

linux下簡單cp命令的實現

日期:2017/4/19 14:16:16   编辑:Linux技術

  在使用cp拷貝當前目錄下所有文件到目標目錄的時候,源和目標目錄大小不同。原來一直沒有留意有這樣的問題,後來查了些資料,才知道以前一直使用的格式有誤。那麼你知道linux下簡單cp命令的實現麼?接下來是小編為大家收集的linux下簡單cp命令的實現,歡迎大家閱讀:

  linux下簡單cp命令的實現

  實現功能:

  $./cp ~/filename ~/OtherName //文件到文件的拷貝

  $./cp ~/directory/filename . //文件到當前目錄的拷貝

  $./cp ~/directory/filename ~/directory/ //文件到目錄的拷貝

  不白費口舌,直接上代碼才是王道!

  001

  #include

  002

  #include

  003

  #include

  004

  #include

  005

  #include

  006

  #include

  007

  #include

  008

  #include

  009 www.2cto.com

  010

  #define BUF_SIZE 1024

  011

  #define PATH_LEN 128

  012

  013

  void my_err(char *err_string, int line )

  014

  {

  015

  fprintf(stderr,"line:%d ",line);

  016

  perror(err_string);

  017

  exit(1);

  018

  }

  019

  020

  void copy_data(const int frd,const int fwd)

  021

  {

  022

  int read_len = 0, write_len = 0;

  023

  unsigned char buf[BUF_SIZE], *p_buf;

  024

  025

  while ( (read_len = read(frd,buf,BUF_SIZE)) ) {

  026

  027

  if (-1 == read_len) {

  028

  my_err("Read error", __LINE__);

  029

  }

  030

  else if (read_len > 0) { //把讀取部分寫入目標文件

  031

  p_buf = buf; www.2cto.com

  032

  while ( (write_len = write(fwd,p_buf,read_len)) ) {

  033

  if(write_len == read_len) {

  034

  break;

  035

  }

  036

  else if (write_len > 0) { //只寫入部分

  037

  p_buf += write_len;

  038

  read_len -= write_len;

  039

  }

  040

  else if(-1 == write_len) {

  041

  my_err("Write error", __LINE__);

  042

  }

  043

  }

  044

  if (-1 == write_len) break;

  045

  }

  046

  }

  047

  }

  048

  049

  int main(int argc, char **argv)

  050

  {

  051

  052

  int frd, fwd; //讀寫文件描述符

  053

  int len = 0;

  054

  char *pSrc, *pDes; //分別指向源文件路徑和目標文件路徑

  055

  struct stat src_st,des_st;

  056

  057

  if (argc < 3) {

  058

  printf("用法 ./MyCp <源文件路徑> <目標文件路徑>\n");

  059

  my_err("arguments error ", __LINE__);

  060

  }

  061

  062

  frd = open(argv[1],O_RDONLY);

  063

  if (frd == -1) {

  064

  my_err("Can not opne file", __LINE__);

  065

  }

  066

  067

  if (fstat(frd,&src_st) == -1) {

  068

  my_err("stat error",__LINE__);

  069

  } www.2cto.com

  070

  /*檢查源文件路徑是否是目錄*/

  071

  if (S_ISDIR(src_st.st_mode)) {

  072

  my_err("略過目錄",__LINE__);

  073

  }

  074

  075

  pDes = argv[2];

  076

  stat(argv[2],&des_st);

  077

  if (S_ISDIR(des_st.st_mode)) { //目標路徑是目錄,則使用源文件的文件名

  078

  079

  len = strlen(argv[1]);

  080

  pSrc = argv[1] + (len-1); //指向最後一個字符

  081

  /*先找出源文件的文件名*/

  082

  while (pSrc >= argv[1] && *pSrc != '/') {

  083

  pSrc--;

  084

  }

  085

  pSrc++;//指向源文件名

  086

  087

  len = strlen(argv[2]);

  088

  // . 表示復制到當前工作目錄

  089

  if (1 == len && '.' == *(argv[2])) {

  090

  len = 0; //沒有申請空間,後面就不用釋放

  091

  pDes = pSrc;

  092

  }

  093

  else { //復制到某目錄下,使用源文件名

  094

  pDes = (char *)malloc(sizeof(char)*PATH_LEN);

  095 www.2cto.com

  if (NULL == pDes) {

  096

  my_err("malloc error ", __LINE__);

  097

  }

  098

  099

  strcpy(pDes,argv[2]);

  100

  101

  if ( *(pDes+(len-1)) != '/' ) { //目錄缺少最後的'/',則補上’/‘

  102

  strcat(pDes,"/");

  103

  }

  104

  strcat(pDes+len,pSrc);

  105

  }

  106

  }

  107

  108

  /* 打開目標文件, 使權限與源文件相同*/

  109

  fwd = open(pDes,O_WRONLY | O_CREAT | O_TRUNC,src_st.st_mode);

  110

  if (fwd == -1) {

  111

  my_err("Can not creat file", __LINE__);

  112

  }

  113

  copy_data(frd,fwd);

  114

  //puts("end of copy");

  115

  if (len > 0 && pDes != NULL)

  116 www.2cto.com

  free(pDes);

  117

  118

  close(frd);

  119

  close(fwd);

  120

  121

  return 0;

  122

  }

  
看了“linux下簡單cp命令的實現”還想看:

1.linux cp命令簡單的實現

2.Linux下如何使用cp命令

3.Linux系統怎麼使用cp命令

4.linux cp命令使用介紹

5.Linux cp命令學習筆記

Copyright © Linux教程網 All Rights Reserved