重定向问题 [英] Redirection issue

查看:67
本文介绍了重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我写了一个C程序,它执行了我前几天发布的上一个主题中提到的一些功能。

。这个shell

应该:

1-执行来自标准输入的输入命令,还来自一个文件

包含命令

2-对文件的输入和输出进行重定向。

3-检索环境变量,如HOME,..


I做了第一个问题并且它工作正常,但是当我修改

代码来实现重定向时它不再起作用了。


你能不能请一个看看我的代码并告诉我哪个是错的

with?


如果你能告诉我如何实施第二个我也会很感激
函数parse_line中的
问题(请参阅下面的代码):


非常感谢您对此的帮助


Souissipro :


--------------- SHELL --------------------- ------

#include< stdio.h>

#include< stdlib.h>

#include< string.h> ;

#include< unistd.h&g t;

#include< fcntl.h>


/ *最大命令参数数量* /


#define NARGS 50

#define MAXL 255

static char * prompt =" $" ;;

static char * sep =" \ n" ;;


int

do_exit(char * argv [])

{

如果(argv [1] == NULL)

退出(EXIT_SUCCESS);

else

fprintf(stderr,"错误:don 不要带参数。#n;

返回1;

}


int

simple_cmd(char * argv [])

{

int i;

int pid,status;


if(strcmp(argv [0]," exit")== 0)

返回do_exit(argv);

for(i = 0; argv [i]!= NULL; i ++)

printf(" argv [%i] = \"%s \"; \ n",i,argv [ i]);


if(strcmp(argv [0]," cd")== 0)

{

返回chdir(* argv);

}


/ *

*获取子进程。

* /

if((pid = fork())< 0){

perror(" fork");

退出(1);

}


/ *

*里面的子执行代码如果。

* /

if(pid == 0){

execvp(* argv,argv);

perror(* argv);

exit(1);


}


/ *

*父执行等待。

* /

while(等待(& status)!= pid)

/ *空* /;

}


int

redir_cmd(char * argv [],char * in,char * out)

{

int i;

int pid,status;

int fd1,fd2;

int dummy;


if(strcmp (argv [0]," exit")== 0)

返回do_exit(argv);

for(i = 0; argv [i]!= NULL ; i ++)

printf(" argv [%i] = \"%s \" ;; \ n",i,argv [i]);


if(strcmp(argv [0]," cd")== 0)

{

return chdir(* argv);

}


if(fork()== 0){

fd1 = open(in,O_RDONLY);

if(fd1< 0){

perror(不能打开文件:in);

退出(1);

}


if(dup2(fd1,out)!= 0){

perror(" catf1f2:dup2(in,0)");

退出(1);

}

关闭(fd1);


fd2 =开放(外出, O_WRONLY | O_TRUNC | O_CREAT,0644);

if(fd2< 0){

perror(" catinout:out");

退出(2);

}

/ *

if(dup2(fd2,1)!= 1){

perror(catf1f2:dup2(out,1));

退出(1);

}

* /

关闭(fd2);


execvp(* argv,argv);

perror(* argv); < br $> b $ b退出(1);


}


/ *

*父执行等等。

* /

else {

等待(& dummy);

}


}


int

parse_line(char * s,char * in,char * out)

{

char * argv [NARGS],* cur,* old;

int i = 0;


if((s [0] ==''#'')||(s [0] ==''\ n''))

返回0;

old = cur = s;

while((cur = strpbrk(cur,sep))!= NULL){

argv [i ++] = old;

cur [0] =''\ 0'';

cur ++;

old = cur;

}

argv [i] = NULL;

如果

返回redir_cmd (argv,in,out);

}


/ *主要功能

* /

int

main(int argc,char * argv [])

{

char s [MAXL];


int i;

char buf [255];

FILE * f;


if(argc 3){

fprintf(stderr,不正确的参数\ n);

退出(EXIT_FAILURE);

}

if(argc == 2){

f = fopen(argv [1]," r");

if(f == NULL){

fprintf(stderr,无法打开文件阅读\ n);

退出(EXIT_FAILURE);

}


w hile(fgets(buf,255,f)!= NULL){

parse_line(buf,argv [1]," stdout");

printf(" %s",buf);

}

fclose(f);

}

if(argc = = 3){

f = fopen(argv [1]," r");

if(f == NULL){

fprintf(stderr,无法打开文件阅读\ n);

退出(EXIT_FAILURE);

}


while(fgets(buf,255,f)!= NULL){

parse_line(buf,argv [1],argv [2]);

printf ("%s",buf);

}

fclose(f);

}

如果(argc == 1){


printf(" vsh - mini-shell5.\\\
\\\
%s",提示);

while(fgets(s,MAXL,stdin)!= NULL){

parse_line(s," stdin"," stdout");

printf(" %s",提示);

}

}

返回EXIT_FAILURE;

}

解决方案

" ;;

static char * sep =" \ n" ;;


int

do_exit(char * argv [])

{

如果(argv [1] == NULL)

退出(EXIT_SUCCESS);

else

fprintf(stderr,"错误:don 不要带参数。#n;

返回1;

}


int

simple_cmd(char * argv [])

{

int i;

int pid,status;


if(strcmp(argv [0]," exit")== 0)

返回do_exit(argv);

for(i = 0; argv [i]!= NULL; i ++)

printf(" argv [%i] = \"%s \"; \ n",i,argv [ i]);


if(strcmp(argv [0]," cd")== 0)

{

返回chdir(* argv);

}


/ *

*获取子进程。

* /

if((pid = fork())< 0){

perror(" fork");

退出(1);

}


/ *

*里面的子执行代码如果。

* /

if(pid == 0){

execvp(* argv,argv);

perror(* argv);

exit(1);


}


/ *

*父执行等待。

* /

while(等待(& status)!= pid)

/ *空* /;

}


int

redir_cmd(char * argv [],char * in,char * out)

{

int i;

int pid,status;

int fd1,fd2;

int dummy;


if(strcmp (argv [0]," exit")== 0)

返回do_exit(argv);

for(i = 0; argv [i]!= NULL ; i ++)

printf(" argv [%i] = \"%s \" ;; \ n",i,argv [i]);


if(strcmp(argv [0]," cd")== 0)

{

return chdir(* argv);

}


if(fork()== 0){

fd1 = open(in,O_RDONLY);

if(fd1< 0){

perror(不能打开文件:in);

退出(1);

}


if(dup2(fd1,out)!= 0){

perror(" catf1f2:dup2(in,0)");

退出(1);

}

关闭(fd1);


fd2 =开放(外出, O_WRONLY | O_TRUNC | O_CREAT,0644);

if(fd2< 0){

perror(" catinout:out");

退出(2);

}

/ *

if(dup2(fd2,1)!= 1){

perror(catf1f2:dup2(out,1));

退出(1);

}

* /

关闭(fd2);


execvp(* argv,argv);

perror(* argv); < br $> b $ b退出(1);


}


/ *

*父执行等等。

* /

else {

等待(& dummy);

}


}


int

parse_line(char * s,char * in,char * out)

{

char * argv [NARGS],* cur,* old;

int i = 0;


if((s [0] ==''#'')||(s [0] ==''\ n''))

返回0;

old = cur = s;

while((cur = strpbrk(cur,sep))!= NULL){

argv [i ++] = old;

cur [0] =''\ 0'';

cur ++;

old = cur;

}

argv [i] = NULL;

如果

返回redir_cmd (argv,in,out);

}


/ *主要功能

* /

int

main(int argc,char * argv [])

{

char s [MAXL];


int i;

char buf [255];

FILE * f;


if(argc 3){

fprintf(stderr,不正确的参数\ n);

退出(EXIT_FAILURE);

}

if(argc == 2){

f = fopen(argv [1]," r");

if(f == NULL){

fprintf(stderr,无法打开文件阅读\ n);

退出(EXIT_FAILURE);

}


w hile(fgets(buf,255,f)!= NULL){

parse_line(buf,argv [1]," stdout");

printf(" %s",buf);

}

fclose(f);

}

if(argc = = 3){

f = fopen(argv [1]," r");

if(f == NULL){

fprintf(stderr,无法打开文件阅读\ n);

退出(EXIT_FAILURE);

}


while(fgets(buf,255,f)!= NULL){

parse_line(buf,argv [1],argv [2]);

printf ("%s",buf);

}

fclose(f);

}

如果(argc == 1){


printf(" vsh - mini-shell5.\\\
\\\
%s",提示);

while(fgets(s,MAXL,stdin)!= NULL){

parse_line(s," stdin"," stdout");

printf(" %s",提示);

}

}

返回EXIT_FAILURE;

}


souissipro写道:




我编写了一个C程序来完成上一个主题中提到的一些功能

几天前发布的。这个shell

应该:

1-执行来自标准输入的输入命令,还来自一个文件

包含命令

2-对文件的输入和输出进行重定向。

3-检索环境变量,如HOME,..


I做了第一个问题并且它工作正常,但是当我修改

代码来实现重定向时它不再起作用了。


你能不能请一个看看我的代码并告诉我哪个是错的

with?


如果你能告诉我如何实施第二个我也会很感激
函数parse_line中的
问题(请参阅下面的代码):


非常感谢您对此的帮助


Souissipro :


--------------- SHELL --------------------- ------

#include< stdio.h>

#include< stdlib.h>

#include< string.h> ;

#include< unistd.h>

#include< fcntl.h>



< SNIP其余UNIX特定代码>


我们这里只处理标准C;你需要在comp.unix.programmer上问你的问题




souissipro写道:





我写了一个C程序,它可以完成一些功能

在我前几天发表的主题中提到过。这个shell

应该:

1-执行来自标准输入的输入命令,还来自一个文件

包含命令

2-对文件的输入和输出进行重定向。

3-检索环境变量,如HOME,..


I做了第一个问题并且它工作正常,但是当我修改

代码来实现重定向时它不再起作用了。


你能不能请一个看看我的代码并告诉我哪个是错的

with?


如果你能告诉我如何实施第二个我也会很感激
函数parse_line中的
问题(请参阅下面的代码):


非常感谢您对此的帮助



而不是我们查看你的代码,我有一些一般性的建议

帮助你自己帮助。


首先:描述问题。 />
短语它不再起作用了 OT;带有很少的意义。

不起作用可能是任何事情,从不编译,不执行,

立即崩溃,崩溃整个计算机,以及许多其他方式

的不工作。你有一个berfore和之后的案例。它做了什么

之前它现在没有做?请明确点。正确定义

问题通常会导致找到正确的解决方案。


第二:隔离更改。

运行差异该程序的当前版本和以前版本(你这样做是否需要使用源代码控制系统?b $ b确定改变的方式

是否破坏了代码工作。例如,查找与局部变量同名的全局

变量。注意未终止的

块评论。其他常见错误。


三:跟踪你的步骤。

如果你无法从这些想法中找到答案,请尝试添加一些调试

记录程序或在调试器下运行程序。


四:斜线和刻录。

如果问题严重(例如,崩溃和核心转储)没有别的

帮助你隔离它,然后开始从

当前版本中删除部分代码,直到它不再崩溃,或者你回到

以前的工作版本。例如,切割功能的主体,只留下一个

的回报可能很有用。


你很有可能找到原因问题

自从你从一个工作版本转到一个非工作版本的b $ b版本。通过学习如何自己找到这个

,你将是一个更好的程序员。请记住,该程序正在完成您所说的

要做的事情。


ed


Hi,

I have written a C program that does some of the functionalities
mentionned in my previous topic posted some days ago. This shell
should:
1- execute input commands from standard input, and also from a file
conatining the commands
2- does the redirection of the input and output from and to files.
3- retrieve the environment variables like HOME,..

I did the first question and it works fine, however when I modified the
code to implement the redirection it does not work anymore.

Could you please have a look to my code and tell me which is wrong
with?

I would also appreciate if you can tell me how to implement the 2nd
question in the function parse_line (see the code below):

Thank you very much for your help on this

Souissipro:

---------------SHELL---------------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>

/* maximal number of command arguments */

#define NARGS 50
#define MAXL 255

static char *prompt = "$ ";
static char *sep = " \n";

int
do_exit(char *argv[])
{
if (argv[1]==NULL)
exit(EXIT_SUCCESS);
else
fprintf(stderr,"Error: don''t take arguments.\n");
return 1;
}

int
simple_cmd(char *argv[])
{
int i;
int pid, status;

if (strcmp(argv[0],"exit")==0)
return do_exit(argv);
for(i=0;argv[i]!=NULL;i++)
printf("argv[%i]=\"%s\";\n",i,argv[i]);

if (strcmp(argv[0],"cd")==0)
{
return chdir(*argv);
}

/*
* get the child process.
*/
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}

/*
* child execute code inside if.
*/
if (pid == 0) {
execvp(*argv, argv);
perror(*argv);
exit(1);

}

/*
* parent execute wait.
*/
while (wait(&status) != pid)
/* empty */ ;
}

int
redir_cmd(char *argv[],char *in, char *out)
{
int i;
int pid, status;
int fd1, fd2;
int dummy;

if (strcmp(argv[0],"exit")==0)
return do_exit(argv);
for(i=0;argv[i]!=NULL;i++)
printf("argv[%i]=\"%s\";\n",i,argv[i]);

if (strcmp(argv[0],"cd")==0)
{
return chdir(*argv);
}

if (fork() == 0) {
fd1 = open(in, O_RDONLY);
if (fd1 < 0) {
perror("can''t open file : in");
exit(1);
}

if (dup2(fd1, out) != 0) {
perror("catf1f2: dup2(in, 0)");
exit(1);
}
close(fd1);

fd2 = open(out, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (fd2 < 0) {
perror("catinout: out");
exit(2);
}
/*
if (dup2(fd2, 1) != 1) {
perror("catf1f2: dup2(out, 1)");
exit(1);
}
*/
close(fd2);

execvp(*argv, argv);
perror(*argv);
exit(1);

}

/*
* parent executes wait.
*/
else {
wait(&dummy);
}

}

int
parse_line(char *s,char *in, char *out)
{
char *argv[NARGS], *cur, *old;
int i=0;

if ((s[0]==''#'')||(s[0]==''\n''))
return 0;
old=cur=s;
while((cur=strpbrk(cur,sep))!=NULL) {
argv[i++]=old;
cur[0]=''\0'';
cur++;
old=cur;
}
argv[i]=NULL;
if
return redir_cmd(argv,in, out);
}

/* Main function
*/

int
main(int argc, char *argv[])
{
char s[MAXL];

int i;
char buf[255];
FILE *f;

if (argc 3) {
fprintf(stderr,"incorrect arguments \n");
exit(EXIT_FAILURE);
}
if (argc == 2) {
f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "impossible to open the file for reading\n");
exit(EXIT_FAILURE);
}

while (fgets(buf, 255, f) != NULL) {
parse_line(buf,argv[1],"stdout");
printf("%s",buf);
}
fclose(f);
}
if (argc == 3) {
f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "impossible to open the file for reading\n");
exit(EXIT_FAILURE);
}

while (fgets(buf, 255, f) != NULL) {
parse_line(buf,argv[1],argv[2]);
printf("%s",buf);
}
fclose(f);
}
if (argc == 1) {

printf("vsh - mini-shell5.\n\n%s",prompt);
while(fgets(s,MAXL,stdin)!=NULL) {
parse_line(s,"stdin","stdout");
printf("%s",prompt);
}
}
return EXIT_FAILURE;
}

解决方案

";
static char *sep = " \n";

int
do_exit(char *argv[])
{
if (argv[1]==NULL)
exit(EXIT_SUCCESS);
else
fprintf(stderr,"Error: don''t take arguments.\n");
return 1;
}

int
simple_cmd(char *argv[])
{
int i;
int pid, status;

if (strcmp(argv[0],"exit")==0)
return do_exit(argv);
for(i=0;argv[i]!=NULL;i++)
printf("argv[%i]=\"%s\";\n",i,argv[i]);

if (strcmp(argv[0],"cd")==0)
{
return chdir(*argv);
}

/*
* get the child process.
*/
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}

/*
* child execute code inside if.
*/
if (pid == 0) {
execvp(*argv, argv);
perror(*argv);
exit(1);

}

/*
* parent execute wait.
*/
while (wait(&status) != pid)
/* empty */ ;
}

int
redir_cmd(char *argv[],char *in, char *out)
{
int i;
int pid, status;
int fd1, fd2;
int dummy;

if (strcmp(argv[0],"exit")==0)
return do_exit(argv);
for(i=0;argv[i]!=NULL;i++)
printf("argv[%i]=\"%s\";\n",i,argv[i]);

if (strcmp(argv[0],"cd")==0)
{
return chdir(*argv);
}

if (fork() == 0) {
fd1 = open(in, O_RDONLY);
if (fd1 < 0) {
perror("can''t open file : in");
exit(1);
}

if (dup2(fd1, out) != 0) {
perror("catf1f2: dup2(in, 0)");
exit(1);
}
close(fd1);

fd2 = open(out, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (fd2 < 0) {
perror("catinout: out");
exit(2);
}
/*
if (dup2(fd2, 1) != 1) {
perror("catf1f2: dup2(out, 1)");
exit(1);
}
*/
close(fd2);

execvp(*argv, argv);
perror(*argv);
exit(1);

}

/*
* parent executes wait.
*/
else {
wait(&dummy);
}

}

int
parse_line(char *s,char *in, char *out)
{
char *argv[NARGS], *cur, *old;
int i=0;

if ((s[0]==''#'')||(s[0]==''\n''))
return 0;
old=cur=s;
while((cur=strpbrk(cur,sep))!=NULL) {
argv[i++]=old;
cur[0]=''\0'';
cur++;
old=cur;
}
argv[i]=NULL;
if
return redir_cmd(argv,in, out);
}

/* Main function
*/

int
main(int argc, char *argv[])
{
char s[MAXL];

int i;
char buf[255];
FILE *f;

if (argc 3) {
fprintf(stderr,"incorrect arguments \n");
exit(EXIT_FAILURE);
}
if (argc == 2) {
f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "impossible to open the file for reading\n");
exit(EXIT_FAILURE);
}

while (fgets(buf, 255, f) != NULL) {
parse_line(buf,argv[1],"stdout");
printf("%s",buf);
}
fclose(f);
}
if (argc == 3) {
f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "impossible to open the file for reading\n");
exit(EXIT_FAILURE);
}

while (fgets(buf, 255, f) != NULL) {
parse_line(buf,argv[1],argv[2]);
printf("%s",buf);
}
fclose(f);
}
if (argc == 1) {

printf("vsh - mini-shell5.\n\n%s",prompt);
while(fgets(s,MAXL,stdin)!=NULL) {
parse_line(s,"stdin","stdout");
printf("%s",prompt);
}
}
return EXIT_FAILURE;
}


souissipro wrote:

Hi,

I have written a C program that does some of the functionalities
mentionned in my previous topic posted some days ago. This shell
should:
1- execute input commands from standard input, and also from a file
conatining the commands
2- does the redirection of the input and output from and to files.
3- retrieve the environment variables like HOME,..

I did the first question and it works fine, however when I modified the
code to implement the redirection it does not work anymore.

Could you please have a look to my code and tell me which is wrong
with?

I would also appreciate if you can tell me how to implement the 2nd
question in the function parse_line (see the code below):

Thank you very much for your help on this

Souissipro:

---------------SHELL---------------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>

<SNIP rest of UNIX specific code>

We only deal with standard C here ; you
need to ask your question at comp.unix.programmer



souissipro wrote:

Hi,

I have written a C program that does some of the functionalities
mentionned in my previous topic posted some days ago. This shell
should:
1- execute input commands from standard input, and also from a file
conatining the commands
2- does the redirection of the input and output from and to files.
3- retrieve the environment variables like HOME,..

I did the first question and it works fine, however when I modified the
code to implement the redirection it does not work anymore.

Could you please have a look to my code and tell me which is wrong
with?

I would also appreciate if you can tell me how to implement the 2nd
question in the function parse_line (see the code below):

Thank you very much for your help on this

Rather then us looking at your code, I have some general suggestions to
help you help yourself.

First: describe the problem.
the phrase "it does not work anymore" carries very little meaning.
"does not work" could be anything from not compiling, not executing,
crashing immediately, crashing the entire computer, and many other ways
of "not working". You have a case of berfore and after. What did it do
before that it does not do now? BE SPECIFIC. Properly defining the
problem often leads to finding the right solution.

Second: isolate the changes.
Run a diff on the current and previous versions of the program (You do
use a source control system don''t you?) Determine how the changes could
have broken the code that was working. For example, look for global
variables with the same name as local variables. Watch for unterminated
block comments. Other common errors.

Three: trace your steps.
If you cannot figure it out from those ideas, try adding some debug
logging through the program OR run the program under a debugger.

Four: slash and burn.
If the problem is serious (e.g., crash and core dump) and nothing else
helps you isolate it, then start removing parts of code from the
current version until it no longer crashes, or you are back to the
previous working version. Cutting the body of functions, leaving only a
return, for example, may be useful.

You have a very good chance of finding the cause of this problem
yourself since you went from a working version to a non-working
version. You''ll be a better programmer by learning how to find this
yourself. Just remember, the program is doing exactly what you told it
to do.

ed


这篇关于重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆