2005-07-01 21:00:04 +00:00
|
|
|
/* Test/example program for the base library
|
|
|
|
|
|
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
|
|
|
|
2005-07-15 22:51:23 +00:00
|
|
|
Copying and distribution of this file, with or without modification,
|
|
|
|
are permitted in any medium without royalty provided the copyright
|
|
|
|
notice and this notice are preserved.
|
|
|
|
|
2005-07-01 21:00:04 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
*/
|
2001-04-19 08:31:19 +00:00
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
|
|
|
|
|
|
|
|
@interface TaskObs : NSObject
|
|
|
|
- (void) terminated: (NSNotification*)aNotification;
|
|
|
|
@end
|
|
|
|
@implementation TaskObs
|
|
|
|
- (void) terminated: (NSNotification*)aNotification
|
|
|
|
{
|
|
|
|
NSLog(@"Task (%@) terminated", [aNotification object]);
|
|
|
|
}
|
|
|
|
@end
|
1998-01-21 14:56:24 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
2001-04-19 08:31:19 +00:00
|
|
|
NSAutoreleasePool *pool;
|
1998-04-02 14:27:40 +00:00
|
|
|
NSDictionary *env;
|
1998-01-21 14:56:24 +00:00
|
|
|
NSTask *task;
|
2001-04-19 08:31:19 +00:00
|
|
|
NSTask *t0, *t1;
|
2001-02-05 09:28:19 +00:00
|
|
|
NSData *d;
|
2001-04-19 08:31:19 +00:00
|
|
|
TaskObs *obs = [TaskObs new];
|
1998-01-21 14:56:24 +00:00
|
|
|
|
2001-04-19 08:31:19 +00:00
|
|
|
pool = [NSAutoreleasePool new];
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver: obs
|
|
|
|
selector: @selector(terminated:)
|
|
|
|
name: NSTaskDidTerminateNotification
|
|
|
|
object: nil];
|
1998-01-21 14:56:24 +00:00
|
|
|
|
2005-10-11 19:09:26 +00:00
|
|
|
#ifdef __MINGW32__
|
2002-03-25 10:54:59 +00:00
|
|
|
task = [NSTask launchedTaskWithLaunchPath: @"C:\\windows\\system32\\mem.exe"
|
2000-09-22 04:20:52 +00:00
|
|
|
arguments: nil];
|
|
|
|
#else
|
1998-01-21 14:56:24 +00:00
|
|
|
task = [NSTask launchedTaskWithLaunchPath: @"/bin/ls"
|
|
|
|
arguments: nil];
|
2000-09-22 04:20:52 +00:00
|
|
|
#endif
|
1998-01-21 14:56:24 +00:00
|
|
|
[task waitUntilExit];
|
2002-03-25 10:54:59 +00:00
|
|
|
printf("Exit status - %d\n", [task terminationStatus]); fflush(stdout);
|
1998-01-21 14:56:24 +00:00
|
|
|
|
2001-04-19 08:31:19 +00:00
|
|
|
RELEASE(pool);
|
|
|
|
pool = [NSAutoreleasePool new];
|
1998-04-02 14:27:40 +00:00
|
|
|
|
|
|
|
task = [NSTask new];
|
|
|
|
env = [[[[NSProcessInfo processInfo] environment] mutableCopy] autorelease];
|
|
|
|
[task setEnvironment: env];
|
|
|
|
[task setLaunchPath: @"/bin/sh"];
|
|
|
|
[task setArguments: [NSArray arrayWithObjects: @"-c", @"echo $PATH", nil]];
|
2001-02-05 09:28:19 +00:00
|
|
|
if ([task usePseudoTerminal] == NO)
|
|
|
|
printf("Argh - unable to use pseudo terminal\n");
|
1998-04-02 14:27:40 +00:00
|
|
|
[task launch];
|
2001-02-05 09:28:19 +00:00
|
|
|
d = [[task standardOutput] availableData];
|
|
|
|
NSLog(@"Got PATH of '%*s'", [d length], [d bytes]);
|
|
|
|
|
1998-04-02 14:27:40 +00:00
|
|
|
[task waitUntilExit];
|
2001-04-19 08:31:19 +00:00
|
|
|
RELEASE(task);
|
|
|
|
|
|
|
|
NSLog(@"Testing two tasks at the same time");
|
|
|
|
t0 = [NSTask launchedTaskWithLaunchPath: @"/bin/sh"
|
|
|
|
arguments:
|
|
|
|
[NSArray arrayWithObjects: @"-c", @"echo task0", nil]];
|
|
|
|
NSLog(@"Launched task0 - %@", t0);
|
|
|
|
|
|
|
|
t1 = [NSTask launchedTaskWithLaunchPath: @"/bin/sh"
|
|
|
|
arguments:
|
|
|
|
[NSArray arrayWithObjects: @"-c", @"echo task1", nil]];
|
|
|
|
NSLog(@"Launched task1 - %@", t1);
|
|
|
|
|
|
|
|
while ([t0 isRunning] == YES || [t1 isRunning] == YES)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
|
|
|
|
2002-03-25 10:54:59 +00:00
|
|
|
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:
|
2001-04-19 08:31:19 +00:00
|
|
|
[NSDate dateWithTimeIntervalSinceNow: 1]];
|
|
|
|
RELEASE(arp);
|
|
|
|
}
|
|
|
|
RELEASE(pool);
|
1998-01-21 14:56:24 +00:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|