Yes
This commit is contained in:
parent
20f7eefb3e
commit
07742fcdb9
2
Makefile
2
Makefile
@ -2,7 +2,7 @@ C_SOURCES:=$(wildcard coreutils/*.c)
|
||||
C_TARGETS:=$(patsubst coreutils/%.c,bin/%,$(C_SOURCES))
|
||||
TARGETS:=$(C_TARGETS)
|
||||
|
||||
CFLAGS:=-s -Os -pedantic -Wall -Wextra
|
||||
CFLAGS:=-s -Os -flto -pedantic -Wall -Wextra
|
||||
all: bin $(TARGETS)
|
||||
|
||||
bin/%: coreutils/%.c
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
char *make_path(const char *src, const char *dst) {
|
||||
@ -22,6 +23,7 @@ char *make_path(const char *src, const char *dst) {
|
||||
|
||||
|
||||
int write_to_file(const char *src, const char *dst) {
|
||||
int ret = 0;
|
||||
int ifd = open(src, O_RDONLY);
|
||||
if (ifd < 0)
|
||||
return 1;
|
||||
@ -32,14 +34,27 @@ int write_to_file(const char *src, const char *dst) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
char buf[4096];
|
||||
ssize_t len = 0;
|
||||
while ((len = read(ifd, buf, sizeof(buf))))
|
||||
write(ofd, buf, len);
|
||||
off_t len = lseek(ifd, 0, SEEK_END);
|
||||
if (len <= 0)
|
||||
goto EXIT;
|
||||
|
||||
void *buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, ifd, 0);
|
||||
if (buf == MAP_FAILED) {
|
||||
ret = 1;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
write(ofd, buf, len);
|
||||
|
||||
if (munmap(buf, len)) {
|
||||
ret = 0;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
EXIT:
|
||||
close(ifd);
|
||||
close(ofd);
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int copyf(const char *src, const char *dst) {
|
||||
|
Loading…
Reference in New Issue
Block a user