linux中cp命令如何用 C语言实现

linux中cp命令如何用 C语言实现,第1张

1,首先需要了解cp的原理。

2,可以参考cp的源码去了解其原理

3,cp命令的源码可以在linux内核中找到。

4,或者下载busybox其中也会有cp的源码

只有了解其原理之后才能谈如何实现。参考代码如下:

#include <stdio.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <fcntl.h>

#include <errno.h>

#include <unistd.h>

#include <string.h>

#define BUF_SIZE 1024

#define PATH_LEN 128

void my_err(char *err_string, int line )

{

    fprintf(stderr,"line:%d ",line)

    perror(err_string) 

    exit(1)

}

void copy_data(const int frd,const int fwd)

{

    int read_len = 0, write_len = 0

    unsigned char buf[BUF_SIZE], *p_buf

    while ( (read_len = read(frd,buf,BUF_SIZE)) ) {

        

        if (-1 == read_len) {

            my_err("Read error", __LINE__)

        }

        else if (read_len > 0) { //把读取部分写入目标文件

            p_buf = buf

            while ( (write_len = write(fwd,p_buf,read_len)) ) {

                if(write_len == read_len) {

                    break

                }

                else if (write_len > 0) { //只写入部分

                    p_buf += write_len

                    read_len -= write_len

                }

                else if(-1 == write_len) {

                    my_err("Write error", __LINE__)

                }

            }

            if (-1 == write_len) break

        }

    }

}

int main(int argc, char **argv) 

{

    

    int frd, fwd //读写文件描述符

    int len = 0

    char *pSrc, *pDes //分别指向源文件路径和目标文件路径

    struct stat src_st,des_st

    

    if (argc < 3) {

        printf("用法 ./MyCp <源文件路径> <目标文件路径>\n")

        my_err("arguments error ", __LINE__)

    }

    

    frd = open(argv[1],O_RDONLY)

    if (frd == -1) {

        my_err("Can not opne file", __LINE__)

    }

    if (fstat(frd,&src_st) == -1) {

        my_err("stat error",__LINE__)

    }

    /*检查源文件路径是否是目录*/

    if (S_ISDIR(src_st.st_mode)) {

        my_err("略过目录",__LINE__)

    }

    

    pDes = argv[2]

    stat(argv[2],&des_st)

    if (S_ISDIR(des_st.st_mode)) { //目标路径是目录,则使用源文件的文件名

        

        len = strlen(argv[1])

        pSrc = argv[1] + (len-1) //指向最后一个字符

        /*先找出源文件的文件名*/

        while (pSrc >= argv[1] && *pSrc != '/') {

            pSrc--

        }

        pSrc++//指向源文件名

        

        len = strlen(argv[2]) 

        // . 表示复制到当前工作目录

        if (1 == len && '.' == *(argv[2])) {

            len = 0 //没有申请空间,后面就不用释放

            pDes = pSrc

        }

        else { //复制到某目录下,使用源文件名

            pDes = (char *)malloc(sizeof(char)*PATH_LEN)

            if (NULL == pDes) {

                my_err("malloc error ", __LINE__)

            }

            

            strcpy(pDes,argv[2])

        

            if ( *(pDes+(len-1)) != '/' ) { //目录缺少最后的'/',则补上’/‘

                strcat(pDes,"/")

            }

            strcat(pDes+len,pSrc)

        }

    }

    

    /* 打开目标文件, 使权限与源文件相同*/ 

    fwd = open(pDes,O_WRONLY | O_CREAT | O_TRUNC,src_st.st_mode)

    if (fwd == -1) {

        my_err("Can not creat file", __LINE__)

    }

    copy_data(frd,fwd)

    //puts("end of copy")

    if (len > 0 && pDes != NULL)

        free(pDes)

    

    close(frd)

    close(fwd)

    return 0

}

一个是本地 , 一个是远程。

cp 是普通的文件复制scp 是通过ssh 本机和其他电脑上的复制。

scp是secure copy,也是一个复制命令,是与ssh有关的。

scp user@ip:/dir/file file

或者:scp file user@ip:/dir/file

要输入user在远端用户的密码。

cp ,使用权限:所有使用者

使用方式:

cp [options] source dest

cp [options] source... directory

说明:将一个档案拷贝至另一档案,或将数个档案拷贝至另一目录。

关于CP的具体使用办法:

命令基本格式: 

cp [OPTIONS] SOURCE DEST --- 从源路径copy文件到目的路径 

cp [OPTIONS] SOURCE... DIRECTORY --- 将多个源文件copy到指定的目录(多个源文件用空格分隔)

OPTIONS:

-a same as -dpR 尽可能将源文件状态、权限等资料都照原装予以复制,并且是递归copy; 

-r 表示递归copy,若source中含有目录名,则将目录下之档案亦皆依序拷贝至目的地; 

-f 表示force,若目的地已经有相同档名的档案存在,则在复制前先予以删除再行复制;

关于SCP的具体使用办法:

就是ftp,也就是其中一台Linux安装ftp Server,这样可以在另外一台机器使用ftp的client程序来进行文件的copy。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/6024014.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-12
下一篇2023-03-12

发表评论

登录后才能评论

评论列表(0条)

    保存