iphone - How to capture standard output of the process started by system() -
how save output of system("openssl enc -aes-128-cbc -k secret -p -md sha1 > filename")
file.
i have tried following:
nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filename = [nsstring stringwithformat:@"%@/textfile.txt", documentsdirectory]; nsstring *str=[nsstring stringwithformat:@"openssl enc -aes-128-cbc -k secret -p -md sha1 > %s",[filename utf8string]]; nslog(@"aes key %@",str); system([str utf8string]); nsstring *straes = [nsstring stringwithcontentsoffile:filename encoding:nil error:nil]; nslog(@"straes key %@",straes); nsstring *straeskey=[[[[straes componentsseparatedbystring:@"\n"] objectatindex:1] componentsseparatedbystring:@"="] objectatindex:1]; nslog(@"straeskey key %@",straeskey); // nsstring *content = @"one\ntwo\nthree\nfour\nfive"; [str writetofile:filename atomically:no encoding:nsstringencodingconversionallowlossy error:nil];`
where going wrong ?
the system()
function sends output console since iphone not have console, need redirect output text file , key file utilize encryption/decryption.
first of all: aware, don't think can utilize system(3)
on not jail broken ios. i'm not sure, explain how it:
this little bit tricky.
to accomplish it, have know each process gets opened 3 file descriptors: 1 reading (stdin
standard input) , 2 writing (stdout
standard output , stderr
standard error output). have fd numbers 0 (stdin
), 1 (stdout
) , 2 (stderr
).
to redirect standard output or standard error file program, have following:
first,fork(2)
process. in parent process waitpid(2)
kid prozess. in kid process: reopen (freopen(3)
) stdout
, stderr
file want output redirected. use execve(2)
execute programme want call when command terminates, parent gets sigchld , waitpid(2)
should return the number in brackets describes chapter in man pages (man 2 waitpid
, e.g.) function.
hope helps, if have more questions, inquire :-)
update: since op wants output , not specificly file, can utilize popen(3)
:
int status; char value[1024]; file *fp = popen("openssl enc -aes-128-cbc -k secret -p -md sha1", "r"); if (fp == null) exit(1); // handle error while (fgets(value, 1024, fp) != null) { printf("value: %s", value); } status = pclose(fp); if (status == -1) { /* error reported pclose() */ } else { /* utilize macros described under wait() inspect `status' in order determine success/failure of command executed popen() */ }
iphone ios io-redirection
No comments:
Post a Comment