r/C_Programming • • Aug 27 '26

forkpty error

i'm trying to make a terminal emulator but i can't figure out how to open a pty.

when i try to open a pty bouth forkpty from pty.h and my own implementation:

int init_pty() {
    int ptymaster_fd = posix_openpt(O_RDWR);
    if (ptymaster_fd == -1) {
        perror("failed to open pty master");
        close(ptymaster_fd);
        return 1;
    }

    if (grantpt(ptymaster_fd) == -1) {
        perror("failed to grantpt");
        close(ptymaster_fd);
        return 1;
    }

    if (unlockpt(ptymaster_fd) == -1) {
        perror("failed to unlockpt");
        close(ptymaster_fd);
        return 1;
    }

    char* ptyslave_name = ptsname(ptymaster_fd);
    if (ptyslave_name == NULL) {
        perror("failed to get pty slave name");
        close(ptymaster_fd);
        return 1;
    }

    pid_t pid = fork();
    if (pid != 0) {
        perror("fork");
        close(ptymaster_fd);
        return 1;
    }

    setsid();

    int ptyslave_fd = open(ptyslave_name, O_RDWR);
    if (ptyslave_fd == -1) {
        perror("failed to open pty slave");
        return 1;
    }

    ioctl(ptyslave_fd, TIOCSCTTY, 0);

    dup2(ptyslave_fd, STDIN_FILENO);
    dup2(ptyslave_fd, STDOUT_FILENO);
    dup2(ptyslave_fd, STDERR_FILENO);
    
    return ptymaster_fd;
}

fail when forking with the error directory not empty, ai says that it fails because /dev/pts is not empty but it's obviously trippin balls as usual =), so why does it fail then (?_?)

6 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/Senior-Question693 Sep 04 '26

The example worked flawlessly, thanks. Turns out it did work, it's just that when a pty gets opened all the text that i print with printf gets redirected to the pty's masterfd. Anyway now when i try to read from the masterfd, i get an i/o error, but why though, that looks about right:

void drawfunc(void* data) {
    WS_Shell* shell = (WS_Shell*)data;
    eglMakeCurrent(shell->egl->display, shell->egl->surface, shell->egl->surface, shell->egl->context);

    glClearColor(0.7, 0.6, 0.5, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);

    char buf[1024] = "notextyet\0";
        int n = read(masterfd, buf, sizeof(buf) - 1);
        if (n<0) {
            perror("read");
            exit(1);
        } else if (n == 0) {
            printf("eof\n");
        }
        buf[n] = '\0';

    // just a wrapper around SDL_TTF
    DrawText(font, buf, (SDL_Color){255, 255, 255, 255}, 0, 0);

    WS_ExecFrameCallback(shell); 
}

that function gets executed every frame so maybe it has something to do with that

1

u/Playa_Sin_Nombre Sep 04 '26

The only thing I can think of is: is the child process alive when drawfunc() runs? If the child exits, the slave side is closed and read can't work.

2

u/Senior-Question693 Sep 04 '26

i see, fastfetch doesn't run in a loop so when it exits i get an i/o error, with programs that do run in a loop like bash, it works, bro thank you so much for your help

now i just gotta figure out how to deal with the escape sequences :))