平常用cp命令的时候,cp一个大文件,不知道要等多久。自己就写了一个mcp。只有最基本的cp功能,加上一个进度条。
抛砖引玉,望能于同好者一起交流,学习。hstking AT hotmail.com
#include
#include
#include
#include
#include
#include
#include
#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;
}
haohu137 于 2013-05-09 19:07:16发表:
路过,看看。
Dsucov 于 2013-03-16 11:36:48发表:
强