+/**
+ * Write a value in hexadecimal form, with "0x" followed by digits, if
+ * it fits within the given limit. Returns characters written, which
+ * is either 0 or all.
+ */
+static int ItemKeyFun_puthexlong(char *p,unsigned long x,int limit) {
+ int n = 3;
+ if ( x ) {
+ unsigned long y = x;
+ char *q = p;
+ for ( ; y; y >>= 4, q++ );
+ n = q - p + 2;
+ if ( n >= limit ) {
+ return 0;
+ }
+ *(p++) = '0';
+ *(p++) = 'x';
+ for ( q++; x; x >>= 4, q-- ) {
+ *q = x & 0xf;
+ }
+ p += n - 2;
+ } else {
+ if ( 3 >= limit ) {
+ return 0;
+ }
+ *(p++) = '0';
+ *(p++) = 'x';
+ *(p++) = '0';
+ }
+ *(p++) = 0;
+ return n;
+}
+