【问题标题】:How to write to UNIX terminal through system calls?如何通过系统调用写入 UNIX 终端?
【发布时间】:2015-09-19 03:03:26
【问题描述】:

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

当我的队友在一天中第一次打开终端并登录时,我想跟他们打个招呼

我发现这篇文章很有用

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

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

【问题讨论】:

  • 为什么要从终端读取?您只需要写入它即可显示消息。 (顺便说一下,你的同事可能只会觉得烦人。)
  • @Ross 我编辑了这个问题,这只是我为找出写入任何终端的方法而创建的一个场景
  • 您是在问如何写入终端或如何找出要写入的终端?
  • @Ross 我想写信给组中选定的用户以分享一些重要信息,我需要找到终端并写信给它
  • 您将需要考虑如何确定上次查看时系统上的人员、现在系统上的人员以及需要问候的人员。根据您的操作方式 (who?),您可能手头有终端信息。不要忘记,您的同事可能会认为他们不喜欢这种处理方式,并且可能会通过让您无法写入他们的终端来阻止您。

标签: c unix


【解决方案1】:

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

#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;
}

尝试在此基础上构建。

【讨论】:

  • 谢谢。我会努力的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
  • 1970-01-01
  • 2018-07-20
  • 2013-09-10
  • 1970-01-01
相关资源
最近更新 更多