summaryrefslogtreecommitdiff
path: root/Userland/ls.cpp
blob: 2ff32a9c29af44cc3755065c37df76c983f2c2c8 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <AK/HashMap.h>
#include <AK/QuickSort.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
#include <LibCore/CDirIterator.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

static int do_file_system_object_long(const char* path);
static int do_file_system_object_short(const char* path);

static bool flag_colorize = true;
static bool flag_long = false;
static bool flag_show_dotfiles = false;
static bool flag_show_inode = false;
static bool flag_print_numeric = false;
static bool flag_human_readable = false;
static bool flag_sort_by_timestamp = false;
static bool flag_reverse_sort = false;

static size_t terminal_rows = 0;
static size_t terminal_columns = 0;
static bool output_is_terminal = false;

static HashMap<uid_t, String> users;
static HashMap<gid_t, String> groups;

int main(int argc, char** argv)
{
    if (pledge("stdio rpath tty", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    struct winsize ws;
    int rc = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
    if (rc == 0) {
        terminal_rows = ws.ws_row;
        terminal_columns = ws.ws_col;
        output_is_terminal = true;
    }

    if (pledge("stdio rpath", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    static const char* valid_option_characters = "ltraiGnh";
    int opt;
    while ((opt = getopt(argc, argv, valid_option_characters)) != -1) {
        switch (opt) {
        case 'a':
            flag_show_dotfiles = true;
            break;
        case 'l':
            flag_long = true;
            break;
        case 't':
            flag_sort_by_timestamp = true;
            break;
        case 'r':
            flag_reverse_sort = true;
            break;
        case 'G':
            flag_colorize = false;
            break;
        case 'i':
            flag_show_inode = true;
            break;
        case 'n':
            flag_print_numeric = true;
            break;
        case 'h':
            flag_human_readable = true;
            break;
        default:
            fprintf(stderr, "usage: ls [-%s] [paths...]\n", valid_option_characters);
            return 1;
        }
    }

    if (flag_long) {
        setpwent();
        for (auto* pwd = getpwent(); pwd; pwd = getpwent())
            users.set(pwd->pw_uid, pwd->pw_name);
        endpwent();
        setgrent();
        for (auto* grp = getgrent(); grp; grp = getgrent())
            groups.set(grp->gr_gid, grp->gr_name);
        endgrent();
    }

    auto do_file_system_object = [&](const char* path) {
        if (flag_long)
            return do_file_system_object_long(path);
        return do_file_system_object_short(path);
    };

    int status = 0;
    if (optind >= argc) {
        status = do_file_system_object(".");
    } else if (optind + 1 >= argc) {
        status = do_file_system_object(argv[optind]);
    } else {
        for (; optind < argc; ++optind) {
            printf("%s:\n", argv[optind]);
            status = do_file_system_object(argv[optind]);
        }
    }
    return status;
}

int print_escaped(const char* name)
{
    int printed = 0;

    for (int i = 0; name[i] != '\0'; i++) {
        if (isprint(name[i])) {
            putchar(name[i]);
            printed++;
        } else {
            printed += printf("\\%03d", name[i]);
        }
    }

    return printed;
}

int print_name(const struct stat& st, const String& name, const char* path_for_link_resolution = nullptr)
{
    int nprinted = 0;

    if (!flag_colorize || !output_is_terminal) {
        nprinted = printf("%s", name.characters());
    } else {
        const char* begin_color = "";
        const char* end_color = "\033[0m";

        if (st.st_mode & S_ISVTX)
            begin_color = "\033[42;30;1m";
        else if (st.st_mode & S_ISUID)
            begin_color = "\033[41;1m";
        else if (S_ISLNK(st.st_mode))
            begin_color = "\033[36;1m";
        else if (S_ISDIR(st.st_mode))
            begin_color = "\033[34;1m";
        else if (st.st_mode & 0111)
            begin_color = "\033[32;1m";
        else if (S_ISSOCK(st.st_mode))
            begin_color = "\033[35;1m";
        else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
            begin_color = "\033[33;1m";
        printf("%s", begin_color);
        nprinted = print_escaped(name.characters());
        printf("%s", end_color);
    }
    if (S_ISLNK(st.st_mode)) {
        if (path_for_link_resolution) {
            char linkbuf[PATH_MAX];
            ssize_t nread = readlink(path_for_link_resolution, linkbuf, sizeof(linkbuf));
            if (nread < 0)
                perror("readlink failed");
            else
                nprinted += printf(" -> ") + print_escaped(linkbuf);
        } else {
            nprinted += printf("@");
        }
    } else if (S_ISDIR(st.st_mode)) {
        nprinted += printf("/");
    } else if (st.st_mode & 0111) {
        nprinted += printf("*");
    }
    return nprinted;
}

// FIXME: Remove this hackery once printf() supports floats.
// FIXME: Also, we should probably round the sizes in ls -lh output.
static String number_string_with_one_decimal(float number, const char* suffix)
{
    float decimals = number - (int)number;
    return String::format("%d.%d%s", (int)number, (int)(decimals * 10), suffix);
}

static String human_readable_size(size_t size)
{
    if (size < 1 * KB)
        return String::number(size);
    if (size < 1 * MB)
        return number_string_with_one_decimal((float)size / (float)KB, "K");
    if (size < 1 * GB)
        return number_string_with_one_decimal((float)size / (float)MB, "M");
    return number_string_with_one_decimal((float)size / (float)GB, "G");
}

bool print_filesystem_object(const String& path, const String& name, const struct stat& st)
{
    if (flag_show_inode)
        printf("%08u ", st.st_ino);

    if (S_ISDIR(st.st_mode))
        printf("d");
    else if (S_ISLNK(st.st_mode))
        printf("l");
    else if (S_ISBLK(st.st_mode))
        printf("b");
    else if (S_ISCHR(st.st_mode))
        printf("c");
    else if (S_ISFIFO(st.st_mode))
        printf("f");
    else if (S_ISSOCK(st.st_mode))
        printf("s");
    else if (S_ISREG(st.st_mode))
        printf("-");
    else
        printf("?");

    printf("%c%c%c%c%c%c%c%c",
        st.st_mode & S_IRUSR ? 'r' : '-',
        st.st_mode & S_IWUSR ? 'w' : '-',
        st.st_mode & S_ISUID ? 's' : (st.st_mode & S_IXUSR ? 'x' : '-'),
        st.st_mode & S_IRGRP ? 'r' : '-',
        st.st_mode & S_IWGRP ? 'w' : '-',
        st.st_mode & S_ISGID ? 's' : (st.st_mode & S_IXGRP ? 'x' : '-'),
        st.st_mode & S_IROTH ? 'r' : '-',
        st.st_mode & S_IWOTH ? 'w' : '-');

    if (st.st_mode & S_ISVTX)
        printf("t");
    else
        printf("%c", st.st_mode & S_IXOTH ? 'x' : '-');

    auto username = users.get(st.st_uid);
    auto groupname = groups.get(st.st_gid);
    if (!flag_print_numeric && username.has_value()) {
        printf(" %7s", username.value().characters());
    } else {
        printf(" %7u", st.st_uid);
    }
    if (!flag_print_numeric && groupname.has_value()) {
        printf(" %7s", groupname.value().characters());
    } else {
        printf(" %7u", st.st_gid);
    }

    if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
        printf("  %4u,%4u ", major(st.st_rdev), minor(st.st_rdev));
    } else {
        if (flag_human_readable) {
            ASSERT(st.st_size > 0);
            printf(" %10s ", human_readable_size((size_t)st.st_size).characters());
        } else {
            printf(" %10u ", st.st_size);
        }
    }

    auto* tm = localtime(&st.st_mtime);
    printf("  %4u-%02u-%02u %02u:%02u:%02u  ",
        tm->tm_year + 1900,
        tm->tm_mon + 1,
        tm->tm_mday,
        tm->tm_hour,
        tm->tm_min,
        tm->tm_sec);

    print_name(st, name, path.characters());

    printf("\n");
    return true;
}

int do_file_system_object_long(const char* path)
{
    CDirIterator di(path, !flag_show_dotfiles ? CDirIterator::SkipDots : CDirIterator::Flags::NoFlags);
    if (di.has_error()) {
        if (di.error() == ENOTDIR) {
            struct stat stat;
            int rc = lstat(path, &stat);
            if (rc < 0) {
                perror("lstat");
                memset(&stat, 0, sizeof(stat));
            }
            if (print_filesystem_object(path, path, stat))
                return 0;
            return 2;
        }
        fprintf(stderr, "CDirIterator: %s\n", di.error_string());
        return 1;
    }

    struct FileMetadata {
        String name;
        String path;
        struct stat stat;
    };

    Vector<FileMetadata> files;
    while (di.has_next()) {
        FileMetadata metadata;
        metadata.name = di.next_path();
        ASSERT(!metadata.name.is_empty());
        if (metadata.name[0] == '.' && !flag_show_dotfiles)
            continue;
        StringBuilder builder;
        builder.append(path);
        builder.append('/');
        builder.append(metadata.name);
        metadata.path = builder.to_string();
        ASSERT(!metadata.path.is_null());
        int rc = lstat(metadata.path.characters(), &metadata.stat);
        if (rc < 0) {
            perror("lstat");
            memset(&metadata.stat, 0, sizeof(metadata.stat));
        }
        files.append(move(metadata));
    }

    quick_sort(files.begin(), files.end(), [](auto& a, auto& b) {
        if (flag_sort_by_timestamp) {
            if (flag_reverse_sort)
                return a.stat.st_mtime > b.stat.st_mtime;
            return a.stat.st_mtime < b.stat.st_mtime;
        }
        // Fine, sort by name then!
        if (flag_reverse_sort)
            return a.name > b.name;
        return a.name < b.name;
    });

    for (auto& file : files) {
        if (!print_filesystem_object(file.path, file.name, file.stat))
            return 2;
    }
    return 0;
}

bool print_filesystem_object_short(const char* path, const char* name, int* nprinted)
{
    struct stat st;
    int rc = lstat(path, &st);
    if (rc == -1) {
        printf("lstat(%s) failed: %s\n", path, strerror(errno));
        return false;
    }

    *nprinted = print_name(st, name);
    return true;
}

int do_file_system_object_short(const char* path)
{
    CDirIterator di(path, !flag_show_dotfiles ? CDirIterator::SkipDots : CDirIterator::Flags::NoFlags);
    if (di.has_error()) {
        if (di.error() == ENOTDIR) {
            int nprinted;
            bool status = print_filesystem_object_short(path, path, &nprinted);
            printf("\n");
            if (status)
                return 0;
            return 2;
        }
        fprintf(stderr, "CDirIterator: %s\n", di.error_string());
        return 1;
    }

    Vector<String> names;
    size_t longest_name = 0;
    while (di.has_next()) {
        String name = di.next_path();
        names.append(name);
        if (names.last().length() > longest_name)
            longest_name = name.length();
    }
    quick_sort(names.begin(), names.end(), [](auto& a, auto& b) { return a < b; });

    size_t printed_on_row = 0;
    int nprinted;
    for (int i = 0; i < names.size(); ++i) {
        auto& name = names[i];
        StringBuilder builder;
        builder.append(path);
        builder.append('/');
        builder.append(name);
        if (!print_filesystem_object_short(builder.to_string().characters(), name.characters(), &nprinted))
            return 2;
        int offset = 0;
        if (terminal_columns > longest_name)
            offset = terminal_columns % longest_name / (terminal_columns / longest_name);

        // The offset must be at least 2 because:
        // - With each file an additional char is printed e.g. '@','*'.
        // - Each filename must be separated by a space.
        size_t column_width = longest_name + (offset > 0 ? offset : 2);
        printed_on_row += column_width;

        for (int j = nprinted; i != (names.size() - 1) && j < (int)column_width; ++j)
            printf(" ");
        if ((printed_on_row + column_width) >= terminal_columns) {
            printf("\n");
            printed_on_row = 0;
        }
    }
    if (printed_on_row)
        printf("\n");
    return 0;
}