gpt4 book ai didi

c - 具有间接输入的简单 shell

转载 作者:太空宇宙 更新时间:2023-11-04 08:34:40 26 4
gpt4 key购买 nike

我正在编写一个简单的代码来实现 unix/linux shell 的间接输入功能。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

extern void error(char* message);
void
cisshRedirectedInput(char* command[], char* inputFile)
{
//Try to implement the RedirectInput from here
pid_t pid;
int status;
int fd;
//For the child process
if ((pid=fork())==0)
{
//Try to input files, failing on an error
fd=open(inputFile,O_RDONLY);//To read input file

if(fd < 0)
{
error("sampleSh: error opening standard input file");
exit(1);
}
//use dup() to copy file
close(1);
if(dup(fd) < 0)
{
error("sampleSh: error duplicating standard input");
perror("dup()");
exit(1);
}

//Close file and exec()
close(fd);
execvp(command[0], command);
//If failure in any case
error("sampleSh: failure to execute command");
exit(1);
}
else
{
/* This is the parent process.
* Wait for the child to terminate.
*/
if(wait(&status) < 0)
{
error("sampleSh: error waiting for child.");
perror("wait");
}

if(status != 0)
error("sampleSh: command exited with nonzero error status.");
}

}

但是,在编译之后(没有报错),但是当我尝试的时候(已经创建了fileList)

sort -r <fileList

shell 一直卡在那里没有给我答案,请问是什么问题?

最佳答案

标准输入文件描述符是 0(或 STDIN_FILENO),而不是 1(或 STDOUT_FILENO)。

要么使用:

int fd = open(inputFile, O_RDONLY);

if (fd < 0) …
close(0);
if (dup(fd) < 0) …
close(fd);

或者:

int fd = open(inputFile, O_RDONLY);

if (fd < 0) …
if (dup2(fd, 0) < 0) …
close(fd);

您的代码在复制到标准 I/O 描述符后执行 close(fd) 是很好的——这几乎总是正确的。检查关键系统调用是否成功也很好。 (如果 close() 失败,您无能为力。)


这种对代码的简单修改(关键更改:使用 close(0); 而不是 close(1);)对我有用。你是否终止了你的参数列表?

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

static inline void error(char *message)
{
fprintf(stderr, "%s\n", message);
}

void
cisshRedirectedInput(char *command[], char *inputFile);

void
cisshRedirectedInput(char *command[], char *inputFile)
{
// Try to implement the RedirectInput from here
pid_t pid;
int status;
int fd;
// For the child process
if ((pid = fork()) == 0)
{
// Try to input files, failing on an error
fd = open(inputFile, O_RDONLY); // To read input file

if (fd < 0)
{
error("sampleSh: error opening standard input file");
exit(1);
}
// use dup() to copy file
close(0);
if (dup(fd) < 0)
{
error("sampleSh: error duplicating standard input");
perror("dup()");
exit(1);
}

// Close file and exec()
close(fd);
execvp(command[0], command);
// If failure in any case
error("sampleSh: failure to execute command");
exit(1);
}
else
{
/* This is the parent process.
* Wait for the child to terminate.
*/
if (wait(&status) < 0)
{
error("sampleSh: error waiting for child.");
perror("wait");
}

if (status != 0)
error("sampleSh: command exited with nonzero error status.");
}
}

int main(void)
{
char *args[] = { "sort", "-r", 0 };
cisshRedirectedInput(args, "fileList");
return 0;
}

输入文件:

bash-assoc-arrays.sh
cissh.c
fileList
kwargs.py
makefile
posixver.h
rangeinc.c
select.c
spc.py
testcsv.py
uncrustify.bug
yield.py

输出:

yield.py
uncrustify.bug
testcsv.py
spc.py
select.c
rangeinc.c
posixver.h
makefile
kwargs.py
fileList
cissh.c
bash-assoc-arrays.sh

关于c - 具有间接输入的简单 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26784077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com