From fe4a52ecfa055cebec48c08c40dcdc3659ee9a9b Mon Sep 17 00:00:00 2001 From: javalsai Date: Wed, 7 Aug 2024 00:14:19 +0200 Subject: [PATCH] chore: chvt_str into chvt.c --- include/chvt.h | 7 +++++++ src/chvt.c | 18 ++++++++++++++++++ src/main.c | 16 ---------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/include/chvt.h b/include/chvt.h index 5d291f5..a39af33 100644 --- a/include/chvt.h +++ b/include/chvt.h @@ -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 diff --git a/src/chvt.c b/src/chvt.c index b729a11..8445ca7 100644 --- a/src/chvt.c +++ b/src/chvt.c @@ -1,10 +1,28 @@ +#include +#include #include +#include #include 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; diff --git a/src/main.c b/src/main.c index 20e562c..de97cc7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,3 @@ -#include -#include #include #include #include @@ -12,20 +10,6 @@ #include #include -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]);