Jim,

i have an odd issue with NSTask. for some reason, no matter what result code my executable returns (im running xcodebuild, if that matters), NSTask's terminationStatus always reports back as 1. if instead i use system() to run the exact same command, i get bak proper error codes (0 for success., other
non-zero, non-one values for real failures.

anyone have any ideas what could be going wrong to cause this?

You'll have to show your code. NSTask correctly returns the exit code
(see below).

got it: i had been staring at this for hours, but when i pasted it here, it jumped at m right away: i accidentally passed the executable name as first item in the argument array. what threw me off is that according tot he console output, it seems that xcodebuild (which happens to be what i was running) does the build just fine, so i could not easily yell it was failing, compared to running it with system() - the output looked (close to) identical, down to the final "BULD SCUCCEEDED".

ftr, the code i was using is this. passing in the appropriate subarray of params fixed it. so thanx!


        NSArray *split = [command componentsSeparatedByString:@" "];
        
NSMutableArray *params = [NSMutableArray arrayWithCapacity:[split count]];
        for (NSString *s in split)
        {
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                if ([s length] > 0) [params addObject:s];
        }
        
        NSTask *t = [[[NSTask alloc] init] autorelease];
        [t setLaunchPath:[split objectAtIndex:0]];
        [t setArguments:params]; // oops. first element should not be here!
        [t setStandardOutput:[NSPipe pipe]];
        
        [t launch];
        NSFileHandle *stdOut = [[t standardOutput] fileHandleForReading];
        NSMutableData *outputData = nil;
        
        while ([t isRunning])
        {
                NSData *d = [stdOut availableData];
                if (d)
                {
                        if (outputData) [outputData appendData:d];
                        else outputData = [[d mutableCopy] autorelease];
                }
                [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];
        }

        if (outputData)
        {
NSString *s = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
                [output setArray:[s componentsSeparatedByString:@"\n"]];
        }

        [t waitUntilExit];
        int result = [t terminationStatus];
//int result = system([command cStringUsingEncoding:NSUTF8StringEncoding]);
        if (result)
        {
self.error = [NSString stringWithFormat:@"Execution of \"%...@\" failed with error code %d.", [split objectAtIndex:0], result];
                return NO;
        }
        return YES;


thanx,
marc
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to