diff options
-rw-r--r-- | src/consoletype.1 | 35 | ||||
-rw-r--r-- | src/consoletype.c | 28 |
2 files changed, 42 insertions, 21 deletions
diff --git a/src/consoletype.1 b/src/consoletype.1 index 2720f871..3f4ec14b 100644 --- a/src/consoletype.1 +++ b/src/consoletype.1 @@ -1,28 +1,39 @@ .TH CONSOLETYPE 1 "Red Hat, Inc" "RH" \" -*- nroff -*- .SH NAME -.B consoletype +\fBconsoletype \- print type of the console connected to standard input .SH SYNOPSIS -.B consoletype +\fBconsoletype [\fIfg\fR] .SH DESCRIPTION -.B consoletype -prints the type of console connected to standard input. It prints -.I vt +\fBconsoletype +prints the type of console connected to standard input, and checks +whether the console connected to standard input is the current +foreground virtual console. With no arguments, it prints +\fIvt\fR if console is a virtual terminal (/dev/tty* or /dev/console device if not on a serial console), -.I serial +\fIserial\fR if standard input is a serial console (/dev/console or /dev/ttyS*) and -.I pty +\fIpty\fR if standard input is a pseudo terminal. .SH RETURN VALUE -.B consoletype -returns +\fBconsoletype +when passed no arguments returns .TP -.I 0 +\fI0 if on virtual terminal .TP -.I 1 +\fI1 if on serial console .TP -.I 2 +\fI2 if on a pseudo terminal. +.TP +When passed the \fIfg\fR argument, \fBconsoletype\fR returns +.TP +\fI0 +if the console connected to standard input is the current virtual +terminal +.TP +\fI1 +otherwise.
\ No newline at end of file diff --git a/src/consoletype.c b/src/consoletype.c index 6bfda4bd..1ce98901 100644 --- a/src/consoletype.c +++ b/src/consoletype.c @@ -7,21 +7,31 @@ int main(int argc, char **argv) { unsigned char twelve = 12; - int maj; + char *type; + int maj, min, ret = 0, fg = -1; struct stat sb; - + fstat(0, &sb); maj = major(sb.st_rdev); + min = minor(sb.st_rdev); if (maj != 3 && (maj < 136 || maj > 143)) { - if (ioctl (0, TIOCLINUX, &twelve) < 0) { - printf ("serial\n"); - return 1; + if ((fg = ioctl (0, TIOCLINUX, &twelve)) < 0) { + type = "serial"; + ret = 1; } else { - printf ("vt\n"); - return 0; + type = "vt"; + ret = 0; } } else { - printf ("pty\n"); - return 2; + type = "pty"; + ret = 2; + } + if (argc > 1 && !strcmp(argv[1],"fg")) { + if (fg < 0 || fg != (min-1)) + return 1; + return 0; + } else { + printf("%s\n",type); + return ret; } } |