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
418
419
420
421
422
423
424
425
426
|
/*
* UNG's Not GNU
*
* Copyright (c) 2011-2022, Jakob Kaivo <jkk@ung.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define _XOPEN_SOURCE 700
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <ftw.h>
#include <time.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include <libgen.h>
#ifndef PATH_MAX
#ifdef _XOPEN_PATH_MAX
#define PATH_MAX _XOPEN_PATH_MAX
#else
#define PATH_MAX _POSIX_PATH_MAX
#endif
#endif
#define NONE 0
#define ALL 1
#define ALMOST 2
static enum {
ALPHA,
SIZE,
DIRECTORY,
CTIME,
MTIME,
ATIME,
} sort = ALPHA;
#define CHARACTER 1 << 0
#define INODES 1 << 1
#define DIRS 1 << 2
#define QUOTE 1 << 3
#define BLOCKS 1 << 4
#define FOLLOW 1
#define LLONG 2
#define DEFAULT_BLOCK_SIZE 512
static int all = NONE;
static int format = NONE;
static int links = NONE;
static int recurse = 0;
static int listdirs = 0;
static int noowner = 0;
static int blocksize = DEFAULT_BLOCK_SIZE;
static int numeric = 0;
static int nogroup = 0;
static int reverse = 0;
//static int totaldirs = 0;
static int columns = 80;
struct file_info {
char *path;
struct stat st;
};
static void (*ls_print)(size_t n, struct file_info[static n]);
static char * ls_filename(struct file_info *fi)
{
static char name[PATH_MAX];
/* TODO: decorate with /@|* if needed */
snprintf(name, sizeof(name), "%s", fi->path);
return name;
}
static int ls_compare_files(const void *op_a, const void *op_b)
{
const struct file_info *a = op_a;
const struct file_info *b = op_b;
int ret = 0;
switch (sort) {
case SIZE:
ret = a->st.st_size - b->st.st_size;
break;
case CTIME:
ret = a->st.st_ctime - b->st.st_ctime;
break;
case MTIME:
ret = a->st.st_mtime - b->st.st_mtime;
break;
case ATIME:
ret = a->st.st_atime - b->st.st_atime;
break;
case DIRECTORY:
ret = 1;
break;
case ALPHA:
/* handled below */
break;
}
if (ret == 0) {
ret = strcoll(a->path, b->path);
}
return (reverse ? -1 : 1) * ret;
}
/*
static int ls_dir(char *dir)
{
DIR *d = opendir(dir);
struct dirent *de;
int blocks = 0;
char filename[PATH_MAX];
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] != '.' || (de->d_name[0] == '.' && all == ALL)
|| (strcmp(".", de->d_name) && strcmp("..", de->d_name)
&& all == ALMOST)) {
strcpy(filename, dir);
strcat(filename, "/");
strcat(filename, de->d_name);
//blocks += ls_add(filename, 1);
}
}
closedir(d);
return blocks * DEFAULT_BLOCK_SIZE / blocksize;
}
*/
static size_t ls_find_widest(size_t n, struct file_info files[static n])
{
size_t ret = 0;
for (size_t i = 0; i < n; i++) {
size_t len = strlen(ls_filename(files + i));
if (len > ret) {
ret = len;
}
}
return ret + 1;
}
static void ls_print_single(size_t n, struct file_info files[static n])
{
for (size_t i = 0; i < n; i++) {
printf("%s\n", ls_filename(files + i));
}
}
static void ls_print_serial(size_t n, struct file_info files[static n])
{
if (n == 0) {
return;
}
/* TODO: lines */
for (size_t i = 0; i < n - 1; i++) {
printf("%s, ", ls_filename(files + i));
}
printf("%s\n", ls_filename(files + n - 1));
}
static void ls_print_columns(size_t n, struct file_info files[static n])
{
/* TODO: partial columns */
size_t widest = ls_find_widest(n, files);
size_t ncolumns = columns / widest;
size_t nrows = n / ncolumns;
char format[32] = "";
snprintf(format, sizeof(format), "%%-%zds", widest);
for (size_t i = 0; i < nrows; i++) {
for (size_t j = 0; j < ncolumns; j++) {
printf(format, ls_filename(files + i + (nrows * j)));
}
printf("\n");
}
}
static void ls_print_rows(size_t n, struct file_info files[static n])
{
size_t widest = ls_find_widest(n, files);
size_t ncolumns = columns / widest;
char format[32] = "";
snprintf(format, sizeof(format), "%%-%zds", widest);
for (size_t i = 0; i < n; i++) {
printf(format, ls_filename(files + i));
if (i % ncolumns == ncolumns - 1) {
printf("\n");
}
}
}
static int ls_compare_operands(const void *op_a, const void *op_b)
{
const struct file_info *a = op_a;
const struct file_info *b = op_b;
if (a->path == NULL && b->path == NULL) {
return 0;
}
if (a->path == NULL) {
return 1;
}
if (b->path == NULL) {
return -1;
}
if (S_ISDIR(a->st.st_mode) && !S_ISDIR(b->st.st_mode)) {
return 1;
}
if (!S_ISDIR(a->st.st_mode) && S_ISDIR(b->st.st_mode)) {
return -1;
}
return strcoll(a->path, b->path);
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "");
char *cols = getenv("COLUMNS");
if (cols != NULL) {
columns = atoi(cols);
}
/* TODO: default to ls_print_columns */
ls_print = isatty(STDOUT_FILENO) ? ls_print_columns : ls_print_single;
int c;
while ((c = getopt(argc, argv, ":ACFRSacdgfiklmnoprstux1HL")) != -1) {
switch (c) {
case 'A': /** all but . and .. **/
all = ALMOST;
break;
case 'C': /** sort in columns **/
ls_print = ls_print_columns;
break;
case 'F': /** append file type characters **/
format |= CHARACTER;
break;
case 'H': /** follow symbolic links on command line **/
links = FOLLOW;
break;
case 'L': /** follow all symbolic links **/
links = LLONG;
break;
case 'R': /** run recursively **/
recurse = 1;
break;
case 'S': /** sort by size **/
sort = SIZE;
break;
case 'a': /** output all files **/
all = ALL;
break;
case 'c': /** sort by change time **/
sort = CTIME;
break;
case 'd': /** do not enter directories **/
listdirs = 1;
break;
case 'f': /** do not sort **/
sort = DIRECTORY;
all = ALL;
//display = ROWS;
format ^= BLOCKS;
recurse = 0;
break;
case 'g': /** like -l, but without owner **/
//display = LONG;
noowner = 1;
break;
case 'i': /** include file serial numbers **/
format |= INODES;
break;
case 'k': /** use a blocksize of 1024 **/
blocksize = 1024;
break;
case 'l': /** output full details **/
//display = LONG;
break;
case 'm': /** stream output separated by commas **/
ls_print = ls_print_serial;
break;
case 'n': /** like -l, but with numeric UID and GID **/
//display = LONG;
numeric = 1;
break;
case 'o': /** like -l, but without group **/
//display = LONG;
nogroup = 1;
break;
case 'p': /** append / after directory names **/
format |= DIRS;
break;
case 'q': /** replace non-printable characters with ? **/
format |= QUOTE; // FIXME
break;
case 'r': /** reverse sort order **/
reverse = 1;
break;
case 's': /** outpu total number of blocks **/
format |= BLOCKS; // FIXME
break;
case 't': /** sort by modified time **/
sort = MTIME;
break;
case 'u': /** sort by access time **/
sort = ATIME;
break;
case 'x': /** sort across rows first **/
ls_print = ls_print_rows;
break;
case '1': /** output one file per line **/
ls_print = ls_print_single;
break;
default:
return 1;
}
}
/* sort operands */
/* TODO: deal with -L/-H */
argv += optind;
argc -= optind;
struct file_info operands[argc + 1];
int ret = 0;
for (int i = 0; i < argc; i++) {
operands[i].path = argv[i];
if (stat(argv[i], &operands[i].st) == -1) {
operands[i].path = NULL;
fprintf(stderr, "ls: %s: %s\n", argv[i], strerror(errno));
ret = 1;
}
}
operands[argc].path = NULL;
qsort(operands, argc, sizeof(operands[0]), ls_compare_operands);
/* list files */
size_t nfiles = 0;
struct file_info *op = operands;
while (op->path != NULL && !S_ISDIR(op->st.st_mode)) {
op++;
nfiles++;
}
qsort(operands, nfiles, sizeof(operands[0]), ls_compare_files);
ls_print(nfiles, operands);
/* list directories */
/* TODO: now always necessary */
printf("\n");
while (op->path != NULL) {
printf("%s: (directory)\n", op->path);
op++;
}
return ret;
}
|