国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > linux下簡單cp命令的實(shí)現(xiàn)

    linux下簡單cp命令的實(shí)現(xiàn)

    時(shí)間: 志藝942 分享

    linux下簡單cp命令的實(shí)現(xiàn)

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

      linux下簡單cp命令的實(shí)現(xiàn)

      實(shí)現(xiàn)功能:

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

      $./cp ~/directory/filename . //文件到當(dāng)前目錄的拷貝

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

      不白費(fèi)口舌,直接上代碼才是王道!

      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) { //把讀取部分寫入目標(biāo)文件

      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; //分別指向源文件路徑和目標(biāo)文件路徑

      055

      struct stat src_st,des_st;

      056

      057

      if (argc < 3) {

      058

      printf("用法 ./MyCp <源文件路徑> <目標(biāo)文件路徑>\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)) { //目標(biāo)路徑是目錄,則使用源文件的文件名

      078

      079

      len = strlen(argv[1]);

      080

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

      081

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

      082

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

      083

      pSrc--;

      084

      }

      085

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

      086

      087

      len = strlen(argv[2]);

      088

      // . 表示復(fù)制到當(dāng)前工作目錄

      089

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

      090

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

      091

      pDes = pSrc;

      092

      }

      093

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

      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)) != '/' ) { //目錄缺少最后的'/',則補(bǔ)上’/‘

      102

      strcat(pDes,"/");

      103

      }

      104

      strcat(pDes+len,pSrc);

      105

      }

      106

      }

      107

      108

      /* 打開目標(biāo)文件, 使權(quán)限與源文件相同*/

      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命令的實(shí)現(xiàn)”還想看:

    1.linux cp命令簡單的實(shí)現(xiàn)

    2.Linux下如何使用cp命令

    3.Linux系統(tǒng)怎么使用cp命令

    4.linux cp命令使用介紹

    5.Linux cp命令學(xué)習(xí)筆記

    2999669