c语言int 函数的用法,详解C语言中的ttyname()函数和isatty()函数的用法

C语言ttyname()函数:返回一终端机名称头文件:

#include

定义函数:

char * ttyname(int desc);

函数说明:如果参数desc 所代表的文件描述词为一终端机, 则会将此终端机名称由一字符串指针返回, 否则返回NULL.

返回值:如果成功则返回指向终端机名称的字符串指针, 有错误情况发生时则返回NULL.

范例

#include

#include

#include

#include

main()

{

int fd;

char * file = "/dev/tty";

fd = open (fiel, O_RDONLY);

printf("%s", file);

if(isatty(fd))

{

printf("is a tty. \n");

printf("ttyname = %s \n", ttyname(fd));

}

else

printf(" is not a tty\n");

close(fd);

}

执行:

/dev/tty is a tty ttyname = /dev/tty

C语言isatty()函数:判断文件描述词是否是为终端机头文件:

#include

定义函数:

int isatty(int desc);

函数说明:如果参数 desc 所代表的文件描述词为一终端机则返回1, 否则返回0.

返回值:如果文件为终端机则返回1, 否则返回0.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个支持文的Write命令的C语言实现,包括基于Linux文件系统的管道和重定向功能,以及支持who命令和给用户终端设备写操作的实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <locale.h> #define BUFFER_SIZE 1024 // 支持文的Write命令实现 void write_cmd(char *target, char *msg) { // 打开目标终端设备 int fd = open(target, O_WRONLY); if (fd == -1) { perror("open"); return; } // 将消息写入目标终端设备 if (write(fd, msg, strlen(msg)) == -1) { perror("write"); } // 关闭目标终端设备 close(fd); } // 基于Linux文件系统的管道和重定向功能实现 void pipe_redirect(char *input, char *output, char *msg) { int fd_in = 0, fd_out = 1; // 如果存在输入重定向符号,则打开指定文件作为输入 if (input) { fd_in = open(input, O_RDONLY); if (fd_in == -1) { perror("open"); return; } dup2(fd_in, STDIN_FILENO); close(fd_in); } // 如果存在输出重定向符号,则打开指定文件作为输出 if (output) { fd_out = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd_out == -1) { perror("open"); return; } dup2(fd_out, STDOUT_FILENO); close(fd_out); } // 判断是否存在管道符号 if (strstr(msg, "|")) { char *cmd1 = strtok(msg, "|"); char *cmd2 = strtok(NULL, "|"); int fd[2]; pid_t pid; // 创建管道 if (pipe(fd) == -1) { perror("pipe"); return; } // 创建子进程 pid = fork(); if (pid == -1) { perror("fork"); return; } else if (pid == 0) { // 子进程执行第一个命令 close(fd[0]); dup2(fd[1], STDOUT_FILENO); close(fd[1]); system(cmd1); exit(EXIT_SUCCESS); } else { // 父进程执行第二个命令 close(fd[1]); dup2(fd[0], STDIN_FILENO); close(fd[0]); system(cmd2); exit(EXIT_SUCCESS); } } else { // 没有管道符号,直接执行命令 system(msg); } } // who命令的C语言实现方法,获取登录用户终端设备名 void who_cmd() { char buffer[BUFFER_SIZE]; FILE *fp = popen("who", "r"); if (fp == NULL) { perror("popen"); return; } while (fgets(buffer, BUFFER_SIZE, fp)) { char *user = strtok(buffer, " "); char *tty = strtok(NULL, " "); printf("%s is logged in on %s\n", user, tty); } pclose(fp); } // 给用户终端设备写操作 void write_terminal(char *msg) { char *tty = ttyname(STDOUT_FILENO); write_cmd(tty, msg); } // 主函数 int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); // 设置当前环境为支持文 if (argc < 2) { printf("Usage: %s target [msg]\n", argv[0]); return EXIT_FAILURE; } char *target = argv[1]; char *msg = argv[2] ? argv[2] : "Hello, world!"; char *input = NULL, *output = NULL; // 解析命令行参数,获取输入输出文件名 for (int i = 2; i < argc; i++) { if (strcmp(argv[i], "<") == 0) { input = argv[++i]; } else if (strcmp(argv[i], ">") == 0) { output = argv[++i]; } } // 执行命令,并支持管道和重定向功能 pipe_redirect(input, output, msg); // 支持文的Write命令实现 write_cmd(target, msg); // who命令的C语言实现方法,获取登录用户终端设备名 who_cmd(); // 给用户终端设备写操作 write_terminal("Hello, terminal!"); return EXIT_SUCCESS; } ``` 注意:此代码仅作为示例,可能存在漏洞和不足之处,不适用于生产环境。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值