1. LeonBlade's Avatar
    Hello everyone,

    How can I run apt command lines from my code like using system() or something equivalent so that I can get a return back and parse my data to create such an app.

    I already have something working in a bash script, I just need to implement this on my device.

    Can someone help?

    Thanks,
    LeonBlade
    2010-06-25 01:48 AM
  2. NetMage's Avatar
    I think you need to use fork/popen/execv to capture stdout from your child process.

    There may be something simpler in Cocoa somewhere...
    Starlight Computer Wizardry
    Pocket-sized Development
    Follow me on twitter: @NetMage
    2010-06-26 08:27 AM
  3. LeonBlade's Avatar
    I assume I need to use NSTask, but it's disabled in the iOS SDK.
    Does anyone know how I can get it to work for the iPhone?
    There's no place like ~/
    2010-06-29 01:54 PM
  4. thebombzen's Avatar
    I'm not sure, but it's not execv. That kills your program and passes control to the program you exec. I'm not sure what fork and popen are, but it's not execv.
    2010-06-30 01:16 AM
  5. LeonBlade's Avatar
    Bah, the Cydia source is way too messy to look through, but I guess I can attempt to?
    I just wish that NSTask was working on iPhone...

    I haven't done anything with trying to capture the stdout before, so I'm unsure of how to go about it.
    There's no place like ~/
    2010-06-30 01:19 AM
  6. thebombzen's Avatar
    By capture stdout, you might want to do either a redirect or a pipe.
    If to redirect standard output to myfile, add >myfile to the end of your command line run. To redirect it to standard error, do >&2. To redirect error to output, do 2>&1. To redirect error to a file, do 2>myfile.

    You might also want a pipe. That is done by doing this:
    command1 | command2
    This effectively feeds standard output of one binary into standard input of another. It doesn't work exactly like this, but it sorta does. Examples are easier. For example, look at this command:
    find / -name '*.dylib' -name '*.a' | grep 'gcc'
    find / -name '*.dylib' -name '*.a' will find all files on the hard drive with the extension .dylib or .a.
    The command "grep" searches for Regular Expressions. The output of find lists a bunch of files but is not printed, it is fed into grep. The grep command will then search the output, and only print lines that have the letters 'gcc' in them. This is capturing the output and feeding it to something else, if this is what you are trying to do. If not, then IDK.
    2010-06-30 01:41 AM
  7. LeonBlade's Avatar
    Yeah, that's what I want to do.
    How can I pass the output though to a variable lets say?
    There's no place like ~/
    2010-06-30 01:47 AM
  8. thebombzen's Avatar
    I'm pretty sure what your saying is this:
    You want to execute a program from within your app.
    You want to grab the standard output of the program, and redirect it to a variable in your app. I'm not sure how to do this. What I would try to do is to redirect standard output to a file, then read it. For example:

    Code:
    #include <all.necessary.headers>
    using namespace std;
    
    int main(int argc, char* argv[]{
      pid_t pID = vfork();
    
      if (pID == 0){
        int fd;
        if((fd = open("temp.txt", O_RDWR | O_CREAT))==-1){ /*open the file */
          cerr << "Error opening output" << endl;
          exit(1);
        }
        if((fde = open("error.log", O_RDWR | O_CREAT))==-1{
          cerr << "Error opening error.log (how ironic)" << endl;
          exit(1);
        }
        dup2(fd,STDOUT_FILENO);
        dup2(fde,STDERR_FILENO);
        execl("/my_exec_file", "my_exec_file", arg1, arg2, arg3..., (char*) 0);
    
      } else if (pID < 0) //Error Code handler;
      else {
        //here is where you read the file temp.txt, and then finish your program.
      }
      return 0;
    }
    For more info, look here:
    Redirecting STDOUT with execl
    and here
    Using vfork
    If this is what you wanted, then woohoo!
    Last edited by thebombzen; 2010-06-30 at 03:52 AM. Reason: added the code format
    2010-06-30 03:51 AM
  9. NetMage's Avatar
    *sigh*

    Let me google that for you
    Starlight Computer Wizardry
    Pocket-sized Development
    Follow me on twitter: @NetMage
    2010-06-30 07:08 AM
  10. linuxmac09's Avatar
    I assume I need to use NSTask, but it's disabled in the iOS SDK.
    Does anyone know how I can get it to work for the iPhone?
    NSTask is available in ios however you have to copy the NSTask header file into your project in order to use it
    2010-10-13 01:09 AM
LINK TO POST COPIED TO CLIPBOARD