简体   繁体   English

无法打印 C 中的 ls 错误之类的确切程序

[英]Not able to print the exact program like ls error in C

I created my own shell and trying to redirect the STDERR to output file.我创建了自己的 shell 并尝试将 STDERR 重定向到 output 文件。

I did:我做了:

dup2(output_fd, 2);

and when I am executing child, I am using当我执行孩子时,我正在使用

error_message = strerror(errno);
 write(STDERR_FILENO, error_message, strlen(error_message));

but the problem is I am getting this error message only:但问题是我只收到此错误消息:

No such file or directory没有这样的文件或目录

whereas I should be getting而我应该得到

ls: cannot access '/no/such/file': No such file or directory

running program: ls /no/such/file > output运行程序:ls /no/such/file > output

ls is printing the message ls: cannot access 'stuff': by itself - in ls.c file_failure(... "cannot access") call and file_failure() definition and error() is a standard function from error.h . ls正在打印消息ls: cannot access 'stuff': by itself - in ls.c file_failure(... "cannot access") 调用file_failure() 定义error() 是来自错误的标准 function There's even no strerror call in ls sources - error() in glibc sources calls it. ls源中甚至没有strerror调用 - glibc 源中的 error()调用它。

So write the message yourself, use fprintf(stderr to format your message, or you may use GNU extension erorr() if you want.所以你自己写消息,使用fprintf(stderr来格式化你的消息,或者如果你愿意,你可以使用 GNU 扩展erorr()

strerror should return just "No such file or directory" and this is the standard message from strerror for ENOENT . strerror应该只返回"No such file or directory" ,这是来自strerrorENOENT标准消息。 There is errno -l to list errors with strings in terminal and here is list of errors in glibc sources .errno -l列出终端中带有字符串的错误, 这里是 glibc 源中的错误列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM