-
Notifications
You must be signed in to change notification settings - Fork 113
/
41.sort-with-d.c
163 lines (133 loc) · 3.17 KB
/
41.sort-with-d.c
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
#include <stdio.h>
#include <string.h>
#define MAXLEN 10000
#define MAXLINES 5000
char *lineptr[MAXLINES];
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void swap(void *v[], int, int);
void qsort(void *lineptr[], int left, int right,
int reverse, int foldcase, int (*comp)(void *, void *));
// because qsort is unstable, to test -d
void bubblesort(void *lineptr[], int len, int (*comp)(void*, void*));
int numcmp(const char *, const char *);
int dircmp(const char *, const char *);
void strtolower(const char *src, char *dst);
// gcc 41.sort-with-d.c numcmp.c readlines-writelines.c 6.alloc.c case-convert.c
main(int argc, char *argv[])
{
char c;
int nlines;
int numeric = 0;
int reverse = 0;
int foldcase = 0;
int directory = 0;
while (--argc > 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch(c) {
case 'n':
numeric = 1;
break;
case 'r':
reverse = 1;
break;
case 'f':
foldcase = 1;
break;
case 'd':
directory = 1;
break;
default:
printf("find illegal option %c\n", c);
return 1;
}
if (argc != 0) {
printf("Usage: sort -n -r\n");
return 1;
}
if (numeric && directory) {
printf("Cannot use the -n and -d at the same\n");
return 1;
}
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
int (*comp)(void*, void*) = (int (*)(void*, void*))(numeric ? numcmp : strcmp);
if (directory)
comp = (int (*)(void*, void*))dircmp;
if (directory)
bubblesort((void **) lineptr, nlines, comp);
else
qsort((void **) lineptr, 0, nlines-1, reverse, foldcase, comp);
writelines(lineptr, nlines);
return 0;
} else {
printf("input too big to sort\n");
return 1;
}
}
void qsort(void *v[], int left, int right,
int reverse, int foldcase, int (*comp)(void *, void *))
{
int i, last;
char s1[MAXLEN], s2[MAXLEN];
if (left >= right)
return;
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; i <= right; i++) {
char *vi;
char *vleft;
if (foldcase) {
strtolower(v[i], s1);
vi = s1;
strtolower(v[left], s2);
vleft = s2;
} else {
vi = v[i];
vleft = v[left];
}
if (reverse && ((*comp)(vi, vleft) > 0))
swap(v, ++last, i);
else if (!reverse && ((*comp)(vi, vleft) < 0))
swap(v, ++last, i);
}
swap(v, left, last);
qsort(v, left, last-1, reverse, foldcase, comp);
qsort(v, last+1, right, reverse, foldcase, comp);
}
void bubblesort(void *v[], int len, int (*comp)(void*, void*))
{
int i, j;
for (i = 0; i < len; i++)
for (j = 0; j < len; j++) {
if ((*comp)(v[i], v[j]) < 0)
swap(v, i, j);
}
}
void swap(void *v[], int i, int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
int dircmp(const char *p1, const char *p2)
{
char isdircase(const char c);
const unsigned char *s1 = (const unsigned char *) p1;
const unsigned char *s2 = (const unsigned char *) p2;
unsigned char c1, c2;
do {
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
// only compare letters, numbers, blanks
if (!isdircase(c1) || !isdircase(c2))
c2 = c1;
if (c1 == '\0')
return c1 - c2;
} while (c1 == c2);
return c1 - c2;
}
char isdircase(const char c)
{
return isalnum(c) || c == ' ';
}