This name better reflects that it handles arrays, and doesn't shout.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
len = strnlen(grp, sizeof(enc_group)/2);
if (audit_value_needs_encoding(grp, len)) {
- SNPRINTF(buf, "%s %s=%s", op, grp_type,
+ stprintf_a(buf, "%s %s=%s", op, grp_type,
audit_encode_value(enc_group, grp, len));
} else {
- SNPRINTF(buf, "%s %s=\"%s\"", op, grp_type, grp);
+ stprintf_a(buf, "%s %s=\"%s\"", op, grp_type, grp);
}
audit_log_acct_message(audit_fd, type, NULL, buf, name, id,
}
pid = getpid ();
- SNPRINTF(buf, "%lu", (unsigned long) pid);
+ stprintf_a(buf, "%lu", (unsigned long) pid);
len = (ssize_t) strlen (buf) + 1;
if (write_full(fd, buf, len) == -1) {
if (log) {
int commonio_setname (struct commonio_db *db, const char *name)
{
- SNPRINTF(db->filename, "%s", name);
+ stprintf_a(db->filename, "%s", name);
db->setname = true;
return 1;
}
* then call ulckpwdf() (if used) on last unlock.
*/
db->locked = false;
- SNPRINTF(lock, "%s.lock", db->filename);
+ stprintf_a(lock, "%s.lock", db->filename);
unlink (lock);
dec_lock_count ();
return 1;
/*
* Create backup file.
*/
- if (SNPRINTF(buf, "%s-", db->filename) == -1) {
+ if (stprintf_a(buf, "%s-", db->filename) == -1) {
(void) fclose (db->fp);
db->fp = NULL;
goto fail;
sb.st_gid = db->st_gid;
}
- if (SNPRINTF(buf, "%s+", db->filename) == -1)
+ if (stprintf_a(buf, "%s+", db->filename) == -1)
goto fail;
#ifdef WITH_SELINUX
cp = strchr (*argv, '=');
if (NULL == cp) {
- assert(SNPRINTF(variable, "L%d", noname) != -1);
+ assert(stprintf_a(variable, "L%d", noname) != -1);
noname++;
addenv (variable, *argv);
} else {
if (get_pid(pidstr, &target) == -1)
return -ENOENT;
- if (SNPRINTF(proc_dir_name, "/proc/%d/", target) == -1) {
+ if (stprintf_a(proc_dir_name, "/proc/%d/", target) == -1) {
fprintf(stderr, "snprintf of proc path failed for %d: %s\n",
target, strerrno());
return -EINVAL;
*/
if (hushfile[0] != '/') {
- SNPRINTF(buf, "%s/%s", pw->pw_dir, hushfile);
+ stprintf_a(buf, "%s/%s", pw->pw_dir, hushfile);
return (access (buf, F_OK) == 0);
}
fprintf(shadow_logfd, "Using files\n");
goto null_subid;
}
- SNPRINTF(libname, "libsubid_%s.so", p);
+ stprintf_a(libname, "libsubid_%s.so", p);
h = dlopen(libname, RTLD_LAZY);
if (!h) {
fprintf(shadow_logfd, "Error opening %s: %s\n", libname, dlerror());
}
#endif
- SNPRINTF(prompt, cp, user);
+ stprintf_a(prompt, cp, user);
clear = agetpass(prompt);
input = (clear == NULL) ? "" : clear;
* don't want to tell us what it is themselves.
*/
if (arg == NULL) {
- SNPRINTF(arg0, "-%s", Basename(file));
+ stprintf_a(arg0, "-%s", Basename(file));
arg = arg0;
}
* how to execute this stupid shell, so I might as well give
* up in disgust ...
*/
- SNPRINTF(arg0, _("Cannot execute %s"), file);
+ stprintf_a(arg0, _("Cannot execute %s"), file);
errno = err;
perror (arg0);
return err;
snprintf(3) wrapper that reports truncation with -1.
If you need more than one call to form a string,
use seprintf() instead.
- STPRINTF() // Current name: SNPRINTF()
+ stprintf_a()
Like stprintf(), but takes an array.
seprintf() // Current name: stpeprintf())
#include "sizeof.h"
-#define SNPRINTF(s, fmt, ...) \
+// stprintf_a - string truncate print formatted array
+#define stprintf_a(s, fmt, ...) \
( \
snprintf_(s, countof(s), fmt __VA_OPT__(,) __VA_ARGS__) \
)
return NULL;
}
owner_uid = pwd->pw_uid;
- if (SNPRINTF(owner_uid_string, "%lu", (unsigned long) owner_uid) == -1)
+ if (stprintf_a(owner_uid_string, "%lu", (unsigned long) owner_uid) == -1)
return NULL;
commonio_rewind(db);
char path[41];
char buf[512], buf2[512];
- SNPRINTF(path, "/proc/%s/ns/user", sname);
+ stprintf_a(path, "/proc/%s/ns/user", sname);
if (readlinknul_a(path, buf) == -1)
return 0;
char line[1024];
FILE *sfile;
- SNPRINTF(status, "/proc/%s/status", sname);
+ stprintf_a(status, "/proc/%s/status", sname);
sfile = fopen (status, "r");
if (NULL == sfile) {
}
/* Check if the process is in our chroot */
- SNPRINTF(root_path, "/proc/%lu/root", (unsigned long) pid);
+ stprintf_a(root_path, "/proc/%lu/root", (unsigned long) pid);
if (stat (root_path, &sbroot_process) != 0) {
continue;
}
return 1;
}
- SNPRINTF(task_path, "/proc/%lu/task", (unsigned long) pid);
+ stprintf_a(task_path, "/proc/%lu/task", (unsigned long) pid);
task_dir = opendir (task_path);
if (task_dir != NULL) {
while (NULL != (ent = readdir(task_dir))) {
(void) puts (_("Enter the new value, or press ENTER for the default"));
(void) puts ("");
- SNPRINTF(buf, "%ld", mindays);
+ stprintf_a(buf, "%ld", mindays);
change_field (buf, sizeof buf, _("Minimum Password Age"));
if (a2sl(&mindays, buf, NULL, 0, -1, LONG_MAX) == -1)
return 0;
- SNPRINTF(buf, "%ld", maxdays);
+ stprintf_a(buf, "%ld", maxdays);
change_field (buf, sizeof buf, _("Maximum Password Age"));
if (a2sl(&maxdays, buf, NULL, 0, -1, LONG_MAX) == -1)
return 0;
}
}
- SNPRINTF(buf, "%ld", warndays);
+ stprintf_a(buf, "%ld", warndays);
change_field (buf, sizeof buf, _("Password Expiration Warning"));
if (a2sl(&warndays, buf, NULL, 0, -1, LONG_MAX) == -1)
return 0;
- SNPRINTF(buf, "%ld", inactdays);
+ stprintf_a(buf, "%ld", inactdays);
change_field (buf, sizeof buf, _("Password Inactive"));
if (a2sl(&inactdays, buf, NULL, 0, -1, LONG_MAX) == -1)
return 0;
{
char buf[1024];
- SNPRINTF(buf, " in %s", gr_dbname());
+ stprintf_a(buf, " in %s", gr_dbname());
log_gpasswd_failure (buf);
}
{
char buf[1024];
- SNPRINTF(buf, " in %s", sgr_dbname());
+ stprintf_a(buf, " in %s", sgr_dbname());
log_gpasswd_failure (buf);
}
#endif /* SHADOWGRP */
"password of group %s removed by %s%s",
group, myname, suffix));
#ifdef WITH_AUDIT
- SNPRINTF(buf, "password of group %s removed by %s%s",
+ stprintf_a(buf, "password of group %s removed by %s%s",
group, myname, suffix);
audit_logger_with_group (AUDIT_GRP_CHAUTHTOK,
"delete-group-password",
"access to group %s restricted by %s%s",
group, myname, suffix));
#ifdef WITH_AUDIT
- SNPRINTF(buf, "access to group %s restricted by %s%s",
+ stprintf_a(buf, "access to group %s restricted by %s%s",
group, myname, suffix);
audit_logger_with_group (AUDIT_GRP_MGMT,
"restrict-group",
{
char buf[1024];
- SNPRINTF(buf, " in %s", gr_dbname());
+ stprintf_a(buf, " in %s", gr_dbname());
log_gpasswd_success (buf);
}
}
if (!streq(cp, "")) {
- SNPRINTF(fromhost, " on '%.100s' from '%.200s'", tty, cp);
+ stprintf_a(fromhost, " on '%.100s' from '%.200s'", tty, cp);
} else {
- SNPRINTF(fromhost, " on '%.100s'", tty);
+ stprintf_a(fromhost, " on '%.100s'", tty);
}
free(host);
top:
/* only allow ALARM sec. for login */
timeout = getdef_unum ("LOGIN_TIMEOUT", ALARM);
- SNPRINTF(tmsg, _("\nLogin timed out after %u seconds.\n"), timeout);
+ stprintf_a(tmsg, _("\nLogin timed out after %u seconds.\n"), timeout);
(void) signal (SIGALRM, alarm_handler);
if (timeout > 0) {
(void) alarm (timeout);
/* Make the login prompt look like we want it */
if (gethostname (hostn, sizeof (hostn)) == 0) {
- SNPRINTF(loginprompt, _("%s login: "), hostn);
+ stprintf_a(loginprompt, _("%s login: "), hostn);
} else {
STRTCPY(loginprompt, _("login: "));
}
if (streq(grp->gr_passwd, "") ||
!streq(grp->gr_passwd, cpasswd)) {
#ifdef WITH_AUDIT
- SNPRINTF(audit_buf, "authentication new_gid=%lu",
+ stprintf_a(audit_buf, "authentication new_gid=%lu",
(unsigned long) grp->gr_gid);
audit_logger (AUDIT_GRP_AUTH,
audit_buf, NULL, getuid (), SHADOW_AUDIT_FAILURE);
goto failure;
}
#ifdef WITH_AUDIT
- SNPRINTF(audit_buf, "authentication new_gid=%lu",
+ stprintf_a(audit_buf, "authentication new_gid=%lu",
(unsigned long) grp->gr_gid);
audit_logger (AUDIT_GRP_AUTH,
audit_buf, NULL, getuid (), SHADOW_AUDIT_SUCCESS);
if (setgid (gid) != 0) {
perror ("setgid");
#ifdef WITH_AUDIT
- SNPRINTF(audit_buf, "changing new_gid=%lu", (unsigned long) gid);
+ stprintf_a(audit_buf, "changing new_gid=%lu", (unsigned long) gid);
audit_logger (AUDIT_CHGRP_ID,
audit_buf, NULL, getuid (), SHADOW_AUDIT_FAILURE);
#endif
if (setuid (getuid ()) != 0) {
perror ("setuid");
#ifdef WITH_AUDIT
- SNPRINTF(audit_buf, "changing new_gid=%lu", (unsigned long) gid);
+ stprintf_a(audit_buf, "changing new_gid=%lu", (unsigned long) gid);
audit_logger (AUDIT_CHGRP_ID,
audit_buf, NULL, getuid (), SHADOW_AUDIT_FAILURE);
#endif
closelog ();
execl (SHELL, "sh", "-c", command, (char *) NULL);
#ifdef WITH_AUDIT
- SNPRINTF(audit_buf, "changing new_gid=%lu", (unsigned long) gid);
+ stprintf_a(audit_buf, "changing new_gid=%lu", (unsigned long) gid);
audit_logger (AUDIT_CHGRP_ID,
audit_buf, NULL, getuid (), SHADOW_AUDIT_FAILURE);
#endif
}
#ifdef WITH_AUDIT
- SNPRINTF(audit_buf, "changing new_gid=%lu", (unsigned long) gid);
+ stprintf_a(audit_buf, "changing new_gid=%lu", (unsigned long) gid);
audit_logger (AUDIT_CHGRP_ID,
audit_buf, NULL, getuid (), SHADOW_AUDIT_SUCCESS);
#endif
if (freopen (argv[optind], "r", stdin) == NULL) {
char buf[BUFSIZ];
- SNPRINTF(buf, "%s: %s", Prog, argv[1]);
+ stprintf_a(buf, "%s: %s", Prog, argv[1]);
perror (buf);
fail_exit (EXIT_FAILURE, !flags->chroot);
}
stderr);
(void) kill (-pid_child, caught);
- SNPRINTF(kill_msg, _(" ...killed.\n"));
- SNPRINTF(wait_msg, _(" ...waiting for child to terminate.\n"));
+ stprintf_a(kill_msg, _(" ...killed.\n"));
+ stprintf_a(wait_msg, _(" ...waiting for child to terminate.\n"));
/* Any signals other than SIGCHLD and SIGALRM will no longer have any effect,
* so it's time to block all of them. */
/*
* Rename the current default file to its backup name.
*/
- assert(SNPRINTF(buf, "%s-", default_file) != -1);
+ assert(stprintf_a(buf, "%s-", default_file) != -1);
unlink (buf);
if ((link (default_file, buf) != 0) && (ENOENT != errno)) {
fprintf (stderr,
/* FIXME: the following should have variable sizes */
char filebackup[1024], fileedit[1024];
- SNPRINTF(filebackup, "%s-", file);
+ stprintf_a(filebackup, "%s-", file);
#ifdef WITH_TCB
if (tcb_mode) {
if ( (mkdir (TCB_DIR "/" SHADOWTCB_SCRATCHDIR, 0700) != 0)
if (shadowtcb_drop_priv () == SHADOWTCB_FAILURE) {
vipwexit (_("failed to drop privileges"), errno, 1);
}
- SNPRINTF(fileedit,
+ stprintf_a(fileedit,
TCB_DIR "/" SHADOWTCB_SCRATCHDIR "/.vipw.shadow.%s",
user);
} else {
#endif /* WITH_TCB */
- SNPRINTF(fileedit, "%s.edit", file);
+ stprintf_a(fileedit, "%s.edit", file);
#ifdef WITH_TCB
}
#endif /* WITH_TCB */
#include "string/sprintf/snprintf.h"
-static void test_SNPRINTF_trunc(void **state);
-static void test_SNPRINTF_ok(void **state);
+static void test_stprintf_a_trunc(void **state);
+static void test_stprintf_a_ok(void **state);
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_SNPRINTF_trunc),
- cmocka_unit_test(test_SNPRINTF_ok),
+ cmocka_unit_test(test_stprintf_a_trunc),
+ cmocka_unit_test(test_stprintf_a_ok),
};
return cmocka_run_group_tests(tests, NULL, NULL);
static void
-test_SNPRINTF_trunc(void **state)
+test_stprintf_a_trunc(void **state)
{
char buf[countof("foo")];
// Test that we're not returning SIZE_MAX
- assert_true(SNPRINTF(buf, "f%su", "oo") < 0);
+ assert_true(stprintf_a(buf, "f%su", "oo") < 0);
assert_true(strcmp(buf, "foo") == 0);
- assert_true(SNPRINTF(buf, "barbaz") == -1);
+ assert_true(stprintf_a(buf, "barbaz") == -1);
assert_true(strcmp(buf, "bar") == 0);
}
static void
-test_SNPRINTF_ok(void **state)
+test_stprintf_a_ok(void **state)
{
char buf[countof("foo")];
- assert_true(SNPRINTF(buf, "%s", "foo") == strlen("foo"));
+ assert_true(stprintf_a(buf, "%s", "foo") == strlen("foo"));
assert_true(strcmp(buf, "foo") == 0);
- assert_true(SNPRINTF(buf, "%do", 1) == strlen("1o"));
+ assert_true(stprintf_a(buf, "%do", 1) == strlen("1o"));
assert_true(strcmp(buf, "1o") == 0);
- assert_true(SNPRINTF(buf, "f") == strlen("f"));
+ assert_true(stprintf_a(buf, "f") == strlen("f"));
assert_true(strcmp(buf, "f") == 0);
- assert_true(SNPRINTF(buf, "") == strlen(""));
+ assert_true(stprintf_a(buf, "") == strlen(""));
assert_true(strcmp(buf, "") == 0);
}