-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortlines.c
More file actions
295 lines (255 loc) · 6.55 KB
/
sortlines.c
File metadata and controls
295 lines (255 loc) · 6.55 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXLINES 5000 /* max #lines to be sorted */
char *lineptr[MAXLINES]; /* pointers to text lines */
int readlines(char *lineptr[], int nlines, int fields);
void writelines(char *lineptr[], int nlines, int reversed);
void qsort_(void *v[], int left, int right, int (*comp)(const void *, const void *));
void swap(void *v[], int i, int j);
int numcmp(const char *, const char *);
int strcmp_(const char *s1, const char *s2);
int strcmpf(const char *s1, const char *s2);
int strcmpd(const char *s, const char *t);
int strcmpfd(const char *s1, const char *s2);
int fieldcmp(const char *s1, const char *s2);
static int **options;
static char ***keys;
/* sort input lines */
int main(int argc, char *argv[])
{
int nlines;
int numeric = 0;
int reverse = 0;
int fold = 0;
int dir = 0;
int field = 0;
int fields = 0;
char c;
int i, j, k;
int nchars;
int (*cmpfun)(const void *, const void *) = (int (*)(const void *, const void *))strcmp_;
options = calloc(argc - 1, sizeof(*options));
int atfield;
for (i = 0; i < argc - 1; i++)
{
*(options + i) = calloc(5, sizeof(int));
}
for (i = 0; i < argc - 1 && **++argv == '-'; i++)
{
while ((c = *++*argv))
{
if (field)
{
if (c == 'n')
options[i][1] = 1;
else if (c == 'r')
options[i][2] = 1;
else if (c == 'f')
options[i][3] = 1;
else if (c == 'd')
options[i][4] = 1;
if (isdigit(c) && options[i][0] == 0)
{
options[i][0] = atoi(*argv);
}
}
else
{
if (c == 'n')
numeric = 1;
if (c == 'r')
reverse = 1;
if (c == 'f')
fold = 1;
if (c == 'd')
dir = 1;
if (c == 'k')
field = 1;
}
}
if (field)
{
fields = 1;
i++;
}
field = 0;
}
/* number of input lines read */
if ((nlines = readlines(lineptr, MAXLINES, fields)) >= 0)
{
if (fields)
{
keys = calloc(nlines, sizeof(*keys));
for (i = 0; i < nlines; i++)
{
keys[i] = calloc(argc - 1, sizeof(**keys));
nchars = strlen(lineptr[i]);
atfield = 0;
for (j = 0, k = 0; j < nchars && k < argc - 1; j++)
{
if (!isblank(c = lineptr[i][j]) && !atfield)
{
keys[i][k] = lineptr[i] + j;
atfield = 1;
k++;
}
if (isblank(c) && atfield)
{
atfield = 0;
}
}
}
}
if (fields)
{
cmpfun = (int (*)(const void *, const void *))fieldcmp;
}
else if (numeric)
cmpfun = (int (*)(const void *, const void *))numcmp;
else
{
if (fold && dir)
cmpfun = (int (*)(const void *, const void *))strcmpfd;
else if (fold)
cmpfun = (int (*)(const void *, const void *))strcmpf;
else if (dir)
cmpfun = (int (*)(const void *, const void *))strcmpd;
}
qsort_((void **)lineptr, 0, nlines - 1, cmpfun);
writelines(lineptr, nlines, reverse);
return 0;
}
else
{
printf("error: input too big to sort\n");
return 1;
}
}
size_t MAXLEN = 1000;
/* readlines: read input lines */
int readlines(char *lineptr[], int maxlines, int fields)
{
int len, nlines;
char *p, *line;
nlines = 0;
line = malloc(MAXLEN);
while ((len = getline(&line, &MAXLEN, stdin)) > 0)
{
if (nlines >= maxlines || (p = malloc(len)) == NULL)
return -1;
else
{
line[len - 1] = '\0'; /* delete newline */
strcpy(p, line);
lineptr[nlines++] = p;
}
}
return nlines;
}
/* writelines: write output lines */
void writelines(char *lineptr[], int nlines, int reversed)
{
if (reversed)
lineptr = lineptr + nlines;
while (nlines-- > 0)
if (reversed)
printf("%s\n", *--lineptr);
else
printf("%s\n", *lineptr++);
}
/* qsort_: sort v[left]...v[right] into increasing order */
void qsort_(void *v[], int left, int right, int (*comp)(const void *, const void *))
{
int i, last;
if (left >= right) /* do nothing if array contains */
return; /* fewer than two elements */
swap(v, left, (left + right) / 2);
last = left;
for (i = left + 1; i <= right; i++)
if ((*comp)(v[i], v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
qsort_(v, left, last - 1, comp);
qsort_(v, last + 1, right, comp);
}
/* swap: interchange v[i] and v[j] */
void swap(void *v[], int i, int j)
{
char *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
int fieldcmp(const char *s1, const char *s2)
{
int field = options[0][0];
int fields = 1;
while ((fields < field) && &s1)
{
while (isblank(&s1))
s1++;
while (!isblank(&s1))
s1++;
fields++;
}
return 0;
}
int numcmp(const char *s1, const char *s2)
{
double v1, v2;
v1 = atof(s1);
v2 = atof(s2);
if (v1 < v2)
return -1;
else if (v1 > v2)
return 1;
else
return 0;
}
int i(char c)
{
return c;
}
int lower(char c)
{
if (c >= 65 && c <= 90)
return c + 32;
return c;
}
int dirc(char c)
{
if (isalnum(c) || isblank(c))
return c;
return ' ';
}
int dircf(char c)
{
return dirc(lower(c));
}
int _strcmp(const char *s, const char *t, int (*transform)(char))
{
int i;
int a, b;
for (i = 0; (a = (*transform)(s[i])) == (b = (*transform)(t[i])); i++)
if (s[i] == '\0')
return 0;
return a - b;
}
int strcmp_(const char *s, const char *t)
{
return _strcmp(s, t, i);
}
int strcmpf(const char *s, const char *t)
{
return _strcmp(s, t, lower);
}
int strcmpd(const char *s, const char *t)
{
return _strcmp(s, t, dirc);
}
int strcmpfd(const char *s, const char *t)
{
return _strcmp(s, t, dircf);
}