git: 70046933082e - main - libutil: Introduce fhexdump(3)

From: Ryan Moeller <rmoeller_at_FreeBSD.org>
Date: Sat, 14 Nov 2026 14:30:43 UTC
The branch main has been updated by rmoeller:

URL: https://cgit.FreeBSD.org/src/commit/?id=70046933082e2e22646fb270320f070b59957b72

commit 70046933082e2e22646fb270320f070b59957b72
Author:     Ryan Moeller <rmoeller@FreeBSD.org>
AuthorDate: 2026-11-12 04:57:53 +0000
Commit:     Ryan Moeller <rmoeller@FreeBSD.org>
CommitDate: 2026-11-14 14:26:26 +0000

    libutil: Introduce fhexdump(3)
    
    Extend hexdump(3) to write to a given stream.
    
    Reviewed by:    kevans
    Approved by:    kevans (mentor)
    Differential Revision:  https://reviews.freebsd.org/D61177
---
 lib/libutil/Makefile   |  1 +
 lib/libutil/Symbol.map |  1 +
 lib/libutil/hexdump.3  | 19 +++++++++++++++----
 lib/libutil/hexdump.c  | 29 +++++++++++++++++------------
 lib/libutil/libutil.h  |  2 ++
 5 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile
index d54d9af38a34..b5582c775958 100644
--- a/lib/libutil/Makefile
+++ b/lib/libutil/Makefile
@@ -42,6 +42,7 @@ MAN+=	fparseln.3
 MAN+=	ftime.3
 MAN+=	getlocalbase.3
 MAN+=	hexdump.3
+MLINKS+=hexdump.3 fhexdump.3
 MAN+=	humanize_number.3
 MAN+=	kinfo_getallproc.3
 MAN+=	kinfo_getfile.3
diff --git a/lib/libutil/Symbol.map b/lib/libutil/Symbol.map
index fbd10769b2a0..c3c326c453ed 100644
--- a/lib/libutil/Symbol.map
+++ b/lib/libutil/Symbol.map
@@ -130,6 +130,7 @@ FBSD_1.8 {
 };
 
 FBSD_1.9 {
+	fhexdump;
 	trimdomain;
 };
 
diff --git a/lib/libutil/hexdump.3 b/lib/libutil/hexdump.3
index e5a18e201bbd..7adfd00f5149 100644
--- a/lib/libutil/hexdump.3
+++ b/lib/libutil/hexdump.3
@@ -26,23 +26,30 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd May 8, 2020
+.Dd November 14, 2026
 .Dt HEXDUMP 3
 .Os
 .Sh NAME
+.Nm fhexdump ,
 .Nm hexdump
-.Nd "dump a block of bytes to standard out in hexadecimal form"
+.Nd "dump a block of bytes to a stream in hexadecimal form"
 .Sh LIBRARY
 .Lb libutil
 .Sh SYNOPSIS
+.In stdio.h
 .In libutil.h
 .Ft void
-.Fn hexdump "void *ptr" "int length" "const char *hdr" "int flags"
+.Fn hexdump "const void *ptr" "int length" "const char *hdr" "int flags"
+.Ft void
+.Fn fhexdump "FILE *stream" "const void *ptr" "int length" "const char *hdr" "int flags"
 .Sh DESCRIPTION
 The
 .Fn hexdump
 function prints an array of bytes to standard out in hexadecimal form,
-along with the ASCII representation of the bytes, if possible.
+along with the ASCII representation of the bytes, if possible;
+.Fn fhexdump
+writes to the given output
+.Fa stream .
 By default, each line of output will start with an offset count, followed by 16
 hexadecimal values, followed by 16 ASCII characters.
 .Bl -tag -width indent
@@ -88,6 +95,10 @@ The
 .Fn hexdump
 function first appeared in
 .Fx 6.4 .
+The
+.Fn fhexdump
+function first appeared in
+.Fx 16.0 .
 .Sh AUTHORS
 This manual page was written by
 .An Scott Long .
diff --git a/lib/libutil/hexdump.c b/lib/libutil/hexdump.c
index 26ec2df66fcb..3afdbb61fae6 100644
--- a/lib/libutil/hexdump.c
+++ b/lib/libutil/hexdump.c
@@ -35,11 +35,11 @@
  */
 
 #include <sys/types.h>
-#include <libutil.h>
 #include <stdio.h>
+#include <libutil.h>
 
 void
-hexdump(const void *ptr, int length, const char *hdr, int flags)
+fhexdump(FILE *fp, const void *ptr, int length, const char *hdr, int flags)
 {
 	int i, j, k;
 	int cols;
@@ -59,35 +59,40 @@ hexdump(const void *ptr, int length, const char *hdr, int flags)
 	cp = ptr;
 	for (i = 0; i < length; i+= cols) {
 		if (hdr != NULL)
-			printf("%s", hdr);
+			fprintf(fp, "%s", hdr);
 
 		if ((flags & HD_OMIT_COUNT) == 0)
-			printf("%04x  ", i);
+			fprintf(fp, "%04x  ", i);
 
 		if ((flags & HD_OMIT_HEX) == 0) {
 			for (j = 0; j < cols; j++) {
 				k = i + j;
 				if (k < length)
-					printf("%c%02x", delim, cp[k]);
+					fprintf(fp, "%c%02x", delim, cp[k]);
 				else
-					printf("   ");
+					fprintf(fp, "   ");
 			}
 		}
 
 		if ((flags & HD_OMIT_CHARS) == 0) {
-			printf("  |");
+			fprintf(fp, "  |");
 			for (j = 0; j < cols; j++) {
 				k = i + j;
 				if (k >= length)
-					printf(" ");
+					fprintf(fp, " ");
 				else if (cp[k] >= ' ' && cp[k] <= '~')
-					printf("%c", cp[k]);
+					fprintf(fp, "%c", cp[k]);
 				else
-					printf(".");
+					fprintf(fp, ".");
 			}
-			printf("|");
+			fprintf(fp, "|");
 		}
-		printf("\n");
+		fprintf(fp, "\n");
 	}
 }
 
+void
+hexdump(const void *ptr, int length, const char *hdr, int flags)
+{
+	fhexdump(stdout, ptr, length, hdr, flags);
+}
diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h
index 17e4f1fba247..54c298d1ae21 100644
--- a/lib/libutil/libutil.h
+++ b/lib/libutil/libutil.h
@@ -152,6 +152,8 @@ int	uu_lock_txfr(const char *_ttyname, pid_t _pid);
  * files upon which they depend have been included.
  */
 #ifdef _STDIO_H_
+void	fhexdump(FILE *_fp, const void *_ptr, int _length, const char *_hdr,
+	    int _flags);
 char	*fparseln(FILE *_fp, size_t *_len, size_t *_lineno,
 	    const char _delim[3], int _flags);
 #endif