Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ libshadow_la_SOURCES = \
lockpw.c \
loginprompt.c \
mail.c \
memory/memcpy/strncpytail.c \
memory/memcpy/strncpytail.h \
motd.c \
myname.c \
nss.c \
Expand Down
13 changes: 13 additions & 0 deletions lib/memory/memcpy/strncpytail.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "memory/memcpy/strncpytail.h"

#include <stddef.h>


extern inline char *strncpytail(char *restrict dst, const char *restrict src,
size_t dsize);
38 changes: 38 additions & 0 deletions lib/memory/memcpy/strncpytail.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2025-2026, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause

Comment thread
alejandro-colomar marked this conversation as resolved.

#ifndef SHADOW_INCLUDE_LIB_MEMORY_MEMCPY_STRNCPYTAIL_H_
#define SHADOW_INCLUDE_LIB_MEMORY_MEMCPY_STRNCPYTAIL_H_


#include "config.h"

#include <memory.h>
#include <stddef.h>
#include <string.h>
#include <sys/param.h>

#include "attr.h"
#include "sizeof.h"
#include "string/strchr/strnul.h"


// strncpytail_a - nonstring copy tail-of-string array
#define strncpytail_a(dst, src) strncpytail(dst, src, countof(dst))


ATTR_STRING(2)
inline char *strncpytail(char *restrict dst, const char *restrict src,
size_t dsize);


// strncpytail - nonstring copy tail-of-string
inline char *
strncpytail(char *restrict dst, const char *restrict src, size_t dsize)
{
return strncpy(dst, strnul(src) - MIN(strlen(src), dsize), dsize);
}


#endif // include guard
2 changes: 1 addition & 1 deletion lib/string/README
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ strcpy/ - String copying
strncpytail()
Like strncpy(), but when truncating, the tail of the string is
kept instead of the beginning. This is useful for ut_id.
STRNCPYTAIL()
strncpytail_a()
Like strncpytail, but takes an array.

strncat_a() // To be removed
Expand Down
9 changes: 4 additions & 5 deletions lib/utmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "alloc/malloc.h"
#include "attr.h"
#include "io/syslog.h"
#include "memory/memcpy/strncpytail.h"
#include "sizeof.h"
#include "string/strchr/strnul.h"
#include "string/strcmp/streq.h"
Expand Down Expand Up @@ -285,12 +286,10 @@ prepare_utmp(const char *name, const char *line, const char *host,
utent->ut_type = USER_PROCESS;
utent->ut_pid = main_pid;
strncpy_a(utent->ut_line, line);
if ( (NULL != ut)
&& ('\0' != ut->ut_id[0])) {
if (NULL != ut && !strneq_a(ut->ut_id, ""))
strncpy_a(utent->ut_id, ut->ut_id);
} else {
strncpy_a(utent->ut_id, strnul(line) - MIN(strlen(line), countof(utent->ut_id)));
}
else
strncpytail_a(utent->ut_id, line);
#if defined(HAVE_STRUCT_UTMPX_UT_NAME)
strncpy_a(utent->ut_name, name);
#endif
Expand Down
Loading