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)
97 if (vp->attribute == PW_NAS_PORT_ID)
98 a = (char *)vp->strvalue;
100 if (delimitst && vp->flags.has_tag) {
101 /* Tagged attribute: print delimter and ignore tag */
103 librad_safeprint((char *)(vp->strvalue),
104 vp->length, buf + 1, sizeof(buf) - 2);
106 } else if (delimitst) {
107 /* Non-tagged attribute: print delimter */
109 librad_safeprint((char *)vp->strvalue,
110 vp->length, buf + 1, sizeof(buf) - 2);
113 /* Non-tagged attribute: no delimiter */
114 librad_safeprint((char *)vp->strvalue,
115 vp->length, buf, sizeof(buf));
120 case PW_TYPE_INTEGER:
121 if ( vp->flags.has_tag ) {
122 /* Attribute value has a tag, need to ignore it */
123 if ((v = dict_valbyattr(vp->attribute, (vp->lvalue & 0xffffff)))
127 snprintf(buf, sizeof(buf), "%u", (vp->lvalue & 0xffffff));
131 /* Normal, non-tagged attribute */
132 if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
136 snprintf(buf, sizeof(buf), "%u", vp->lvalue);
144 strftime(buf, sizeof(buf), "\"%b %e %Y\"", gmtime(&t));
146 strftime(buf, sizeof(buf), "%b %e %Y", gmtime(&t));
152 a = (char *)vp->strvalue;
154 a = ip_hostname((char *)vp->strvalue,
155 sizeof(vp->strvalue),
158 case PW_TYPE_ABINARY:
161 print_abinary(vp, (unsigned char *)buf, sizeof(buf));
169 for (t = 0; t < vp->length; t++) {
170 sprintf(a, "%02x", vp->strvalue[t]);
180 strNcpy(out, a?a:"UNKNOWN-TYPE", outlen);
187 * Print one attribute and value into a string.
189 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
196 if (strlen(vp->name) + 3 > (size_t)outlen) {
200 if( vp->flags.has_tag ) {
202 #ifdef MERIT_STYLE_TAGS
203 snprintf(out, outlen, "%s = :%d:", vp->name, vp->flags.tag);
205 snprintf(out, outlen, "%s:%d = ", vp->name, vp->flags.tag);
209 vp_prints_value(out + len, outlen - len, vp, 1);
213 snprintf(out, outlen, "%s = ", vp->name);
215 vp_prints_value(out + len, outlen - len, vp, 1);
224 * Print one attribute and value.
226 void vp_print(FILE *fp, VALUE_PAIR *vp)
230 vp_prints(buf, sizeof(buf), vp);
236 * Print a whole list of attributes, indented by a TAB
237 * and with a newline at the end.
239 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
241 for (; vp; vp = vp->next) {