#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
/* dirname - return the directory portion of a pathname */
/* fails if pathname are all "////" and probably some other circumstances too!! */
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm = [ NSFileManager defaultManager];
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];
/* NSMutableArray * pComponents; */
NSString *command_line_path, *dirPath ;
BOOL isDirect, exists;
int pathCount;
/* check usage */
if ( args.count != 2)
{
NSLog(@"Usage:%@ pathname NAME", [proc processName]);
return 1;
}
command_line_path = [args objectAtIndex: 1];
pathCount = [[ command_line_path pathComponents]count];
/* is path a directory */
exists = [fm fileExistsAtPath: command_line_path isDirectory: &isDirect];
if (!isDirect && pathCount > 1)
dirPath = [command_line_path stringByDeletingLastPathComponent];
else if (!isDirect && pathCount == 1)
dirPath = @".";
else
dirPath = command_line_path;
NSLog(@"%@", dirPath);
[pool drain];
return 0;
}