small changes

This commit is contained in:
javalsai 2024-08-07 00:07:44 +02:00
parent e745b27a68
commit 4de2720bed
Signed by: javalsai
SSH Key Fingerprint: SHA256:3G83yKhBUWVABVX/vPWH88xnK4+ptMtHkZGCRXD4Mk8
3 changed files with 22 additions and 14 deletions

View File

@ -2,14 +2,11 @@
#define _CHVTH_ #define _CHVTH_
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <linux/kd.h>
#include <linux/vt.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#define KDGKBTYPE 0x4b33
#define VT_ACTIVATE 0x5606
#define VT_WAITACTIVE 0x5607
/** /**
* @brief change foreground virtual terminal to `n` * @brief change foreground virtual terminal to `n`
* *

View File

@ -1,15 +1,18 @@
#include "chvt.h" #include <stdio.h>
#include <chvt.h>
static char *vterms[] = {"/dev/tty", "/dev/tty0", "/dev/vc/0", "/dev/systty", static char *vterms[] = {"/dev/tty", "/dev/tty0", "/dev/vc/0", "/dev/systty",
"/dev/console"}; "/dev/console"};
int chvt(int n) { int chvt(int n) {
fprintf(stderr, "activating vt %d\n", n);
char c = 0; char c = 0;
for (int i = 0; i < sizeof(vterms) / sizeof(vterms[0]); i++) { for (int i = 0; i < sizeof(vterms) / sizeof(vterms[0]); i++) {
int fd = open(vterms[i], O_RDWR); int fd = open(vterms[i], O_RDWR);
if (fd >= 0 && isatty(fd) && ioctl(fd, KDGKBTYPE, &c) == 0 && c < 3) { if (fd >= 0 && isatty(fd) && ioctl(fd, KDGKBTYPE, &c) == 0 && c < 3) {
if (ioctl(fd, VT_ACTIVATE, n) < 0 || ioctl(fd, VT_WAITACTIVE, n) < 0) { if (ioctl(fd, VT_ACTIVATE, n) < 0 || ioctl(fd, VT_WAITACTIVE, n) < 0) {
printf("Couldn't active vt %d\n", n); fprintf(stderr, "Couldn't activate vt %d\n", n);
return -1; return -1;
} }
return 0; return 0;
@ -17,6 +20,6 @@ int chvt(int n) {
close(fd); close(fd);
} }
printf("Couldn't get a file descriptor referring to the console.\n"); fprintf(stderr, "Couldn't get a file descriptor referring to the console.\n");
return -1; return -1;
} }

View File

@ -1,3 +1,5 @@
#include <errno.h>
#include <limits.h>
#include <pwd.h> #include <pwd.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -10,17 +12,23 @@
#include <ui.h> #include <ui.h>
#include <users.h> #include <users.h>
int chvt_char(char c) { int chvt_str(char *str) {
int i = c - '0'; char *err;
if (i >= 0 && i <= 9) { long i = strtol(str, &err, 10);
return chvt(i); if (errno) {
perror("strol");
return -1;
} }
return -1; // I'm not gonna elaborate on this....
if (i > INT_MAX || i < INT_MIN || *err)
return -1;
return chvt((int)i);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc == 2) if (argc == 2)
chvt_char(argv[1][0]); chvt_str(argv[1]);
struct config *config = parse_config("/etc/lidm.ini"); struct config *config = parse_config("/etc/lidm.ini");
if (config == NULL) { if (config == NULL) {