Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #17457

Re: copying files

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: copying files
Date 2012-02-16 09:49 -0800
Organization None to speak of
Message-ID <ln8vk2hdn3.fsf@nuthaus.mib.org> (permalink)
References <f62ccd5a-1cf4-4585-b8ca-c515a53d8a73@w9g2000vbv.googlegroups.com> <jhiofu$d1g$1@dont-email.me>

Show all headers | View raw


Kleuske <kleuske@nowhere.net> writes:
> On Thu, 16 Feb 2012 01:03:12 -0800, Hans Vlems saw fit to publish the
> following:
>> I'm maintaing large numbers of Adobe Reader files (.pdf). One of my
>> programs, written in C (gcc 4.4.4), must make a copies between different
>> filesystems of these pdf files. There is AFAIK no library function that
>> does this, which leaves me two options:
>> 1- use the console interface, i.e. build a command string and pass this
>> to system().
>
> Don't. It's ineffective and may open up your system to abuse.

It's entirely effective (but non-portable, since the command string
you pass to system() is entirely system-specific).

As for abuse, that's a good point.  If someone creates a pdf file
named, say, "foo ; rm -rf ~ ; bar.pdf", then incorporating that name
into the string you pass to system() could be really bad; see also
Little Bobby Tables, <http://xkcd.com/327/>.  But if you're aware of
the problem, you can avoid it; if nothing else, you can just ignore
any files whose names contain any characters outside a safe set.

Somebody spent a great deal of time and effort writing an efficient
file copying command for your operating system.  Why reinvent
the wheel?

>> 2- open the file, copy the contents and close the target I'd rather
>> avoid option 1 because system runs out of control of my program.
>> My question is what read and write functions are best suited to copy the
>> (binary) pdf files?
>
> Try fopen, fread, fwrite and fclose. Use a big buffer, since PDF's
> (especially with grahics) tend to be big.

fread and fwrite do their own buffering.  Copying a character at a time
is likely to be about as efficient as using a big buffer.  (Don't take
my word for that; measure it!)

>> 'Performance is not the main objective, but I want to be sure that the
>> copy finished succesfully and accurately. Hans
>
> Check for error codes.

Such as the value returned by system().

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 01:03 -0800
  Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 01:23 -0800
    Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 01:31 -0800
      Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:14 -0800
        Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:43 -0800
          Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:59 -0800
            Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 15:06 -0800
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:13 -0800
      Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:56 -0800
        Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 10:11 -0800
          Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 15:11 -0800
            Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 15:21 -0800
              Re: copying files Philip Lantz <prl@canterey.us> - 2012-02-17 09:53 -0800
                Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:22 -0800
            Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-17 03:36 -0800
              Re: copying files Willem <willem@toad.stack.nl> - 2012-02-17 22:04 +0000
                Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:28 -0800
                Re: copying files Willem <willem@toad.stack.nl> - 2012-02-18 21:28 +0000
              Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:19 -0800
        Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 15:02 -0800
  Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 01:34 -0800
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:27 -0800
      Re: copying files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-16 17:11 +0000
        Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:57 -0800
      Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:54 -0800
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:41 -0800
  Re: copying files JohnF <john@please.see.sig.for.email.com> - 2012-02-16 09:49 +0000
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:10 -0800
      Re: copying files JohnF <john@please.see.sig.for.email.com> - 2012-02-16 11:52 +0000
        Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:56 -0800
          Re: copying files David Thompson <dave.thompson2@verizon.net> - 2012-02-24 04:43 -0500
        Re: copying files Ike Naar <ike@iceland.freeshell.org> - 2012-02-16 16:19 +0000
          Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:50 -0800
        Re: copying files David Thompson <dave.thompson2@verizon.net> - 2012-02-24 04:43 -0500
  Re: copying files Nobody <nobody@nowhere.com> - 2012-02-16 10:58 +0000
    Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:41 -0800
      Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:59 -0800
      Re: copying files Nobody <nobody@nowhere.com> - 2012-02-17 06:25 +0000
  Re: copying files Kleuske <kleuske@nowhere.net> - 2012-02-16 11:15 +0000
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:45 -0800
    Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:49 -0800

csiph-web