1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12 /*
13 * Copyright (c) 2014 Joyent, Inc. All rights reserved.
14 */
15
16 #include <sys/feature_tests.h>
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <sys/types.h>
20 #include <zone.h>
21
22 /*
23 * Common routines for ptools.
24 */
25
26 int
27 proc_snprintf(char *_RESTRICT_KYWD s, size_t n,
28 const char *_RESTRICT_KYWD fmt, ...)
29 {
30 static boolean_t ptools_zroot_valid = B_FALSE;
31 static const char *ptools_zroot = NULL;
32 va_list args;
33 int ret, nret = 0;
34
35 if (ptools_zroot_valid == B_FALSE) {
36 ptools_zroot_valid = B_TRUE;
37 ptools_zroot = zone_get_nroot();
38 }
39
40 if (ptools_zroot != NULL) {
41 nret = snprintf(s, n, "%s", ptools_zroot);
42 if (nret > n)
43 return (nret);
44 }
45 va_start(args, fmt);
46 ret = vsnprintf(s + nret, n - nret, fmt, args);
47 va_end(args);
48
49 return (ret + nret);
50 }