Modern software (systemd, utemper) usually use full "ut_line" as "ut_id"
if string is up to four chars or last four chars if "ut_line" is longer.
For reference:
* libutemper (used by many graphical terminal emulator applications
(konsole, xterm, others) to deal with utmp file):
https://github.com/altlinux/libutempter/blob/
4caa8ab94ff8b207228fa723a89214bf5e929321/libutempter/utempter.c#L99-L103
* systemd:
uses full name of the device:
https://github.com/systemd/systemd/blob/
8013beb4a2221680b2c741bac6b3a33fe66406e1/units/getty%40.service.in#L41-L44
or **last** four characters:
https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#UtmpIdentifier=
The code in the commit is optimised for visual C code size.
Signed-off-by: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Reviewed-by: Alejandro Colomar <alx@kernel.org>
#include "alloc/x/xcalloc.h"
#include "alloc/x/xmalloc.h"
#include "sizeof.h"
+#include "string/strchr/strnul.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strcpy/strncpy.h"
if (NULL != ut) {
STRNCPY(utent->ut_id, ut->ut_id);
} else {
- STRNCPY(utent->ut_id, strprefix(line, "tty") ?: line);
+ STRNCPY(utent->ut_id, strnul(line) - MIN(strlen(line), countof(utent->ut_id)));
}
#if defined(HAVE_STRUCT_UTMPX_UT_NAME)
STRNCPY(utent->ut_name, name);