summaryrefslogtreecommitdiffstats
path: root/src/terminal.c
blob: 2954669606d118992fef802ac2e9bf5442d3119a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>

#if defined(__OpenBSD__) || defined(__APPLE__)
#include <util.h>
#elif defined(__FreeBSD__)
#include <libutil.h>
#else
#include <pty.h>
#endif

#include "utils.h"

pid_t pty_fork(int *pty, const char *file, char *const argv[], const char *term) {
  pid_t pid = forkpty(pty, NULL, NULL, NULL);

  if (pid < 0) {
    return pid;
  } else if (pid == 0) {
    setenv("TERM", term, true);
    int ret = execvp(file, argv);
    if (ret < 0) {
      perror("execvp failed\n");
      _exit(-errno);
    }
  }

  // set the file descriptor non blocking
  int flags = fcntl(*pty, F_GETFL);
  if (flags != -1) {
    fcntl(*pty, F_SETFD, flags | O_NONBLOCK);
  }
  // set the file descriptor close-on-exec
  fd_set_cloexec(*pty);

  return pid;
}

int pty_resize(int pty, int cols, int rows) {
  struct winsize size;

  size.ws_col = (unsigned short)cols;
  size.ws_row = (unsigned short)rows;
  size.ws_xpixel = 0;
  size.ws_ypixel = 0;

  return ioctl(pty, TIOCSWINSZ, &size);
}