Skip to content

Instantly share code, notes, and snippets.

@Howard-Chang
Last active March 25, 2019 13:43
Show Gist options
  • Save Howard-Chang/12eee7057dcc7d792cf8d6c88e229265 to your computer and use it in GitHub Desktop.
Save Howard-Chang/12eee7057dcc7d792cf8d6c88e229265 to your computer and use it in GitHub Desktop.
copy.c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>
#define BUF_SIZE 4096
int main(int argc, char* argv[]) {
int inputFd, outputFd, tmpFd;
ssize_t numIn, numOut;
char buffer[BUF_SIZE];
off_t begin=0, end=0;
long long fileSize, blockSize, pos=0;
FILE* stream;
char tmpStr[] = "tmp_XXXXXX";
mktemp(tmpStr);
printf("%s\n", tmpStr);
stream = fopen(tmpStr, "w+"); /*權限為600,只有owner才可以讀寫*/
if (stream == NULL)
perror("error: ");
tmpFd = fileno(stream);
inputFd = open (argv [1], O_RDONLY);
outputFd = open(tmpFd, O_WRONLY | O_CREAT, S_IRUSR| S_IWUSR);
ftruncate(outputFd, 0);
fileSize = lseek(inputFd, 0, SEEK_END);
lseek(inputFd, 0, SEEK_SET);
while (1) {
pos = lseek(inputFd, pos, SEEK_DATA);
begin = pos;
pos = lseek(inputFd, pos, SEEK_HOLE);
end = pos;
blockSize=end-begin;
lseek(inputFd, begin, SEEK_SET);
lseek(outputFd, begin, SEEK_SET);
while((numIn = read (inputFd, buffer, BUF_SIZE)) > 0) {
numOut = write (outputFd, buffer, (ssize_t) numIn);
if (numIn != numOut) perror("numIn != numOut");
blockSize-=numIn;
if (blockSize == 0) break;
}
if (lseek(outputFd, 0, SEEK_CUR) == fileSize) break;
}
close (inputFd);
close (outputFd);
return (EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment