More robust technique for reading from pipeline

This commit is contained in:
Matthias Neeracher 2006-11-10 08:54:00 +00:00
parent 24b15c0f14
commit 97de91380c

View File

@ -66,16 +66,26 @@
{ {
char line[1000]; char line[1000];
FILE * output = popen([command UTF8String], "r"); FILE * output = popen([command UTF8String], "r");
NSString * outLine = nil;
for (int attempts=0; attempts<5; ++attempts)
if (fgets(line, 1000, output)) { if (fgets(line, 1000, output)) {
size_t len = strlen(line); size_t len = strlen(line);
if (len && line[len-1]=='\n') { if (len && line[len-1]=='\n') {
line[len-1] = 0; line[len-1] = 0;
return [NSString stringWithUTF8String:line]; outLine = [NSString stringWithUTF8String:line];
} }
} else } else if (feof(output))
NSLog(@"Failed command: %@ %s (%d)\n", command, feof(output) ? "EOF" : "Error", errno); break;
else
clearerr(output);
if (!outLine)
NSLog(@"Failed command: %@ %s (%d)\n", command,
feof(output) ? "EOF" : "Error", errno);
pclose(output); pclose(output);
return nil;
return outLine;
} }
- (NSString *)lilypondVersion:(NSString *)path - (NSString *)lilypondVersion:(NSString *)path