From f1bba77e6ed7d0fd0b8ad19853492ddab380bc6d Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 6 Nov 2023 13:41:52 +0300 Subject: [PATCH] truncate --- TODO | 13 ++++++++---- coreutils/chown.c | 19 +++--------------- coreutils/rm.c | 2 +- coreutils/shred.c | 1 + coreutils/touch.c | 1 - coreutils/truncate.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ obj/.gitignore | 0 7 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 coreutils/truncate.c create mode 100644 obj/.gitignore diff --git a/TODO b/TODO index 08aaddf..82c586b 100644 --- a/TODO +++ b/TODO @@ -10,7 +10,6 @@ nice renice nohup split -truncate date tee tr @@ -22,10 +21,9 @@ dd stty stat sort -printf test -install -dmesg +tar +chgrp Other: kill @@ -41,6 +39,7 @@ Other: dhcp-client getopt fdisk + ntpd Loginutils: su @@ -62,3 +61,9 @@ Findutils: Shell: lsh - lavash + +Init: + sinit - Simple init + +Libs: + readline diff --git a/coreutils/chown.c b/coreutils/chown.c index 41c7cf8..0511ecf 100644 --- a/coreutils/chown.c +++ b/coreutils/chown.c @@ -16,25 +16,12 @@ long gid; long uid; int change(const char *file) { - struct stat old_file; - if (get_stat("chown", file, &old_file)) + if (chown_func(file, uid, gid)) { + fprintf(stderr, "chown: unable to chown %s: %s\n", file, strerror(errno)); return 1; - - if (chown_func(file, uid, gid) == 0) { - struct stat new_file; - if (get_stat("chown", file, &new_file)) - return 1; - - if (old_file.st_gid != new_file.st_gid || old_file.st_uid != new_file.st_uid) - return 0; - - fprintf(stderr, "chown: %s unchanged\n", file); } - else - fprintf(stderr, "chown: unable to chown %s: %s\n", file, strerror(errno)); - - return 1; + return 0; } int cntree(const char *dst) { diff --git a/coreutils/rm.c b/coreutils/rm.c index e2e2905..d29590e 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c @@ -65,7 +65,7 @@ int main(const int argc, char **argv) { f_flag = 1; else if (!strcmp(argv[i], "--help")) { - printf("rm [-f force] [src1 src2...]\n"); + printf("rm [-f force] file1 file2...\n"); return 0; } } diff --git a/coreutils/shred.c b/coreutils/shred.c index 59d45f9..a45ebf6 100644 --- a/coreutils/shred.c +++ b/coreutils/shred.c @@ -83,6 +83,7 @@ int main(const int argc, const char **argv) { } shred(rand_fd, fd); + fsync(fd); close(fd); if (u_flag) diff --git a/coreutils/touch.c b/coreutils/touch.c index b18b2f2..b636f9f 100644 --- a/coreutils/touch.c +++ b/coreutils/touch.c @@ -1,7 +1,6 @@ #include #include #include - #include #include #include diff --git a/coreutils/truncate.c b/coreutils/truncate.c new file mode 100644 index 0000000..3eee63a --- /dev/null +++ b/coreutils/truncate.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include + +int main(const int argc, const char **argv) { + unsigned int c_flag = 0; + unsigned int s_size = 0; + + int i; + for (i = 1; i < argc; i++) { + if (argv[i][0] != '-') + break; + + else if (!strncmp(argv[i], "-s=", 3)) + s_size = atoi(argv[i] + 3); + + else if (!strcmp(argv[i], "-c")) + c_flag = 1; + + else if (!strcmp(argv[i], "--help")) { + printf("truncate [-c Do not create files] [-s=SIZE] file1 file2...\n"); + return 0; + } + } + + int flags = O_WRONLY | O_NONBLOCK; + if (!c_flag) + flags |= O_CREAT; + + int fd = open(argv[i], flags, 0666); + if (fd < 0) { + fprintf(stderr, "truncate %s %s\n", argv[i], strerror(errno)); + return 1; + } + + if (ftruncate(fd, s_size) == -1) { + close(fd); + fprintf(stderr, "truncate %s %s\n", argv[i], strerror(errno)); + return 1; + } + + close(fd); + return 0; +} diff --git a/obj/.gitignore b/obj/.gitignore new file mode 100644 index 0000000..e69de29