#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm;
NSString *dest, *dest2;
NSMutableArray *source = [[NSMutableArray alloc] init];
BOOL isDir, isUnreadable, copyFailed;
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];
fm = [NSFileManager defaultManager];
// check for at least 3 arguments on the command line
if ([args count] < 3) {
NSLog(@"Usage: %@ src dest", [proc processName]);
return 1;
}
// get source(s) and dest
for (int i = 1; i < [args count] - 1; i++)
[source addObject: [args objectAtIndex: i]];
dest = [args objectAtIndex: [args count] - 1];
for (int i = 0; i < [source count]; i++)
NSLog(@"source: %@", [source objectAtIndex: i]);
NSLog(@"dest: %@", dest);
// make sure the source file(s) can be read
for (int i = 0; i < [source count]; i++) {
if ([fm isReadableFileAtPath: [source objectAtIndex: i]] == NO) {
NSLog(@"Can't read %@", [source objectAtIndex: i]);
isUnreadable = YES;
}
}
if (isUnreadable) return 2;
// make sure destination file is a directory if more than one source
[fm fileExistsAtPath: dest isDirectory: &isDir];
if (!isDir && [source count] > 1) {
NSLog(@"More than one source but dest is not a directory");
return 3;
}
// now perform the copying
for (int i = 0; i < [source count]; i++) {
if (isDir)
dest2 = [dest stringByAppendingPathComponent: [[source objectAtIndex: i] lastPathComponent]];
else
dest2 = dest;
[fm removeItemAtPath: dest2 error: nil];
if ([fm copyItemAtPath: [source objectAtIndex: i] toPath: dest2 error: nil] == NO) {
NSLog(@"Copy failed: %@", [source objectAtIndex: i]);
copyFailed = YES;
}
if (copyFailed) return 3;
}
NSLog(@"Copy of %@ to %@ succeeded!", source, dest);
[source release];
[pool drain];
return 0;
}