+
+NCURSES_EXPORT(char *)
+_nc_fmt_funcptr(char *target, const char *source, size_t size)
+{
+ size_t n;
+ char *dst = target;
+ bool leading = TRUE;
+
+ union {
+ int value;
+ char bytes[sizeof(int)];
+ } byteorder;
+
+ byteorder.value = 0x1234;
+
+ *dst++ = '0';
+ *dst++ = 'x';
+
+ for (n = 0; n < size; ++n) {
+ unsigned ch = ((byteorder.bytes[0] == 0x34)
+ ? UChar(source[size - n - 1])
+ : UChar(source[n]));
+ if (ch != 0 || (n + 1) >= size)
+ leading = FALSE;
+ if (!leading) {
+ _nc_SPRINTF(dst, _nc_SLIMIT(TR_FUNC_LEN - (size_t) (dst - target))
+ "%02x", ch & 0xff);
+ dst += 2;
+ }
+ }
+ *dst = '\0';
+ return target;
+}
+
+#if USE_REENTRANT
+/*
+ * Check if the given trace-mask is enabled.
+ *
+ * This function may be called from within one of the functions that fills
+ * in parameters for _tracef(), but in that case we do not want to lock the
+ * mutex, since it is already locked.
+ */
+NCURSES_EXPORT(int)
+_nc_use_tracef(unsigned mask)
+{
+ bool result = FALSE;
+
+ _nc_lock_global(tst_tracef);
+ if (!MyNested++) {
+ if ((result = (_nc_tracing & (mask))) != 0
+ && _nc_try_global(tracef) == 0) {
+ /* we will call _nc_locked_tracef(), no nesting so far */
+ } else {
+ /* we will not call _nc_locked_tracef() */
+ MyNested = 0;
+ }
+ } else {
+ /* we may call _nc_locked_tracef(), but with nested_tracef > 0 */
+ result = (_nc_tracing & (mask));
+ }
+ _nc_unlock_global(tst_tracef);
+ return result;
+}
+
+/*
+ * We call this if _nc_use_tracef() returns true, which means we must unlock
+ * the tracef mutex.
+ */
+NCURSES_EXPORT(void)
+_nc_locked_tracef(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ _nc_va_tracef(fmt, ap);
+ va_end(ap);
+
+ if (--(MyNested) == 0) {
+ _nc_unlock_global(tracef);
+ }
+}
+#endif /* USE_REENTRANT */
+