免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2044 | 回复: 2
打印 上一主题 下一主题

[C] 带进度条的cp [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-20 19:03 |只看该作者 |倒序浏览
很喜欢C,但水平很差。希望能向高手们学习。
用cp命令时,有时遇到大点的文件,就只有傻等,不知道什么时候才能cp完毕。于是写了一个带有进度条的mcp,仅仅有最基本的功能。望高手们指点,改进。
新手上路,请轻拍。

mcp.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define ERR_EXIT(m)\
                do\
                {\
                        perror(m);\
                        exit(-1);\
                }while(0)

int p_len(int p_len, int sum_len);

int main(int argc, char* argv[])
{
        int s_file,d_file,s_size,d_size,num;
        struct stat st_buf;
        char buf[512];

        if(argc!=3)
        {
                printf("This is a program for copy file\n");
                printf("please input 2 file name\n");
                printf("example:\tmycp file1 file2\n");
                exit(-1);
        }

        s_file=open(argv[1],O_RDONLY);
        if(s_file==-1)
                ERR_EXIT("Open source file error");
        if(stat(argv[1],&st_buf))
                ERR_EXIT("Get source file stat error");
        s_size=st_buf.st_size;

        d_file=open(argv[2],O_WRONLY|O_CREAT,0644);
        if(d_file==-1)
                ERR_EXIT("Open destination file error");



        d_size=0;
        while((num=read(s_file,buf,512))>0)
        {
                if((write(d_file,buf,512))==-1)
                        ERR_EXIT("Write file error");
                sync();
                if(stat(argv[2],&st_buf))
                        ERR_EXIT("Get destination file error");
                d_size=st_buf.st_size;

                p_len(d_size,s_size);
        }

        printf("\n");
        printf("File copy to complete\n");
        close(s_file);
        close(d_file);

    return 0;
}

int p_len(int p_len, int sum_len)
{
        int i,width,sum_width;

    system("export COLUMNS");
    sum_width=atoi(getenv("COLUMNS"));
//    printf("The terminal width is:%d\n",width);
        width=sum_width-15;       

        printf("\r[");
        for(i=1;i<=abs(p_len*width/sum_len);i++)
                printf(">");
        for(i=1;i<=width-abs(p_len*width/sum_len);i++)
                printf("=");
        printf("]    %.2f%%",(float)p_len*100/sum_len);
        fflush(stdout);

        return 0;
}

论坛徽章:
30
摩羯座
日期:2013-12-23 17:28:38牛市纪念徽章
日期:2015-07-13 11:35:582022北京冬奥会纪念版徽章
日期:2015-08-07 17:10:57青铜圣斗士
日期:2015-11-27 17:45:3815-16赛季CBA联赛之天津
日期:2016-02-15 13:44:3615-16赛季CBA联赛之江苏
日期:2018-05-02 16:56:2715-16赛季CBA联赛之辽宁
日期:2018-08-08 13:41:1015-16赛季CBA联赛之深圳
日期:2018-10-02 18:05:0315-16赛季CBA联赛之天津
日期:2019-05-31 15:05:0615-16赛季CBA联赛之北京
日期:2022-06-30 13:34:1115-16赛季CBA联赛之同曦
日期:2022-07-06 19:33:5415-16赛季CBA联赛之吉林
日期:2022-12-28 14:16:22
2 [报告]
发表于 2013-03-21 13:57 |只看该作者
不支持目录copy这个程序还有什么意义

论坛徽章:
0
3 [报告]
发表于 2013-03-23 21:04 |只看该作者
不好意思,刚开始写。后面是准备加上目录copy功能的。
这个只能算是0.1版本。呵呵……
水平有限,见笑了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP