Improve checking for `ps -t <dev>', and give better error messages when

an invalid <dev> is specified.  Aside:  It turns out that the S_ISCHR()
check is true for almost every device that we have (not just tty's).
This commit is contained in:
Garance A Drosehn 2004-06-27 22:56:58 +00:00
parent e3e244bff6
commit f2fbfae6b0
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=131209

View File

@ -715,9 +715,10 @@ addelem_tty(struct listinfo *inf, const char *elem)
{
const char *ttypath;
struct stat sb;
char pathbuf[PATH_MAX];
char pathbuf[PATH_MAX], pathbuf2[PATH_MAX];
ttypath = NULL;
pathbuf2[0] = '\0';
switch (*elem) {
case '/':
ttypath = elem;
@ -732,28 +733,35 @@ addelem_tty(struct listinfo *inf, const char *elem)
strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf));
strlcat(pathbuf, elem, sizeof(pathbuf));
ttypath = pathbuf;
if (strncmp(pathbuf, _PATH_TTY, sizeof(_PATH_TTY)) == 0)
if (strncmp(pathbuf, _PATH_TTY, strlen(_PATH_TTY)) == 0)
break;
if (strcmp(pathbuf, _PATH_CONSOLE) == 0)
break;
if (stat(pathbuf, &sb) == 0 && S_ISCHR(sb.st_mode)) {
/* Check to see if /dev/tty${elem} exists */
strlcpy(pathbuf2, _PATH_TTY, sizeof(pathbuf2));
strlcat(pathbuf2, elem, sizeof(pathbuf2));
if (stat(pathbuf2, &sb) == 0 && S_ISCHR(sb.st_mode)) {
/* No need to repeat stat() && S_ISCHR() checks */
ttypath = NULL;
break;
}
/* /dev/${elem} does not exist, so try /dev/tty${elem} */
strlcpy(pathbuf, _PATH_TTY, sizeof(pathbuf));
strlcat(pathbuf, elem, sizeof(pathbuf));
break;
}
if (ttypath) {
if (stat(ttypath, &sb) == -1) {
warn("%s", ttypath);
if (pathbuf2[0] != '\0')
warn("%s and %s", pathbuf2, ttypath);
else
warn("%s", ttypath);
optfatal = 1;
return (0);
}
if (!S_ISCHR(sb.st_mode)) {
warn("%s: Not a terminal", ttypath);
if (pathbuf2[0] != '\0')
warnx("%s and %s: Not a terminal", pathbuf2,
ttypath);
else
warnx("%s: Not a terminal", ttypath);
optfatal = 1;
return (0);
}