This repository was archived by the owner on Feb 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.m
More file actions
144 lines (113 loc) · 3.11 KB
/
watch.m
File metadata and controls
144 lines (113 loc) · 3.11 KB
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
#import <Foundation/Foundation.h>
#import <stdio.h>
#import <stdlib.h>
#import <getopt.h>
#import "XFWatchNode.h"
// Update version with: agvtool new-version -all <new version number>
extern const double watchVersionNumber;
int
main (int argc, char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSInteger maxDepth = 0;
NSString *executablePath = nil;
/* Parse options */
char ch, *q;
while( ( ch = getopt_long(argc, argv, "e:r:svh", NULL, NULL) ) != -1 ) {
switch(ch) {
case 'r':
maxDepth = (NSInteger)strtol(optarg, &q, 10);
/* If the string could be converted */
if(*q == '\0' && maxDepth > 0)
{
break;
}
break;
case 'v':
fprintf(stdout, "Version %d\n", (int)watchVersionNumber);
exit(0);
case 'e':
executablePath = [[NSString alloc] initWithUTF8String:optarg];
break;
case 's':
[XFWatchNode setEventLoggingEnabled:NO];
break;
case 'h':
default:
fprintf(stderr,
"watch [-h] [-r depth] [-e path_to_executable] [-v] [path path2 ...]\n");
exit(1);
}
}
if(!executablePath && ![XFWatchNode eventLoggingEnabled])
{
// Nothing for the tool to do, exit
printf("watch called with -s and no executable path, nothing to do, exiting...\n");
exit(0);
}
if(executablePath)
{
executablePath = [executablePath stringByResolvingSymlinksInPath];
}
NSMutableArray *directories = nil;
NSString *currentDirectory = [[NSFileManager defaultManager] currentDirectoryPath];
// Decrement the count for the options
argc -= optind;
if(argc < 1)
{
directories = [NSMutableArray arrayWithObject:currentDirectory];
}
else
{
directories = [NSMutableArray arrayWithCapacity:argc];
int count = 0;
while(count < argc)
{
[directories addObject:[NSString stringWithUTF8String:argv[optind+count++]]];
}
}
NSMutableArray *filtedDirectories = [NSMutableArray arrayWithCapacity:[directories count]];
for(NSString *directory in directories)
{
if([directory isAbsolutePath])
{
if(![[NSFileManager defaultManager] fileExistsAtPath:directory])
{
printf("Skipping invalid path: %s\n", [directory UTF8String]);
}
else
{
[filtedDirectories addObject:directory];
}
}
else
{
NSString *fullPath = [currentDirectory stringByAppendingPathComponent:directory];
if(![[NSFileManager defaultManager] fileExistsAtPath:fullPath])
{
printf("Skipping invalid path: %s\n", [fullPath UTF8String]);
}
else
{
[filtedDirectories addObject:fullPath];
}
}
}
NSMutableArray *topLevelNodes = [[NSMutableArray alloc] initWithCapacity:[filtedDirectories count]];
for(NSString *path in filtedDirectories)
{
NSInteger currentDepth = 0;
printf("Watching: %s\n", [path UTF8String]);
XFWatchNode *node = [XFWatchNode nodeWithPath:path];
[node setDepth:currentDepth];
[node setMaxDepth:maxDepth];
[node setParent:nil];
[node setExecutablePath:executablePath];
[topLevelNodes addObject:node];
[node createChildNodes];
}
[[NSRunLoop mainRunLoop] run];
[executablePath release];
[topLevelNodes release];
[pool drain];
return 0;
}