Chinaunix首页 | 论坛 | 博客
  • 博客访问: 486828
  • 博文数量: 23
  • 博客积分: 398
  • 博客等级: 一等列兵
  • 技术积分: 850
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 22:18
文章分类

全部博文(23)

文章存档

2013年(9)

2012年(14)

分类: LINUX

2012-06-05 10:41:16

实现功能:
$./cp ~/filename ~/OtherName //文件到文件的拷贝

$./cp ~/directory/filename . //文件到当前目录的拷贝

$./cp ~/directory/filename ~/directory/ //文件到目录的拷贝
 
好了,不白费口舌了,直接上代码才是王道!
 

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/stat.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <string.h>

  9. #define BUF_SIZE 1024
  10. #define PATH_LEN 128

  11. void my_err(char *err_string, int line )
  12. {
  13.     fprintf(stderr,"line:%d ",line);
  14.     perror(err_string);
  15.     exit(1);
  16. }

  17. void copy_data(const int frd,const int fwd)
  18. {
  19.     int read_len = 0, write_len = 0;
  20.     unsigned char buf[BUF_SIZE], *p_buf;

  21.     while ( (read_len = read(frd,buf,BUF_SIZE)) ) {
  22.         
  23.         if (-1 == read_len) {
  24.             my_err("Read error", __LINE__);
  25.         }
  26.         else if (read_len > 0) { //把读取部分写入目标文件
  27.             p_buf = buf;
  28.             while ( (write_len = write(fwd,p_buf,read_len)) ) {
  29.                 if(write_len == read_len) {
  30.                     break;
  31.                 }
  32.                 else if (write_len > 0) { //只写入部分
  33.                     p_buf += write_len;
  34.                     read_len -= write_len;
  35.                 }
  36.                 else if(-1 == write_len) {
  37.                     my_err("Write error", __LINE__);
  38.                 }
  39.             }
  40.             if (-1 == write_len) break;
  41.         }
  42.     }
  43. }

  44. int main(int argc, char **argv)
  45. {
  46.     
  47.     int frd, fwd; //读写文件描述符
  48.     int len = 0;
  49.     char *pSrc, *pDes; //分别指向源文件路径和目标文件路径
  50.     struct stat src_st,des_st;
  51.     
  52.     if (argc < 3) {
  53.         printf("用法 ./MyCp <源文件路径> <目标文件路径>\n");
  54.         my_err("arguments error ", __LINE__);
  55.     }
  56.     
  57.     frd = open(argv[1],O_RDONLY);
  58.     if (frd == -1) {
  59.         my_err("Can not opne file", __LINE__);
  60.     }

  61.     if (fstat(frd,&src_st) == -1) {
  62.         my_err("stat error",__LINE__);
  63.     }
  64.     /*检查源文件路径是否是目录*/
  65.     if (S_ISDIR(src_st.st_mode)) {
  66.         my_err("略过目录",__LINE__);
  67.     }
  68.     
  69.     pDes = argv[2];
  70.     stat(argv[2],&des_st);
  71.     if (S_ISDIR(des_st.st_mode)) { //目标路径是目录,则使用源文件的文件名
  72.         
  73.         len = strlen(argv[1]);
  74.         pSrc = argv[1] + (len-1); //指向最后一个字符
  75.         /*先找出源文件的文件名*/
  76.         while (pSrc >= argv[1] && *pSrc != '/') {
  77.             pSrc--;
  78.         }
  79.         pSrc++;//指向源文件名
  80.         
  81.         len = strlen(argv[2]);
  82.         // . 表示复制到当前工作目录
  83.         if (1 == len && '.' == *(argv[2])) {
  84.             len = 0; //没有申请空间,后面就不用释放
  85.             pDes = pSrc;
  86.         }
  87.         else { //复制到某目录下,使用源文件名
  88.             pDes = (char *)malloc(sizeof(char)*PATH_LEN);
  89.             if (NULL == pDes) {
  90.                 my_err("malloc error ", __LINE__);
  91.             }
  92.             
  93.             strcpy(pDes,argv[2]);
  94.         
  95.             if ( *(pDes+(len-1)) != '/' ) { //目录缺少最后的'/',则补上’/
  96.                 strcat(pDes,"/");
  97.             }
  98.             strcat(pDes+len,pSrc);
  99.         }
  100.     }
  101.     
  102.     /* 打开目标文件, 使权限与源文件相同*/
  103.     fwd = open(pDes,O_WRONLY | O_CREAT | O_TRUNC,src_st.st_mode);
  104.     if (fwd == -1) {
  105.         my_err("Can not creat file", __LINE__);
  106.     }
  107.     copy_data(frd,fwd);
  108.     //puts("end of copy");
  109.     if (len > 0 && pDes != NULL)
  110.         free(pDes);
  111.     
  112.     close(frd);
  113.     close(fwd);

  114.     return 0;
  115. }
注:本博文来自我原来的blog,因不再更新所以贴到这里了。

阅读(5963) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:使用 vim ctags cscope taglist 阅读源码

给主人留下些什么吧!~~