[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 597
  • Last Modified:

Mac file copy function


Urgently needed Mac File Copy function with only two parameters: source and destination POSIX-pathname.

OSErr MacSimpleFileCopy( const char * srcName, const char * dstName );

OR two CFURLRef parameters:

OSErr MacSimpleFileCopy( CFURLRef srcName, CFURLRef dstName );

Required: Mac OS X/Carbon, copying data & resource forks
Unneeded: network support, Mac OS Classic support

I'm newbie in mac world, and completely confused with FSSpec, FSRef, dirID etc.
1 Solution
This is very simple copy file function that you can use in Mac (or Windows):

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int copyFile (const char *inPath, const char * outPath) {
  int inF, ouF;
  char line[512];
  int bytes;

  if((inF = open( inPath , O_RDONLY)) == -1) {

  if((ouF = open(outPath , O_WRONLY | O_CREAT)) == -1) {

  while((bytes = read(inF, line, sizeof(line))) > 0)
    write(ouF, line, bytes);


Korsar_13Author Commented:
unfortunately, this routine don't copy FileType/FileCreator tags and resource fork

carbon-dev@lists.apple.com suggest me use FSCopyOject, but it's parameters is very strange to me:

/* This routine acts as the top level of the copy engine.  */
OSErr FSCopyObject(  const FSRef        *source,
          const FSRef        *destDir,
           ItemCount        maxLevels,
           FSCatalogInfoBitmap    whichInfo,
           DupeAction        dupeAction,
           const HFSUniStr255    *newObjectName,  /* can be NULL */
          Boolean          wantFSSpec,
          Boolean          wantName,
          CopyObjectFilterProcPtr filterProcPtr,  /* can be NULL */
          void          *yourDataPtr,  /* can be NULL */
          FSRef          *newObjectRef,  /* can be NULL */
          FSSpec          *newObjectSpec)  /* can be NULL */

Alex CuryloCommented:
Well, there should be only two tricky bits, if you're doing this for a simple file. First off, you need to change your source path into an FSRef. Do that like this.

   FSRef srcRef;
   if (!CFURLGetFSRef (srcURLPath, &srcRef))
     return bdNamErr;

Next, you need an FSRef to the directory you're copying to; get that the same way.

If you're not changing the name, then you're done, call FSCopyObject with those two FSRefs and everything else NULL.

If you are changing the name, then you need one more parameter, the HFSUniStr255 of the new name you want. Put the new name into a CFStringRef from whatever source format you have it in, then use this to get an HFSUniStr255 out of that.

HFSUniStr255 name;
name.length = CFStringGetBytes( input, CFRangeMake(0, MIN(CFStringGetLength(input), 255)),
        kCFStringEncodingUnicode, 0, false, (UInt8 *)(name.unicode), 255, NULL );
if ( output->length == 0 )
    ;// handle error

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now