APUE course source code
guowenxue
2 days ago 01c119252bae1e252aa9e6bdba99f1b2ee756624
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  mycp.c
 *    Description:  This file a simple file copy command implement
 *
 *        Version:  1.0.0(10/15/2025)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/15/2025 11:19:27 AM"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
 
int main(int argc, char *argv[])
{
    struct stat     src_stat;
    int             src_fd = -1;
    int             dst_fd = -1;
    char            buf[512];
    ssize_t         bytes_read;
    ssize_t         to_read;
    off_t           remaining;
    int             rv = 0;
 
    /* 检查输入参数合法性, 此程序需要两个参数: 源文件和目标文件 */
    if (argc != 3)
    {
        fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]);
        return 1;
    }
 
    /* 获取源文件的属性信息 */
    if (lstat(argv[1], &src_stat) == -1)
    {
        fprintf(stderr, "lstat file %s failure: %s\n", argv[1], strerror(errno));
        return 2;
    }
 
    /* 此程序只支持普通文件拷贝,不支持文件夹或其它文件 */
    if (!S_ISREG(src_stat.st_mode))
    {
        fprintf(stderr, "Error: %s is not a regular file\n", argv[1]);
        return 2;
    }
 
    /* 打以只读模式打开要拷贝的源文件 */
    src_fd = open(argv[1], O_RDONLY);
    if (src_fd == -1)
    {
        fprintf(stderr, "open file %s failure: %s\n", argv[1], strerror(errno));
        rv = 3;
        goto cleanup;
    }
 
    /* 创建要拷贝的目标文件,如果不存在就创建,权限位与源文件一致。如果文件已经存在则截取 */
    dst_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, src_stat.st_mode & 0777);
    if (dst_fd == -1)
    {
        fprintf(stderr, "open file %s failure: %s\n", argv[2], strerror(errno));
        rv = 3;
        goto cleanup;
    }
 
    /* 获取源文件的实际内容大小,并将其所有内容拷贝到目标文件中去 */
    remaining = src_stat.st_size;
    while (remaining > 0)
    {
        to_read = remaining > sizeof(buf) ? sizeof(buf) : remaining;
 
        bytes_read = read(src_fd, buf, to_read);
        if (bytes_read == -1)
        {
            fprintf(stderr, "read file %s failure: %s\n", argv[1], strerror(errno));
            rv = 3;
            goto cleanup;
        }
 
        if (bytes_read == 0)
        {
            fprintf(stderr, "Unexpected end of file\n");
            rv = 3;
            goto cleanup;
        }
 
        if (write(dst_fd, buf, bytes_read) != bytes_read)
        {
            fprintf(stderr, "write file %s failure: %s\n", argv[2], strerror(errno));
            rv = 3;
            goto cleanup;
        }
 
        remaining -= bytes_read;
    }
 
    /* 完成后关闭所有打开的文件描述符,如果出错也会goto到这里集中处理 */
cleanup:
    if(src_fd >= 0)
       close(src_fd);
 
    if(dst_fd >= 0)
       close(src_fd);
 
    /* rv在定义时默认初始化为0表示成功,如果代码中有错误发生则会改变rv值,标识相应的错误原因 */
    return rv;
}