diff options
author | gad <gad@FreeBSD.org> | 2004-06-27 22:56:58 +0000 |
---|---|---|
committer | gad <gad@FreeBSD.org> | 2004-06-27 22:56:58 +0000 |
commit | 1c753c98f948e22649755ff7e1b63676b2dc5622 (patch) | |
tree | 028cc00e0e5dc84949fc855ea8c2f04f512a3c48 /bin/ps/ps.c | |
parent | a56b28be2a895447368f6e5fa2f0dabb07e54d60 (diff) | |
download | FreeBSD-src-1c753c98f948e22649755ff7e1b63676b2dc5622.zip FreeBSD-src-1c753c98f948e22649755ff7e1b63676b2dc5622.tar.gz |
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).
Diffstat (limited to 'bin/ps/ps.c')
-rw-r--r-- | bin/ps/ps.c | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/bin/ps/ps.c b/bin/ps/ps.c index 6bbb00f..bafbfa1 100644 --- a/bin/ps/ps.c +++ b/bin/ps/ps.c @@ -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); } |