/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2022 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/syscall_support-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/errfuns.h"

extern const unsigned FIODGNAME;
extern const unsigned TIOCPTSNAME;
extern const unsigned TIOCPTYGNAME;

struct fiodgname_arg {
  int len;
  void *buf;
};

struct ptmget {
  int cfd;
  int sfd;
  char cn[1024];
  char sn[1024];
};

int _ptsname(int fd, char *buf, size_t size) {
  int pty;
  size_t n;
  struct ptmget t;

  if (_isptmaster(fd)) {
    return -1;
  }

  t.sn[0] = '/';
  t.sn[1] = 'd';
  t.sn[2] = 'e';
  t.sn[3] = 'v';
  t.sn[4] = '/';
  t.sn[5] = 0;

  if (IsLinux()) {
    if (sys_ioctl(fd, TIOCGPTN, &pty)) return -1;
    t.sn[5] = 'p';
    t.sn[6] = 't';
    t.sn[7] = 's';
    t.sn[8] = '/';
    FormatInt32(t.sn + 9, pty);
  } else if (IsXnu()) {
    if (sys_ioctl(fd, TIOCPTYGNAME, t.sn)) {
      return -1;
    }
  } else if (IsFreebsd()) {
    struct fiodgname_arg fgn = {sizeof(t.sn) - 5, t.sn + 5};
    if (sys_ioctl(fd, FIODGNAME, &fgn) == -1) {
      if (errno == EINVAL) {
        errno = ERANGE;
      }
      return -1;
    }
  } else if (IsNetbsd()) {
    if (sys_ioctl(fd, TIOCPTSNAME, &t)) {
      return -1;
    }
  } else {
    return enosys();
  }

  if ((n = strlen(t.sn)) < size) {
    memcpy(buf, t.sn, n + 1);
    return 0;
  } else {
    return erange();
  }
}
