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 6956199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:50:13+00:00 2026-05-27T14:50:13+00:00

In my continuing attempt to understand how pseudo-terminals work, I have written a small

  • 0

In my continuing attempt to understand how pseudo-terminals work, I have written a small program to try to run bash.

The problem is, my line-breaking seems to be off. (The shell prompt only appears AFTER I press enter.)

Furthermore, I still cannot properly use ncurses programs, like vi. Can anyone tell me how to setup the pseudo-terminal for this?

My badly written program can be found here, I encourage you to compile it. The operating system is GNU/Linux, thanks.

EDIT: Compile like this: gcc program.c -lutil -o program

EDIT AGAIN: It looks like the issue with weird spacing was due to using printf(), still doesn’t fix the issue with ncurses programs though.

  • 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. Editorial Team
    Editorial Team
    2026-05-27T14:50:13+00:00Added an answer on May 27, 2026 at 2:50 pm

    There are several issues in your program. Some are relatively easy to fix – others not so much:

    1. forkpty() and its friends come from BSD and are not POSIX compatible. They should be avoided for new programs. From the pty(7) manual page:

      Historically, two pseudoterminal APIs have evolved: BSD and System V. SUSv1 standardized a pseudoterminal API based on the System V API, and this API should be employed in all new programs that use pseudoterminals.

      You should be using posix_openpt() instead. This issue is probably not critical, but you should be aware of it.

    2. You are mixing calls to raw system calls (read(), write()) and file-stream (printf(), fgets()) functions. This is a very good way to confuse yourself. In general you should choose one approach and stick with it. In this case, it’s probably best to use the low-level system calls (read(), write()) to avoid any issues that would arise from the presence of the I/O buffers that the C library functions use.

    3. You are assuming a line-based paradigm for your terminals, by using printf() and fgets(). This is not always true, especially when dealing with interactive programs like vim.

    4. You are assuming a C-style single-byte null-terminated string paradigm. Terminals normally deal with characters and bytes – not strings. And while most character set encodings avoid using a zero byte, not all do so.

    5. As a result of (2), (3) and (4) above, you are not using read() and write() correctly. You should be using their return values to determine how many bytes they processed, not string-based functions like strlen().

    6. This is the issue that, in my opinion, will be most difficult to solve: You are implicitly assuming that:

      • The terminal (or its driver) is stateless: It is not. Period. There are at least two stateful controls that I suspect are the cause of ncurses-based programs not working correctly: The line mode and the local echo control of the terminal. At least these have to match between the parent/master and the slave terminal in order to avoid various strange artifacts.

      • The control-interface of a terminal can be passed-through just by passing the bytes back and forth: It is not always so. Modern virtual terminals allow for a degree of out-of-band control via ioctl() calls, as described for Linux here.

      The simplest way to deal with this issue is probably to set the parent terminal to raw mode and let the slave pseudo-terminal driver deal with the awkward details.

    You may want to have a look at this program which seems to work fine. It comes from the book The Linux Programming Interface and the full source code is here. Disclaimer: I have not read the book, nor am I promoting it – I just found the program using Google.

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

Sidebar

Related Questions

Continuing my problem from yesterday, the Silverlight datagrid I have from this issue is
Continuing on my attempt to create a DateTime class , I am trying to
I am facing a continuing problem distinguishing delegation, composition and aggregation from each other,
I have a program with two methods. The first method takes two arrays as
I have been trying for quite a while to work out how to accomplish
[Edit: The solution to this problem is in the routes.rb file. I have resource
I'm using the ssTab control (that's probably my entire problem) in a VB6 program,
In a C# winforms program, I have an event that fires when someone attempts
Continuing my quest to learn more about Backbone.js I have arrived to a situation
I have two functions that generate DOM elements. They both attempt to create the

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.