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 @@ -223,6 +223,8 @@ libshadow_la_SOURCES = \
string/strcpy/strncat.h \
string/strcpy/strncpy.c \
string/strcpy/strncpy.h \
string/strcpy/strncpytail.c \
string/strcpy/strncpytail.h \
string/strcpy/strtcpy.c \
string/strcpy/strtcpy.h \
string/strdup/memdup.c \
Expand Down
2 changes: 1 addition & 1 deletion lib/string/README
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,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
13 changes: 13 additions & 0 deletions lib/string/strcpy/strncpytail.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include "config.h"

#include "string/strcpy/strncpytail.h"

#include <stddef.h>


extern inline char *strncpytail(char *restrict dst, const char *restrict src,
size_t dsize);
37 changes: 37 additions & 0 deletions lib/string/strcpy/strncpytail.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2025, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_STRING_STRCPY_STRNCPYTAIL_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCPY_STRNCPYTAIL_H_


#include "config.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
9 changes: 4 additions & 5 deletions lib/utmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "string/strcmp/strneq.h"
#include "string/strcmp/strprefix.h"
#include "string/strcpy/strncpy.h"
#include "string/strcpy/strncpytail.h"
#include "string/strdup/memdup.h"
#include "string/strdup/strdup.h"
#include "string/strdup/strndup.h"
Expand Down Expand Up @@ -286,12 +287,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