Send input str direct to tty

Submitted by lepton on Fri, 07/10/2020 - 13:16

//simulate terminal input

#include <sys/ioctl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>

 

void stackchar(char c) {
    if (ioctl(0, TIOCSTI, &c) < 0) {
        perror("ioctl");
        exit(1);
    }
}
int main(int argc, char *argv[]) {
    char *str=argv[1];
    int len=(int)strlen(str);
    for (int i=0; i<len; i++) {
        stackchar(str[i]);
    }
    stackchar('\n');
    return 0;;
}

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.