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;
101 if (delimitst && vp->flags.has_tag) {
102 /* Tagged attribute: print delimter and ignore tag */
104 librad_safeprint((char *)(vp->strvalue),
105 vp->length, buf + 1, sizeof(buf) - 2);
107 } else if (delimitst) {
108 /* Non-tagged attribute: print delimter */
110 librad_safeprint((char *)vp->strvalue,
111 vp->length, buf + 1, sizeof(buf) - 2);
114 /* Non-tagged attribute: no delimiter */
115 librad_safeprint((char *)vp->strvalue,
116 vp->length, buf, sizeof(buf));
121 case PW_TYPE_INTEGER:
122 if ( vp->flags.has_tag ) {
123 /* Attribute value has a tag, need to ignore it */
124 if ((v = dict_valbyattr(vp->attribute, (vp->lvalue & 0xffffff)))
128 snprintf(buf, sizeof(buf), "%u", (vp->lvalue & 0xffffff));
132 /* Normal, non-tagged attribute */
133 if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
137 snprintf(buf, sizeof(buf), "%u", vp->lvalue);
145 strftime(buf, sizeof(buf), "\"%b %e %Y\"", gmtime_r(&t, &s_tm));
147 strftime(buf, sizeof(buf), "%b %e %Y", gmtime_r(&t, &s_tm));
153 a = (char *)vp->strvalue;
155 a = ip_hostname((char *)vp->strvalue,
156 sizeof(vp->strvalue),
159 case PW_TYPE_ABINARY:
162 print_abinary(vp, (unsigned char *)buf, sizeof(buf));
170 for (t = 0; t < vp->length; t++) {
171 sprintf(a, "%02x", vp->strvalue[t]);
181 strNcpy(out, a?a:"UNKNOWN-TYPE", outlen);
188 * Print one attribute and value into a string.
190 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
197 if (strlen(vp->name) + 3 > (size_t)outlen) {
201 if( vp->flags.has_tag ) {
203 snprintf(out, outlen, "%s:%d = ", vp->name, vp->flags.tag);
206 vp_prints_value(out + len, outlen - len, vp, 1);
210 snprintf(out, outlen, "%s = ", vp->name);
212 vp_prints_value(out + len, outlen - len, vp, 1);
221 * Print one attribute and value.
223 void vp_print(FILE *fp, VALUE_PAIR *vp)
227 vp_prints(buf, sizeof(buf), vp);
233 * Print a whole list of attributes, indented by a TAB
234 * and with a newline at the end.
236 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
238 for (; vp; vp = vp->next) {