简体   繁体   English

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

[英]How to write to UNIX terminal through system calls?

I have been working on 'C' for quite sometime.我已经在“C”上工作了一段时间。

I would like to say 'Hello ' to my team mates when they open the terminal and logon for the first time of the day当我的队友打开终端并在一天中第一次登录时,我想对他们说“你好”

I found this post a useful one我发现这篇文章很有用

Not able to read data from other terminal using read() system call 无法使用 read() 系统调用从其他终端读取数据

How can I write to others terminal (because, in pts/n - n will differ from person to person) and say the welcome message ?我如何写信给其他终端(因为,在 pts/n-n 中会因人而异)并说出欢迎信息?

This possibly is the simplest of programs you can get with out much validations and error handling etc.这可能是最简单的程序,无需大量验证和错误处理等。

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

Try building on top of this.尝试在此基础上构建。

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

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