This name better reflects that it handles arrays, and doesn't shout.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
if (*cons != '/') {
char *pbuf;
- STRTCPY(buf, cons);
+ strtcpy_a(buf, cons);
pbuf = buf;
while (NULL != (s = strsep(&pbuf, ":"))) {
if (streq(s, tty)) {
fl->fail_cnt++;
}
- STRTCPY(fl->fail_line, tty);
+ strtcpy_a(fl->fail_line, tty);
fl->fail_time = time(NULL);
/*
ll_time = newlog.ll_time;
ll_time = time(NULL);
newlog.ll_time = ll_time;
- STRTCPY(newlog.ll_line, line);
+ strtcpy_a(newlog.ll_line, line);
#if HAVE_LL_HOST
strncpy_a(newlog.ll_host, host);
#endif
This is what the Linux kernel calls strscpy().
If you need more than one call to form a string,
use stpecpy() instead.
- STRTCPY()
+ strtcpy_a()
Like strtcpy(), but takes an array.
strtcat() // Unimplemented
#include "sizeof.h"
-/*
- * SYNOPSIS
- * [[gnu::null_terminated_string_arg(2)]]
- * int STRTCPY(char dst[restrict], const char *restrict src);
- *
- * ARGUMENTS
- * dst Destination buffer where to copy a string.
- * src Source string to be copied into dst.
- *
- * DESCRIPTION
- * This macro copies the string pointed to by src, into a string
- * at the buffer pointed to by dst. If the destination buffer,
- * isn't large enough to hold the copy, the resulting string is
- * truncated. The size of the buffer is calculated internally via
- * countof().
- *
- * RETURN VALUE
- * -1 If this call truncated the resulting string.
- *
- * strlen(dst)
- * On success.
- *
- * ERRORS
- * This function doesn't set errno.
- */
-
-
-#define STRTCPY(dst, src) strtcpy(dst, src, countof(dst))
+// strtcpy_a - string truncate copy array
+#define strtcpy_a(dst, src) strtcpy(dst, src, countof(dst))
ATTR_STRING(2)
size_t dsize);
+// strtcpy - string truncate copy
inline ssize_t
strtcpy(char *restrict dst, const char *restrict src, size_t dsize)
{
fail_exit (E_NOPERM, process_selinux);
}
- STRTCPY(user_name, pw->pw_name);
+ strtcpy_a(user_name, pw->pw_name);
#ifdef WITH_TCB
if (shadowtcb_set_user (pw->pw_name) == SHADOWTCB_FAILURE) {
fail_exit (E_NOPERM, process_selinux);
exit (E_NOPERM);
}
fflg = true;
- STRTCPY(fullnm, optarg);
+ strtcpy_a(fullnm, optarg);
break;
case 'h':
if (!may_change_field ('h')) {
exit (E_NOPERM);
}
hflg = true;
- STRTCPY(homeph, optarg);
+ strtcpy_a(homeph, optarg);
break;
case 'o':
if (!amroot) {
_("%s: fields too long\n"), Prog);
exit (E_NOPERM);
}
- STRTCPY(slop, optarg);
+ strtcpy_a(slop, optarg);
break;
case 'r':
if (!may_change_field ('r')) {
exit (E_NOPERM);
}
rflg = true;
- STRTCPY(roomno, optarg);
+ strtcpy_a(roomno, optarg);
break;
case 'R': /* no-op, handled in process_root_flag () */
flags->chroot = true;
exit (E_NOPERM);
}
wflg = true;
- STRTCPY(workph, optarg);
+ strtcpy_a(workph, optarg);
break;
default:
usage (E_USAGE);
char old_gecos[BUFSIZ];
const char *f;
- STRTCPY(old_gecos, gecos);
+ strtcpy_a(old_gecos, gecos);
p = old_gecos;
f = strsep(&p, ",");
break;
case 's':
sflg = true;
- STRTCPY(loginsh, optarg);
+ strtcpy_a(loginsh, optarg);
break;
default:
usage (E_USAGE);
* file, or use the value from the command line.
*/
if (!sflg) {
- STRTCPY(loginsh, pw->pw_shell);
+ strtcpy_a(loginsh, pw->pw_shell);
}
/*
exit (1);
}
- STRTCPY(pass, cp);
+ strtcpy_a(pass, cp);
erase_pass (cp);
cp = agetpass (_("Re-enter new password: "));
if (NULL == cp) {
if (NULL == tmptty) {
tmptty = "UNKNOWN";
}
- STRTCPY(tty, tmptty);
+ strtcpy_a(tty, tmptty);
#ifndef USE_PAM
is_console = console (tty);
if (gethostname (hostn, sizeof (hostn)) == 0) {
stprintf_a(loginprompt, _("%s login: "), hostn);
} else {
- STRTCPY(loginprompt, _("login: "));
+ strtcpy_a(loginprompt, _("login: "));
}
retcode = pam_set_item (pamh, PAM_USER_PROMPT, loginprompt);
pw->pw_name);
return -1;
}
- STRTCPY(orig, clear);
+ strtcpy_a(orig, clear);
erase_pass (clear);
strzero (cipher);
} else {
if (NULL == cp) {
return -1;
}
- ret = STRTCPY (pass, cp);
+ ret = strtcpy_a(pass, cp);
erase_pass (cp);
if (ret == -1) {
(void) fputs (_("Password is too long.\n"), stderr);
if (warned && !streq(pass, cp)) {
warned = false;
}
- ret = STRTCPY (pass, cp);
+ ret = strtcpy_a(pass, cp);
erase_pass (cp);
if (ret == -1) {
(void) fputs (_("Password is too long.\n"), stderr);
return -1;
}
- STRTCPY(crypt_passwd, cp);
+ strtcpy_a(crypt_passwd, cp);
return 0;
}
* If there are no other flags, just change the password.
*/
if (!anyflag) {
- STRTCPY(crypt_passwd, cp);
+ strtcpy_a(crypt_passwd, cp);
/*
* See if the user is permitted to change the password.
SYSLOG ((LOG_INFO,
"Change user from '%s' to '%s' as requested by PAM",
name, tmp_name));
- if (STRTCPY(name, tmp_name) == -1) {
+ if (strtcpy_a(name, tmp_name) == -1) {
fprintf (stderr, _("Overlong user name '%s'\n"),
tmp_name);
SYSLOG ((LOG_NOTICE, "Overlong user name '%s'",
(unsigned long) caller_uid));
su_failure (caller_tty, true); /* unknown target UID*/
}
- STRTCPY(caller_name, pw->pw_name);
+ strtcpy_a(caller_name, pw->pw_name);
#ifndef USE_PAM
#ifdef SU_ACCESS
}
if (optind < argc) {
- STRTCPY(name, argv[optind++]); /* use this login id */
+ strtcpy_a(name, argv[optind++]); /* use this login id */
}
if (streq(name, "")) { /* use default user */
struct passwd *root_pw = getpwnam ("root");
#include "string/strcpy/strtcpy.h"
-static void test_STRTCPY_trunc(void **state);
-static void test_STRTCPY_ok(void **state);
+static void test_strtcpy_a_trunc(void **state);
+static void test_strtcpy_a_ok(void **state);
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_STRTCPY_trunc),
- cmocka_unit_test(test_STRTCPY_ok),
+ cmocka_unit_test(test_strtcpy_a_trunc),
+ cmocka_unit_test(test_strtcpy_a_ok),
};
return cmocka_run_group_tests(tests, NULL, NULL);
static void
-test_STRTCPY_trunc(void **state)
+test_strtcpy_a_trunc(void **state)
{
char buf[countof("foo")];
// Test that we're not returning SIZE_MAX
- assert_true(STRTCPY(buf, "fooo") < 0);
+ assert_true(strtcpy_a(buf, "fooo") < 0);
assert_string_equal(buf, "foo");
- assert_int_equal(STRTCPY(buf, "barbaz"), -1);
+ assert_int_equal(strtcpy_a(buf, "barbaz"), -1);
assert_string_equal(buf, "bar");
}
static void
-test_STRTCPY_ok(void **state)
+test_strtcpy_a_ok(void **state)
{
char buf[countof("foo")];
- assert_int_equal(STRTCPY(buf, "foo"), strlen("foo"));
+ assert_int_equal(strtcpy_a(buf, "foo"), strlen("foo"));
assert_string_equal(buf, "foo");
- assert_int_equal(STRTCPY(buf, "fo"), strlen("fo"));
+ assert_int_equal(strtcpy_a(buf, "fo"), strlen("fo"));
assert_string_equal(buf, "fo");
- assert_int_equal(STRTCPY(buf, "f"), strlen("f"));
+ assert_int_equal(strtcpy_a(buf, "f"), strlen("f"));
assert_string_equal(buf, "f");
- assert_int_equal(STRTCPY(buf, ""), strlen(""));
+ assert_int_equal(strtcpy_a(buf, ""), strlen(""));
assert_string_equal(buf, "");
}