Last active
September 22, 2025 07:46
-
-
Save quanta-kt/cf694cc4bf2f88c0e0bf2f8b9033045e to your computer and use it in GitHub Desktop.
Call xxd/hexdump from C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef HEXDUMP_H | |
| #define HEXDUMP_H | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <unistd.h> | |
| #include <sys/wait.h> | |
| void hexdump(char* data, size_t size) { | |
| int fd[2]; | |
| pid_t pid; | |
| char buf[128]; | |
| if (pipe(fd) == -1) { | |
| perror("pipe"); | |
| exit(1); | |
| } | |
| pid = fork(); | |
| if (pid == -1) { | |
| perror("fork"); | |
| exit(1); | |
| } | |
| if (pid == 0) { | |
| close(fd[1]); | |
| dup2(fd[0], STDIN_FILENO); | |
| char *args[] = {"xxd", NULL}; | |
| execvp("xxd", args); | |
| perror("exec"); | |
| exit(1); | |
| } else { | |
| close(fd[0]); | |
| if (write(fd[1], data, size) == -1) { | |
| perror("write"); | |
| } | |
| close(fd[1]); | |
| wait(NULL); | |
| } | |
| } | |
| #endif // HEXDUMP_H | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment