legionus / kbd

Mirror of https://git.kernel.org/pub/scm/linux/kernel/git/legion/kbd.git

Home Page:https://kbd-project.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why `conspath`?

nnym opened this issue · comments

commented

static const char *conspath[] = {
"/proc/self/fd/0",
"/dev/tty",
"/dev/tty0",
"/dev/vc/0",
"/dev/systty",
"/dev/console",
NULL
};

I first came across it in vtcol and assumed that it was excessive because one of ttyname and /dev/tty would suffice; but then I found linux-vt-setcolors and then this—the original—and now I'm intrigued: what is the reason for this? I was unable to find any explanation in the commit log.

@nnym This code was written in the days before git. When git came out I imported tarballs.

This code was written in the days when /dev was manually filled with device files. Some of them might be missing.

The getfd() job is to find a suitable terminal device and open the descriptor. The calling program may not have the terminal descriptor as stdin/stdout/stderr. This descriptor is needed to call ioctls.

The ttyname takes file descriptor and return its name. That is, the descriptor should already be opened on the terminal. Here are some examples of implementing this function:

https://github.com/bminor/musl/blob/master/src/unistd/ttyname_r.c
https://github.com/bminor/glibc/blob/master/sysdeps/posix/ttyname_r.c

So I don't understand how ttyname can help me in getfd.

commented

I see. That explains it; thanks. You're right about ttyname; I missed that it takes a file descriptor. I assumed that it would do the right thing since it's what coreutils' tty uses and I hadn't seen it fail until I ran tty with input redirection just a while ago.