2 * print.c Routines to print stuff.
8 static const char rcsid[] = "$Id$";
14 #include <sys/types.h>
20 #include "libradius.h"
23 * Convert a string to something printable.
24 * The output string has to be _at least_ 4x the size
25 * of the input string!
27 void librad_safeprint(char *in, int inlen, char *out, int outlen)
29 unsigned char *str = (unsigned char *)in;
33 if (inlen < 0) inlen = strlen(str);
35 while (inlen-- > 0 && (done + 3) < outlen) {
37 * Hack: never print trailing zero.
38 * Some clients send strings with an off-by-one
39 * length (confused with strings in C).
41 if (inlen == 0 && *str == 0)
60 if (*str < 32 || (*str >= 128)){
61 sprintf(out, "\\%03o", *str);
81 * Print one value into a string.
82 * delimitst will define if strings and dates will be delimited by '"'
84 int vp_prints_value(char * out,int outlen, VALUE_PAIR *vp,int delimitst)
96 if (vp->attribute == PW_NAS_PORT_ID)
101 librad_safeprint(vp->strvalue, vp->length,
102 buf + 1, sizeof(buf) - 2);
105 librad_safeprint(vp->strvalue, vp->length,
112 case PW_TYPE_INTEGER:
113 if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
117 sprintf(buf, "%d", vp->lvalue);
124 strftime(buf, sizeof(buf), "\"%b %e %Y\"",gmtime(&t));
126 strftime(buf, sizeof(buf), "%b %e %Y",gmtime(&t));
134 a = ip_ntoa(NULL, vp->lvalue);
136 case PW_TYPE_ABINARY:
139 print_abinary(vp, buf, sizeof(buf));
145 for (t = 0; t < vp->length; t++) {
146 sprintf(a, "%02x", vp->strvalue[t]);
156 strncpy(out, a, outlen);
164 * Print one attribute and value into a string.
166 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
173 if (strlen(vp->name) + 3 > outlen) {
177 sprintf(out, "%s = ", vp->name);
179 vp_prints_value(out+len,outlen-len,vp,1);
186 * Print one attribute and value.
188 void vp_print(FILE *fp, VALUE_PAIR *vp)
192 vp_prints(buf, sizeof(buf), vp);
198 * Print a whole list of attributes, indented by a TAB
199 * and with a newline at the end.
201 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
203 for (; vp; vp = vp->next) {