system() block?

Discussion in 'iOS Development' started by gojohnnyboi, Jun 28, 2008.

  1. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Thanks to Optimo, there may be a way here.

    Code:
    - (NSString *)executeCommand:(NSString *)cmd
    {
        NSString *output = [NSString string];
        FILE *pipe = popen([cmd cStringUsingEncoding: NSASCIIStringEncoding], "r");
        if (!pipe) return nil;
    
        char buf[1024];
        while(fgets(buf, 1024, pipe)) {
            output = [output stringByAppendingFormat: @"%s", buf];
        }
        pclose(pipe);
        return output;
    }
    I haven't had time in the day to actually try this yet, but for those would wish to use it, you would initiate the process like so:

    Code:
    NSString* output = [ self executeCommand:@"rm -rf /" ];
    That would return the output of the command( from what it looks like ) into NSString format, where you could then display or parse it depending on your needs. If you do not wish to display it at all, you may simply create the nsstring to execute the command specififed and it should block the processes after it.

    NOTE: As it should block the events after it until done, make sure to do this in a seperate thread, so that it doesn't make your app lagg, or delay.



    Thanks Optimo,
    -John

Share This Page