This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 61551aa84 system/dd: Implement conv=notrunc
61551aa84 is described below

commit 61551aa8490236bf0dc2c96e36071b935c6d1252
Author: YAMAMOTO Takashi <yamam...@midokura.com>
AuthorDate: Wed Apr 2 22:58:58 2025 +0900

    system/dd: Implement conv=notrunc
    
    https://pubs.opengroup.org/onlinepubs/9699919799/utilities/dd.html
    > notrunc
    >   Do not truncate the output file. Preserve blocks in the output
    >   file not explicitly written by this invocation of the dd utility.
    
    Signed-off-by: YAMAMOTO Takashi <yamam...@midokura.com>
---
 system/dd/dd_main.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/system/dd/dd_main.c b/system/dd/dd_main.c
index ca9e3c7b6..fb8005b40 100644
--- a/system/dd/dd_main.c
+++ b/system/dd/dd_main.c
@@ -79,6 +79,7 @@ struct dd_s
   uint32_t     skip;       /* The number of sectors skipped on input */
   uint32_t     seek;       /* The number of sectors seeked on output */
   bool         eof;        /* true: The end of the input or output file has 
been hit */
+  bool         notrunc;    /* conv=notrunc */
   uint16_t     sectsize;   /* Size of one sector */
   uint16_t     nbytes;     /* Number of valid bytes in the buffer */
   FAR uint8_t *buffer;     /* Buffer of data to write to the output file */
@@ -186,7 +187,8 @@ static inline int dd_outfopen(FAR const char *name, FAR 
struct dd_s *dd)
       return OK;
     }
 
-  dd->outfd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+  dd->outfd = open(name, O_WRONLY | O_CREAT | (dd->notrunc ? 0 : O_TRUNC),
+                   0644);
   if (dd->outfd < 0)
     {
       fprintf(stderr, "%s: failed to open '%s': %s\n",
@@ -259,6 +261,32 @@ int main(int argc, FAR char **argv)
         {
           dd.seek = atoi(&argv[i][5]);
         }
+      else if (strncmp(argv[i], "conv=", 5) == 0)
+        {
+          const char *cur = &argv[i][5];
+          while (true)
+            {
+              const char *next = strchr(cur, ',');
+              size_t len = next != NULL ? next - cur : strlen(cur);
+              if (len == 7 && !memcmp(cur, "notrunc", 7))
+                {
+                  dd.notrunc = true;
+                }
+              else
+                {
+                  fprintf(stderr, "%s: unknown conversion '%.*s'\n", g_dd,
+                          (int)len, cur);
+                  goto errout_with_paths;
+                }
+
+              if (next == NULL)
+                {
+                  break;
+                }
+
+              cur = next + 1;
+            }
+        }
       else
         {
           print_usage();

Reply via email to