LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2005, 01:08 PM   #1
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
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
 
Old 05-02-2005, 01:28 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
But this is not working effectively in all cases.
In what cases ?
 
Old 05-02-2005, 01:47 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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;
}
 
Old 05-02-2005, 03:15 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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 : )
 
Old 05-02-2005, 11:27 PM   #5
mehuljv
Member
 
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
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
 
Old 05-03-2005, 01:09 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 05-03-2005, 11:25 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Just use the code I posted then. It doesn't require talking to the shell to get the job done.
 
Old 05-03-2005, 01:38 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ...
 
Old 05-03-2005, 02:29 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy files dunmarie Linux - Newbie 2 10-29-2003 09:18 AM
Copy files andy18 Linux - General 4 09-02-2003 02:26 PM
can't copy files from cd linuxmad Linux - General 2 08-28-2003 11:34 PM
how to copy and paste between files and within files Bheki Linux - General 1 02-05-2002 05:29 AM
Why can't I copy files? rdaves@earthlink.net Linux - General 1 05-28-2001 10:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration