This commit is contained in:
Your Name 2023-10-17 13:50:40 +03:00
parent 20f7eefb3e
commit 07742fcdb9
3 changed files with 21 additions and 7 deletions

View File

@ -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

1
TODO
View File

@ -1,5 +1,4 @@
*1/2:
cp
ln
*Todo:

View File

@ -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) {