OSX Preference Pane and AFS Backgrounder
[openafs.git] / src / platform / DARWIN / AFSPreference / TaskUtil.m
1 //
2 //  TaskUtil.m
3 //  AFSCommander
4 //
5 //  Created by Claudio Bisegni on 25/06/07.
6 //  Copyright 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
7 //
8
9 #import "TaskUtil.h"
10
11
12 @implementation TaskUtil
13
14 // -------------------------------------------------------------------------------
15 //  executeTaskSearchingPath:
16 // -------------------------------------------------------------------------------
17 +(NSString*) executeTaskSearchingPath:(NSString*)unixCommand args:(NSArray*)args
18 {
19         NSString *commResult = nil;
20         NSString *commPath = [self searchExecutablePath:unixCommand];
21         if(commPath != nil){
22                 commResult = [self executeTask:commPath
23                                                    arguments:args];
24         }
25         return commResult;      
26 }
27
28 // -------------------------------------------------------------------------------
29 //  executeTask:
30 // -------------------------------------------------------------------------------
31 +(NSString*) searchExecutablePath:(NSString*)unixCommand
32 {
33         NSString *commPath = [self executeTask:@"/usr/bin/which" arguments:[NSArray arrayWithObjects:unixCommand, nil]];
34         return commPath;        
35 }
36
37 // -------------------------------------------------------------------------------
38 //  executeTask:
39 // -------------------------------------------------------------------------------
40 +(NSString*) executeTask:(NSString*) taskName arguments:(NSArray *)args{
41         NSString *result = nil;
42         int status = 0;
43         NSFileHandle *file = nil;
44         NSDictionary *environment =  [NSDictionary dictionaryWithObjectsAndKeys: @"$PATH:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin",@"PATH",nil];
45                 
46         NSPipe *pipe = [NSPipe pipe];
47         NSTask *taskToRun  = [[NSTask alloc] init];
48         
49         [taskToRun setLaunchPath: taskName];
50         [taskToRun setArguments: args];
51         [taskToRun setEnvironment:environment];
52         [taskToRun setStandardOutput: pipe];
53         file = [pipe fileHandleForReading];
54         [taskToRun launch];
55         [taskToRun waitUntilExit];
56         status = [taskToRun terminationStatus];
57         if (status == 0){
58                 NSData *data = [file readDataToEndOfFile];
59                 // remove the \n character from unix command
60                 if([data length] > 0){
61                         NSData *realData = [NSData dataWithBytes:[data bytes] 
62                                                                                           length:[data length]-1];
63                 
64                         [taskToRun release];
65                         result = [[NSString alloc] initWithData: realData 
66                                                                                    encoding: NSUTF8StringEncoding];
67                 }
68         } else {
69                 NSLog(@"Task failed.");
70         }
71         return [result autorelease];
72 }
73
74
75 // -------------------------------------------------------------------------------
76 //  executeTask:
77 // -------------------------------------------------------------------------------
78 +(int) executeTaskWithAuth:(NSString*) taskName arguments:(NSArray *)args authExtForm:(NSData*)auth {
79         NSString *result = nil;
80         int status = 0;
81         NSFileHandle *file = nil;
82         NSDictionary *environment =  [NSDictionary dictionaryWithObjectsAndKeys: @"$PATH:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin",@"PATH",nil];
83         
84         NSPipe *pipe = [NSPipe pipe];
85         NSPipe *pipeIn = [NSPipe pipe];
86         NSTask *taskToRun  = [[NSTask alloc] init];
87         
88         [taskToRun setLaunchPath: taskName];
89         [taskToRun setArguments: args];
90         [taskToRun setEnvironment:environment];
91         [taskToRun setStandardOutput: pipe];
92         [taskToRun setStandardInput: pipeIn];
93         file = [pipe fileHandleForReading];
94         //Write to standard in
95         [taskToRun launch];
96         
97         NSFileHandle* taskInput = [[ taskToRun standardInput ] fileHandleForWriting ];
98         [taskInput writeData:auth];
99         [taskToRun waitUntilExit];
100         status = [taskToRun terminationStatus];
101         if (status == 0){
102                 NSData *data = [file readDataToEndOfFile];
103                 // remove the \n character from unix command
104                 if([data length] > 0){
105                         NSData *realData = [NSData dataWithBytes:[data bytes] 
106                                                                                           length:[data length]-1];
107                         
108                         [taskToRun release];
109                         result = [[NSString alloc] initWithData: realData 
110                                                                                    encoding: NSUTF8StringEncoding];
111                         [result release];
112                 }
113         } else {
114         }
115         
116         return status;
117 }
118 @end