1

I'm running lsof through NSTask, pipe output and read into NSData. Then I create NSString with this data:

[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Problem I see, is how NSTask interprets special characters. For file with name: !@#$%^±^&*()ľščťžýáíé.docx I get this result: !@#$%^\xc2\xb1^&*()l\xcc\x8cs\xcc\x8cc\xcc\x8ct\xcc\x8cz\xcc\x8cy\xcc\x81a\xcc\x81i\xcc\x81e\xcc\x81.docx Seems like decomposed UTF8 with hex encoded values. Unfortunately I'm not able to find a way of converting this to proper UTF8.

Marcel Vyberal
  • 101
  • 1
  • 6

3 Answers3

3

I found out that setting environment variable LC_ALL to en_US.UTF-8 does the trick.

[task setEnvironment:@{@"LC_ALL" : @"en_US.UTF-8"}];
Marcel Vyberal
  • 101
  • 1
  • 6
1

In Swift NSTask is replaced by Process:

let process: Process = Process()
process.environment = ["LC_ALL": "en_US.UTF-8"]
pableiros
  • 14,932
  • 12
  • 99
  • 105
0

It work for me.

    NSTask *task = [[NSTask alloc] init];
    NSMutableDictionary * e = [NSMutableDictionary dictionaryWithDictionary:[[NSProcessInfo processInfo] environment]];
    [e setObject:@"en_US.UTF-8" forKey:@"LC_ALL"];
    [e setObject:@"en_US.UTF-8" forKey:@"LANG"];
    [task setEnvironment:e];
T_Wang
  • 1
  • 1