2 * print.c Routines to print stuff.
8 static const char rcsid[] = "$Id$";
14 #include <sys/types.h>
19 #include "libradius.h"
22 * Convert a string to something printable.
23 * The output string has to be _at least_ 4x the size
24 * of the input string!
26 void librad_safeprint(char *in, int inlen, char *out, int outlen)
28 unsigned char *str = (unsigned char *)in;
32 if (inlen < 0) inlen = strlen(str);
34 while (inlen-- > 0 && (done + 3) < outlen) {
36 * Hack: never print trailing zero.
37 * Some clients send strings with an off-by-one
38 * length (confused with strings in C).
40 if (inlen == 0 && *str == 0)
59 if (*str < 32 || (*str >= 128)){
60 sprintf(out, "\\%03o", *str);
80 * Print one value into a string.
81 * delimitst will define if strings and dates will be delimited by '"'
83 int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
95 if (vp->attribute == PW_NAS_PORT_ID)
100 librad_safeprint(vp->strvalue, vp->length,
101 buf + 1, sizeof(buf) - 2);
104 librad_safeprint(vp->strvalue, vp->length,
111 case PW_TYPE_INTEGER:
112 if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
116 sprintf(buf, "%d", vp->lvalue);
123 strftime(buf, sizeof(buf), "\"%b %e %Y\"", gmtime(&t));
125 strftime(buf, sizeof(buf), "%b %e %Y", gmtime(&t));
133 a = ip_hostname(vp->strvalue,
134 sizeof(vp->strvalue),
137 case PW_TYPE_ABINARY:
140 print_abinary(vp, buf, sizeof(buf));
148 for (t = 0; t < vp->length; t++) {
149 sprintf(a, "%02x", vp->strvalue[t]);
159 strncpy(out, a, outlen);
167 * Print one attribute and value into a string.
169 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
176 if (strlen(vp->name) + 3 > outlen) {
180 sprintf(out, "%s = ", vp->name);
182 vp_prints_value(out + len, outlen - len, vp, 1);
189 * Print one attribute and value.
191 void vp_print(FILE *fp, VALUE_PAIR *vp)
195 vp_prints(buf, sizeof(buf), vp);
201 * Print a whole list of attributes, indented by a TAB
202 * and with a newline at the end.
204 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
206 for (; vp; vp = vp->next) {