mycp命令,功能主要包括
1.文件到文件
2.文件到目录
3.目录到目录(当目标目录不存在,创建后复制,如果存在就复制到目标目录下面)
复制了多少字节。
现该程序有编译警告,即使运行成功且功能能实现,此篇文章仅供参考
my.h文件代码:
#ifndef __MYCP_H__
#define __MYCP_H__
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <dirent.h>
//判断path是否为目录
bool is_dir(char *path);
//复制文件到文件
int file_to_file(char *src_file, char* dest_file);
//复制目录到目录
int dir_to_dir(char* src_dir, char* dest_dir);
//复制文件到目录
int file_to_dir(char *src_file, char *dest_dir);
#endif
mycp.c文件代码:
#include "mycp.h"
int total_size = 0;//定义一个全局变量来计算文件的大小
int main(int argc, char *argv[])//argc[0]:可执行文件,argv[1]:源文件,argv[2]:目标文件
{
if (argc != 3)
{
perror("Usage srcfile destfile");//Usage(执行文件):./my_cp srcfile:源文件 destfile:目标文件
return -1;
}
if (access(argv[1], F_OK))//判断文件是否存在 返回值0
{
printf("%s 文件不存在!\n", argv[1])