1

My code is to copy any file from one folder to another. Below is my approach but i am not sure whether this is the best approach?There are two problem with my code :

int copycommand(char *source, char *destination)
{
        int childExitStatus;
        pid_t pid;
        int status;
        pid = fork();

        if (pid == 0) { /* child */
                execl("/bin/cp", "/bin/cp", "-R", source, destination, (char *)0);
        }

        }
        else {

                pid_t ws = waitpid( pid, &childExitStatus, WNOHANG);
                if (ws == -1)
                { /* error - handle as you wish */
                }
                if( WIFEXITED(childExitStatus)) /* exit code in childExitStatus */
                {
                        status = WEXITSTATUS(childExitStatus); /* zero is normal exit */
                        /* handle non-zero as you wish */
                   printf("%d\n",status);
                }
                else if (WIFSIGNALED(childExitStatus)) /* killed */
                {
                }
                else if (WIFSTOPPED(childExitStatus)) /* stopped */
                {
                }
        }
}

1. Not able to find successful copied info ?

i tired by extraction status from waitpid in the parent process( 0 means success) but even if the usb(destination) is removed and try the operation it throw same 0 value i.e success how can i report for successful copied?

2. It takes 10-15 min to copy 1GB of data how can i increased it speed or time complexity?

  • Why are you writing this and not just using a script? Perhaps [system](https://linux.die.net/man/3/system) will do – Ed Heal Jan 15 '17 at 18:49
  • Can you please share any best approach if possible – user7953556 Jan 15 '17 at 18:52
  • Just follow the link – Ed Heal Jan 15 '17 at 18:53
  • 1
    @user3588044 I think you should answer Ed's question. Why not just use a script? Are you trying to copy files as part of your application? If so, I'd suggest a different way of doing it rather than calling out to the system. If you're trying to automate some work though, a script would be a better solution. But it's hard to point you in a good direction without knowing why you're doing what you're doing. – John Szakmeister Jan 15 '17 at 19:00
  • I need to clone my all the setting of embedded system and send it to usb connected . – user7953556 Jan 15 '17 at 19:05

0 Answers0