forked from shadkam/recentmost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recentmost.c
188 lines (177 loc) · 4.25 KB
/
recentmost.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
179
180
181
182
183
184
185
186
187
188
/* `recentmost.c'
*
* Pick N most-recently-modified files from list on stdin.
*
* Origin: https://github.com/shadkam/recentmost
* This fork: https://github.com/ConradHughes/recentmost
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <time.h>
#else
#include <wchar.h>
#endif
#include "heap.h"
#define LINEBUFLEN 100
#define NoTimeArg "-noTime"
#define NullsNotNewlinesArg "-0"
int
getFileModTime(const char* filepath, rmU64* out_FileModTime)
{
#ifdef _WIN32
FILETIME ft;
HANDLE fh;
fh = CreateFile(filepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
if (fh == INVALID_HANDLE_VALUE) {
fprintf(stderr, "error: could not CreateFile(%s)\n", filepath);
return 1;
}
if (GetFileTime(fh, NULL, NULL, &ft) == 0) {
fprintf(stderr, "error: could not GetFileTime(%s)\n", filepath);
return 2;
}
*out_FileModTime = ft.dwHighDateTime;
*out_FileModTime = (*out_FileModTime)<<32;
*out_FileModTime |= ft.dwLowDateTime;
#else
struct stat st;
if (stat(filepath, &st)) {
fprintf(stderr, "error: filepath '%s' not found", filepath);
return 1;
}
*out_FileModTime = st.st_mtime;
#endif
return 0;
}
void
offertoheap(Heap h, char *filepath)
{
HeapElement elem = NULL;
rmU64 fileModTime = 0;
if (0!=getFileModTime(filepath, &fileModTime)) {
/* error - but msg already shown */
/* fprintf(stderr, "error: could not find or get mod time for filepath '%s'\n", filepath); */
} else {
elem = heap_newElement(fileModTime, filepath);
if (!heap_push(h, elem)) {
heap_freeElement(elem);
elem = NULL;
}
}
}
void
processLines(Heap h, char eol)
{
int c = EOF;
size_t linelen = 0;
char* filepath = NULL;
size_t allocated = LINEBUFLEN+1;
char *linebuf = (char*)malloc(sizeof(char)*allocated);
while (1) {
c = fgetc(stdin);
if (c == EOF) {
break;
}
if (linelen >= allocated) {
allocated += LINEBUFLEN;
linebuf = realloc(linebuf, sizeof(char) * allocated);
}
if (c == eol) {
linebuf[linelen] = '\0';
offertoheap(h, linebuf);
memset(linebuf, '\0', sizeof(char)*allocated);
linelen = 0;
continue;
}
linebuf[linelen++] = (unsigned char)c;
}
free(filepath);
}
void
usage(char* progname)
{
fprintf(stderr, "Usage:%s <filecount> [%s (print last modified time)]\n", progname, NoTimeArg);
fprintf(stderr, " [%s (\\0-separated inputs and outputs)]\n", NullsNotNewlinesArg);
}
int
checkInputs(int argc, char** argv, int* N, int* bPrintTime, char *eol)
{
if (argc<2) {
usage(argv[0]);
return 0;
}
*N = atoi(argv[1]);
if (*N==0) {
fprintf(stderr, "Error: need numeric non zero value (%s) for filecount\n", argv[1]);
usage(argv[0]);
return 0;
}
*bPrintTime = 1;
if (argc>2 && 0==strncmp(NoTimeArg, argv[2], sizeof(NoTimeArg))) {
*bPrintTime = 0;
}
*eol = '\n';
if (argc > 2 && ! strcmp(NullsNotNewlinesArg, argv[2])) {
*eol = '\0';
*bPrintTime = 0;
}
return 1;
}
void
fillTimeStr(char* timeStr, rmU64 time)
{
#ifdef _WIN32
FILETIME ft;
SYSTEMTIME st;
ft.dwHighDateTime = time>>32;
ft.dwLowDateTime = time&0xFFFF;
FileTimeToLocalFileTime( &ft, &ft );
FileTimeToSystemTime ( &ft, &st );
#else
struct tm *tm1;
struct stat st;
memset(&st, '0', sizeof(st));
st.st_mtime = time;
tm1 = localtime(&st.st_mtime);
#endif
sprintf(timeStr, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d%s",
#ifdef _WIN32
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond
#else
1900+tm1->tm_year, 1+tm1->tm_mon, tm1->tm_mday, tm1->tm_hour,
tm1->tm_min, tm1->tm_sec
#endif
, " ");
}
int
main(int argc, char** argv)
{
int i;
int N;
Heap hh;
int bPrintTime = 0;
char timeStr[] = "year-MM-dd hh:mm:ss "; /* just to take care of size */
char eol = '\n';
HeapElement popped;
if (!checkInputs(argc, argv, &N, &bPrintTime, &eol)) {
return -1;
}
memset(timeStr, '\0', sizeof(timeStr));
hh = heap_alloc(N);
processLines(hh, eol);
for (i=0; i<N*10; i++) {
popped = heap_pop(hh);
if (!popped)
break;
if (bPrintTime) {
fillTimeStr(timeStr, popped->modtime);
}
printf("%s%s%c", timeStr, popped->name, eol);
heap_freeElement(popped);
}
heap_free(hh);
return 0;
}