-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrash.m
78 lines (64 loc) · 2.22 KB
/
trash.m
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
#include <AppKit/AppKit.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>
#include <stdlib.h>
#include <unistd.h>
#define VERSION "1.0.0"
void printUsage() {
printf("Usage: trash [-v|--version] [-h|--help] <file1> [<file2> ...]\n");
printf("Move files to the trash.\n\n");
printf("Options:\n");
printf(" -v, --version Display the version number and exit.\n");
printf(" -h, --help Display this help message and exit.\n");
}
NSString *currentWorkingDirectory() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
return [NSString stringWithUTF8String:cwd];
} else {
exit(1);
return nil;
}
}
int main(int argc, const char *argv[]) {
@autoreleasepool {
NSArray *args = [[NSProcessInfo processInfo] arguments];
NSMutableArray *filePaths = [NSMutableArray arrayWithArray:args];
[filePaths removeObjectAtIndex:0];
if ([filePaths containsObject:@"-v"] || [filePaths containsObject:@"--version"]) {
printf("trash version %s\n", VERSION);
return 0;
}
if ([filePaths containsObject:@"-h"] || [filePaths containsObject:@"--help"] ||
[filePaths count] == 0) {
printUsage();
return 0;
}
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSString *cwd = currentWorkingDirectory();
NSMutableArray *fileURLs = [NSMutableArray array];
for (NSString *path in filePaths) {
NSString *absolutePath = [path stringByStandardizingPath];
if ([path hasPrefix:@"~"]) {
absolutePath = [absolutePath stringByExpandingTildeInPath];
} else if (![path hasPrefix:@"/"]) {
absolutePath = [[[NSFileManager defaultManager] currentDirectoryPath]
stringByAppendingPathComponent:path];
} else {
absolutePath = [cwd stringByAppendingPathComponent:path];
}
NSURL *fileURL = [NSURL fileURLWithPath:absolutePath];
[fileURLs addObject:fileURL];
}
__block BOOL completed = NO;
[workspace recycleURLs:fileURLs
completionHandler:^(NSDictionary<NSURL *, NSURL *> *newURLs, NSError *error) {
completed = YES;
CFRunLoopStop(CFRunLoopGetCurrent());
}];
while (!completed) {
CFRunLoopRun();
}
}
return 0;
}