嗨我一直在用C写一个linux shell。我想将输出重定向到文件和终端,我发现tee是要走的路。我去了tee的linux手册页,发现tee可以作为函数调用在C程序中调用。所以我写道
int size =tee(pipeends[1], 1,INT_MAX,SPLICE_F_NONBLOCK);
但这根本行不通。它说
函数'tee'的隐式声明[-Wimplicit-function-declaration] size = tee(pipeends [1],1,INT_MAX,SPLICE_F_NONBLOCK);
我在互联网上搜索了很多,它返回的一切是如何在终端中使用tee命令,我知道是使用tee。但是我想在程序中编写代码而不是让用户明确地输入它。我添加了头文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/stat.h>
#include< fcntl.h>
作为我的linux shell代码的一部分。我不知道tee是否使用了其他一些头文件但是我一无所知。
The manual page提供了必要的步骤:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <fcntl.h>
这将引入一个声明,即:
ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags);
因此,您应该能够根据该信息编写程序来设置T恤。请注意,该调用是特定于Linux的,这不是标准的C(也不是POSIX,Linux经常遵守的Unix标准)功能。
似乎您没有在文件中包含正确的标题:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <fcntl.h>
你没有在你的问题中提到_GNU_SOURCE
。也许你需要那个?
包括这些:
#define _GNU_SOURCE
#include <fcntl.h>
ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags);`
第三行是删除该警告的行。(它对我有用)
此外,如果您要在tee之后使用splice(),请使用以下宏:
ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);
#ifndef SPLICE_F_MOVE
#define SPLICE_F_MOVE 0x01
#endif
#ifndef SPLICE_F_NONBLOCK
#define SPLICE_F_NONBLOCK 0x02
#endif
#ifndef SPLICE_F_MORE
#define SPLICE_F_MORE 0x04
#endif
#ifndef SPLICE_F_GIFT
#define SPLICE_F_GIFT 0x08
#endif
希望它有所帮助。我知道截止日期已经结束:(