/* * Copyright 1996 Devin Reade . * All rights reserved. * * For copying and distribution information, see the file "COPYING" * accompanying this file. * * $Id: copyfile.c,v 1.1 1996/03/31 23:38:31 gdr Exp $ */ #include #include #include #include #include #include "install.h" /* the chunk size in which we copy files */ #define COPY_BUFFER_SIZE 1024 /* * copyfile * * copy a file from the pathname to the location , which * may be a directory. Ensure that file types and other information * (except for the backup bit) is matched. * * Returns NULL and sets errno on failure. On success, returns a * pointer to an internal buffer containing the final pathname. * * +++ THIS ROUTINE IS NOT REENTRANT +++ */ char * copyfile (char *from, char *to) { static char buffer[COPY_BUFFER_SIZE]; static FileInfoRecGS inforec; static OpenRecGS openrec; static ExpandPathRecGS expandrec; static ResultBuf255 resultbuf; static struct { Word pCount; Word refNum; Longword dataBuffer; Longword requestCount; Longword transferCount; Word cachePriority; } iobuf; static struct { Word pCount; Word refNum; } closerec; static GSString255 fromGS, toGS; static char *result = NULL; /* we only use this if our path is */ /* exactly 255 chars long */ size_t len1, len2; Word refNumIn, refNumOut; /* GS/OS ref numbers for I/O */ int isDir, i, j, k, done; char *p, *q, *r; /* concheck and convert filenames to GSString255 type */ if (!from || !to || ((len1 = strlen(from)) > 254) || ((len2 = strlen(to)) > 254) ) { errno = EINVAL; return NULL; } fromGS.length = len1; toGS.length = len2; strcpy(fromGS.text,from); strcpy(toGS.text,to); /* expand the original file name */ expandrec.pCount = 3; expandrec.inputPath = &fromGS; expandrec.outputPath = &resultbuf; expandrec.flags = 0x0000; resultbuf.bufSize = 255; ExpandPathGS(&expandrec); if ((i = toolerror()) != 0) { errno = _mapErr(i); return NULL; } strcpyGSString255(&fromGS,&(resultbuf.bufString)); /* expand the destination name */ expandrec.pCount = 3; expandrec.inputPath = &toGS; expandrec.outputPath = &resultbuf; expandrec.flags = 0x0000; resultbuf.bufSize = 255; ExpandPathGS(&expandrec); if ((i = toolerror()) != 0) { errno = _mapErr(i); return NULL; } strcpyGSString255(&toGS,&(resultbuf.bufString)); /* find out if is a directory */ inforec.pCount = 5; inforec.pathname = &toGS; GetFileInfoGS(&inforec); i = toolerror(); switch(i) { case 0: isDir = ((inforec.storageType == 0x0D) || (inforec.storageType == 0x0F)) ? 1 : 0; break; case fileNotFound: isDir = 0; break; default: errno = _mapErr(i); return NULL; } /* it's a directory? tack on the file name */ if (isDir) { /* expand the directory name */ expandrec.pCount = 3; expandrec.inputPath = &toGS; expandrec.outputPath = &resultbuf; expandrec.flags = 0x0000; resultbuf.bufSize = 255; ExpandPathGS(&expandrec); if ((i = toolerror()) != 0) { errno = _mapErr(i); return NULL; } /* tack on the final component */ p = basename(from); len1 = strlen(p); if (len1 + toGS.length + 1 > 255) { errno = EINVAL; return NULL; } q = &(toGS.text[toGS.length]); r = p; *q++ = ':'; for (i=0; i