-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshowconsole.c
112 lines (95 loc) · 2.39 KB
/
showconsole.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* showconsole.c
*
* Copyright 2000,2015 Werner Fink, 2000,2015 SuSE GmbH Nuernberg, Germany.
*
* This source is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <sys/types.h> /* Defines the macros major and minor */
#include <sys/sysmacros.h> /* Defines the macros major and minor */
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include "libconsole.h"
/*
* Internal logger
*/
static char *myname = NULL;
/*
* Cry and exit.
*/
void error (const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(EXIT_FAILURE, fmt, ap);
va_end(ap);
}
/*
* Now do the job
*/
int main(int argc, char *argv[])
{
const char* opt = argv[1];
struct console *c;
char numeric = 0;
myname = program_invocation_short_name;
if (!strcmp(myname, "setconsole")) {
char *curtty = ttyname(0);
int fd, isconsole = 0;
if (curtty && (isconsole = (strcmp("/dev/console", curtty) == 0)))
fd = 0;
else
fd = open("/dev/console", O_RDWR|O_NOCTTY);
if (fd < 0)
error("can not open console: %m");
(void)ioctl(fd, TIOCCONS, NULL); /* Undo any current map if any */
close(fd);
if (argc < 2)
goto out;
if (argc > 2)
error("Usage: %s [-r | <tty> ]", myname);
if (opt && *opt++ == '-' && *opt++ == 'r' && *opt == '\0')
goto out;
if ((fd = open(argv[1], O_WRONLY|O_NOCTTY)) < 0)
error("can not open %s: %m", argv[1]);
if (!isatty(fd))
error("%s is not a tty", argv[1]);
if (ioctl(fd, TIOCCONS, NULL) < 0)
error("can not set console device: %m");
goto out;
}
if (argc == 2) {
if (opt && *opt++ == '-' && *opt++ == 'n' && *opt == '\0')
numeric++;
else
error("Usage: %s [-n]", myname);
} else if (argc > 2)
error("Usage: %s [-n]", myname);
getconsoles(&cons, 0);
list_for_each_entry(c, &cons->node, node) {
if (c->flags & CON_CONSDEV) {
if (numeric)
printf("%u %u\n", major(c->dev), minor(c->dev));
else
printf("%s\n", c->tty);
numeric = 2;
break;
}
}
if (numeric != 2)
error("real console unknown");
out:
return 0;
}