本文介绍了在这种情况下使用PERROR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个小程序(与SO code)该做 printenv |排序|少
现在,我想添加错误处理与PERROR并检查返回值。我从来没有这样做过,但我想这是类似的异常处理。我需要检查execvp,叉,钢管和dup2错误。我有这个code
I've written a small program (with code from SO) that does printenv | sort | less
and now I want to add error-handling with perror and checking the return values. I've never done this before but I suppose it is similar to exception handling. I need to check errors for execvp, fork, pipe and dup2. I have this code
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct command
{
const char **argv;
};
/* Helper function that spawns processes */
int spawn_proc (int in, int out, struct command *cmd) {
pid_t pid;
if ((pid = fork ()) == 0) {
if (in != 0) {
dup2 (in, 0);
close (in);
}
if (out != 1) {
dup2 (out, 1);
close (out);
}
return execvp (cmd->argv [0], (char * const *)cmd->argv);
}
return pid;
}
/* Helper function that forks pipes */
int fork_pipes (int n, struct command *cmd) {
int i;
int in, fd [2];
for (i = 0; i < n - 1; ++i) {
pipe (fd);
spawn_proc (in, fd [1], cmd + i);
close (fd [1]);
in = fd [0];
}
dup2 (in, 0);
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
int main (int argc, char ** argv) {
int i;
if (argc == 1) { /* There were no arguments */
const char *printenv[] = { "printenv", 0};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {sort}, {less} };
return fork_pipes (3, cmd);
}
if (argc > 1) { /* I'd like an argument */
if (strncmp(argv[1], "cd", 2) && strncmp(argv[1], "exit", 2)) {
char *tmp;
int len = 1;
for( i=1; i<argc; i++)
{
len += strlen(argv[i]) + 2;
}
tmp = (char*) malloc(len);
tmp[0] = '\0';
int pos = 0;
for( i=1; i<argc; i++)
{
pos += sprintf(tmp+pos, "%s%s", (i==1?"":"|"), argv[i]);
}
const char *printenv[] = { "printenv", 0};
const char *grep[] = { "grep", "-E", tmp, NULL};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
return fork_pipes (4, cmd);
free(tmp);
} else if (! strncmp(argv[1], "cd", 2)) { /* change directory */
printf("change directory to %s\n" , argv[2]);
chdir(argv[2]);
} else if (! strncmp(argv[1], "exit", 2)) { /* change directory */
printf("exit\n");
exit(0);
}
}
exit(0);
}
然后可以请你告诉我怎么写PERROR使用。我想我应该补充code像下面的
Then can you please tell me how to write the perror usage. I suppose I should add code like the following
if( -1 == return_value )
{ perror( "Program failed" ); exit( 1 ); }
但我并不确切地知道如何。
But I don't know exactly how.
推荐答案
这是pretty就像你说的,例如:
it's pretty much as you said, for example
int spawn_proc (int in, int out, struct command *cmd) {
pid_t pid;
if ((pid = fork()) == 0) {
if (in != 0) {
if (dup2(in, 0) < 0) {
perror("dup2 failed");
exit(1);
}
close(in);
}
if (out != 1) {
if (dup2(out, 1) < 0) {
perror("dup2 failed");
exit(1);
}
close(out);
}
if (execvp(cmd->argv [0], (char * const *)cmd->argv) < 0) {
perror("execvp failed");
exit(1);
}
}
else if (pid < 0) {
perror("fork failed");
exit(1);
}
return pid;
}
这篇关于在这种情况下使用PERROR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!