File: sort.c

package info (click to toggle)
moxftp 2.2-18.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,160 kB
  • ctags: 3,837
  • sloc: ansic: 43,420; makefile: 353; perl: 262; sh: 192
file content (214 lines) | stat: -rw-r--r-- 4,828 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 1992 The Regents of the University of Texas System.
 * Copyright (c) 1993 The Regents of the University of Texas System.
 * Copyright (c) 1994 The Regents of the University of Texas System.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that the above copyright notice and this paragraph are duplicated in all
 * such forms and that any documentation, advertising materials,  and other
 * materials  related to such  distribution  and use  acknowledge  that the
 * software  was  developed  by the  University of Texas.  The  name of the
 * University may not be  used to endorse or promote  products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1993 The Regents of the University of Texas System.\n\
 All rights reserved.\n";
static char rcsid[] =
"$Id: sort.c,v 1.1 1994/03/14 18:55:53 jones Exp $\n";
static char rcspath[] =
"$Source: /mintaka/u0/xx/ss/jones/jones.old/mftp.src/mftpnew/RCS/sort.c,v $\n";
#endif

/* $Log: sort.c,v $
 * Revision 1.1  1994/03/14  18:55:53  jones
 * Initial revision
 *
 */

#include "defs.h"
#include "proto/sort.h"

static int reverse = 0;
static int by_type = 0;

static struct {
	int mode;
	int weight;
} weights[] = {
    {MODE_TYPE_DIR ,     0},
    {MODE_TYPE_LINK,     1},
    {MODE_TYPE_FILE,     2}, 
    {MODE_TYPE_BLOCK,    3},
    {MODE_TYPE_CHAR,     4},
    {MODE_TYPE_BLOCK,    5},
    {MODE_TYPE_SOCK,     6}, 
    {0		   ,     7},
};


int
Sort_Files(dir, t, r, bt)
struct _dirs *dir;
int t;
int r;
int bt;
{
    int width = sizeof(struct _files);
    char *highname = NULL;
    char *pos[LIST_MAX];
    int i,j;

    if (!dir || dir->n <=0) return 0;

    if (dir->sort_type == t && dir->reverse == r && 
	dir->sort_by_type == bt) return 0;

    reverse = r;
    by_type = bt;

    for (i=0; i<dir->n; i++)  dir->files[i].temp = 0;

    if (dir->highlight > -1) highname = dir->files[dir->highlight].name;

    for(i=0; i<LIST_MAX; i++) {
        if (dir->position[i] >= 0) {
            dir->files[dir->position[i]].temp = 1;
            pos[i] = dir->files[dir->position[i]].name;
        } else {
	    pos[i] = NULL;
	}
    }


    switch (t) {
	case SORT_BY_NAME:
	    qsort((char *)dir->files, dir->n, width, sort_by_name);
	    break;
	case SORT_BY_SIZE:
	    qsort((char *)dir->files, dir->n, width, sort_by_size);
	    break;
	case SORT_BY_AGE:
	    qsort((char *)dir->files, dir->n, width, sort_by_age);
	     break; 
	default:
	    qsort((char *)dir->files, dir->n, width, sort_by_name);
	    t = SORT_BY_NAME;
	    break;
    }

    dir->highlight = -1;
    if (highname != NULL) {
	for (i=0; i<dir->n; i++) {
            if (strcmp(highname, dir->files[i].name) == 0) {
    	 	dir->highlight = i;
   	        for(j=0; j<LIST_MAX; j++) dir->position[j] = i;
		break;
	    }
	}
    } else {
        for (i=0; i<dir->n; i++) {
 	    if (dir->files[i].temp) {
   	        for(j=0; j<LIST_MAX; j++) {
		    if (pos[j] && (strcmp(pos[j], dir->files[i].name) == 0)) {
                        dir->position[j] = i;
		    }
	        }
	   }
	}
    }

    dir->sort_type = t;
    dir->reverse = r;
    dir->sort_by_type = bt;
    return 1;
}

static int
sort_by_age(a, b)
char *a;
char *b;
{
     struct _files *x = (struct _files *)a;
     struct _files *y = (struct _files *)b;
     int value = 0;

     value = sort_by_type(a, b);

     if (value == 0) value = x->time - y->time;

     if (reverse) {
	value = -value;
     }
     return value;
}

static int
sort_by_name(a,b)
char *a;
char *b;
{
     struct _files *x = (struct _files *)a;
     struct _files *y = (struct _files *)b;
     int value = 0;

     value = sort_by_type(a, b);
     if (value == 0) value = strcmp(x->name, y->name);

     if (reverse) {
	value = -value;
     }
     return value;
}

static int
sort_by_size(a,b)
char *a;
char *b;
{
     struct _files *x = (struct _files *)a;
     struct _files *y = (struct _files *)b;
     int value = 0;

     value = sort_by_type(a, b);

     if (value == 0)  value = x->size - y->size;

     if (reverse) {
	value = -value;
     }
     return value;
}

static int
sort_by_type(a, b)
char *a;
char *b;
{
     struct _files *x = (struct _files *)a;
     struct _files *y = (struct _files *)b;
     int value;

     if (!by_type) return 0;
     value = weight_type(x) - weight_type(y);
     return value;
}

static int
weight_type(x)
struct _files *x;
{
    int i;

    for (i=0; weights[i].mode; i++) {
	if (weights[i].mode & x->mode) break;
    }
    return weights[i].weight;
}