c238905adae25292756d62dfb6401e66a7c700fd
[musl] / src / unistd / ctermid.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <limits.h>
7 #include "syscall.h"
8
9 char *ctermid(char *s)
10 {
11         static char *s2;
12         int fd;
13         if (!s) {
14                 if (!s2) s2 = malloc(L_ctermid);
15                 s = s2;
16         }
17         fd = open("/dev/tty", O_WRONLY | O_NOCTTY | O_CLOEXEC);
18         if (fd < 0)
19                 return strcpy(s, "");
20         if (ttyname_r(fd, s, L_ctermid))
21                 strcpy(s, "");
22         __syscall(SYS_close, fd);
23         return s;
24 }