-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcsort.c
More file actions
328 lines (293 loc) · 8.18 KB
/
gcsort.c
File metadata and controls
328 lines (293 loc) · 8.18 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
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
/*
* gc3 - Gram's Commander v3.3: A configureable file manager
*
* YAGH! (tm) (Yet Another Gramish Hack)
*
* (c) 1994 by Graham Wheeler. All Rights Reserved.
* gram@aztec.co.za
*
* DISCLAIMER: The author is not responsible for any loss, damage,
* riots, earthquakes, catastrophes, marriages, annulments,
* etc that arise from the use of this program. Sorry...
*
* This software may be freely copied and distributed, provided it is
* distributed with all files intact and unmodified. You may use this
* software for a 30 day trial period, after which you should register
* your copy with me. I have spent a large number of weekends and nights
* trying to make a file manager for UNIX (and DOS) which combines ease
* of use and power, and complements the power of UNIX to process the
* contents of files with the power to *select* the files to be processed.
* By registering, you will encourage me to keep improving gc3, instead
* of seeking paying work which benefits only a few.
*
* Full details of how to register, and what you will receive if
* you do, are contained in the file LEGAL.DOC. Of course, if you
* don't register, I can't do much about it, but in that case
* may an obscure bug in gc3 erase all of your files!
*
* Enjoy using GC!
*
* See INSTALL.DOC for instructions on how to install GC3
*
* Version History
* ---------------
* v1.0 Nov 1992 Initial release
* v2.0 Nov 1992 Largely rewritten to be table-driven
* v2.1 Dec 1992 Support for non-ANSI C compilers contributed
* by George Sipe
* Support for old ioctls TCGETA and TCSETAW for
* those with no POSIX-style tcgetattr and tcsetattr.
* v2.2 Dec 1992 Termio ioctls removed.Hopefully much more
* portable. Command mode removed. A few bugs
* fixed. Placeholder replacement now done in
* a preprocessing phase before parsing commands.
* v2.3 Jan 1993 Split into a number of source files
* Basic port to MS-DOS
* v3.0 Feb/March '93 New script language; large parts rewritten
* v3.1 March '93 Ported back to UNIX and cleaned up
* v3.2b July '93 Many bugs fixed and features added
* v3.2 Oct '93 Viewer added, .gc3rul file handling
* added, bugs fixed, local variables and
* parameters, tree view and container handling.
* v3.3 April '94 Fixed lots of bugs. Large parts of compiler
* and interpreter are now table-driven. Changed
* screen repainting, and added support for colour
* and mono terminals. Added script-driven menus
* and forms, and hypertext help browser.
* Made windows moveable and resizeable.
* Made command handling in the script much more
* sophisticated with multiple prefix/suffixes.
* Improved command line history facility. Added
* repeat counts to most commands.
*/
/********************************************************
GC3 DIRECTORY SORTING
(c) 1993 by Graham Wheeler
*********************************************************/
#include "gcport.h"
#include "gc3.h"
#include "gclib.h"
#if __STDC__
typedef int (*cmpFunc)(fInfo_t *, fInfo_t *);
#else
typedef int (*cmpFunc)();
#endif
static int
sortStyle[2],
secSortStyle[2],
reverseSort[2],
secReverseSort[2];
static int
sortingL,
priStyle,
secStyle,
priRev,
secRev;
/*
* sortList - sort the fInfo entry indexes according to the
* sort keys sortStyle[l] and secSortStyle[l].
*/
/*
* File name extension compare
*/
#if __STDC__
static int extCmp(fInfo_t *a, fInfo_t *b)
#else
static int extCmp(a, b)
fInfo_t *a, *b;
#endif /* __STDC__ */
{
char *ap = (char *)STRRCHR(a->name,'.');
char *bp = (char *)STRRCHR(b->name,'.');
if (ap==NULL) ap=a->name;
if (bp==NULL) bp=b->name;
return strcmp(ap,bp);
}
/*
* File name compare
*/
#if __STDC__
static int nameCmp(fInfo_t *a, fInfo_t *b)
#else
static int nameCmp(a, b)
fInfo_t *a, *b;
#endif /* __STDC__ */
{
return strcmp(a->name, b->name);
}
/*
* File size compare
*/
#if __STDC__
static int sizeCmp(fInfo_t *a, fInfo_t *b)
#else
static int sizeCmp(a, b)
fInfo_t *a, *b;
#endif /* __STDC__ */
{
if (a->size == b->size) return 0;
else return ((a->size - b->size) <0l) ? -1 : 1;
}
/*
* Modification time compare
*/
#if __STDC__
static int mtimCmp(fInfo_t *a, fInfo_t *b)
#else
static int mtimCmp(a, b)
fInfo_t *a, *b;
#endif /* __STDC__ */
{
if (a->modtime == b->modtime) return 0;
else return ((a->modtime - b->modtime) <0l) ? -1 : 1;
}
/*
* Access time compare
*/
#if __STDC__
static int atimCmp(fInfo_t *a, fInfo_t *b)
#else
static int atimCmp(a, b)
fInfo_t *a, *b;
#endif /* __STDC__ */
{
if (a->acctime == b->acctime) return 0;
else return ((a->acctime - b->acctime) <0l) ? -1 : 1;
}
/*
* Owner compare
*/
#if __STDC__
static int ownCmp(fInfo_t *a, fInfo_t *b)
#else
static int ownCmp(a, b)
fInfo_t *a, *b;
#endif /* __STDC__ */
{
return (int)(a->uid - b->uid);
}
/*
* Group compare
*/
#if __STDC__
static int grpCmp(fInfo_t *a, fInfo_t *b)
#else
static int grpCmp(a, b)
fInfo_t *a, *b;
#endif /* __STDC__ */
{
return (int)(a->gid - b->gid);}
/*
* Table of pointers to above functions. The order is important
* as the sort style is used as an index into this table.
*/
static cmpFunc cmpTbl[] =
{
nameCmp,
sizeCmp,
mtimCmp,
atimCmp,
ownCmp,
grpCmp,
extCmp
};
/*
* The `generic' compare routine. This uses the primary and
* secondary sort keys and reverse sorting, if applicable.
*/
#if __STDC__
static int fcmp(const void *a, const void *b)
#else
static int fcmp(a, b)
char *a;
char *b;
#endif /* __STDC__ */
{
int v;
/*
* If directories must come first, and one (and only one)
* of the files being comared is a directory, we use this
* as the basis of the compare.
*/
if (testOption(VAR_DIRSFIRST))
{
if (IsDirectory(sortingL, *((int *)a) ))
{
if (!IsDirectory(sortingL, *((int *)b) ))
return -1;
}
else if (IsDirectory(sortingL, *((int *)b) ))
return 1;
}
/* Compare by primary key, and reverse if necessary */
v = (*(cmpTbl[priStyle]))(&fInfo[sortingL][*((int *)a)],
&fInfo[sortingL][*((int *)b)]);
if (priRev) v = -v;
if (v==0) /* Primary key doesn't distinguish, so we use secondary */
{
v = (*(cmpTbl[secStyle]))(&fInfo[sortingL][*((int *)a)],
&fInfo[sortingL][*((int *)b)]);
if (secRev) v = -v;
}
return v;
}
/*****************************************************************
External entry points
******************************************************************/
/*
* sortList(n, name) sorts the list of files in the window given
* by n. If name is given, then an attempt to position the
* cursor on that name is made after the sort is complete.
*/
#if __STDC__
void sortList(int n, char *name)
#else
void sortList(n, name)
int n;
char *name;
#endif /* __STDC__ */
{
if (isListWindow(n))
{
int (*ftmp)() = fcmp; /* Attempt to fool SPARC compiler */
sortingL = n;
priStyle = sortStyle[n]; priRev = reverseSort[n];
secStyle = secSortStyle[n]; secRev = secReverseSort[n];
if (numfiles[n]>1)
#if __STDC__
qsort((void *)fIndex[n],(size_t)(numfiles[n]),
sizeof(int), ftmp);
#else
qsort((char *)fIndex[n],numfiles[n],sizeof(int),ftmp);
#endif
if (name)
{
int i = findFile(n,0,name,NULL);
#ifdef DEBUG
if (testOption(VAR_DEBUGGING))
fprintf(debug,"findFile(%s) returned %di (%s,%s)\n",name,i,fInfo[n][i].name,fInfo[n][fIndex[n][i]].name);
#endif
if (i>=0)
highlight[n] = i;
}
else startLn[n] = highlight[n]= 0;
}
}
/*
* resortList is used when a new sort key and direction are
* given. The old primary key/direction are made secondary.
*/
#if __STDC__
void resortList(int n, int dir, short key)
#else
void resortList(n, dir, key)
int n, dir;
short key;
#endif
{
secReverseSort[n] = reverseSort[n];
secSortStyle[n] = sortStyle[n];
reverseSort[n] = (dir < 0);
sortStyle[n] = (int)key;
sortList(n, INFO_NOW(n).name);
}