Skip to content

Commit

Permalink
Rewrite ttyname
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Sep 10, 2023
1 parent a845387 commit 92ebc72
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 41 deletions.
4 changes: 2 additions & 2 deletions elkscmd/screen/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ extern char AnsiVersion[];
extern flowctl;
extern errno;
extern sys_nerr;
extern char *getenv(), *MakeTermcap();
extern char *getlogin(), *ttyname();
extern char *MakeTermcap();
extern char *getlogin();
static AttacherFinit(), Finit(), SigHup(), SigChld();
static char *MakeBellMsg(), *Filename(), **SaveArgs(), *GetTtyName();

Expand Down
1 change: 1 addition & 0 deletions elkscmd/sys_utils/ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void process_name(int fd, unsigned int off, unsigned int seg)
}
}

/* fast cached version of devname() */
char *dev_name(unsigned int minor)
{
struct dirent *d;
Expand Down
3 changes: 1 addition & 2 deletions libc/termios/isatty.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ isatty(int fd)

rv = (ioctl(fd, TCGETS, &term) == 0);

if(rv == 0
&& errno == ENOSYS)
if(rv == 0 && errno == ENOSYS)
rv = (fd < 3);

errno = err;
Expand Down
65 changes: 28 additions & 37 deletions libc/termios/ttyname.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,36 @@
#include <unistd.h>
#include <paths.h>

char * ttyname (int fd)
char *ttyname(int fd)
{
static char dev[] = _PATH_DEV;
struct stat st, dst;
DIR *fp;
struct dirent *d;
static char name[MAXNAMLEN];
int noerr = errno;
DIR *dp;
struct dirent *d;
struct stat src, dst;
static char path[MAXNAMLEN+6] = _PATH_DEVSL; /* /dev/ */
#define NAMEOFF (sizeof(_PATH_DEVSL) - 1)

if (fstat(fd, &st) < 0)
return 0;
if (!isatty(fd))
{
errno = ENOTTY;
return 0;
}
if (fstat(fd, &src) < 0)
return NULL;
if (!isatty(fd)) {
errno = ENOTTY;
return NULL;
}

fp = opendir(dev);
if (fp == 0)
return 0;
strcpy(name, dev);
strcat(name, "/");
dp = opendir(_PATH_DEV);
if (!dp)
return NULL;

while ((d = readdir(fp)) != 0)
{
if (strlen(d->d_name) > sizeof(name) - sizeof(dev) - 1)
continue;
if (d->d_name[0] == '.')
continue;
strcpy(name + sizeof(dev), d->d_name);
if (stat(name, &dst) == 0
&& st.st_dev == dst.st_dev && st.st_ino == dst.st_ino)
{
closedir(fp);
errno = noerr;
return name;
}
}
closedir(fp);
errno = noerr;
return 0;
while ((d = readdir(dp)) != 0) {
if (d->d_name[0] == '.')
continue;
strcpy(&path[NAMEOFF], d->d_name);
if (stat(path, &dst) == 0) {
if (src.st_dev == dst.st_dev && src.st_ino == dst.st_ino) {
closedir(dp);
return path;
}
}
}
closedir(dp);
return NULL;
}

0 comments on commit 92ebc72

Please sign in to comment.