資源簡介
完成一個目錄復制命令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
評論
共有 條評論