如何捕获由system()启动的过程的标准输出 [英] How to capture standard output of the process started by system()
问题描述
如何将system("openssl enc -aes-128-cbc -k secret -P -md sha1 > FILENAME")
的输出保存到文件中.
How do I save the output of system("openssl enc -aes-128-cbc -k secret -P -md sha1 > FILENAME")
into a file.
我尝试了以下操作:
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 is %@",str);
system([str UTF8String]);
NSString *strAES = [NSString stringWithContentsOfFile:fileName encoding:nil error:nil];
NSLog(@"strAES Key is %@",strAES);
NSString *strAESKey=[[[[strAES componentsSeparatedByString:@"\n"] objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1];
NSLog(@"strAESKey Key is %@",strAESKey);
// NSString *content = @"One\nTwo\nThree\nFour\nFive";
[str writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];`
我要去哪里错了?
system()
函数将输出发送到控制台,但是由于iPhone没有控制台,因此我需要将输出重定向到文本文件,并从文件中获取密钥,以将其用于加密/解密. /p>
The system()
function sends the output to the console but since the iPhone does not have a console, I need to redirect the output to a text file and get the key from the file to use it for encryption/decryption.
推荐答案
首先:请注意,我认为您不能在没有越狱的iOS上使用system(3)
.但是我不确定,所以我将解释如何做:
First of all: be aware, I don't think you can use system(3)
on an not jail broken iOS. But I'm not sure, so I will explain how to do it:
这有点棘手.
要实现它,您必须知道每个进程都使用三个文件描述符打开:一个用于读取(stdin
用于标准输入)和两个用于写入(stdout
用于标准输出,stderr
用于标准错误)输出).它们的fd编号分别为0(stdin
),1(stdout
)和2(stderr
).
To achieve it, you have to know that each process gets opened with three file descriptors: one for reading (stdin
for standard input) and two for writing (stdout
for standard output and stderr
for standard error output). They have the fd numbers 0 (stdin
), 1 (stdout
) and 2 (stderr
).
要将标准输出或标准错误从程序重定向到文件,您必须执行以下操作:
To redirect standard output or standard error to a file from a program, you have to do the following:
- 首先,
fork(2)
该过程.在父进程中,子进程waitpid(2)
会ze头. - 在子进程中:
- 重新打开(
freopen(3)
)stdout
和stderr
到要重定向输出的文件. - 使用
execve(2)
执行要调用的程序 - 命令终止时,父级将获得SIGCHLD,并且
waitpid(2)
应该返回
- first,
fork(2)
the process. In the parent processwaitpid(2)
for the child prozess. - In the child process:
- reopen (
freopen(3)
)stdout
andstderr
to the file you want the output to be redirected. - use
execve(2)
to execute the program you want to call - when the command terminates, the parent gets a SIGCHLD and
waitpid(2)
should return
括号中的数字描述了该功能的手册页中的章节(例如,
man 2 waitpid
).The number in the brackets describes the chapter in the man pages (
man 2 waitpid
, e.g.) for the function.希望这会有所帮助,如果您还有其他问题,请询问:-)
Hope this helps, if you have more questions, ask :-)
更新:由于OP只是想获取输出,而不是专门获取文件,因此可以使用
popen(3)
:Update: Since the OP just wants to get the output and not specificly into a file, you can use
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 by pclose() */ } else { /* Use macros described under wait() to inspect `status' in order to determine success/failure of command executed by popen() */ }
这篇关于如何捕获由system()启动的过程的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- reopen (
- 重新打开(