2 * print.c Routines to print stuff.
8 static const char rcsid[] = "$Id$";
14 #include <sys/types.h>
18 #include "libradius.h"
21 * Convert a string to something printable.
22 * The output string has to be _at least_ 4x the size
23 * of the input string!
25 void librad_safeprint(char *in, int inlen, char *out, int outlen)
27 unsigned char *str = (unsigned char *)in;
31 if (inlen < 0) inlen = strlen(in);
33 while (inlen-- > 0 && (done + 3) < outlen) {
35 * Hack: never print trailing zero.
36 * Some clients send strings with an off-by-one
37 * length (confused with strings in C).
39 if (inlen == 0 && *str == 0)
58 if (*str < 32 || (*str >= 128)){
59 snprintf(out, outlen, "\\%03o", *str);
82 * Print one value into a string.
83 * delimitst will define if strings and dates will be delimited by '"'
85 int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
98 if (vp->attribute == PW_NAS_PORT_ID)
99 a = (char *)vp->strvalue;
103 librad_safeprint((char *)vp->strvalue,
104 vp->length, buf + 1, sizeof(buf) - 2);
107 librad_safeprint((char *)vp->strvalue,
108 vp->length, buf, sizeof(buf));
114 case PW_TYPE_INTEGER:
115 if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
119 snprintf(buf, sizeof(buf), "%u", vp->lvalue);
126 strftime(buf, sizeof(buf), "\"%b %e %Y\"", gmtime(&t));
128 strftime(buf, sizeof(buf), "%b %e %Y", gmtime(&t));
134 a = (char *)vp->strvalue;
136 a = ip_hostname((char *)vp->strvalue,
137 sizeof(vp->strvalue),
140 case PW_TYPE_ABINARY:
143 print_abinary(vp, (unsigned char *)buf, sizeof(buf));
151 for (t = 0; t < vp->length; t++) {
152 sprintf(a, "%02x", vp->strvalue[t]);
158 case PW_TYPE_T_STRING:
160 offset = snprintf(buf, sizeof(buf),
161 "\"%u:", vp->strvalue[0]);
162 librad_safeprint((char *)(vp->strvalue + 1),
165 sizeof(buf) - offset);
168 offset = snprintf(buf, sizeof(buf),
169 "%u:", vp->strvalue[0]);
170 librad_safeprint((char *)(vp->strvalue + 1),
173 sizeof(buf) - offset);
180 case PW_TYPE_T_INTEGER:
181 offset = snprintf(buf, sizeof(buf), "%u:", (vp->lvalue >> 24));
182 if ((v = dict_valbyattr(vp->attribute, (vp->lvalue)))
184 snprintf(buf + offset, sizeof(buf) - offset,
187 snprintf(buf + offset, sizeof(buf) - offset,
199 strNcpy(out, a?a:"UNKNOWN-TYPE", outlen);
206 * Print one attribute and value into a string.
208 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
215 if (strlen(vp->name) + 3 > (size_t)outlen) {
219 snprintf(out, outlen, "%s = ", vp->name);
221 vp_prints_value(out + len, outlen - len, vp, 1);
228 * Print one attribute and value.
230 void vp_print(FILE *fp, VALUE_PAIR *vp)
234 vp_prints(buf, sizeof(buf), vp);
240 * Print a whole list of attributes, indented by a TAB
241 * and with a newline at the end.
243 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
245 for (; vp; vp = vp->next) {