-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresort.c
167 lines (138 loc) · 3.5 KB
/
presort.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
/*
* Created 2018-03-12
*/
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <string.h>
static char pathsep='/';
typedef struct {
char *sz;
char *atime;
char *path;
} agedu_t;
/*
* compare in ascii order except "/" takes precedence
*/
int strcmppathsep(const char *a, const char *b)
{
while (*a == *b && *a)
a++, b++;
if (*b == pathsep && !*a) return -1; // parent directory precedence
if (*a == pathsep && !*b) return 1; // parent directory precedence
if (*a == pathsep) return -1;
if (*b == pathsep) return 1;
return (int)(unsigned char)*a - (int)(unsigned char)*b;
}
/*
* insertion sort is most efficienc when input is already mostly sorted, as it is with Starfish Database output
*/
int compareentries(const agedu_t *a, const agedu_t *b) {
char *p;
char *q;
p = a->path;
q = b->path;
return strcmppathsep(p, q);
}
/*
* insertion sort routine
*/
void insert_sort(agedu_t ** unsorted, const int nel) {
int c, d;
agedu_t ap, *p1, *p2;
for (c = 1 ; c <= nel - 1; c++) {
d = c;
p1 = (*unsorted)+d-1;
p2 = (*unsorted)+d;
while ( d > 0 && compareentries(p1, p2) > 0) {
ap.sz = p2->sz;
ap.path = p2->path;
p2->sz = p1->sz;
p2->path = p1->path;
p1->sz = ap.sz;
p1->path = ap.path;
d--;
p1 = (*unsorted)+d-1;
p2 = (*unsorted)+d;
}
}
}
/*
* print out the sorted list
*/
void printsorted(const agedu_t * adup, const int len) {
int i;
for (i = 0; i < len; i++) {
printf("%s\n", (adup+i)->sz );
}
}
static int debug=1;
main (const int argc, const char *argv[]) {
int fd;
int offset = 0;
int rownum;
size_t length;
struct stat sb;
agedu_t *presortlist, *adup;
char *addr;
char *p;
char *header; // saveheader for later
char newline = (const char ) '\012';
if (argc < 2 || argc > 2) {
fprintf(stderr, "usage: %s <file> \n", argv[0]);
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_RDONLY);
if (fd == -1) {
fprintf(stderr,"Couldn't open input file %s, check permissions\n", argv[1]);
exit(EXIT_FAILURE);
}
if (fstat(fd, &sb) == -1) { /* To obtain file size */
perror("failed fstat");
exit(EXIT_FAILURE);
}
// setup all the pointers for sorting - arbitrarily limited to 1,000,000 entries
presortlist = (agedu_t *) malloc(sizeof(agedu_t) * 1000000);
if (presortlist == NULL) {
perror("error allocating memory");
exit(EXIT_FAILURE);
}
memset(presortlist, 0, sizeof(presortlist));
adup = presortlist;
offset = 0;
length = sb.st_size;
addr = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, offset);
if (addr == MAP_FAILED) {
perror("mmap failed");
exit(EXIT_FAILURE);
}
/* skip header */
p = header = addr;
p = strchr(p, newline);
/* mark it and skip to first item */
*p++ = (char) 0;
rownum = 0;
while (p < addr + length) {
adup->sz = p;
p = strchr(p, ' ') + 1;
adup->atime = p;
p = strchr(p, ' ') + 1;
adup->path = p;
p = strchr(p, newline); // end of line and replace with null
*p = (char) 0;
p++; // next row
rownum++;
adup++;
}
if (debug > 1)
fprintf (stderr,"%d rows\n", rownum);
insert_sort(&presortlist, rownum);
printf("%s\n", header);
printsorted((const agedu_t *) presortlist, rownum);
/*qsort(names, nnames, sizeof(*names), str_cmp);*/
return (EXIT_SUCCESS);
}