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
|
/*
* ls.c
* Implementation of SUSv3 XCU ls utility
* Copyright © 2007 Rich Felker
* Licensed under the terms of the GNU General Public License, v2 or later
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
#include <unistd.h>
#include <limits.h>
#include <locale.h>
#include <errno.h>
static size_t mbswidth(const char *s)
{
size_t l, w=0;
int v;
wchar_t wc;
mbstate_t mbs = { 0 };
for (w=0; *s; s+=l, w+=v)
if ((l=mbrtowc(&wc, s, MB_LEN_MAX, &mbs))+2<2
|| (v=wcwidth(wc)) < 0)
return -1;
return w;
}
static void my_perror(char *prog, char *msg)
{
char *err = strerror(errno);
write(2, prog, strlen(prog));
write(2, ": ", 2);
write(2, msg, strlen(msg));
write(2, ": ", 2);
write(2, err, strlen(err));
write(2, "\n", 1);
}
static int cmp(const void *a, const void *b)
{
return strcoll(*(const char **)a, *(const char **)b);
}
int main(int argc, char *argv[])
{
struct stat st;
int (*xstat)(const char *, struct stat *) = stat;
if (!setlocale(LC_ALL, ""))
my_perror(argv[0], "setlocale");
while ((b=getopt(argc,argv,"CFHLRacdfgilmnoprstux1"))!=EOF) switch(b) {
default:
return 1;
}
if (optind == argc) {
}
for (i=optind; i<argc; ) {
if (xstat(argv[i], &st)) {
my_perror(argv[0], argv[i]);
memmove(&argv[i], &argv[i+1], argc-i-1);
argc--;
} else if (S_ISDIR(st.st_mode) && !opt_d) {
char *tmp = argv[i];
argv[i] = argv[--firstdir];
argv[fisrtdir] = tmp;
if (i == firstdir) break;
} else i++;
}
qsort(argv+optind, firstdir-optind, sizeof(char *), cmp);
qsort(argv+firstdir, argc-firstdir, sizeof(char *), cmp);
}
|