chore: chvt_str into chvt.c

This commit is contained in:
javalsai 2024-08-07 00:14:19 +02:00
parent a393cda7e1
commit fe4a52ecfa
Signed by: javalsai
SSH Key Fingerprint: SHA256:3G83yKhBUWVABVX/vPWH88xnK4+ptMtHkZGCRXD4Mk8
3 changed files with 25 additions and 16 deletions

View File

@ -14,5 +14,12 @@
* @return int non-negative value on success
*/
int chvt(int n);
/**
* @brief change foreground virtual terminal to `str`
*
* @param str virtual terminal number (string)
* @return int non-negative value on success
*/
int chvt_str(char* str);
#endif

View File

@ -1,10 +1,28 @@
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <chvt.h>
static char *vterms[] = {"/dev/tty", "/dev/tty0", "/dev/vc/0", "/dev/systty",
"/dev/console"};
int chvt_str(char *str) {
char *err;
errno = 0;
long i = strtol(str, &err, 10);
if (errno) {
perror("strol");
return -1;
}
// I'm not gonna elaborate on this....
if (i > INT_MAX || i < INT_MIN || *err)
return -1;
return chvt((int)i);
}
int chvt(int n) {
fprintf(stderr, "activating vt %d\n", n);
char c = 0;

View File

@ -1,5 +1,3 @@
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <stdbool.h>
#include <sys/ioctl.h>
@ -12,20 +10,6 @@
#include <ui.h>
#include <users.h>
int chvt_str(char *str) {
char *err;
long i = strtol(str, &err, 10);
if (errno) {
perror("strol");
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[]) {
if (argc == 2)
chvt_str(argv[1]);