gpt4 book ai didi

c - 如何通过系统调用写入UNIX终端?

转载 作者:行者123 更新时间:2023-11-30 20:37:25 26 4
gpt4 key购买 nike

我已经研究“C”有一段时间了。

当我的队友今天第一次打开终端并登录时,我想向他们说“你好”

我发现这篇文章很有用

Not able to read data from other terminal using read() system call

我如何写信给其他终端(因为,在 pts/n - n 中会因人而异)并说出欢迎信息?

最佳答案

这可能是您可以获得的最简单的程序,无需进行大量验证和错误处理等。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main ( int argc, char *argv[] )
{
if ( argc != 3 ) {
fprintf(stderr, "usage : msend \"terminal\" \"message\" ");
exit(1);
}

char filePath[256] = {0,};

if ( strncmp( argv[1], "/dev/pts", strlen("/dev/pts") ) ) {
sprintf( filePath, "/dev/%s" , argv[1] );
} else {
sprintf( filePath, "%s", argv[1] );
}

int fd = open( filePath , O_RDWR , O_APPEND);

write( fd, argv[2], strlen(argv[2]) );

return 0;
}

尝试在此基础上进行构建。

关于c - 如何通过系统调用写入UNIX终端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32663729/

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