int fcopy(char *name2, char *name1) { FILE *pfs, *pfd; /* Source & destination file pointers */ int tocopy; /* Number of bytes to copy in one pass */ static char *buffer; /* Pointer on the intermediate copy buffer */ int err; DEBUG_ENTER(("fcopy(\"%s\", \"%s\");\n", name2, name1)); if (!buffer) { buffer = malloc(BUFFERSIZE); /* Allocate memory for copying */ if (!buffer) { DEBUG_LEAVE(("return 1; // Out of memory for copy buffer\n")); return 1; } } pfs = fopen(name1, "rb"); if (!pfs) { DEBUG_LEAVE(("return 2; // Cannot open source file\n")); return 2; } pfd = fopen(name2, "wb"); if (!pfd) { fclose(pfs); DEBUG_LEAVE(("return 3; // Cannot open destination file\n")); return 3; } while ((tocopy = (int)fread(buffer, 1, BUFFERSIZE, pfs))) { if (!fwrite(buffer, tocopy, 1, pfd)) { fclose(pfs); fclose(pfd); DEBUG_LEAVE(("return 3; // Cannot write to destination file\n")); return 3; } } /* Flush buffers into the destination file, */ fflush(pfd); /* and give the same date than the source file */ fclose(pfs); fclose(pfd); /* 2011-05-12 Use an OS-independant method, _after_ closing the files */ err = copydate(name2, name1); DEBUG_LEAVE(("return %d; // Copy successful\n", err)); return err; }
int main(int argc, char *argv[]) { if(argc!=3) usage(); if(access(argv[1],0)) { fprintf(stderr,"%s: %s does not exist\n",PROGNAME,argv[1]); exit(2); } if(access(argv[2],02)) { fprintf(stderr,"%s: %s does not exist or not writable\n",PROGNAME,argv[2]); exit(3); } copydate(argv[1],argv[2]); return 0; }