[Toybox] [PATCH 1/2] dd: Add support for conv=nocreat

Jeffery Miller jefferymiller at google.com
Wed Apr 16 16:54:01 PDT 2025


With `conv=nocreat` set O_CREAT will not be set when
creating the output file, causing an error if of does
not exist.

Test: dd if=/dev/zero of=/tmp/doesnotexist conv=nocreat count=1
---
 toys/posix/dd.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/toys/posix/dd.c b/toys/posix/dd.c
index 7fc721fc..ebd55f10 100644
--- a/toys/posix/dd.c
+++ b/toys/posix/dd.c
@@ -52,7 +52,7 @@ struct dd_flag {
 };
 
 static const struct dd_flag dd_conv[] = TAGGED_ARRAY(DD_conv,
-  {"fsync"}, {"noerror"}, {"notrunc"}, {"sync"}, // TODO sparse excl nocreat
+  {"fsync"}, {"noerror"}, {"notrunc"}, {"sync"}, {"nocreat"}, // TODO sparse excl
 );
 
 static const struct dd_flag dd_iflag[] = TAGGED_ARRAY(DD_iflag,
@@ -149,7 +149,7 @@ void dd_main()
     count = ULLONG_MAX, buflen;
   long long len;
   struct iovec iov[2];
-  int opos, olen, ifd = 0, ofd = 1, trunc = O_TRUNC, ii;
+  int opos, olen, ifd = 0, ofd = 1, trunc = O_TRUNC, creat = O_CREAT, ii;
   unsigned conv = 0, iflag = 0, oflag = 0;
 
   TT.show_xfer = TT.show_records = 1;
@@ -186,10 +186,11 @@ void dd_main()
   buf = xmalloc(buflen = ibs+obs*!bs);
   if (buflen<ibs || buflen<obs) error_exit("tilt");
 
+  if (conv & _DD_conv_nocreat) creat = 0;
   if (conv & _DD_conv_notrunc) trunc = 0;
   if (iname) ifd = xopenro(iname);
   else iname = "stdin";
-  if (oname) ofd = xcreate(oname, O_WRONLY|O_CREAT|(trunc*!seek),0666);
+  if (oname) ofd = xcreate(oname, O_WRONLY|creat|(trunc*!seek),0666);
   else oname = "stdout";
 
   // Implement skip=
-- 
2.49.0.805.g082f7c87e0-goog



More information about the Toybox mailing list