fix bogus lazy allocation in ctermid and missing malloc failure check
[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[L_ctermid];
12         int fd;
13         if (!s) s = s2;
14         *s = 0;
15         fd = open("/dev/tty", O_WRONLY | O_NOCTTY | O_CLOEXEC);
16         if (fd >= 0) {
17                 ttyname_r(fd, s, L_ctermid);
18                 __syscall(SYS_close, fd);
19         }
20         return s;
21 }