Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 120169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:46:07+00:00 2026-05-11T03:46:07+00:00

I want to write a simple, dumb, X terminal emulator in C on a

  • 0

I want to write a simple, dumb, X terminal emulator in C on a Linux system.

At first, I just thought I would have to popen a shell and display its output. I checked xterm and rxvt code, and it looks a bit more complicated.

First, I have to open a pseudo-terminal with openpty. So I look at the man page and see that openpty fills 2 file descriptors, the master and the slave. Both xterm and rxvt code are messy because of the system-dependent-ness of those specials files.

I understand the termios stuff : it’s just a bunch of information about the escape code of the terminal. What I really don’t get is : what am I suppose to do with the master/slave file descriptor ?

An example program which open a terminal, logs in, executes a ‘ls’ on the shell would be awesome.

(English is not my native language, excuse my eventual mistake)

Edit: Here’s the sample code I came up with :

#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <pty.h> #include <utmp.h> #include <ctype.h>  void safe_print (char* s) {     while(*s) {          if(*s == '\n')             putchar('\n');         else if(iscntrl(*s))             printf('\\e(%d)', *s);         else             putchar(*s);         s++;     } }   int main (int argc, char** argv) {     char buf[BUFSIZ] = {0};     int master;     int ret = forkpty(&master, NULL, NULL, NULL);      if(ret == -1)         puts('no fork'), exit(0);      if(!ret) {          execl('/bin/sh', 'sh', NULL);         exit(0);     }      sleep(1); /* let the shell run */       if(argc >= 2) {         write(master, argv[1], strlen(argv[1]));         write(master, '\n', 1);     } else {         write(master, 'date\n', sizeof 'date\n');     }       while(1) {         switch(ret = read(master, buf, BUFSIZ)) {         case -1:             puts('error!');              exit(1);             break;         case 0:             puts('nothing..'), sleep(1);             break;         default:             buf[ret] = '\0';             safe_print(buf);          }     }      close(master);      return 0; }     
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:46:08+00:00Added an answer on May 11, 2026 at 3:46 am

    With respect to the master/slave part of your question, from the pty(4) man page (which is referenced from the openpty(3) man page on my system):

    A pseudo terminal is a pair of character devices, a master device and a slave device. The slave device provides to a process an interface identical to that described in tty(4). However, whereas all other devices which provide the interface described in tty(4) have a hardware device of some sort behind them, the slave device has, instead, another process manipulating it through the master half of the pseudo terminal. That is, anything written on the master device is given to the slave device as input and anything written on the slave device is presented as input on the master device.

    Man pages are your friends.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a simple chat-client in Ruby for the terminal. The Problem
I want to write a simple compiler for educational purposes in Delphi. I have
I want to write simple counter for links/tags. So I have tags and random
I want to write a simple istream object, that would simply transform another istream
I want to write a simple text to speech program. First, I want to
sometimes, i just feel dumb... i have a simple class: public class myClass {
Well i want to write a simple application that would scan for a specific
Hi all I want to write a simple program (just for learning purposes) that
I want to write a simple SSL HTTP client in Python and have heard
I want to write simple script to copy/backup directory then remove on server startup.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.