APUE Learning Example Source Code
guowenxue
2023-11-06 14bbb45d375b611d19494874996e94aad64cf912
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*********************************************************************************
 *      Copyright:  (C) 2023 LingYun IoT System Studio.
 *                  All rights reserved.
 *
 *       Filename:  mycp.c
 *    Description:  This file is ls command example code.
 *
 *        Version:  1.0.0(2023年08月08日)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "2023年08月08日 17时17分00秒"
 *
 ********************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
 
#define PROGNAME        "myls"
 
static int myls(char *path);
 
int main (int argc, char **argv)
{
    int           fd;
    int           i;
 
    if( 1 == argc )
    {
        myls(".");
        return 0;
    }
 
    for(i=1; i<argc; i++)
    {
        myls(argv[i]);
    }
 
    return 0;
}
 
void print_filemode(mode_t mode)
{
    /* print file type */
    if( S_ISREG(mode) )
    {
        printf("-");
    }
    else if( S_ISDIR(mode) )
    {
        printf("d");
    }
    else if( S_ISCHR(mode) )
    {
        printf("c");
    }
    else if( S_ISBLK(mode) )
    {
        printf("b");
    }
    else if( S_ISFIFO(mode) )
    {
        printf("f");
    }
    else if( S_ISLNK(mode) )
    {
        printf("l");
    }
    else if( S_ISSOCK(mode) )
    {
        printf("s");
    }
 
    /* print file mode  */
    printf("%c", (S_IRUSR&mode) ? 'r' : '-');
    printf("%c", (S_IWUSR&mode) ? 'w' : '-');
    printf("%c", (S_IXUSR&mode) ? 'x' : '-');
 
    printf("%c", (S_IRGRP&mode) ? 'r' : '-');
    printf("%c", (S_IWGRP&mode) ? 'w' : '-');
    printf("%c", (S_IXGRP&mode) ? 'x' : '-');
 
    printf("%c", (S_IROTH&mode) ? 'r' : '-');
    printf("%c", (S_IWOTH&mode) ? 'w' : '-');
    printf("%c", (S_IXOTH&mode) ? 'x' : '-');
 
    printf(" ");
 
    return ;
}
 
int print_info(char *file)
{
    struct stat   sbuf;
    struct passwd *u;
    struct group  *g;
    time_t         t;
    struct tm     *ptm;
    char           mtime[32];
 
    stat(file, &sbuf);
 
    print_filemode(sbuf.st_mode);
 
    /* print number of hard links  */
    printf("%2lu ", sbuf.st_nlink);
 
    /* print user name or user id */
    u = getpwuid (sbuf.st_uid);
    if( u )
        printf("%s ", u->pw_name);
    else
        printf("%d ", sbuf.st_uid);
 
    /* print group name or group id */
    g = getgrgid (sbuf.st_gid);
    if( g )
        printf("%s ", g->gr_name);
    else
        printf("%d ", sbuf.st_uid);
 
    /* print file size  */
    printf("%6lu ", sbuf.st_size);
 
    /* print time of last modification, Time functions:
     *    https://www.jianshu.com/p/e92ced44f216
     *    https://blog.csdn.net/chen1415886044/article/details/106883544
     */
    t = (time_t)sbuf.st_mtim.tv_sec;
    ptm = localtime(&t);
    strftime(mtime, sizeof(mtime), "%b %e %R", ptm);
    printf("%s ", mtime);
 
    printf("%s", file);
 
    printf("\n");
 
    return 0;
}
 
int myls(char *path)
{
    struct stat    sbuf;
    DIR           *dirp;
    struct dirent *dir;
    char           filepath[512];
 
    /* Check file exist or not */
    if( access(path, F_OK) )
    {
        printf("%s: cannot access '%s': %s\n", PROGNAME, path, strerror(errno));
        return -2;
    }
 
    stat(path, &sbuf);
 
    /* list file */
    if( !S_ISDIR(sbuf.st_mode) )
    {
        print_info(path);
        return 0;
    }
 
    /* list folder */
    dirp = opendir(path);
    if( !dirp )
    {
        printf("%s: cannot open directory '%s': %s\n", PROGNAME, path, strerror(errno));
        return -3;
    }
 
    while( NULL != (dir=readdir(dirp)) )
    {
        if( !strcmp(path, ".") )
            strncpy(filepath, dir->d_name, sizeof(filepath));
        else
            snprintf(filepath, sizeof(filepath), "%s/%s", path, dir->d_name);
 
        print_info(filepath);
    }
 
    return 0;
}