-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
178 lines (144 loc) · 3.34 KB
/
sort.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* SPDX-License-Identifier: ISC
* SPDX-URL: https://spdx.org/licenses/ISC.html
*
* Copyright (C) 2023 Aaron M. D. Jones <[email protected]>
*/
#include <stddef.h>
#include <string.h>
#include "filemap.h"
static int FM_NONNULL(1, 2)
fm_sortby_extoff_cb(const struct fm_extent *const restrict in1, const struct fm_extent *const restrict in2)
{
if (in1->off < in2->off)
return -1;
if (in1->off > in2->off)
return 1;
return 0;
}
static int FM_NONNULL(1, 2)
fm_sortby_extlen_cb(const struct fm_extent *const restrict in1, const struct fm_extent *const restrict in2)
{
if (in1->len < in2->len)
return -1;
if (in1->len > in2->len)
return 1;
return 0;
}
static int FM_NONNULL(1, 2)
fm_sortby_extcnt_cb(const struct fm_extent *const restrict in1, const struct fm_extent *const restrict in2)
{
if (in1->inode->extcount < in2->inode->extcount)
return -1;
if (in1->inode->extcount > in2->inode->extcount)
return 1;
return 0;
}
static int FM_NONNULL(1, 2)
fm_sortby_inofcnt_cb(const struct fm_extent *const restrict in1, const struct fm_extent *const restrict in2)
{
if (in1->inode->namecount < in2->inode->namecount)
return -1;
if (in1->inode->namecount > in2->inode->namecount)
return 1;
return 0;
}
static int FM_NONNULL(1, 2)
fm_sortby_inonum_cb(const struct fm_extent *const restrict in1, const struct fm_extent *const restrict in2)
{
if (in1->inode->inum < in2->inode->inum)
return -1;
if (in1->inode->inum > in2->inode->inum)
return 1;
return 0;
}
static int FM_NONNULL(1, 2)
fm_sortby_inofsz_cb(const struct fm_extent *const restrict in1, const struct fm_extent *const restrict in2)
{
if (in1->inode->sb.st_size < in2->inode->sb.st_size)
return -1;
if (in1->inode->sb.st_size > in2->inode->sb.st_size)
return 1;
return 0;
}
static int FM_NONNULL(1, 2)
fm_sortby_inofname_cb(const struct fm_extent *const restrict in1, const struct fm_extent *const restrict in2)
{
const int ret = strcmp(in1->inode->names->name, in2->inode->names->name);
if (ret < 0)
return -1;
if (ret > 0)
return 1;
return 0;
}
int FM_NONNULL(1, 2)
fm_sortby_extent_cb(const void *const restrict ptr1, const void *const restrict ptr2)
{
const struct fm_extent *const in1 = ptr1;
const struct fm_extent *const in2 = ptr2;
int ret;
switch (fm_sort_method)
{
case FM_SORTMETH_EXTENT_OFFSET:
{
ret = fm_sortby_extoff_cb(in1, in2);
break;
}
case FM_SORTMETH_EXTENT_LENGTH:
{
ret = fm_sortby_extlen_cb(in1, in2);
break;
}
case FM_SORTMETH_INODE_EXTENT_COUNT:
{
ret = fm_sortby_extcnt_cb(in1, in2);
break;
}
case FM_SORTMETH_INODE_LINK_COUNT:
{
ret = fm_sortby_inofcnt_cb(in1, in2);
break;
}
case FM_SORTMETH_INODE_NUMBER:
{
ret = fm_sortby_inonum_cb(in1, in2);
break;
}
case FM_SORTMETH_FILESIZE:
{
ret = fm_sortby_inofsz_cb(in1, in2);
break;
}
case FM_SORTMETH_FILENAME:
{
ret = fm_sortby_inofname_cb(in1, in2);
break;
}
}
switch (fm_sort_direction)
{
case FM_SORTDIR_ASCENDING:
{
return ret;
}
case FM_SORTDIR_DESCENDING:
{
if (ret < 0)
return 1;
if (ret > 0)
return -1;
return 0;
}
}
return 0;
}
int FM_NONNULL(1, 2)
fm_sortby_filename_cb(const struct fm_name *const restrict in1, const struct fm_name *const restrict in2)
{
const int ret = strcmp(in1->name, in2->name);
if (ret < 0)
return -1;
if (ret > 0)
return 1;
return 0;
}