写一个Linux下的cp命令(mycp命令)

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])
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖小邓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值