From d7c417398b73fc2c86f0bb17432f6ad826544813 Mon Sep 17 00:00:00 2001 From: John-Paul Robinson Date: Tue, 15 Jul 2025 17:58:55 -0500 Subject: [PATCH 1/2] Add a --print0 option to print null terminated strings This option is useful for working with files that have special characters like newlines embedded in them. It makes it it possible to pass this output to other downstream commands that can process null terminated strings, e.g. "parallel --null ..." Added option string, new function MFU_PRED_PRINT0() and argument parsing to select this feature. --- src/dfind/dfind.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/dfind/dfind.c b/src/dfind/dfind.c index c652d106..74241d09 100644 --- a/src/dfind/dfind.c +++ b/src/dfind/dfind.c @@ -22,6 +22,7 @@ int MFU_PRED_EXEC (mfu_flist flist, uint64_t idx, void* arg); int MFU_PRED_PRINT (mfu_flist flist, uint64_t idx, void* arg); +int MFU_PRED_PRINT0 (mfu_flist flist, uint64_t idx, void* arg); int MFU_PRED_EXEC (mfu_flist flist, uint64_t idx, void* arg) { @@ -146,6 +147,13 @@ int MFU_PRED_PRINT (mfu_flist flist, uint64_t idx, void* arg) return 1; } +int MFU_PRED_PRINT0 (mfu_flist flist, uint64_t idx, void* arg) +{ + const char* name = mfu_flist_file_get_name(flist, idx); + printf("%s\0", name); + return 1; +} + static void print_usage(void) { printf("\n"); @@ -191,6 +199,7 @@ static void print_usage(void) printf("\n"); printf("Actions:\n"); printf(" --print - print item name to stdout\n"); + printf(" --print0 - print item name to stdout with null terminator\n"); printf(" --exec CMD ; - execute CMD on item\n"); printf("\n"); fflush(stdout); @@ -347,6 +356,7 @@ int main (int argc, char** argv) { "type", required_argument, NULL, 'T' }, { "print", no_argument, NULL, 'p' }, + { "print0", no_argument, NULL, '0' }, { "exec", required_argument, NULL, 'e' }, { NULL, 0, NULL, 0 }, }; @@ -525,6 +535,10 @@ int main (int argc, char** argv) mfu_pred_add(pred_head, MFU_PRED_PRINT, NULL); break; + case '0': + mfu_pred_add(pred_head, MFU_PRED_PRINT0, NULL); + break; + case 'T': ret = add_type(pred_head, *optarg); if (ret != 1) { From b04bcc876e1e082d7dbdcf04226966eaf5cb20ee Mon Sep 17 00:00:00 2001 From: John-Paul Robinson Date: Tue, 15 Jul 2025 18:30:39 -0500 Subject: [PATCH 2/2] Fixed the null character insertion The null character wasn't showing up in the output because it was included in the printf string format. Had to add it as an explicit character to get \0 to be present in the output. --- src/dfind/dfind.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dfind/dfind.c b/src/dfind/dfind.c index 74241d09..b53571c2 100644 --- a/src/dfind/dfind.c +++ b/src/dfind/dfind.c @@ -150,7 +150,7 @@ int MFU_PRED_PRINT (mfu_flist flist, uint64_t idx, void* arg) int MFU_PRED_PRINT0 (mfu_flist flist, uint64_t idx, void* arg) { const char* name = mfu_flist_file_get_name(flist, idx); - printf("%s\0", name); + printf("%s%c", name, '\0'); return 1; }