LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to copy files using C (https://www.linuxquestions.org/questions/programming-9/how-to-copy-files-using-c-319400/)

skie_knite007 05-02-2005 01:08 PM

how to copy files using C
 
I need to copy a file to some location (in my project). I'm doing this using the system function like

char *cmd;
sprintf(cmd,"cp %s <dest>",args);
system(cmd);

But this is not working effectively in all cases.

Is there some way out to do this???????????????

pls help me out..................................................



Thanx

jlliagre 05-02-2005 01:28 PM

Quote:

But this is not working effectively in all cases.
In what cases ?

itsme86 05-02-2005 01:47 PM

How about something like this?
Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  int fdin, fdout;
  struct stat st;
  char buf[BUFSIZ];
  int n_read;

  if(argc != 3)
  {
    puts("Usage: mycp <source> <destination>");
    exit(1);
  }
  if((fdin = open(argv[1], O_RDONLY)) == -1)
  {
    printf("open(): %s\n", strerror(errno));
    exit(1);
  }
  if(fstat(fdin, &st) == -1)
  {
    printf("stat(): %s\n", strerror(errno));
    exit(1);
  }

  umask(0);
  if((fdout = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, st.st_mode)) == -1)
  {
    printf("open(): %s\n", strerror(errno));
    exit(1);
  }

  while((n_read = read(fdin, buf, sizeof(buf))) > 0)
  {
    if(write(fdout, buf, n_read) == -1)
    {
      printf("write(): %s\n", strerror(errno));
      exit(1);
    }
  }

  close(fdin);
  close(fdout);

  if(n_read == -1)
  {
    printf("read(): %s\n", strerror(errno));
    exit(1);
  }

  return 0;
}


aluser 05-02-2005 03:15 PM

Quote:

In what cases ?
The cases where a filename contains a shell metacharactor or spaces, and where your host doesn't have cp.

And no, putting quotes around the %s in the sprintf statement doesn't protect against all special characters : )

mehuljv 05-02-2005 11:27 PM

hi,

char *cmd;
sprintf(cmd,"cp %s <dest>",args);

i guess this should not work in any class, because you are not allocating memory for cmd, you are directly printing it to the some location contained by cmd. so do it following way

char *cmd;
cmd = (char *)malloc(<size>);
sprintf(cmd,"cp %s <dest>",args);

this should work

Regards
Mehul

jlliagre 05-03-2005 01:09 AM

This wouldn't address the special characters issue, you can avoid most of them with "cp '%s' <dest>".
Still wrong when args is more than one filename, or contain itself the single quote character.

itsme86 05-03-2005 11:25 AM

Just use the code I posted then. It doesn't require talking to the shell to get the job done.

jlliagre 05-03-2005 01:38 PM

Technically, the code you posted can only be used from a shell, so is not exactly adressing the issue.
Of course, slightly modifying it to make a usable function of it will fill the gap ...

itsme86 05-03-2005 02:29 PM

Or making it prompt the user for the source and destination filenames will also fix it. At any rate, it doesn't depend on system(), so it doesn't matter if the host doesn't have the cp command or whatever.


All times are GMT -5. The time now is 05:33 PM.