21b44ec8602206c676f94e8394f10a8bf60ff67c
[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
8 char *ctermid(char *s)
9 {
10         static char *s2;
11         int fd;
12         if (!s) {
13                 if (!s2) s2 = malloc(L_ctermid);
14                 s = s2;
15         }
16         fd = open("/dev/tty", O_WRONLY | O_NOCTTY);
17         if (fd < 0)
18                 return strcpy(s, "");
19         if (ttyname_r(fd, s, L_ctermid))
20                 strcpy(s, "");
21         close(fd);
22         return s;
23 }