Donny9 commented on issue #6710:
URL: https://github.com/apache/nuttx/issues/6710#issuecomment-1751674630

   @pkarashchenko This examples can run with pass result on linux, but can't 
pass on nuttx, because this issue: newd and oldd can't share file offset, 
because they are different file handle in nuttx.
   ```
   //#include <nuttx/config.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <string.h>
   #include <fcntl.h>
   #include <syslog.h>
   //#include "test.h"
   #define TESTFILE "testDup2File1"
   #define TEST_FAIL -1
   #define TEST_PASS 0
   
   /****************************************************************************
    * Name: dup
    * Example description:
       1. Open a file and get the file identifier fd.
       2. Copy the file identifier to newfd
       3. Open the file through newfd. And check the return results
    * Test item: open()  write() dup2()
    * Expect results: TEST PASSED
    
****************************************************************************/
   
   int main(void)
   {
       char buf[16] = {0};
   
       off_t currpos;
       int fd1 = open(TESTFILE, O_RDWR | O_CREAT, 0777);
       if (fd1 == -1)
       {
           printf("open file fail !\n");
           return TEST_FAIL;
       }
   
       int fd2 = open(TESTFILE, O_RDWR | O_CREAT, 0777);
       if (fd2 == -1)
       {
           close(fd1);
           printf("open file fail !\n");
           return TEST_FAIL;
       }
   
       int ret = dup2(fd1, fd2);
       if (ret == -1)
       {
           close(fd1);
           close(fd2);
           printf("dup2 error ,  return : %d!\n", ret);
           return TEST_FAIL;
       }
       syslog(LOG_INFO, "current fd = %d\n", ret);
       char *buf1 = "hello ";
       char *buf2 = "world!";
       write(fd1, buf1, strlen(buf1));
       write(fd2, buf2, strlen(buf2));
   
       fsync(fd1);
   
       /* reset file pos use fd2 */
       lseek(fd2, 0, SEEK_SET);
   
       /* Check if file pos is shared */
       currpos = lseek(fd1, 0, SEEK_CUR);
       if (currpos != 0)
       {
           printf("Do not shared file pos after dup2()\n");
           close(fd1);
           close(fd2);
           return TEST_FAIL;
       }
   
       ret = read(fd1, buf, 16);
       if (ret <= 0)
       {
           printf("read fail\n");
           close(fd1);
           close(fd2);
           return TEST_FAIL;
       }
       close(fd1);
       close(fd2);
   
       if (strncmp(buf, "hello world!", 12) == 0)
       {
           printf("test pass\n");
           return TEST_PASS;
       }
   
       printf("test fail\n");
       return TEST_FAIL;
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to