中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C語言自己實(shí)現(xiàn)linux下cp文件復(fù)制命令

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
 
int main(int argc, char *argv[])
{
    char *buf = NULL;
    int fd_src = -1;
    int fd_dest = -1;
    int buf_size = 4096;
    int cnt = 0;
    struct flock lock;
 
    if (argc < 3) {
        printf("usage: %s <src_file> <dest_file> [buf_size]\n", argv[0]);
        return 0;
    }
 
    if ((fd_src=open(argv[1], O_RDONLY)) == -1) {
        perror("open src. file");
        return 1;
    }
    if ((fd_dest=open(argv[2], O_WRONLY|O_CREAT|O_EXCL, 0664)) == -1) {
        perror("open dest. file");
        close(fd_src);
        return 2;
    }
    if (argc == 4) {
        buf_size = atoi(argv[3]);
    }
    buf = sbrk(buf_size);
    if ((void*)-1 == buf) {
        perror("malloc");
        close(fd_src);
        close(fd_dest);
        return 3;
    }
 
    lock.l_type = F_RDLCK;
    lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len = 0;
    lock.l_pid = -1;
    if (fcntl(fd_src, F_SETLK, &lock) == -1) {
        fprintf(stderr, "%s has been locked by other process\n", argv[1]);
        close(fd_src);
        close(fd_dest);
        return 8;
    }
 
    errno = 0;
    while ((cnt = read(fd_src, buf, buf_size)) > 0) {
        if (write(fd_dest, buf, cnt) < cnt) {
            perror("write");
            close(fd_src);
            close(fd_dest);
            return 4;
        }
    }
    if (0 != errno) {
        perror("read");
    }
    close(fd_src);
    fd_src = -1;
    close(fd_dest);
    fd_dest = -1;
    brk(buf);
 
    return 0;
}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:PHP 實(shí)現(xiàn)頁面無刷新上傳文件

下一篇: PHP時(shí)間處理函數(shù)