X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Funistd%2Fctermid.c;h=ffa9b758cb60be3bc2e55c3224d6e3dc3d2e7082;hb=1d92cddb1e1ed4b6cc0e55461727561e7a2522e0;hp=21b44ec8602206c676f94e8394f10a8bf60ff67c;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01;p=musl diff --git a/src/unistd/ctermid.c b/src/unistd/ctermid.c index 21b44ec8..ffa9b758 100644 --- a/src/unistd/ctermid.c +++ b/src/unistd/ctermid.c @@ -4,20 +4,18 @@ #include #include #include +#include "syscall.h" char *ctermid(char *s) { - static char *s2; + static char s2[L_ctermid]; int fd; - if (!s) { - if (!s2) s2 = malloc(L_ctermid); - s = s2; + if (!s) s = s2; + *s = 0; + fd = open("/dev/tty", O_WRONLY | O_NOCTTY | O_CLOEXEC); + if (fd >= 0) { + ttyname_r(fd, s, L_ctermid); + __syscall(SYS_close, fd); } - fd = open("/dev/tty", O_WRONLY | O_NOCTTY); - if (fd < 0) - return strcpy(s, ""); - if (ttyname_r(fd, s, L_ctermid)) - strcpy(s, ""); - close(fd); return s; }