资源简介
完成一个目录复制命令mycp,包括目录下的文件和子目录, 运行结果如下:
说明:
Linux: creat,read,write等系统调用,要求支持软链接
Windows: CreateFile(), ReadFile(), WriteFile(), CloseHandle()等函数

代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void myfcopy(char *src char *des);
void mydcopy(char *src char *des);
int main(int argc char **argv)
{
struct utimbuf timeby;
struct stat ftype;
if(argc != 3)
{
printf(“command should be like ‘cp src dir‘\n“);
exit(0);
}
else
{
// struct stat ftype;
int fd;
if( lstat(argv[1]&ftype) == -1 )
{
perror(“lstat“);
exit(0);
}
if(S_ISDIR(ftype.st_mode))
{
mkdir(argv[2] ftype.st_mode) ;
mydcopy(argv[1] argv[2]);
}
else
{
myfcopy(argv[1] argv[2]);
}
}
stat(argv[1] &ftype);
timeby.actime = ftype.st_atime;
timeby.modtime = ftype.st_mtime;
utime(argv[2] &timeby);
printf(“cp has sucessed!\n“);
return 0;
}
void mydcopy(char *src char *des)
{
DIR *dirptr = NULL;
struct dirent *entry = NULL;
struct stat ftype;
char source[512];
char target[512];
char buffer[1024];
struct utimbuf timeby;
struct timeval times[2];
memset(buffer 0 sizeof(buffer));
dirptr = opendir(src);
strcpy(source src);
strcpy(target des);
while(entry = readdir(dirptr))
{
//stat(entry->d_name &ftype);
if( (strcmp(entry->d_name “.“) == 0) || (strcmp(entry->d_name “..“) == 0 ))
continue;
if(entry->d_type == DT_DIR)
{
strcat(target “/“);
strcat(target entry->d_name);
strcat(source “/“);
strcat(source entry->d_name);
lstat(source &ftype);
mkdir(target ftype.st_mode);
timeby.actime = ftype.st_atime;
timeby.modtime = ftype.st_mtime;
utime(des &timeby);
mydcopy(source target);
strcpy(source src);
strcpy(target des);
}
else if(entry->d_type == DT_LNK)
{
strcat(target “/“);
strcat(target entry->d_name);
strcat(source “/“);
strcat(source entry->d_name);
lstat(source &ftype);
readlink(source buffer sizeof(buffer));
if(symlink(buffer target) == -1)
{
perror(“symlink“);
exit(0);
}
times[0].tv_sec = ftype.st_atime;
times[0].tv_usec = 0;
times[1].tv_sec = ftype.st_mtime;
times[1].tv_usec = 0;
lutimes(target times);
}
else
{
strcat(target “/“);
strcat(target entry->d_name);
strcat(source “/“);
strcat(source entry->d_name);
myfcopy(source target);
strcpy(source src);
strcpy(target des);
}
}
}
void myfcopy(char *src char *des)
{
struct utimbuf timeby;
struct stat ftype;
int fdfdd;
char buf[1024];
int nbit;
int count = 0;
//struct timbuf timeby;
if(lstat(src &ftype) == -1)
{
perror(“lstat“);
exit(0);
}
fd = open(src O_RDONLY);
fdd = creat(des ftype.st_mode);
while((nbit = read(fd buf 1024)) > 0)
{
// count++;
// printf(“count = %d\n“count);
write(fdd buf nbit);
}
close(fd);
close(fdd);
timeby
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-05-05 16:42 1120122053-杨雯雯-5-源代码\Linux\
文件 3089 2015-05-05 10:56 1120122053-杨雯雯-5-源代码\Linux\linux.c
目录 0 2015-05-05 16:40 1120122053-杨雯雯-5-源代码\window\
文件 5650 2015-05-05 10:44 1120122053-杨雯雯-5-源代码\window\window.cpp
文件 146216 2015-05-05 10:44 1120122053-杨雯雯-5-源代码\window\window.exe
- 上一篇:《数字信号处理》课后习题.zip
- 下一篇:简易异步FIFO代码及testbench
相关资源
- 北理工《电路与电子线路实验Ⅳ硬件
- 北理工《电路与电子线路实验Ⅲ硬件
- 北理工《控制理论基础》复习总结
- 北理工《信号与信息处理实验Ⅱ》实
- 北理工《信号与信息处理实验Ⅰ》实
- 北理工《复变函数与数理方程》复习
- 北理工认定的顶级刊物、重要刊物-
- 计算机原理硬件实验三使用8251A的串行
- 计算机原理硬件实验二使用8259中断
- 北理工随机信号分析实验报告
- 《矩阵分析学习指导》史荣昌 北理工
- 矩阵分析(北理工)--教材.pdf
- 计算机原理硬件实验一8255A可编程并行
- 电磁场虚拟实验报告北理工
- 北理工《数字电路》复习总结
- 自动识别U盘,批量复制文件夹到多个
- 矩阵分析学习指导(完整pdf版) 北理
- 电磁场与微波课程设计北理工
- 计算机原理硬件实验四 A/D和D/A转换
- 北理工《数字通信原理》复习总结
- 北京理工大学信息安全与对抗网课资
- FSK传输实验 通信原理实验报告
- 北理工2018最优化方法大作业90.zip
- 1.2北理工成电东南.pdf
- 北理工《概率与数理统计》复习总结
- 封装CopyFileEx复制文件,提供同步、异
- 北京理工大学答辩模板 (1).pptx
- 北理工《电路与电子线路实验Ⅱ》实
- Qt 复制文件 实现进度条显示
- 调用 SHFileOperation 函数实现复制文件或
评论
共有 条评论